@@ -164,20 +164,20 @@ final class ECKey extends OpenSSLKeyAbstract{
164164 '038186 ' . // BIT STRING, length 134
165165 '00 ' ; // prepend with NUL - pubkey will follow
166166
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 );
169169 }
170170
171- public function createPEM (string $ crv = self ::CRV_P521 ):string {
171+ public static function createPEM (string $ crv = self ::CRV_P521 ):string {
172172
173- if (openssl_pkey_export ($ this -> createKey ($ crv ), $ pem ) === false ){
173+ if (openssl_pkey_export (self :: createKey ($ crv ), $ pem ) === false ){
174174 throw new RuntimeException ('unable to export the key ' );
175175 }
176176
177177 return $ pem ;
178178 }
179179
180- public function privateKeyToPEM (array $ jwk ):string {
180+ public static function privateJWKtoPEM (array $ jwk ):string {
181181
182182 if (!isset ($ jwk ['crv ' ])){
183183 throw new InvalidArgumentException ('"crv" not set ' );
@@ -197,13 +197,13 @@ public function privateKeyToPEM(array $jwk):string{
197197 default => throw new RuntimeException (sprintf ('unsupported curve: "%s" ' , $ jwk ['crv ' ])),
198198 };
199199
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 ' ]);
202202
203203 return Util::formatPEM ($ der , 'EC PRIVATE ' );
204204 }
205205
206- public function publicKeyToPEM (array $ jwk ):string {
206+ public static function publicJWKtoPEM (array $ jwk ):string {
207207
208208 if (!isset ($ jwk ['crv ' ])){
209209 throw new InvalidArgumentException ('"crv" not set ' );
@@ -224,7 +224,7 @@ public function publicKeyToPEM(array $jwk):string{
224224 };
225225
226226 $ der = sodium_hex2bin ($ der );
227- $ der .= $ this -> getKey ($ jwk ['crv ' ], $ key ['x ' ], $ key ['y ' ]);
227+ $ der .= self :: getKey ($ jwk ['crv ' ], $ key ['x ' ], $ key ['y ' ]);
228228
229229 return Util::formatPEM ($ der );
230230 }
@@ -239,18 +239,17 @@ protected static function parseJWK(array $jsonKeyData):array{
239239 throw new InvalidArgumentException ('"crv" not set ' );
240240 }
241241
242- $ ecKey = new self ;
243- $ public = $ ecKey ->publicKeyToPEM ($ jsonKeyData );
242+ $ public = self ::publicJWKtoPEM ($ jsonKeyData );
244243 $ private = null ;
245244
246245 if (isset ($ jsonKeyData ['d ' ])){
247- $ private = $ ecKey -> privateKeyToPEM ($ jsonKeyData );
246+ $ private = self :: privateJWKtoPEM ($ jsonKeyData );
248247 }
249248
250249 return [$ private , $ public ];
251250 }
252251
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 {
254253 $ details = openssl_pkey_get_details ($ key );
255254
256255 if ($ details === false ){
@@ -266,9 +265,9 @@ protected function toArray(OpenSSLAsymmetricKey $key, bool $private, string|null
266265 }
267266
268267 $ 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 );
270269
271- $ curveSize = $ this -> getNistCurveSize ($ crv );
270+ $ curveSize = self :: getNistCurveSize ($ crv );
272271 $ params = self ::PARAMS_PUBLIC ;
273272
274273 if ($ private === true ){
@@ -277,14 +276,14 @@ protected function toArray(OpenSSLAsymmetricKey $key, bool $private, string|null
277276
278277 foreach ($ params as $ param ){
279278 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 ));
281280 }
282281 }
283282
284283 return $ jwk ;
285284 }
286285
287- private function createKey (string $ crv ):OpenSSLAsymmetricKey {
286+ private static function createKey (string $ crv ):OpenSSLAsymmetricKey {
288287
289288 if (!isset (self ::CRV_OPENSSL [$ crv ])){
290289 throw new InvalidArgumentException ('the given curve is not supported ' ); // @codeCoverageIgnore
@@ -302,15 +301,15 @@ private function createKey(string $crv):OpenSSLAsymmetricKey{
302301 return $ key ;
303302 }
304303
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 );
307306
308307 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 );
311310 }
312311
313- private function getNistCurveSize (string $ curve ):int {
312+ private static function getNistCurveSize (string $ curve ):int {
314313
315314 $ size = match ($ curve ){
316315 self ::CRV_P256 , self ::CRV_P256K => 256 ,
@@ -322,7 +321,7 @@ private function getNistCurveSize(string $curve):int{
322321 return (int )ceil ($ size / 8 );
323322 }
324323
325- private function zeropad (string $ str , int $ length ):string {
324+ private static function zeropad (string $ str , int $ length ):string {
326325 return str_pad ($ str , $ length , "\x00" , STR_PAD_LEFT );
327326 }
328327
0 commit comments