@@ -8,7 +8,6 @@ class RSAParameters
8
8
{
9
9
private string $ privateKey ;
10
10
private string $ publicKey ;
11
- private ?string $ passphrase = 'test_passphrase ' ;
12
11
13
12
protected array $ config = [
14
13
'digest_alg ' => 'sha256 ' ,
@@ -27,13 +26,13 @@ public function __construct()
27
26
* @param array|null $configArgs
28
27
* @return $this
29
28
*/
30
- public function generateKeys (? string $ passphrase = null , ?array $ configArgs = null ): RSAParameters
29
+ public function generateKeys (string $ passphrase , ?array $ configArgs = null , string $ salt = ' salt ' ): RSAParameters
31
30
{
32
31
$ keys = openssl_pkey_new ($ this ->config );
33
32
34
33
if ($ keys ) {
35
34
openssl_pkey_export ($ keys , $ private );
36
- $ this ->privateKey = $ this ->encryptPrivateKey (privateKey: $ private );
35
+ $ this ->privateKey = $ this ->encryptPrivateKey (privateKey: $ private, passphrase: $ passphrase , salt: $ salt );
37
36
38
37
$ pub = openssl_pkey_get_details ($ keys );
39
38
@@ -45,22 +44,22 @@ public function generateKeys(?string $passphrase = null, ?array $configArgs = nu
45
44
return $ this ;
46
45
}
47
46
48
- private function encryptPrivateKey (string $ privateKey , string $ salt = ' salt ' ): string
47
+ private function encryptPrivateKey (string $ passphrase , string $ privateKey , string $ salt ): string
49
48
{
50
49
$ aes = new AESCryptoServiceProvider ();
51
50
$ aes ->generateIV ();
52
51
$ k = new CryptoKey ();
53
- $ key = $ k ->getCryptographicKey ($ this -> passphrase , $ salt );
52
+ $ key = $ k ->getCryptographicKey ($ passphrase , $ salt );
54
53
$ aes ->setKey ($ key );
55
54
56
55
return $ aes ->encrypt ($ privateKey );
57
56
}
58
57
59
- private function decryptPrivateKey (string $ privateKey , string $ salt = ' salt ' ): string
58
+ private function decryptPrivateKey (string $ passphrase , string $ privateKey , string $ salt ): string
60
59
{
61
60
$ aes = new AESCryptoServiceProvider ();
62
61
$ k = new CryptoKey ();
63
- $ key = $ k ->getCryptographicKey ($ this -> passphrase , $ salt );
62
+ $ key = $ k ->getCryptographicKey ($ passphrase , $ salt );
64
63
$ aes ->setKey ($ key );
65
64
66
65
return $ aes ->decrypt ($ privateKey );
@@ -72,11 +71,12 @@ private function decryptPrivateKey(string $privateKey, string $salt = 'salt'): s
72
71
* @return string|\OpenSSLAsymmetricKey
73
72
* @throws DecryptPrivateKeyException
74
73
*/
75
- public function getPrivateKey (string $ salt = 'salt ' , bool $ encrypted = false ): \OpenSSLAsymmetricKey |string
74
+ public function getPrivateKey (string $ passphrase , string $ salt = 'salt ' , bool $ encrypted = false ): \OpenSSLAsymmetricKey |string
76
75
{
77
76
if (!$ encrypted ) {
78
77
return $ this ->decryptPrivateKey (
79
78
privateKey: $ this ->privateKey ,
79
+ passphrase: $ passphrase ,
80
80
salt: $ salt
81
81
);
82
82
}
@@ -90,9 +90,8 @@ public function getPrivateKey(string $salt = 'salt', bool $encrypted = false): \
90
90
* @param string $privateKey
91
91
* @param string $passphrase
92
92
*/
93
- public function setPrivateKey (string $ privateKey, string $ passphrase , string $ salt = ' salt ' ): void
93
+ public function setPrivateKey (string $ privateKey ): void
94
94
{
95
- $ this ->passphrase = $ passphrase ;
96
95
$ this ->privateKey = $ privateKey ;
97
96
}
98
97
@@ -116,29 +115,6 @@ public function setPublicKey(string $publicKey): void
116
115
$ this ->publicKey = $ publicKey ;
117
116
}
118
117
119
- /**
120
- * Returns passphrase for private key decryption
121
- *
122
- * @return string
123
- */
124
- public function getPassphrase (): ?string
125
- {
126
- return $ this ->passphrase ;
127
- }
128
-
129
- /**
130
- * Set passphrase for private key
131
- *
132
- * @param string $passphrase
133
- * @return $this
134
- */
135
- public function setPassphrase (string $ passphrase ): RSAParameters
136
- {
137
- $ this ->passphrase = $ passphrase ;
138
-
139
- return $ this ;
140
- }
141
-
142
118
/**
143
119
* @return array
144
120
*/
0 commit comments