2026-02-27 17:57:42 +00:00
|
|
|
using System.Buffers.Text;
|
|
|
|
|
using System.Security.Cryptography;
|
|
|
|
|
using IdentityShroud.Core.Messages;
|
|
|
|
|
|
|
|
|
|
namespace IdentityShroud.Core.Security.Keys.Rsa;
|
|
|
|
|
|
|
|
|
|
public class RsaProvider : IKeyProvider
|
|
|
|
|
{
|
2026-03-16 19:15:04 +01:00
|
|
|
public bool IsPublic => true;
|
|
|
|
|
|
|
|
|
|
public KeyData CreateKey(KeyPolicy policy)
|
2026-02-27 17:57:42 +00:00
|
|
|
{
|
|
|
|
|
if (policy is RsaKeyPolicy p)
|
|
|
|
|
{
|
|
|
|
|
using var rsa = RSA.Create(p.KeySize);
|
2026-03-16 19:15:04 +01:00
|
|
|
var publicParamaters = rsa.ExportParameters(includePrivateParameters: false);
|
|
|
|
|
return new KeyData(
|
|
|
|
|
rsa.ExportPkcs8PrivateKey(),
|
|
|
|
|
new()
|
|
|
|
|
{
|
|
|
|
|
["e"] = Base64Url.EncodeToString(publicParamaters.Exponent),
|
|
|
|
|
["n"] = Base64Url.EncodeToString(publicParamaters.Modulus),
|
|
|
|
|
});
|
2026-02-27 17:57:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
throw new ArgumentException("Incorrect policy type", nameof(policy));
|
|
|
|
|
}
|
|
|
|
|
|
2026-03-16 19:15:04 +01:00
|
|
|
public void SetJwkParameters(Dictionary<string, string> parameters, JsonWebKey jwk)
|
2026-02-27 17:57:42 +00:00
|
|
|
{
|
2026-03-16 19:15:04 +01:00
|
|
|
jwk.Exponent = parameters["e"];
|
|
|
|
|
jwk.Modulus = parameters["n"];
|
2026-02-27 17:57:42 +00:00
|
|
|
}
|
|
|
|
|
}
|