1
+ using System ;
2
+ using System . Security . Claims ;
3
+ using System . Text ;
4
+ using Microsoft . IdentityModel . JsonWebTokens ;
5
+ using Microsoft . IdentityModel . Tokens ;
6
+ using Org . BouncyCastle . Crypto . Generators ;
7
+ using Org . BouncyCastle . Crypto . Parameters ;
8
+ using Org . BouncyCastle . Crypto . Signers ;
9
+ using Org . BouncyCastle . Security ;
10
+ using ScottBrady . IdentityModel . Crypto ;
11
+ using ScottBrady . IdentityModel . Tokens ;
12
+
13
+ namespace ScottBrady91 . BlogExampleCode . EddsaJwtSigning
14
+ {
15
+ public static class Program
16
+ {
17
+ public static void Main ( )
18
+ {
19
+ EdDsaWithBouncyCastle ( ) ;
20
+ EddsaJwt ( ) ;
21
+ }
22
+
23
+ private static void EdDsaWithBouncyCastle ( )
24
+ {
25
+ // message to sign & verify
26
+ var message = Encoding . UTF8 . GetBytes ( "Bob Loblaw" ) ;
27
+
28
+ // private/public key generation
29
+ var keyPairGenerator = new Ed25519KeyPairGenerator ( ) ;
30
+ keyPairGenerator . Init ( new Ed25519KeyGenerationParameters ( new SecureRandom ( ) ) ) ;
31
+ var keyPair = keyPairGenerator . GenerateKeyPair ( ) ;
32
+
33
+ var privateKey = ( Ed25519PrivateKeyParameters ) keyPair . Private ;
34
+ var publicKey = ( Ed25519PublicKeyParameters ) keyPair . Public ;
35
+
36
+ // keys are 32-bytes each
37
+ var privateKeyBytes = privateKey . GetEncoded ( ) ;
38
+ var publicKeyBytes = publicKey . GetEncoded ( ) ;
39
+ Console . WriteLine ( "Private key = " + Convert . ToBase64String ( privateKeyBytes ) ) ;
40
+ Console . WriteLine ( "Public key = " + Convert . ToBase64String ( publicKeyBytes ) ) ;
41
+
42
+ // signature generation
43
+ var signer = new Ed25519Signer ( ) ;
44
+ signer . Init ( true , privateKey ) ;
45
+ signer . BlockUpdate ( message , 0 , message . Length ) ;
46
+
47
+ byte [ ] signature = signer . GenerateSignature ( ) ;
48
+ Console . WriteLine ( "Signature = " + Convert . ToBase64String ( signature ) ) ;
49
+
50
+ // signature validation
51
+ var validator = new Ed25519Signer ( ) ;
52
+ validator . Init ( false , publicKey ) ;
53
+ validator . BlockUpdate ( message , 0 , message . Length ) ;
54
+
55
+ bool isValidSignature = validator . VerifySignature ( signature ) ;
56
+ Console . WriteLine ( "Signature is valid: " + isValidSignature ) ;
57
+ }
58
+
59
+ private static void EddsaJwt ( )
60
+ {
61
+ // private/public key generation
62
+ var keyPairGenerator = new Ed25519KeyPairGenerator ( ) ;
63
+ keyPairGenerator . Init ( new Ed25519KeyGenerationParameters ( new SecureRandom ( ) ) ) ;
64
+ var keyPair = keyPairGenerator . GenerateKeyPair ( ) ;
65
+
66
+ var privateKey = ( Ed25519PrivateKeyParameters ) keyPair . Private ;
67
+ var publicKey = ( Ed25519PublicKeyParameters ) keyPair . Public ;
68
+
69
+ var handler = new JsonWebTokenHandler ( ) ;
70
+
71
+ // create JWT
72
+ var token = handler . CreateToken ( new SecurityTokenDescriptor
73
+ {
74
+ Issuer = "me" ,
75
+ Audience = "you" ,
76
+ Subject = new ClaimsIdentity ( new [ ] { new Claim ( "sub" , "123" ) } ) ,
77
+
78
+ // using JOSE algorithm "EdDSA"
79
+ SigningCredentials = new SigningCredentials ( new EdDsaSecurityKey ( privateKey ) , ExtendedSecurityAlgorithms . EdDsa )
80
+ } ) ;
81
+ Console . WriteLine ( "JWT = " + token ) ;
82
+
83
+ // validate JWT
84
+ var result = handler . ValidateToken ( token , new TokenValidationParameters
85
+ {
86
+ ValidIssuer = "me" ,
87
+ ValidAudience = "you" ,
88
+ IssuerSigningKey = new EdDsaSecurityKey ( publicKey )
89
+ } ) ;
90
+ Console . WriteLine ( "Is JWT signature valid: " + result . IsValid ) ;
91
+ }
92
+ }
93
+ }
0 commit comments