Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
RsaSsaPkcsSha256.cs
1using System.Collections.Generic;
2using System.Security.Cryptography;
3using System.Text;
4using Waher.Content;
5
6namespace Waher.Security.JWS
7{
13 {
14 private RSA rsa;
15 private SHA256 sha;
16 private KeyValuePair<string, object>[] jwk;
17
23 : this(4096)
24 {
25 }
26
32 public RsaSsaPkcsSha256(int KeySize)
33 {
34 try
35 {
36 this.rsa = RSA.Create();
37 this.rsa.KeySize = KeySize;
38 }
39 catch (CryptographicException ex)
40 {
41 throw new CryptographicException("Unable to get access to cryptographic key. Was application initially run using another user?", ex);
42 }
43
44 this.Init();
45 }
46
53 {
54 this.rsa = RSA;
55 this.Init();
56 }
57
63 public RsaSsaPkcsSha256(RSAParameters Parameters)
64 {
65 this.rsa = RSA.Create();
66 this.RSA.ImportParameters(Parameters);
67
68 this.Init();
69 }
70
75 public void ImportKey(RSA RSA)
76 {
77 RSAParameters P = RSA.ExportParameters(true);
78 this.rsa.ImportParameters(P);
79 this.jwk = GetJwk(this.rsa, false);
80 }
81
85 public RSA RSA => this.rsa;
86
87 private void Init()
88 {
89 this.jwk = GetJwk(this.rsa, false);
90 this.sha = SHA256.Create();
91 }
92
99 public static KeyValuePair<string, object>[] GetJwk(RSA RSA, bool IncludePrivate)
100 {
101 RSAParameters Parameters = RSA.ExportParameters(IncludePrivate);
102
103 if (IncludePrivate)
104 {
105 return new KeyValuePair<string, object>[]
106 {
107 new KeyValuePair<string, object>("kty", "RSA"),
108 new KeyValuePair<string, object>("n", Base64Url.Encode(Parameters.Modulus)),
109 new KeyValuePair<string, object>("e", Base64Url.Encode(Parameters.Exponent)),
110 new KeyValuePair<string, object>("d", Base64Url.Encode(Parameters.D)),
111 new KeyValuePair<string, object>("p", Base64Url.Encode(Parameters.P)),
112 new KeyValuePair<string, object>("q", Base64Url.Encode(Parameters.Q)),
113 new KeyValuePair<string, object>("dp", Base64Url.Encode(Parameters.DP)),
114 new KeyValuePair<string, object>("dq", Base64Url.Encode(Parameters.DQ)),
115 new KeyValuePair<string, object>("qi", Base64Url.Encode(Parameters.InverseQ))
116 };
117 }
118 else
119 {
120 return new KeyValuePair<string, object>[]
121 {
122 new KeyValuePair<string, object>("kty", "RSA"),
123 new KeyValuePair<string, object>("n", Base64Url.Encode(Parameters.Modulus)),
124 new KeyValuePair<string, object>("e", Base64Url.Encode(Parameters.Exponent))
125 };
126 }
127 }
128
130 public override void Dispose()
131 {
132 if (!(this.rsa is null))
133 {
134 this.rsa.Dispose();
135 this.rsa = null;
136 }
137
138 if (!(this.sha is null))
139 {
140 this.sha.Dispose();
141 this.sha = null;
142 }
143 }
144
148 public override string Name => "RS256";
149
153 public override bool HasPublicWebKey => true;
154
158 public override IEnumerable<KeyValuePair<string, object>> PublicWebKey => this.jwk;
159
166 public override string Sign(string HeaderEncoded, string PayloadEncoded)
167 {
168 byte[] SignatureBin;
169 string Token = HeaderEncoded + "." + PayloadEncoded;
170 byte[] TokenBin = Encoding.ASCII.GetBytes(Token);
171
172 lock (this.rsa)
173 {
174 SignatureBin = this.rsa.SignHash(this.sha.ComputeHash(TokenBin), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
175 }
176
177 return Base64Url.Encode(SignatureBin);
178 }
179 }
180}
Static class that does BASE64URL encoding (using URL and filename safe alphabet), as defined in RFC46...
Definition: Base64Url.cs:11
static string Encode(byte[] Data)
Converts a binary block of data to a Base64URL-encoded string.
Definition: Base64Url.cs:48
Abstract base class for JWS algorithm.
Definition: JwsAlgorithm.cs:15
RSASSA-PKCS1-v1_5 SHA-256 algorithm. https://tools.ietf.org/html/rfc3447#page-32
RsaSsaPkcsSha256(int KeySize)
RSASSA-PKCS1-v1_5 SHA-256 algorithm. https://tools.ietf.org/html/rfc3447#page-32
RsaSsaPkcsSha256(RSAParameters Parameters)
RSASSA-PKCS1-v1_5 SHA-256 algorithm. https://tools.ietf.org/html/rfc3447#page-32
override bool HasPublicWebKey
If the algorithm has a public key.
RSA RSA
RSA Cryptographic service provider.
void ImportKey(RSA RSA)
Imports a new key from an external RSA Cryptographic service provider.
override IEnumerable< KeyValuePair< string, object > > PublicWebKey
The public JSON web key, if supported.
RsaSsaPkcsSha256()
RSASSA-PKCS1-v1_5 SHA-256 algorithm. https://tools.ietf.org/html/rfc3447#page-32
override string Name
Short name for algorithm.
override void Dispose()
IDisposable.Dispose
RsaSsaPkcsSha256(RSA RSA)
RSASSA-PKCS1-v1_5 SHA-256 algorithm. https://tools.ietf.org/html/rfc3447#page-32
static KeyValuePair< string, object >[] GetJwk(RSA RSA, bool IncludePrivate)
Creaates a JSON Web Key
override string Sign(string HeaderEncoded, string PayloadEncoded)
Signs data.