Developer 02 0ff89b4906 Reapply "refactor(RSACryptographer): Entfernte nullbare Eigenschaft von Issuer und Audience."
This reverts commit 600d17ef40a1ed5092ba3bde0c22c03f825ae1fb.
2024-12-05 23:18:19 +01:00

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}");
}
}
}