@@ -8,7 +8,7 @@ class RSAParameters
8
8
{
9
9
private string $ privateKey ;
10
10
private string $ publicKey ;
11
- private string $ passphrase ;
11
+ private ? string $ passphrase = ' test_passphrase ' ;
12
12
13
13
protected array $ config = [
14
14
'digest_alg ' => 'sha256 ' ,
@@ -31,15 +31,9 @@ public function generateKeys(?string $passphrase = null, ?array $configArgs = nu
31
31
{
32
32
$ keys = openssl_pkey_new ($ this ->config );
33
33
34
- if ($ passphrase != null ) {
35
- $ this ->passphrase = $ passphrase ;
36
- } else {
37
- $ this ->passphrase = (string )rand (100000 , 999999 );
38
- }
39
-
40
34
if ($ keys ) {
41
- openssl_pkey_export ($ keys , $ private, $ passphrase , $ configArgs );
42
- $ this ->privateKey = $ private ;
35
+ openssl_pkey_export ($ keys , $ private );
36
+ $ this ->privateKey = $ this -> _encryptPrivateKey (privateKey: $ private) ;
43
37
44
38
$ pub = openssl_pkey_get_details ($ keys );
45
39
@@ -51,22 +45,40 @@ public function generateKeys(?string $passphrase = null, ?array $configArgs = nu
51
45
return $ this ;
52
46
}
53
47
48
+ protected function _encryptPrivateKey (string $ privateKey , string $ salt = 'salt ' ): string
49
+ {
50
+ $ aes = new AESCryptoServiceProvider ();
51
+ $ aes ->generateIV ();
52
+ $ k = new CryptoKey ();
53
+ $ key = $ k ->getCryptographicKey ($ this ->passphrase , $ salt );
54
+ $ aes ->setKey ($ key );
55
+
56
+ return $ aes ->encrypt ($ privateKey );
57
+ }
58
+
59
+ protected function _decryptPrivateKey (string $ privateKey , string $ salt = 'salt ' ): string
60
+ {
61
+ $ aes = new AESCryptoServiceProvider ();
62
+ $ k = new CryptoKey ();
63
+ $ key = $ k ->getCryptographicKey ($ this ->passphrase , $ salt );
64
+ $ aes ->setKey ($ key );
65
+
66
+ return $ aes ->decrypt ($ privateKey );
67
+ }
68
+
54
69
/**
55
70
* Returns Decrypted Key
56
71
*
57
72
* @return string|\OpenSSLAsymmetricKey
58
73
* @throws DecryptPrivateKeyException
59
74
*/
60
- public function getPrivateKey (): \OpenSSLAsymmetricKey |string
75
+ public function getPrivateKey (string $ salt = ' salt ' , bool $ encrypted = false ): \OpenSSLAsymmetricKey |string
61
76
{
62
- if ($ this ->passphrase != null && $ this ->privateKey != null ) {
63
- $ privateKeyResource = openssl_pkey_get_private ($ this ->privateKey , $ this ->passphrase );
64
-
65
- if ($ privateKeyResource == false ) {
66
- throw new DecryptPrivateKeyException ();
67
- }
68
-
69
- return $ privateKeyResource ;
77
+ if (!$ encrypted ) {
78
+ return $ this ->_decryptPrivateKey (
79
+ privateKey: $ this ->privateKey ,
80
+ salt: $ salt
81
+ );
70
82
}
71
83
72
84
return $ this ->privateKey ;
@@ -78,7 +90,7 @@ public function getPrivateKey(): \OpenSSLAsymmetricKey|string
78
90
* @param string $privateKey
79
91
* @param string $passphrase
80
92
*/
81
- public function setPrivateKey (string $ privateKey , string $ passphrase ): void
93
+ public function setPrivateKey (string $ privateKey , string $ passphrase, string $ salt = ' salt ' ): void
82
94
{
83
95
$ this ->passphrase = $ passphrase ;
84
96
$ this ->privateKey = $ privateKey ;
@@ -109,7 +121,7 @@ public function setPublicKey(string $publicKey): void
109
121
*
110
122
* @return string
111
123
*/
112
- public function getPassphrase (): string
124
+ public function getPassphrase (): ? string
113
125
{
114
126
return $ this ->passphrase ;
115
127
}
0 commit comments