@@ -164,20 +164,20 @@ final class ECKey extends OpenSSLKeyAbstract{
164
164
'038186 ' . // BIT STRING, length 134
165
165
'00 ' ; // prepend with NUL - pubkey will follow
166
166
167
- public function create (string |null $ kid = null , string |null $ use = null , string $ crv = self ::CRV_P521 ):array {
168
- return $ this -> toArray ($ this -> createKey ($ crv ), true , $ kid , $ use );
167
+ public static function create (string |null $ kid = null , string |null $ use = null , string $ crv = self ::CRV_P521 ):array {
168
+ return self :: toArray (self :: createKey ($ crv ), true , $ kid , $ use );
169
169
}
170
170
171
- public function createPEM (string $ crv = self ::CRV_P521 ):string {
171
+ public static function createPEM (string $ crv = self ::CRV_P521 ):string {
172
172
173
- if (openssl_pkey_export ($ this -> createKey ($ crv ), $ pem ) === false ){
173
+ if (openssl_pkey_export (self :: createKey ($ crv ), $ pem ) === false ){
174
174
throw new RuntimeException ('unable to export the key ' );
175
175
}
176
176
177
177
return $ pem ;
178
178
}
179
179
180
- public function privateKeyToPEM (array $ jwk ):string {
180
+ public static function privateJWKtoPEM (array $ jwk ):string {
181
181
182
182
if (!isset ($ jwk ['crv ' ])){
183
183
throw new InvalidArgumentException ('"crv" not set ' );
@@ -197,13 +197,13 @@ public function privateKeyToPEM(array $jwk):string{
197
197
default => throw new RuntimeException (sprintf ('unsupported curve: "%s" ' , $ jwk ['crv ' ])),
198
198
};
199
199
200
- $ der = sodium_hex2bin (sprintf ($ der , sodium_bin2hex ($ this -> zeropad ($ key ['d ' ], $ padSize ))));
201
- $ der .= $ this -> getKey ($ jwk ['crv ' ], $ key ['x ' ], $ key ['y ' ]);
200
+ $ der = sodium_hex2bin (sprintf ($ der , sodium_bin2hex (self :: zeropad ($ key ['d ' ], $ padSize ))));
201
+ $ der .= self :: getKey ($ jwk ['crv ' ], $ key ['x ' ], $ key ['y ' ]);
202
202
203
203
return Util::formatPEM ($ der , 'EC PRIVATE ' );
204
204
}
205
205
206
- public function publicKeyToPEM (array $ jwk ):string {
206
+ public static function publicJWKtoPEM (array $ jwk ):string {
207
207
208
208
if (!isset ($ jwk ['crv ' ])){
209
209
throw new InvalidArgumentException ('"crv" not set ' );
@@ -224,7 +224,7 @@ public function publicKeyToPEM(array $jwk):string{
224
224
};
225
225
226
226
$ der = sodium_hex2bin ($ der );
227
- $ der .= $ this -> getKey ($ jwk ['crv ' ], $ key ['x ' ], $ key ['y ' ]);
227
+ $ der .= self :: getKey ($ jwk ['crv ' ], $ key ['x ' ], $ key ['y ' ]);
228
228
229
229
return Util::formatPEM ($ der );
230
230
}
@@ -239,18 +239,17 @@ protected static function parseJWK(array $jsonKeyData):array{
239
239
throw new InvalidArgumentException ('"crv" not set ' );
240
240
}
241
241
242
- $ ecKey = new self ;
243
- $ public = $ ecKey ->publicKeyToPEM ($ jsonKeyData );
242
+ $ public = self ::publicJWKtoPEM ($ jsonKeyData );
244
243
$ private = null ;
245
244
246
245
if (isset ($ jsonKeyData ['d ' ])){
247
- $ private = $ ecKey -> privateKeyToPEM ($ jsonKeyData );
246
+ $ private = self :: privateJWKtoPEM ($ jsonKeyData );
248
247
}
249
248
250
249
return [$ private , $ public ];
251
250
}
252
251
253
- protected function toArray (OpenSSLAsymmetricKey $ key , bool $ private , string |null $ kid = null , string |null $ use = null ):array {
252
+ protected static function toArray (OpenSSLAsymmetricKey $ key , bool $ private , string |null $ kid = null , string |null $ use = null ):array {
254
253
$ details = openssl_pkey_get_details ($ key );
255
254
256
255
if ($ details === false ){
@@ -266,9 +265,9 @@ protected function toArray(OpenSSLAsymmetricKey $key, bool $private, string|null
266
265
}
267
266
268
267
$ crv = self ::EC_CURVES [$ details ['ec ' ]['curve_name ' ]];
269
- $ jwk = $ this -> addInformationalValues (['kty ' => 'EC ' , 'crv ' => $ crv ], $ kid , $ use );
268
+ $ jwk = self :: addInformationalValues (['kty ' => 'EC ' , 'crv ' => $ crv ], $ kid , $ use );
270
269
271
- $ curveSize = $ this -> getNistCurveSize ($ crv );
270
+ $ curveSize = self :: getNistCurveSize ($ crv );
272
271
$ params = self ::PARAMS_PUBLIC ;
273
272
274
273
if ($ private === true ){
@@ -277,14 +276,14 @@ protected function toArray(OpenSSLAsymmetricKey $key, bool $private, string|null
277
276
278
277
foreach ($ params as $ param ){
279
278
if (isset ($ details ['ec ' ][$ param ])){
280
- $ jwk [$ param ] = Util::base64encode ($ this -> zeropad ($ details ['ec ' ][$ param ], $ curveSize ));
279
+ $ jwk [$ param ] = Util::base64encode (self :: zeropad ($ details ['ec ' ][$ param ], $ curveSize ));
281
280
}
282
281
}
283
282
284
283
return $ jwk ;
285
284
}
286
285
287
- private function createKey (string $ crv ):OpenSSLAsymmetricKey {
286
+ private static function createKey (string $ crv ):OpenSSLAsymmetricKey {
288
287
289
288
if (!isset (self ::CRV_OPENSSL [$ crv ])){
290
289
throw new InvalidArgumentException ('the given curve is not supported ' ); // @codeCoverageIgnore
@@ -302,15 +301,15 @@ private function createKey(string $crv):OpenSSLAsymmetricKey{
302
301
return $ key ;
303
302
}
304
303
305
- private function getKey (string $ crv , string $ x , string $ y ):string {
306
- $ curveSize = $ this -> getNistCurveSize ($ crv );
304
+ private static function getKey (string $ crv , string $ x , string $ y ):string {
305
+ $ curveSize = self :: getNistCurveSize ($ crv );
307
306
308
307
return "\04" .
309
- $ this -> zeropad (ltrim ($ x , "\x00" ), $ curveSize ).
310
- $ this -> zeropad (ltrim ($ y , "\x00" ), $ curveSize );
308
+ self :: zeropad (ltrim ($ x , "\x00" ), $ curveSize ).
309
+ self :: zeropad (ltrim ($ y , "\x00" ), $ curveSize );
311
310
}
312
311
313
- private function getNistCurveSize (string $ curve ):int {
312
+ private static function getNistCurveSize (string $ curve ):int {
314
313
315
314
$ size = match ($ curve ){
316
315
self ::CRV_P256 , self ::CRV_P256K => 256 ,
@@ -322,7 +321,7 @@ private function getNistCurveSize(string $curve):int{
322
321
return (int )ceil ($ size / 8 );
323
322
}
324
323
325
- private function zeropad (string $ str , int $ length ):string {
324
+ private static function zeropad (string $ str , int $ length ):string {
326
325
return str_pad ($ str , $ length , "\x00" , STR_PAD_LEFT );
327
326
}
328
327
0 commit comments