Skip to content

Commit d0c07e5

Browse files
committed
Example code for EdDSA
1 parent faf4ed8 commit d0c07e5

File tree

4 files changed

+124
-0
lines changed

4 files changed

+124
-0
lines changed

EddsaJwtSigning/README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
# JWT Signing using Edwards-curve Digital Signature Algorithm (EdDSA) in .NET Core
2+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
Microsoft Visual Studio Solution File, Format Version 12.00
3+
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "ScottBrady91.BlogExampleCode.EddsaJwtSigning", "ScottBrady91.BlogExampleCode.EddsaJwtSigning\ScottBrady91.BlogExampleCode.EddsaJwtSigning.csproj", "{D4BCC483-1CB6-4E49-9AFD-F1B23EE9EDAF}"
4+
EndProject
5+
Global
6+
GlobalSection(SolutionConfigurationPlatforms) = preSolution
7+
Debug|Any CPU = Debug|Any CPU
8+
Release|Any CPU = Release|Any CPU
9+
EndGlobalSection
10+
GlobalSection(ProjectConfigurationPlatforms) = postSolution
11+
{D4BCC483-1CB6-4E49-9AFD-F1B23EE9EDAF}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
12+
{D4BCC483-1CB6-4E49-9AFD-F1B23EE9EDAF}.Debug|Any CPU.Build.0 = Debug|Any CPU
13+
{D4BCC483-1CB6-4E49-9AFD-F1B23EE9EDAF}.Release|Any CPU.ActiveCfg = Release|Any CPU
14+
{D4BCC483-1CB6-4E49-9AFD-F1B23EE9EDAF}.Release|Any CPU.Build.0 = Release|Any CPU
15+
EndGlobalSection
16+
EndGlobal
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
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+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>netcoreapp3.1</TargetFramework>
6+
</PropertyGroup>
7+
8+
<ItemGroup>
9+
<PackageReference Include="Portable.BouncyCastle" Version="1.8.6.7" />
10+
<PackageReference Include="ScottBrady.IdentityModel" Version="1.1.0" />
11+
</ItemGroup>
12+
13+
</Project>

0 commit comments

Comments
 (0)