@@ -8,7 +8,6 @@ class RSAParameters
88{
99 private string $ privateKey ;
1010 private string $ publicKey ;
11- private ?string $ passphrase = 'test_passphrase ' ;
1211
1312 protected array $ config = [
1413 'digest_alg ' => 'sha256 ' ,
@@ -27,13 +26,13 @@ public function __construct()
2726 * @param array|null $configArgs
2827 * @return $this
2928 */
30- public function generateKeys (? string $ passphrase = null , ?array $ configArgs = null ): RSAParameters
29+ public function generateKeys (string $ passphrase , ?array $ configArgs = null , string $ salt = ' salt ' ): RSAParameters
3130 {
3231 $ keys = openssl_pkey_new ($ this ->config );
3332
3433 if ($ keys ) {
3534 openssl_pkey_export ($ keys , $ private );
36- $ this ->privateKey = $ this ->encryptPrivateKey (privateKey: $ private );
35+ $ this ->privateKey = $ this ->encryptPrivateKey (privateKey: $ private, passphrase: $ passphrase , salt: $ salt );
3736
3837 $ pub = openssl_pkey_get_details ($ keys );
3938
@@ -45,22 +44,22 @@ public function generateKeys(?string $passphrase = null, ?array $configArgs = nu
4544 return $ this ;
4645 }
4746
48- private function encryptPrivateKey (string $ privateKey , string $ salt = ' salt ' ): string
47+ private function encryptPrivateKey (string $ passphrase , string $ privateKey , string $ salt ): string
4948 {
5049 $ aes = new AESCryptoServiceProvider ();
5150 $ aes ->generateIV ();
5251 $ k = new CryptoKey ();
53- $ key = $ k ->getCryptographicKey ($ this -> passphrase , $ salt );
52+ $ key = $ k ->getCryptographicKey ($ passphrase , $ salt );
5453 $ aes ->setKey ($ key );
5554
5655 return $ aes ->encrypt ($ privateKey );
5756 }
5857
59- private function decryptPrivateKey (string $ privateKey , string $ salt = ' salt ' ): string
58+ private function decryptPrivateKey (string $ passphrase , string $ privateKey , string $ salt ): string
6059 {
6160 $ aes = new AESCryptoServiceProvider ();
6261 $ k = new CryptoKey ();
63- $ key = $ k ->getCryptographicKey ($ this -> passphrase , $ salt );
62+ $ key = $ k ->getCryptographicKey ($ passphrase , $ salt );
6463 $ aes ->setKey ($ key );
6564
6665 return $ aes ->decrypt ($ privateKey );
@@ -72,11 +71,12 @@ private function decryptPrivateKey(string $privateKey, string $salt = 'salt'): s
7271 * @return string|\OpenSSLAsymmetricKey
7372 * @throws DecryptPrivateKeyException
7473 */
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
7675 {
7776 if (!$ encrypted ) {
7877 return $ this ->decryptPrivateKey (
7978 privateKey: $ this ->privateKey ,
79+ passphrase: $ passphrase ,
8080 salt: $ salt
8181 );
8282 }
@@ -90,9 +90,8 @@ public function getPrivateKey(string $salt = 'salt', bool $encrypted = false): \
9090 * @param string $privateKey
9191 * @param string $passphrase
9292 */
93- public function setPrivateKey (string $ privateKey, string $ passphrase , string $ salt = ' salt ' ): void
93+ public function setPrivateKey (string $ privateKey ): void
9494 {
95- $ this ->passphrase = $ passphrase ;
9695 $ this ->privateKey = $ privateKey ;
9796 }
9897
@@ -116,29 +115,6 @@ public function setPublicKey(string $publicKey): void
116115 $ this ->publicKey = $ publicKey ;
117116 }
118117
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-
142118 /**
143119 * @return array
144120 */
0 commit comments