forked from scottbrady91/samples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEcdsaKeyLoading.cs
More file actions
118 lines (100 loc) · 4.2 KB
/
Copy pathEcdsaKeyLoading.cs
File metadata and controls
118 lines (100 loc) · 4.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
using System;
using System.Linq;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text;
using FluentAssertions;
using Microsoft.IdentityModel.Tokens;
using Xunit;
namespace EcdsaKeyLoading
{
public class EcdsaKeyLoading
{
private readonly byte[] data = Encoding.UTF8.GetBytes("dooooooooooooom");
[Fact] // OPTION 1: Have .NET generate an EC key for you
public void CreateNewKey()
{
var key = ECDsa.Create(ECCurve.NamedCurves.nistP256);
Verify(key);
}
[Fact] // OPTION 2: Load an EC from a JWK
public void LoadKeyFromParameters()
{
// {
// "kty": "EC",
// "crv": "P-256",
// "x": "MKBCTNIcKUSDii11ySs3526iDZ8AiTo7Tu6KPAqv7D4",
// "y": "4Etl6SRW2YiLUrN5vfvVHuhp7x8PxltmWWlbbM4IFyM",
// "d": "870MB6gfuTJ4HtUnUvYMyJpr5eUZNP4Bk43bVdj3eAE",
// "use: "sig",
// "kid": "my EC key"
// }
const string crv = "P-256";
const string d = "870MB6gfuTJ4HtUnUvYMyJpr5eUZNP4Bk43bVdj3eAE";
const string x = "MKBCTNIcKUSDii11ySs3526iDZ8AiTo7Tu6KPAqv7D4";
const string y = "4Etl6SRW2YiLUrN5vfvVHuhp7x8PxltmWWlbbM4IFyM";
// parse curve from JOSE format
// https://www.iana.org/assignments/jose/jose.xhtml#web-key-elliptic-curve
var curve = crv switch
{
"P-256" => ECCurve.NamedCurves.nistP256,
"P-384" => ECCurve.NamedCurves.nistP384,
"P-521" => ECCurve.NamedCurves.nistP521,
_ => throw new NotSupportedException()
};
var key = ECDsa.Create(new ECParameters
{
Curve = curve,
D = Base64UrlEncoder.DecodeBytes(d), // optional private key
Q = new ECPoint
{
X = Base64UrlEncoder.DecodeBytes(x),
Y = Base64UrlEncoder.DecodeBytes(y)
}
});
Verify(key);
}
[Fact] // OPTION 3: Load an EC from an X.509
public void LoadFromX509()
{
var cert = new CertificateRequest("cn=Test", ECDsa.Create(ECCurve.NamedCurves.nistP256), HashAlgorithmName.SHA256)
.CreateSelfSigned(DateTime.UtcNow.AddDays(-2), DateTime.UtcNow.AddDays(2));
var key = cert.GetECDsaPrivateKey();
Verify(key);
}
[Fact] // OPTION 4: Load an EC from hex string
public void LoadFromHex()
{
const string privateKey = "c711e5080f2b58260fe19741a7913e8301c1128ec8e80b8009406e5047e6e1ef";
const string publicKey = "04e33993f0210a4973a94c26667007d1b56fe886e8b3c2afdd66aa9e4937478ad20acfbdc666e3cec3510ce85d40365fc2045e5adb7e675198cf57c6638efa1bdb";
var privateKeyBytes = FromHexString(privateKey);
var publicKeyBytes = FromHexString(publicKey);
var key = ECDsa.Create(new ECParameters
{
Curve = ECCurve.NamedCurves.nistP256,
D = privateKeyBytes,
Q = new ECPoint
{
X = publicKeyBytes.Skip(1).Take(32).ToArray(),
Y = publicKeyBytes.Skip(33).ToArray()
}
});
Verify(key);
}
private void Verify(ECDsa key)
{
// create signature
var signature = key.SignData(data, HashAlgorithmName.SHA256);
// validate signature with public key
var pubKey = ECDsa.Create(key.ExportParameters(false));
pubKey.VerifyData(data, signature, HashAlgorithmName.SHA256).Should().BeTrue();
}
private static byte[] FromHexString(string hex) {
var numberChars = hex.Length;
var hexAsBytes = new byte[numberChars / 2];
for (var i = 0; i < numberChars; i += 2)
hexAsBytes[i / 2] = Convert.ToByte(hex.Substring(i, 2), 16);
return hexAsBytes;
}
}
}