From a3931414e3db233cb7dad35b61ee0dddb242553f Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Sat, 21 Dec 2024 10:10:50 +0100 Subject: [PATCH] refactor(AsymCryptService): Indexer und IEnumerable-Implementierung zur Vereinfachung entfernt --- .../Security/IAsymCryptService.cs | 4 +- DigitalData.Core.Security/AsymCryptService.cs | 39 +++---------------- 2 files changed, 6 insertions(+), 37 deletions(-) diff --git a/DigitalData.Core.Abstractions/Security/IAsymCryptService.cs b/DigitalData.Core.Abstractions/Security/IAsymCryptService.cs index 5d9b6da..368b576 100644 --- a/DigitalData.Core.Abstractions/Security/IAsymCryptService.cs +++ b/DigitalData.Core.Abstractions/Security/IAsymCryptService.cs @@ -1,13 +1,11 @@ namespace DigitalData.Core.Abstractions.Security { - public interface IAsymCryptService : IRSAFactory, IEnumerable + public interface IAsymCryptService : IRSAFactory { IEnumerable Decryptors { get; } IRSADecryptor Vault { get; } - IRSADecryptor this[string key] { get; } - IEnumerable Encryptors { get; } } } \ No newline at end of file diff --git a/DigitalData.Core.Security/AsymCryptService.cs b/DigitalData.Core.Security/AsymCryptService.cs index 3c363f2..d37c5f2 100644 --- a/DigitalData.Core.Security/AsymCryptService.cs +++ b/DigitalData.Core.Security/AsymCryptService.cs @@ -3,11 +3,10 @@ using DigitalData.Core.Security.Config; using DigitalData.Core.Security.Cryptographer; using Microsoft.Extensions.Logging; using Microsoft.Extensions.Options; -using System.Collections; - + namespace DigitalData.Core.Security { - public class AsymCryptService : RSAFactory, IAsymCryptService, IRSAFactory, IEnumerable + public class AsymCryptService : RSAFactory, IAsymCryptService, IRSAFactory { public IEnumerable Decryptors { get; } @@ -16,26 +15,9 @@ namespace DigitalData.Core.Security /// public IRSADecryptor Vault { get; } - public IRSADecryptor this[string key] - { - get - { - var key_params = key.Split(_params.KeyNameSeparator); - - if (key_params.Length != 2) - throw new ArgumentException($"Invalid key format. Expected two segments separated by '{_params.KeyNameSeparator}', but received: '{key}'.", nameof(key)); - - return _params.Decryptors.FirstOrDefault(d => d.Issuer == key_params[0] && d.Audience == key_params[1]) - ?? throw new KeyNotFoundException($"No decryptor found matching the issuer '{key_params[0]}' and audience '{key_params[1]}'."); - } - } + private readonly Lazy> _lazyEncryptors; - public IRSADecryptor this[int index] => index < 0 || index >= Decryptors.Count() - ? Decryptors.ElementAt(index) - : throw new ArgumentOutOfRangeException( - nameof(index), - index, - $"The index {index} is out of range. The valid indices for {GetType()} are between 0 and {Decryptors.Count() - 1} (inclusive). Please ensure the index is within this range."); + public IEnumerable Encryptors => _lazyEncryptors.Value; public AsymCryptService(IOptions options, ILogger? logger = null) : base(options) { @@ -51,19 +33,8 @@ namespace DigitalData.Core.Security Decryptors = _params.Decryptors; Vault = _params.Vault ?? Decryptors.First(); - } - - public IEnumerator GetEnumerator() => Decryptors.GetEnumerator(); - IEnumerator IEnumerable.GetEnumerator() => Decryptors.GetEnumerator(); - - public IEnumerable Encryptors - { - get - { - foreach (var decryptor in Decryptors) - yield return decryptor.Encryptor; - } + _lazyEncryptors = new(Decryptors.Select(decryptor => decryptor.Encryptor)); } } } \ No newline at end of file