refactor: Validierung für Pem-Eigenschaft hinzugefügt, um Ausnahme bei Nicht-Initialisierung auszulösen
- Die Pem-Eigenschaft wurde aktualisiert, um eine Validierung hinzuzufügen, die eine InvalidOperationException auslöst, falls sie vor der Initialisierung aufgerufen wird. - Nicht verwendeten Import System.Text.Json.Serialization entfernt. - Fehlermeldungen wurden erweitert, um Issuer und Audience für eine bessere Debugging-Kontextbereitschaft einzuschließen.
This commit is contained in:
parent
3f61b5064c
commit
0c6c84852d
@ -1,5 +1,4 @@
|
|||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
using System.Text.Json.Serialization;
|
|
||||||
|
|
||||||
namespace DigitalData.Core.Abstractions.Security
|
namespace DigitalData.Core.Abstractions.Security
|
||||||
{
|
{
|
||||||
@ -12,7 +11,5 @@ namespace DigitalData.Core.Abstractions.Security
|
|||||||
public string Issuer { get; init; }
|
public string Issuer { get; init; }
|
||||||
|
|
||||||
public string Audience { get; init; }
|
public string Audience { get; init; }
|
||||||
|
|
||||||
public void Init();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1,6 +1,5 @@
|
|||||||
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
|
||||||
{
|
{
|
||||||
@ -8,7 +7,12 @@ namespace DigitalData.Core.Security.Cryptographer
|
|||||||
{
|
{
|
||||||
private string? _pem;
|
private string? _pem;
|
||||||
|
|
||||||
public virtual string Pem { get => _pem; init => _pem = value; }
|
public string Pem
|
||||||
|
{
|
||||||
|
get => _pem
|
||||||
|
?? throw new InvalidOperationException($"Pem is not initialized. Please ensure that the PEM is set or properly loaded from the file. Issuer: {Issuer}, Audience: {Audience}.");
|
||||||
|
init => _pem = value;
|
||||||
|
}
|
||||||
|
|
||||||
public string? PemPath => FileName is null ? null : Path.Combine(Directory, FileName);
|
public string? PemPath => FileName is null ? null : Path.Combine(Directory, FileName);
|
||||||
|
|
||||||
|
|||||||
@ -27,10 +27,6 @@ namespace DigitalData.Core.Security.Cryptographer
|
|||||||
|
|
||||||
public IRSAEncryptor Encryptor => _lazyEncryptor.Value;
|
public IRSAEncryptor Encryptor => _lazyEncryptor.Value;
|
||||||
|
|
||||||
private readonly Lazy<RSA> lazyRSA;
|
|
||||||
|
|
||||||
protected override RSA RSA => lazyRSA.Value;
|
|
||||||
|
|
||||||
public RSADecryptor()
|
public RSADecryptor()
|
||||||
{
|
{
|
||||||
_lazyEncryptor = new(() => new RSAEncryptor()
|
_lazyEncryptor = new(() => new RSAEncryptor()
|
||||||
@ -38,21 +34,19 @@ namespace DigitalData.Core.Security.Cryptographer
|
|||||||
Pem = RSA.ExportRSAPublicKeyPem(),
|
Pem = RSA.ExportRSAPublicKeyPem(),
|
||||||
Padding = Padding
|
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);
|
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();
|
||||||
|
|
||||||
|
public override void Init()
|
||||||
|
{
|
||||||
|
base.Init();
|
||||||
|
if (_password is null)
|
||||||
|
RSA.ImportFromPem(Pem);
|
||||||
|
else
|
||||||
|
RSA.ImportFromEncryptedPem(Pem, _password.AsSpan());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -4,21 +4,17 @@ using DigitalData.Core.Security.Extensions;
|
|||||||
namespace DigitalData.Core.Security.Cryptographer
|
namespace DigitalData.Core.Security.Cryptographer
|
||||||
{
|
{
|
||||||
public class RSAEncryptor : RSACryptographer, IRSAEncryptor, IRSACryptographer
|
public class RSAEncryptor : RSACryptographer, IRSAEncryptor, IRSACryptographer
|
||||||
{
|
{
|
||||||
public override string Pem
|
|
||||||
{
|
|
||||||
get => base.Pem;
|
|
||||||
init
|
|
||||||
{
|
|
||||||
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;
|
public bool Verify(string data, string signature) => Encrypt(data) == signature;
|
||||||
|
|
||||||
|
public override void Init()
|
||||||
|
{
|
||||||
|
base.Init();
|
||||||
|
RSA.ImportFromPem(base.Pem);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user