63 lines
1.7 KiB
C#
63 lines
1.7 KiB
C#
using DigitalData.Core.Abstractions.Security;
|
|
using DigitalData.Core.Security.Config;
|
|
using System.Security.Cryptography;
|
|
|
|
namespace DigitalData.Core.Security.Cryptographer
|
|
{
|
|
public class RSADecryptor : RSACryptographer, IRSADecryptor, IRSACryptographer
|
|
{
|
|
private string? _pem;
|
|
|
|
public override string Pem
|
|
{
|
|
#pragma warning disable CS8603 // Possible null reference return.
|
|
get => _pem;
|
|
#pragma warning restore CS8603 // Possible null reference return.
|
|
init
|
|
{
|
|
_pem = value;
|
|
Init();
|
|
}
|
|
}
|
|
|
|
public bool IsPemNull => _pem is null;
|
|
|
|
public bool IsEncrypted { get; init; }
|
|
|
|
private readonly Lazy<IRSAEncryptor> _lazyEncryptor;
|
|
|
|
public IRSAEncryptor Encryptor => _lazyEncryptor.Value;
|
|
|
|
public RSADecryptor()
|
|
{
|
|
_lazyEncryptor = new(() => new RSAEncryptor()
|
|
{
|
|
Pem = RSA.ExportRSAPublicKeyPem(),
|
|
Padding = Padding
|
|
});
|
|
}
|
|
|
|
public byte[] Decrypt(byte[] data) => RSA.Decrypt(data, Padding);
|
|
|
|
public string Decrypt(string data) => RSA.Decrypt(data.Base64ToByte(), Padding).BytesToString();
|
|
|
|
internal void SetPem(string pem)
|
|
{
|
|
_pem = pem;
|
|
Init();
|
|
}
|
|
|
|
private void Init()
|
|
{
|
|
if (string.IsNullOrEmpty(_pem))
|
|
throw PemIsNullException;
|
|
|
|
if (IsEncrypted)
|
|
RSA.ImportFromEncryptedPem(Pem, Secrets.PBE_PASSWORD.AsSpan());
|
|
else
|
|
RSA.ImportFromPem(Pem);
|
|
}
|
|
|
|
private InvalidOperationException PemIsNullException => new($"Pem is null or empty. Issuer: {Issuer}, Audience: {Audience}.");
|
|
}
|
|
} |