From eeb50e837db0a433850e25655e17e1534a90c1ca Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Tue, 19 Nov 2024 23:14:44 +0100 Subject: [PATCH] =?UTF-8?q?feat:=20Unterst=C3=BCtzung=20f=C3=BCr=20`IRSADe?= =?UTF-8?q?cryptor`=20und=20Verwaltung=20der=20RSA-Entschl=C3=BCsselung=20?= =?UTF-8?q?in=20den=20Klassen=20`ICryptFactory`=20und=20`CryptFactory`=20h?= =?UTF-8?q?inzugef=C3=BCgt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ICryptFactory: - `IRSADecryptor this[string key]`-Indexer für den Zugriff auf Entschlüssler per Schlüssel hinzugefügt. - Methode `TryGetRSADecryptor` für das sichere Abrufen von Entschlüsslern eingeführt. CryptFactory: - `IRSADecryptor`-Indexer für die Verwaltung von Entschlüsslern implementiert. - Ein `Decryptors`-Dictionary hinzugefügt, um RSA-Entschlüssler nach Schlüssel zu speichern. - Konstruktor aktualisiert, um `Decryptors` mit einem bereitgestellten oder leeren Dictionary zu initialisieren. - `TryGetRSADecryptor` zur Entschlüssler-Abfrage implementiert. --- .../Security/ICryptFactory.cs | 18 +++++++++++------- DigitalData.Core.Security/CryptFactory.cs | 10 +++++++++- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/DigitalData.Core.Abstractions/Security/ICryptFactory.cs b/DigitalData.Core.Abstractions/Security/ICryptFactory.cs index 2903775..a1d1a12 100644 --- a/DigitalData.Core.Abstractions/Security/ICryptFactory.cs +++ b/DigitalData.Core.Abstractions/Security/ICryptFactory.cs @@ -4,19 +4,19 @@ namespace DigitalData.Core.Abstractions.Security { public interface ICryptFactory { - public int KeySizeInBits { get; init; } + int KeySizeInBits { get; init; } - public string PbePassword { init; } + string PbePassword { init; } - public PbeEncryptionAlgorithm PbeEncryptionAlgorithm { get; init; } + PbeEncryptionAlgorithm PbeEncryptionAlgorithm { get; init; } - public HashAlgorithmName PbeHashAlgorithmName { get; init; } + HashAlgorithmName PbeHashAlgorithmName { get; init; } - public int PbeIterationCount { get; init; } + int PbeIterationCount { get; init; } - public PbeParameters PbeParameters { get; } + PbeParameters PbeParameters { get; } - public string EncryptedPrivateKeyPemLabel { get; init; } + string EncryptedPrivateKeyPemLabel { get; init; } string CreateRSAPrivateKeyPem(int? keySizeInBits = null); @@ -26,5 +26,9 @@ namespace DigitalData.Core.Abstractions.Security PbeEncryptionAlgorithm? pbeEncryptionAlgorithm = null, HashAlgorithmName? hashAlgorithmName = null, int? iterationCount = null); + + IRSADecryptor this[string key] { get; } + + bool TryGetRSADecryptor(string key, out IRSADecryptor? decryptor); } } \ No newline at end of file diff --git a/DigitalData.Core.Security/CryptFactory.cs b/DigitalData.Core.Security/CryptFactory.cs index 0e7c5b1..6481d74 100644 --- a/DigitalData.Core.Security/CryptFactory.cs +++ b/DigitalData.Core.Security/CryptFactory.cs @@ -26,10 +26,16 @@ namespace DigitalData.Core.Security public string EncryptedPrivateKeyPemLabel { get; init; } = "ENCRYPTED PRIVATE KEY"; - public CryptFactory(ILogger? logger = null) + public IDictionary Decryptors { get; init; } + + public IRSADecryptor this[string key] { get => Decryptors[key]; set => Decryptors[key] = value; } + + public CryptFactory(ILogger? logger = null, IDictionary? decryptors = null) { _lazyPbeParameters = new(() => new PbeParameters(PbeEncryptionAlgorithm, PbeHashAlgorithmName, PbeIterationCount)); + Decryptors = decryptors ?? new Dictionary(); + logger?.LogInformation("CryptFactory initialized. Core.Secrets version: {Version}, Created on: {CreationDate}.", Secrets.Version, Secrets.CreationDate.ToString("dd.MM.yyyy")); } @@ -58,5 +64,7 @@ namespace DigitalData.Core.Security return new string(pemChars); } + + public bool TryGetRSADecryptor(string key, out IRSADecryptor? decryptor) => _decryptors.TryGetValue(key, out decryptor); } } \ No newline at end of file