1using System.Collections.Generic;
2using System.Security.Cryptography;
16 private KeyValuePair<string, object>[] jwk;
36 this.rsa =
RSA.Create();
37 this.rsa.KeySize = KeySize;
39 catch (CryptographicException ex)
41 throw new CryptographicException(
"Unable to get access to cryptographic key. Was application initially run using another user?", ex);
65 this.rsa =
RSA.Create();
66 this.
RSA.ImportParameters(Parameters);
77 RSAParameters P =
RSA.ExportParameters(
true);
78 this.rsa.ImportParameters(P);
79 this.jwk =
GetJwk(this.rsa,
false);
89 this.jwk =
GetJwk(this.rsa,
false);
90 this.sha = SHA256.Create();
99 public static KeyValuePair<string, object>[]
GetJwk(
RSA RSA,
bool IncludePrivate)
101 RSAParameters Parameters =
RSA.ExportParameters(IncludePrivate);
105 return new KeyValuePair<string, object>[]
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)),
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))
120 return new KeyValuePair<string, object>[]
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))
132 if (!(this.rsa is
null))
138 if (!(this.sha is
null))
148 public override string Name =>
"RS256";
158 public override IEnumerable<KeyValuePair<string, object>>
PublicWebKey => this.jwk;
166 public override string Sign(
string HeaderEncoded,
string PayloadEncoded)
169 string Token = HeaderEncoded +
"." + PayloadEncoded;
170 byte[] TokenBin = Encoding.ASCII.GetBytes(Token);
174 SignatureBin = this.rsa.SignHash(this.sha.ComputeHash(TokenBin), HashAlgorithmName.SHA256, RSASignaturePadding.Pkcs1);
Static class that does BASE64URL encoding (using URL and filename safe alphabet), as defined in RFC46...
static string Encode(byte[] Data)
Converts a binary block of data to a Base64URL-encoded string.
Abstract base class for JWS algorithm.
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.