@@ -55,7 +55,7 @@ public static function encrypt(string $payload, string $userPublicKey, string $u
55
55
$ curve = NistCurve::curve256 ();
56
56
57
57
// get local key pair
58
- list ($ localPublicKeyObject , $ localPrivateKeyObject ) = self ::createLocalKey ();
58
+ list ($ localPublicKeyObject , $ localPrivateKeyObject ) = self ::createLocalKeyObject ();
59
59
$ localPublicKey = hex2bin (Utils::serializePublicKey ($ localPublicKeyObject ));
60
60
61
61
// get user public key object
@@ -182,19 +182,19 @@ private static function createInfo(string $type, string $context): string
182
182
/**
183
183
* @return array
184
184
*/
185
- private static function createLocalKey (): array
185
+ private static function createLocalKeyObject (): array
186
186
{
187
187
try {
188
- return self ::createLocalKeyUsingOpenSSL ();
188
+ return self ::createLocalKeyObjectUsingOpenSSL ();
189
189
} catch (\Exception $ e ) {
190
- return self ::createLocalKeyUsingPurePhpMethod ();
190
+ return self ::createLocalKeyObjectUsingPurePhpMethod ();
191
191
}
192
192
}
193
193
194
194
/**
195
195
* @return array
196
196
*/
197
- private static function createLocalKeyUsingPurePhpMethod (): array
197
+ private static function createLocalKeyObjectUsingPurePhpMethod (): array
198
198
{
199
199
$ curve = NistCurve::curve256 ();
200
200
$ privateKey = $ curve ->createPrivateKey ();
@@ -208,19 +208,23 @@ private static function createLocalKeyUsingPurePhpMethod(): array
208
208
/**
209
209
* @return array
210
210
*/
211
- private static function createLocalKeyUsingOpenSSL (): array
211
+ private static function createLocalKeyObjectUsingOpenSSL (): array
212
212
{
213
- $ key = openssl_pkey_new ([
213
+ $ keyResource = openssl_pkey_new ([
214
214
'curve_name ' => 'prime256v1 ' ,
215
215
'private_key_type ' => OPENSSL_KEYTYPE_EC ,
216
216
]);
217
- $ res = openssl_pkey_export ( $ key , $ out );
218
- if (false === $ res ) {
217
+
218
+ if (! $ keyResource ) {
219
219
throw new \RuntimeException ('Unable to create the key ' );
220
220
}
221
- $ res = openssl_pkey_get_private ($ out );
222
221
223
- $ details = openssl_pkey_get_details ($ res );
222
+ $ details = openssl_pkey_get_details ($ keyResource );
223
+ openssl_pkey_free ($ keyResource );
224
+
225
+ if (!$ details ) {
226
+ throw new \RuntimeException ('Unable to get the key details ' );
227
+ }
224
228
225
229
return [
226
230
PublicKey::create (Point::create (
0 commit comments