From 7459f057482fe0a8660a60413304870989a0e8e1 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Fri, 13 Dec 2024 13:47:44 +0100 Subject: [PATCH] =?UTF-8?q?refactor(RSAFactory):=20Schaffung=20einer=20nic?= =?UTF-8?q?ht-generischen,=20getrennten=20Schnittstelle,=20um=20eine=20sta?= =?UTF-8?q?tische=20Standardinstanz=20erstellen=20zu=20k=C3=B6nnen.=20=20-?= =?UTF-8?q?=20Statische=20Instanzklasse=20erstellt.=20=20-=20Geordnete=20D?= =?UTF-8?q?I-Registrierungsmethoden.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Security/IRSAFactory.cs | 4 +- .../Cryptographer/RSAFactory.cs | 4 -- DigitalData.Core.Security/DIExtensions.cs | 42 ++++++++++++++++--- DigitalData.Core.Security/Instance.cs | 14 +++++++ 4 files changed, 53 insertions(+), 11 deletions(-) create mode 100644 DigitalData.Core.Security/Instance.cs diff --git a/DigitalData.Core.Abstractions/Security/IRSAFactory.cs b/DigitalData.Core.Abstractions/Security/IRSAFactory.cs index 7ff812e..96ece10 100644 --- a/DigitalData.Core.Abstractions/Security/IRSAFactory.cs +++ b/DigitalData.Core.Abstractions/Security/IRSAFactory.cs @@ -2,7 +2,7 @@ namespace DigitalData.Core.Abstractions.Security { - public interface IRSAFactory + public interface IRSAFactory { string CreateRSAPrivateKeyPem(int? keySizeInBits = null); @@ -13,4 +13,6 @@ namespace DigitalData.Core.Abstractions.Security HashAlgorithmName? hashAlgorithmName = null, int? iterationCount = null); } + + public interface IRSAFactory : IRSAFactory { } } \ No newline at end of file diff --git a/DigitalData.Core.Security/Cryptographer/RSAFactory.cs b/DigitalData.Core.Security/Cryptographer/RSAFactory.cs index ee4cca9..a6a4a88 100644 --- a/DigitalData.Core.Security/Cryptographer/RSAFactory.cs +++ b/DigitalData.Core.Security/Cryptographer/RSAFactory.cs @@ -7,10 +7,6 @@ namespace DigitalData.Core.Security.Cryptographer { public class RSAFactory : IRSAFactory where TRSAFactoryParams : RSAFactoryParams { - private static readonly Lazy> LazyInstance = new(() => new(Options.Create(new()))); - - public static RSAFactory Static => LazyInstance.Value; - protected readonly TRSAFactoryParams _params; public RSAFactory(IOptions options) => _params = options.Value; diff --git a/DigitalData.Core.Security/DIExtensions.cs b/DigitalData.Core.Security/DIExtensions.cs index 652394d..bc7688b 100644 --- a/DigitalData.Core.Security/DIExtensions.cs +++ b/DigitalData.Core.Security/DIExtensions.cs @@ -37,19 +37,49 @@ namespace DigitalData.Core.Security where TAsymCryptParams : AsymCryptParams => services.AddSingleton(Options.Create(param)).AddAsymCryptService(); - private static IServiceCollection AddRSAFactory(this IServiceCollection services) - where TRSAFactoryParams : RSAFactoryParams + /// + /// Registers default RSA Factory instance with default params + /// + /// + /// + public static IServiceCollection AddRSAFactory(this IServiceCollection services) { - services.TryAddScoped, RSAFactory>(); + services.TryAddScoped(_ => Instance.RSAFactory); return services; } + /// + /// Registers a custom RSA Factory instance with specified parameters from the given configuration section. + /// + /// + /// + /// + /// public static IServiceCollection AddRSAFactory(this IServiceCollection services, IConfigurationSection section) where TRSAFactoryParams : RSAFactoryParams - => services.Configure(section).AddRSAFactory(); + { + services.Configure(section); + services.TryAddSingleton, RSAFactory>(); + return services; + } - public static IServiceCollection AddRSAFactory(this IServiceCollection services, TRSAFactoryParams param) + /// + /// Registers an RSA Factory with the specified parameters from the given instance. Optionally, sets it as the default factory. + /// + /// The type of the RSA factory parameters. + /// + /// + /// If true, the factory is registered as the default . Otherwise, it is registered as . + /// The updated with the RSA Factory registered. + public static IServiceCollection AddRSAFactory(this IServiceCollection services, TRSAFactoryParams rsaParams, bool setAsDefault = false) where TRSAFactoryParams : RSAFactoryParams - => services.AddSingleton(Options.Create(param)).AddRSAFactory(); + { + services.TryAddSingleton(Options.Create(rsaParams)); + if (setAsDefault) + services.TryAddSingleton>(); + else + services.TryAddSingleton, RSAFactory>(); + return services; + } } } \ No newline at end of file diff --git a/DigitalData.Core.Security/Instance.cs b/DigitalData.Core.Security/Instance.cs new file mode 100644 index 0000000..e71f5a5 --- /dev/null +++ b/DigitalData.Core.Security/Instance.cs @@ -0,0 +1,14 @@ +using DigitalData.Core.Abstractions.Security; +using DigitalData.Core.Security.Config; +using DigitalData.Core.Security.Cryptographer; +using Microsoft.Extensions.Options; + +namespace DigitalData.Core.Security +{ + public static class Instance + { + private static readonly Lazy> LazyInstance = new(() => new(Options.Create(new()))); + + public static IRSAFactory RSAFactory => LazyInstance.Value; + } +} \ No newline at end of file