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

- Schnittstelle aktualisiert
 - standardmäßig als leerer String zugewiesen.
This commit is contained in:
Developer 02 2024-12-05 20:07:17 +01:00
parent 8787c04917
commit 16565eca4d
3 changed files with 46 additions and 8 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,20 +1,58 @@
using DigitalData.Core.Abstractions.Security;
using System.Security.Cryptography;
using System.Text.Json.Serialization;
namespace DigitalData.Core.Security.Cryptographer
{
public class RSACryptographer : IRSACryptographer
public class RSACryptographer : IRSACryptographer, IJsonOnDeserialized
{
public required virtual string Pem { get; init; }
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; }
public string Issuer { get; init; } = string.Empty;
public string? Audience { get; init; }
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}");
}
}
}

View File

@ -5,7 +5,7 @@ namespace DigitalData.Core.Security.Cryptographer
{
public class RSAEncryptor : RSACryptographer, IRSAEncryptor, IRSACryptographer
{
public override required string Pem
public override 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();