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