58 lines
1.6 KiB
C#
58 lines
1.6 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
|
|
{
|
|
ValidatePemInit();
|
|
_pem = value;
|
|
}
|
|
}
|
|
|
|
public string? PemPath
|
|
{
|
|
get => _pemPath;
|
|
init
|
|
{
|
|
_pemPath = value;
|
|
if (value is null)
|
|
return;
|
|
ValidatePemInit();
|
|
_pem = File.ReadAllText(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()
|
|
{
|
|
if (Pem is null)
|
|
throw new InvalidOperationException($"Pem must be initialized. Issuer: {Issuer} and Audience: {Audience}");
|
|
}
|
|
|
|
private void ValidatePemInit()
|
|
{
|
|
if (_pem is not null)
|
|
throw new InvalidOperationException($"Pem can only be initilized once. Remove one of the Pem or Pem file initilizations. Issuer: {Issuer} and Audience: {Audience}");
|
|
}
|
|
}
|
|
} |