Reapply "refactor(RSACryptographer): Entfernte nullbare Eigenschaft von Issuer und Audience."
This reverts commit 600d17ef40a1ed5092ba3bde0c22c03f825ae1fb.
This commit is contained in:
parent
600d17ef40
commit
0ff89b4906
@ -8,8 +8,8 @@ namespace DigitalData.Core.Abstractions.Security
|
|||||||
|
|
||||||
public RSAEncryptionPadding Padding { get; init; }
|
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; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,20 +1,58 @@
|
|||||||
using DigitalData.Core.Abstractions.Security;
|
using DigitalData.Core.Abstractions.Security;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
|
using System.Text.Json.Serialization;
|
||||||
|
|
||||||
namespace DigitalData.Core.Security.Cryptographer
|
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;
|
public RSAEncryptionPadding Padding { get; init; } = RSAEncryptionPadding.OaepSHA256;
|
||||||
|
|
||||||
protected virtual RSA RSA { get; } = RSA.Create();
|
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() { }
|
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}");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -5,7 +5,7 @@ namespace DigitalData.Core.Security.Cryptographer
|
|||||||
{
|
{
|
||||||
public class RSAEncryptor : RSACryptographer, IRSAEncryptor, IRSACryptographer
|
public class RSAEncryptor : RSACryptographer, IRSAEncryptor, IRSACryptographer
|
||||||
{
|
{
|
||||||
public override required string Pem
|
public override string Pem
|
||||||
{
|
{
|
||||||
get => base.Pem;
|
get => base.Pem;
|
||||||
init
|
init
|
||||||
@ -14,7 +14,7 @@ namespace DigitalData.Core.Security.Cryptographer
|
|||||||
base.Pem = value;
|
base.Pem = value;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] Encrypt(byte[] data) => RSA.Encrypt(data, Padding);
|
public byte[] Encrypt(byte[] data) => RSA.Encrypt(data, Padding);
|
||||||
|
|
||||||
public string Encrypt(string data) => RSA.Encrypt(data.Base64ToByte(), Padding).BytesToString();
|
public string Encrypt(string data) => RSA.Encrypt(data.Base64ToByte(), Padding).BytesToString();
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user