41 lines
1.3 KiB
C#
41 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
|
|
{
|
|
private string? _pem;
|
|
|
|
public virtual string Pem { get => _pem; init => _pem = value; }
|
|
|
|
public string? PemPath => FileName is null ? null : Path.Combine(Directory, FileName);
|
|
|
|
public string Directory { get; set; } = string.Empty;
|
|
|
|
public string? FileName { get; set; }
|
|
|
|
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() { }
|
|
|
|
// 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}.");
|
|
}
|
|
}
|
|
}
|
|
} |