@@ -8,10 +8,10 @@ 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
- 'digest_alg ' => 'sha512 ' ,
14
+ 'digest_alg ' => 'sha256 ' ,
15
15
'private_key_bits ' => 4096 ,
16
16
'private_key_type ' => OPENSSL_KEYTYPE_RSA ,
17
17
];
@@ -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
+ private 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
+ private 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
}
@@ -142,4 +154,23 @@ public function setConfig(array $config): void
142
154
{
143
155
$ this ->config = $ config ;
144
156
}
157
+
158
+ /**
159
+ * Returns the fingerprint of the public key.
160
+ *
161
+ * @param bool $md5 Whether to return the MD5 fingerprint instead of SHA-256.
162
+ * @return string The fingerprint of the public key.
163
+ */
164
+ public function getFingerprint (bool $ md5 = false ): string
165
+ {
166
+ $ derData = preg_replace ('/-----.*?-----/ ' , '' , base64_decode ($ this ->publicKey ));
167
+ $ derData = preg_replace ('/\s+/ ' , '' , $ derData );
168
+ $ derData = base64_decode ($ derData );
169
+
170
+ if ($ md5 ) {
171
+ return implode (': ' , str_split (hash ('md5 ' , $ derData ), 2 ));
172
+ }
173
+
174
+ return hash ('sha256 ' , $ derData );
175
+ }
145
176
}
0 commit comments