From 68ef0a7537e3195e5243d7affc4461c1f2eb2086 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Fri, 13 Dec 2024 16:17:35 +0100 Subject: [PATCH] refactor(RSACryptographer): Entfernen von _pem, IsPemNull, SetPem, Init und Methoden zur Vereinfachung von RSAEncryptor --- .../Security/IRSACryptographer.cs | 2 -- .../Cryptographer/RSACryptographer.cs | 25 +++---------------- .../Cryptographer/RSADecryptor.cs | 16 ++++++++++-- .../Cryptographer/RSAEncryptor.cs | 18 +++++++------ 4 files changed, 29 insertions(+), 32 deletions(-) diff --git a/DigitalData.Core.Abstractions/Security/IRSACryptographer.cs b/DigitalData.Core.Abstractions/Security/IRSACryptographer.cs index bbbafa1..ba3a85f 100644 --- a/DigitalData.Core.Abstractions/Security/IRSACryptographer.cs +++ b/DigitalData.Core.Abstractions/Security/IRSACryptographer.cs @@ -11,7 +11,5 @@ namespace DigitalData.Core.Abstractions.Security public string Issuer { get; init; } public string Audience { get; init; } - - public void Init(); } } \ No newline at end of file diff --git a/DigitalData.Core.Security/Cryptographer/RSACryptographer.cs b/DigitalData.Core.Security/Cryptographer/RSACryptographer.cs index 9c93838..ac438f5 100644 --- a/DigitalData.Core.Security/Cryptographer/RSACryptographer.cs +++ b/DigitalData.Core.Security/Cryptographer/RSACryptographer.cs @@ -5,19 +5,8 @@ namespace DigitalData.Core.Security.Cryptographer { public class RSACryptographer : IRSACryptographer { - protected string? _pem; - - public string Pem - { - get => _pem - ?? throw PemIsNullException; - init => _pem = value; - } - - internal bool IsPemNull => _pem is null; - - private InvalidOperationException PemIsNullException => new($"Pem is not initialized. Please ensure that the PEM is set or properly loaded from the file. Issuer: {Issuer}, Audience: {Audience}."); - + public virtual string Pem { get; init; } + public RSAEncryptionPadding Padding { get; init; } = RSAEncryptionPadding.OaepSHA256; protected virtual RSA RSA { get; } = RSA.Create(); @@ -26,14 +15,8 @@ namespace DigitalData.Core.Security.Cryptographer public string Audience { get; init; } = string.Empty; +#pragma warning disable CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. internal RSACryptographer() { } - - internal void SetPem(string pem) => _pem = pem; - - public virtual void Init() - { - if (_pem is null) - throw PemIsNullException; - } +#pragma warning restore CS8618 // Non-nullable field must contain a non-null value when exiting constructor. Consider declaring as nullable. } } \ No newline at end of file diff --git a/DigitalData.Core.Security/Cryptographer/RSADecryptor.cs b/DigitalData.Core.Security/Cryptographer/RSADecryptor.cs index 566ac44..aeba8ca 100644 --- a/DigitalData.Core.Security/Cryptographer/RSADecryptor.cs +++ b/DigitalData.Core.Security/Cryptographer/RSADecryptor.cs @@ -6,6 +6,12 @@ namespace DigitalData.Core.Security.Cryptographer { public class RSADecryptor : RSACryptographer, IRSADecryptor, IRSACryptographer { + private string? _pem; + + public override string Pem { get => _pem ?? throw PemIsNullException; init => _pem = value; } + + public bool IsPemNull => _pem is null; + public bool IsEncrypted { get; init; } private readonly Lazy _lazyEncryptor; @@ -25,13 +31,19 @@ namespace DigitalData.Core.Security.Cryptographer public string Decrypt(string data) => RSA.Decrypt(data.Base64ToByte(), Padding).BytesToString(); - public override void Init() + internal void SetPem(string pem) => _pem = pem; + + public void Init() { - base.Init(); + if (_pem is null) + throw PemIsNullException; + if (IsEncrypted) RSA.ImportFromEncryptedPem(Pem, Secrets.PBE_PASSWORD.AsSpan()); else RSA.ImportFromPem(Pem); } + + private InvalidOperationException PemIsNullException => new($"Pem is not initialized. Please ensure that the PEM is set or properly loaded from the file. Issuer: {Issuer}, Audience: {Audience}."); } } \ No newline at end of file diff --git a/DigitalData.Core.Security/Cryptographer/RSAEncryptor.cs b/DigitalData.Core.Security/Cryptographer/RSAEncryptor.cs index f25f64e..3a40325 100644 --- a/DigitalData.Core.Security/Cryptographer/RSAEncryptor.cs +++ b/DigitalData.Core.Security/Cryptographer/RSAEncryptor.cs @@ -4,17 +4,21 @@ using DigitalData.Core.Security.Extensions; namespace DigitalData.Core.Security.Cryptographer { public class RSAEncryptor : RSACryptographer, IRSAEncryptor, IRSACryptographer - { + { + public override string Pem + { + get => base.Pem; + init + { + base.Pem = value; + RSA.ImportFromPem(value); + } + } + public byte[] Encrypt(byte[] data) => RSA.Encrypt(data, Padding); public string Encrypt(string data) => RSA.Encrypt(data.Base64ToByte(), Padding).BytesToString(); public bool Verify(string data, string signature) => Encrypt(data) == signature; - - public override void Init() - { - base.Init(); - RSA.ImportFromPem(base.Pem); - } } } \ No newline at end of file