-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathSimpleRSAPubKey.cs
76 lines (62 loc) · 1.87 KB
/
SimpleRSAPubKey.cs
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
namespace X509CertificateTool;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Text.RegularExpressions;
internal class SimpleRSAPubKey
{
string modulus;
string exponent;
private SimpleRSAPubKey() { }
internal SimpleRSAPubKey(X509Certificate2 cert)
{
var key = cert.GetRSAPublicKey();
string keyAsXml = key.ToXmlString(false);
modulus = GetModulus(keyAsXml);
exponent = GetExponent(keyAsXml);
}
internal bool EqualsKeyXml(string keyAsXml)
{
string otherModulus = GetModulus(keyAsXml);
if (!modulus.Equals(otherModulus))
{
return false;
}
string otherExponent = GetExponent(keyAsXml);
return exponent.Equals(otherExponent);
}
static string GetModulus(string keyAsXml)
{
return GetCryptoValue("Modulus", keyAsXml);
}
static string GetExponent(string keyAsXml)
{
return GetCryptoValue("Exponent", keyAsXml);
}
static string GetCryptoValue(string localName, string keyAsXml)
{
//string modStart = String.Format("<{0}>", localName);
//string modEnd = String.Format("</{0}>", localName);
//int modStartI = keyAsXml.IndexOf(modStart) + modStart.Length;
//int modEndI = keyAsXml.IndexOf(modEnd);
//return keyAsXml.Substring(modStartI, modEndI - modStartI);
string regexStr = string.Format(@"<(\S+:)?{0}>(.*)</(\S+:)?{0}>", localName);
string match = Regex.Match(keyAsXml, regexStr).Groups[2].Value;
return match;
}
public override string ToString() =>
$"""
<RSAKeyValue xmlns=\"http://www.w3.org/2000/09/xmldsig#\">
<Modulus>{modulus}</Modulus>
<Exponent>{exponent}</Exponent>
</RSAKeyValue>
""";
public static string CanonicalizeKey(string someKeyXml)
{
SimpleRSAPubKey newKey = new()
{
exponent = GetExponent(someKeyXml),
modulus = GetModulus(someKeyXml)
};
return newKey.ToString();
}
}