@@ -27,7 +27,9 @@ pub const SigningMethodPS512 = JWT(rsa_pss.SigningPS512, crypto_rsa.SecretKey, c
2727
2828pub const SigningMethodES256 = JWT (ecdsa .SigningES256 , ecdsa .ecdsa .EcdsaP256Sha256 .SecretKey , ecdsa .ecdsa .EcdsaP256Sha256 .PublicKey );
2929pub const SigningMethodES384 = JWT (ecdsa .SigningES384 , ecdsa .ecdsa .EcdsaP384Sha384 .SecretKey , ecdsa .ecdsa .EcdsaP384Sha384 .PublicKey );
30- // pub const SigningMethodES512 = JWT(ecdsa.SigningES512, ecdsa.ecdsa.SecretKey, ecdsa.ecdsa.PublicKey);
30+ // pub const SigningMethodES512 = JWT(ecdsa.SigningES512, ecdsa.ecdsa.EcdsaP521Sha512.SecretKey, ecdsa.ecdsa.EcdsaP521Sha512.PublicKey);
31+
32+ pub const SigningMethodES256K = JWT (ecdsa .SigningES256K , ecdsa .ecdsa .EcdsaSecp256k1Sha256 .SecretKey , ecdsa .ecdsa .EcdsaSecp256k1Sha256 .PublicKey );
3133
3234pub const SigningMethodEdDSA = JWT (eddsa .SigningEdDSA , eddsa .Ed25519 .SecretKey , eddsa .Ed25519 .PublicKey );
3335pub const SigningMethodED25519 = JWT (eddsa .SigningED25519 , eddsa .Ed25519 .SecretKey , eddsa .Ed25519 .PublicKey );
@@ -406,6 +408,31 @@ test "SigningMethodES384" {
406408
407409}
408410
411+ test "SigningMethodES256K" {
412+ const alloc = std .heap .page_allocator ;
413+
414+ const kp = ecdsa .ecdsa .EcdsaSecp256k1Sha256 .KeyPair .generate ();
415+
416+ const claims = .{
417+ .aud = "example.com" ,
418+ .sub = "foo" ,
419+ };
420+
421+ const s = SigningMethodES256K .init (alloc );
422+ const token_string = try s .sign (claims , kp .secret_key );
423+ try testing .expectEqual (true , token_string .len > 0 );
424+
425+ // ==========
426+
427+ const p = SigningMethodES256K .init (alloc );
428+ var parsed = try p .parse (token_string , kp .public_key );
429+
430+ const claims2 = try parsed .getClaims ();
431+ try testing .expectEqualStrings (claims .aud , claims2 .object .get ("aud" ).? .string );
432+ try testing .expectEqualStrings (claims .sub , claims2 .object .get ("sub" ).? .string );
433+
434+ }
435+
409436test "SigningMethodHS256" {
410437 const alloc = std .heap .page_allocator ;
411438
@@ -608,6 +635,42 @@ test "SigningMethodES256 Check fail" {
608635
609636}
610637
638+ test "SigningMethodES256K Check" {
639+ const alloc = std .heap .page_allocator ;
640+
641+ const pub_key = "04cbcc2ebfaf9f5e874b3cb7e1c66d77db2d51f26e1d92783bb477bb37eb142d5d84b61e80c445d07ddf84e27b9c791db550d0af40aab1898c02cd5c0829c1defc" ;
642+ const pri_key = "c4e29dedecf2d4fef1bb300cce3fcfca3ec086066fd3d03ebc3cc7a36ee900dd" ;
643+ const token_str = "eyJ0eXAiOiJKV1QiLCJhbGciOiJFUzI1NksifQ.eyJmb28iOiJiYXIifQ.Xe92dmU8MrI1d4edE2LEKqSmObZJpkIuz0fERihfn65ikTeeX5zjpyAdlHy9ZSBX8N8sqmJy5fxBTBzV26WvIQ" ;
644+
645+ const encoded_length = ecdsa .ecdsa .EcdsaSecp256k1Sha256 .SecretKey .encoded_length ;
646+
647+ var pri_key_buf : [encoded_length ]u8 = undefined ;
648+ _ = try fmt .hexToBytes (& pri_key_buf , pri_key );
649+
650+ var pub_key_buf : [pub_key .len / 2 ]u8 = undefined ;
651+ const pub_key_bytes = try fmt .hexToBytes (& pub_key_buf , pub_key );
652+
653+ const secret_key = try ecdsa .ecdsa .EcdsaSecp256k1Sha256 .SecretKey .fromBytes (pri_key_buf );
654+ const public_key = try ecdsa .ecdsa .EcdsaSecp256k1Sha256 .PublicKey .fromSec1 (pub_key_bytes );
655+
656+ const claims = .{
657+ .foo = "bar" ,
658+ };
659+
660+ const s = SigningMethodES256K .init (alloc );
661+ const token_string = try s .sign (claims , secret_key );
662+ try testing .expectEqual (true , token_string .len > 0 );
663+
664+ // ==========
665+
666+ const p = SigningMethodES256K .init (alloc );
667+ var parsed = try p .parse (token_str , public_key );
668+
669+ const claims2 = try parsed .getClaims ();
670+ try testing .expectEqualStrings (claims .foo , claims2 .object .get ("foo" ).? .string );
671+
672+ }
673+
611674test "SigningMethodEdDSA Check" {
612675 const alloc = std .heap .page_allocator ;
613676
0 commit comments