refactor(RSADecryptor): statt der Verwendung einer separaten init-Methode zur Initialisierung von RSA, wurde Lazy Loading verwendet.
This commit is contained in:
parent
5010224500
commit
cdb0009e7c
@ -9,7 +9,7 @@ namespace DigitalData.Core.Security
|
||||
|
||||
public RSAEncryptionPadding Padding { get; init; } = RSAEncryptionPadding.OaepSHA256;
|
||||
|
||||
protected readonly RSA _rsa = RSA.Create();
|
||||
protected virtual RSA RSA { get; } = RSA.Create();
|
||||
|
||||
internal RSACryptographer() { }
|
||||
}
|
||||
|
||||
@ -1,6 +1,6 @@
|
||||
using DigitalData.Core.Abstractions.Security;
|
||||
using DigitalData.Core.Security.Extensions;
|
||||
using System.Runtime.Serialization;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
namespace DigitalData.Core.Security
|
||||
{
|
||||
@ -16,33 +16,32 @@ namespace DigitalData.Core.Security
|
||||
|
||||
public IRSAEncryptor Encryptor => _lazyEncryptor.Value;
|
||||
|
||||
private readonly Lazy<RSA> lazyRSA;
|
||||
|
||||
protected override RSA RSA => lazyRSA.Value;
|
||||
|
||||
public RSADecryptor()
|
||||
{
|
||||
_lazyEncryptor = new(() => new RSAEncryptor()
|
||||
{
|
||||
Pem = _rsa.ExportRSAPublicKeyPem(),
|
||||
Pem = RSA.ExportRSAPublicKeyPem(),
|
||||
Padding = Padding
|
||||
});
|
||||
|
||||
lazyRSA = new(() =>
|
||||
{
|
||||
var rsa = RSA.Create();
|
||||
if (Password is null)
|
||||
RSA.ImportFromPem(Pem);
|
||||
else
|
||||
RSA.ImportFromEncryptedPem(Pem, Password.AsSpan());
|
||||
|
||||
return rsa;
|
||||
});
|
||||
}
|
||||
|
||||
public byte[] Decrypt(byte[] data) => RSA.Decrypt(data, Padding);
|
||||
|
||||
[OnDeserialized]
|
||||
private void OnDeserialized(StreamingContext context) => Init();
|
||||
|
||||
private IRSADecryptor Init()
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Pem))
|
||||
throw new InvalidOperationException("Pem cannot be null or empty.");
|
||||
|
||||
if (Password is null)
|
||||
_rsa.ImportFromPem(Pem);
|
||||
else
|
||||
_rsa.ImportFromEncryptedPem(Pem, Password.AsSpan());
|
||||
|
||||
return this;
|
||||
}
|
||||
|
||||
public byte[] Decrypt(byte[] data) => _rsa.Decrypt(data, Padding);
|
||||
|
||||
public string Decrypt(string data) => _rsa.Decrypt(data.Base64ToByte(), Padding).BytesToString();
|
||||
public string Decrypt(string data) => RSA.Decrypt(data.Base64ToByte(), Padding).BytesToString();
|
||||
}
|
||||
}
|
||||
@ -10,17 +10,14 @@ namespace DigitalData.Core.Security
|
||||
get => base.Pem;
|
||||
init
|
||||
{
|
||||
if (string.IsNullOrWhiteSpace(Pem))
|
||||
throw new InvalidOperationException("Pem cannot be null or empty.");
|
||||
|
||||
_rsa.ImportFromPem(base.Pem);
|
||||
RSA.ImportFromPem(base.Pem);
|
||||
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();
|
||||
|
||||
public bool Verify(string data, string signature) => Encrypt(data) == signature;
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user