44 lines
1.3 KiB
C#

using DigitalData.Core.Abstractions.Security;
using System.Security.Cryptography;
using System.Text.Json.Serialization;
namespace DigitalData.Core.Security.Cryptographer
{
public class RSACryptographer : IRSACryptographer, IJsonOnDeserialized
{
private string? _pem;
private string? _pemPath;
public virtual string Pem { get => _pem; init => _pem = value; }
public string? PemPath { get => _pemPath; init => _pemPath = value; }
public RSAEncryptionPadding Padding { get; init; } = RSAEncryptionPadding.OaepSHA256;
protected virtual RSA RSA { get; } = RSA.Create();
public string Issuer { get; init; } = string.Empty;
public string Audience { get; init; } = string.Empty;
internal RSACryptographer() { }
public void OnDeserialized()
{
Init();
}
// TODO: make file read asynchronous, consider multiple routing
public virtual void Init()
{
if(_pem is null)
{
if (File.Exists(PemPath))
_pem = File.ReadAllText(PemPath);
else
throw new FileNotFoundException($"Pem is not assigned. Furthermore Pem file is not found in {PemPath}. Issuer is {Issuer} and audience {Audience}.");
}
}
}
}