Revert "refactor(RSACryptographer): Entfernte nullbare Eigenschaft von Issuer und Audience."

This reverts commit 16565eca4d0d92182306e72081c73caa19f944c7.
This commit is contained in:
Developer 02 2024-12-05 23:08:13 +01:00
parent 16565eca4d
commit 600d17ef40
3 changed files with 8 additions and 46 deletions

View File

@ -8,8 +8,8 @@ namespace DigitalData.Core.Abstractions.Security
public RSAEncryptionPadding Padding { get; init; }
public string Issuer { get; init; }
public string? Issuer { get; init; }
public string Audience { get; init; }
public string? Audience { get; init; }
}
}

View File

@ -1,58 +1,20 @@
using DigitalData.Core.Abstractions.Security;
using System.Security.Cryptography;
using System.Text.Json.Serialization;
namespace DigitalData.Core.Security.Cryptographer
{
public class RSACryptographer : IRSACryptographer, IJsonOnDeserialized
public class RSACryptographer : IRSACryptographer
{
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 required virtual string Pem { get; init; }
public RSAEncryptionPadding Padding { get; init; } = RSAEncryptionPadding.OaepSHA256;
protected virtual RSA RSA { get; } = RSA.Create();
public string Issuer { get; init; } = string.Empty;
public string? Issuer { get; init; }
public string Audience { get; init; } = string.Empty;
public string? Audience { get; init; }
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}");
}
}
}

View File

@ -5,7 +5,7 @@ namespace DigitalData.Core.Security.Cryptographer
{
public class RSAEncryptor : RSACryptographer, IRSAEncryptor, IRSACryptographer
{
public override string Pem
public override required string Pem
{
get => base.Pem;
init
@ -14,7 +14,7 @@ namespace DigitalData.Core.Security.Cryptographer
base.Pem = value;
}
}
public byte[] Encrypt(byte[] data) => RSA.Encrypt(data, Padding);
public string Encrypt(string data) => RSA.Encrypt(data.Base64ToByte(), Padding).BytesToString();