fix(Core.Security): pem-Importprozess in den Initilizer mehetods verschoben

This commit is contained in:
Developer 02 2024-11-18 17:14:42 +01:00
parent eccf2b32ce
commit 26a68cd477
3 changed files with 32 additions and 5 deletions

View File

@ -5,7 +5,7 @@ namespace DigitalData.Core.Security
{
public class RSACryptographer : IRSACryptographer
{
public required string Pem { get; init; }
public required virtual string Pem { get; init; }
public required RSAEncryptionPadding Padding { get; init; }

View File

@ -1,5 +1,6 @@
using DigitalData.Core.Abstractions.Security;
using DigitalData.Core.Security.Extensions;
using System.Runtime.Serialization;
namespace DigitalData.Core.Security
{
@ -21,8 +22,26 @@ namespace DigitalData.Core.Security
}
}
public RSADecryptor()
public RSADecryptor() { }
public RSADecryptor(string pem, string? password = null)
{
Pem = pem;
Password = password;
Initialize();
}
[OnDeserialized]
private void OnDeserialized(StreamingContext context)
{
Initialize();
}
private void Initialize()
{
if (string.IsNullOrWhiteSpace(Pem))
throw new InvalidOperationException("Pem cannot be null or empty.");
if (Password is null)
_rsa.ImportFromPem(Pem);
else

View File

@ -5,9 +5,17 @@ namespace DigitalData.Core.Security
{
public class RSAEncryptor : RSACryptographer, IRSAEncryptor, IRSACryptographer
{
public RSAEncryptor()
{
_rsa.ImportFromPem(Pem);
public override required string Pem
{
get => base.Pem;
init
{
if (string.IsNullOrWhiteSpace(Pem))
throw new InvalidOperationException("Pem cannot be null or empty.");
_rsa.ImportFromPem(base.Pem);
base.Pem = value;
}
}
public byte[] Encrypt(byte[] data) => _rsa.Encrypt(data, Padding);