44 lines
1.8 KiB
C#
44 lines
1.8 KiB
C#
using DigitalData.Core.Abstractions.Security;
|
|
using System.Security.Cryptography;
|
|
|
|
namespace DigitalData.Core.Security.Extensions
|
|
{
|
|
public static class RSAExtensions
|
|
{
|
|
public static RSA ToRSA(this string pem)
|
|
{
|
|
var rsa = RSA.Create();
|
|
rsa.ImportFromPem(pem);
|
|
return rsa;
|
|
}
|
|
|
|
public static bool TryGetEncryptor(this IDictionary<string, IRSAEncryptor> pairs, string issuer, string audience, out IRSAEncryptor? encryptor)
|
|
=> pairs.TryGetValue($"{issuer}:{audience}", out encryptor);
|
|
|
|
public static IRSAEncryptor? GetEncryptor(this IDictionary<string, IRSAEncryptor> pairs, string issuer, string audience)
|
|
=> pairs.TryGetEncryptor(issuer: issuer, audience: audience, out var encryptor) ? encryptor : null;
|
|
|
|
public static IRSADecryptor GetRSADecryptor(this ICryptFactory factory, string issuer, string audience)
|
|
=> factory[$"{issuer}:{audience}"];
|
|
|
|
public static bool TryGetRSADecryptor(this ICryptFactory factory, string issuer, string audience, out IRSADecryptor? decryptor)
|
|
=> factory.TryGetRSADecryptor($"{issuer}:{audience}", out decryptor);
|
|
|
|
public static IRSAEncryptor GetRSAEncryptor(this ICryptFactory factory, string issuer, string audience)
|
|
=> factory[$"{issuer}:{audience}"].Encryptor;
|
|
|
|
public static bool TryGetRSADecryptor(this ICryptFactory factory, string issuer, string audience, out IRSAEncryptor? encryptor)
|
|
{
|
|
if(factory.TryGetRSADecryptor($"{issuer}:{audience}", out var decryptor) && decryptor is not null)
|
|
{
|
|
encryptor = decryptor.Encryptor;
|
|
return true;
|
|
}
|
|
else
|
|
{
|
|
encryptor = null;
|
|
return false;
|
|
}
|
|
}
|
|
}
|
|
} |