refactor(RSAFactory): Schaffung einer nicht-generischen, getrennten Schnittstelle, um eine statische Standardinstanz erstellen zu können.

- Statische Instanzklasse erstellt.
 - Geordnete DI-Registrierungsmethoden.
This commit is contained in:
Developer 02 2024-12-13 13:47:44 +01:00
parent 36f75d003a
commit 7459f05748
4 changed files with 53 additions and 11 deletions

View File

@ -2,7 +2,7 @@
namespace DigitalData.Core.Abstractions.Security namespace DigitalData.Core.Abstractions.Security
{ {
public interface IRSAFactory<TParams> public interface IRSAFactory
{ {
string CreateRSAPrivateKeyPem(int? keySizeInBits = null); string CreateRSAPrivateKeyPem(int? keySizeInBits = null);
@ -13,4 +13,6 @@ namespace DigitalData.Core.Abstractions.Security
HashAlgorithmName? hashAlgorithmName = null, HashAlgorithmName? hashAlgorithmName = null,
int? iterationCount = null); int? iterationCount = null);
} }
public interface IRSAFactory<TParams> : IRSAFactory { }
} }

View File

@ -7,10 +7,6 @@ namespace DigitalData.Core.Security.Cryptographer
{ {
public class RSAFactory<TRSAFactoryParams> : IRSAFactory<TRSAFactoryParams> where TRSAFactoryParams : RSAFactoryParams public class RSAFactory<TRSAFactoryParams> : IRSAFactory<TRSAFactoryParams> where TRSAFactoryParams : RSAFactoryParams
{ {
private static readonly Lazy<RSAFactory<RSAFactoryParams>> LazyInstance = new(() => new(Options.Create<RSAFactoryParams>(new())));
public static RSAFactory<RSAFactoryParams> Static => LazyInstance.Value;
protected readonly TRSAFactoryParams _params; protected readonly TRSAFactoryParams _params;
public RSAFactory(IOptions<TRSAFactoryParams> options) => _params = options.Value; public RSAFactory(IOptions<TRSAFactoryParams> options) => _params = options.Value;

View File

@ -37,19 +37,49 @@ namespace DigitalData.Core.Security
where TAsymCryptParams : AsymCryptParams where TAsymCryptParams : AsymCryptParams
=> services.AddSingleton(Options.Create(param)).AddAsymCryptService<TAsymCryptParams>(); => services.AddSingleton(Options.Create(param)).AddAsymCryptService<TAsymCryptParams>();
private static IServiceCollection AddRSAFactory<TRSAFactoryParams>(this IServiceCollection services) /// <summary>
where TRSAFactoryParams : RSAFactoryParams /// Registers default RSA Factory instance with default params
/// </summary>
/// <param name="services"></param>
/// <returns></returns>
public static IServiceCollection AddRSAFactory(this IServiceCollection services)
{ {
services.TryAddScoped<IRSAFactory<TRSAFactoryParams>, RSAFactory<TRSAFactoryParams>>(); services.TryAddScoped(_ => Instance.RSAFactory);
return services; return services;
} }
/// <summary>
/// Registers a custom RSA Factory instance with specified parameters from the given configuration section.
/// </summary>
/// <typeparam name="TRSAFactoryParams"></typeparam>
/// <param name="services"></param>
/// <param name="section"></param>
/// <returns></returns>
public static IServiceCollection AddRSAFactory<TRSAFactoryParams>(this IServiceCollection services, IConfigurationSection section) public static IServiceCollection AddRSAFactory<TRSAFactoryParams>(this IServiceCollection services, IConfigurationSection section)
where TRSAFactoryParams : RSAFactoryParams where TRSAFactoryParams : RSAFactoryParams
=> services.Configure<TRSAFactoryParams>(section).AddRSAFactory<TRSAFactoryParams>(); {
services.Configure<TRSAFactoryParams>(section);
services.TryAddSingleton<IRSAFactory<TRSAFactoryParams>, RSAFactory<TRSAFactoryParams>>();
return services;
}
public static IServiceCollection AddRSAFactory<TRSAFactoryParams>(this IServiceCollection services, TRSAFactoryParams param) /// <summary>
/// Registers an RSA Factory with the specified parameters from the given instance. Optionally, sets it as the default factory.
/// </summary>
/// <typeparam name="TRSAFactoryParams">The type of the RSA factory parameters.</typeparam>
/// <param name="services"></param>
/// <param name="rsaParams"></param>
/// <param name="setAsDefault">If true, the factory is registered as the default <see cref="IRSAFactory"/>. Otherwise, it is registered as <see cref="IRSAFactory{TRSAFactoryParams}"/>.</param>
/// <returns>The updated <see cref="IServiceCollection"/> with the RSA Factory registered.</returns>
public static IServiceCollection AddRSAFactory<TRSAFactoryParams>(this IServiceCollection services, TRSAFactoryParams rsaParams, bool setAsDefault = false)
where TRSAFactoryParams : RSAFactoryParams where TRSAFactoryParams : RSAFactoryParams
=> services.AddSingleton(Options.Create(param)).AddRSAFactory<TRSAFactoryParams>(); {
services.TryAddSingleton(Options.Create(rsaParams));
if (setAsDefault)
services.TryAddSingleton<IRSAFactory, RSAFactory<TRSAFactoryParams>>();
else
services.TryAddSingleton<IRSAFactory<TRSAFactoryParams>, RSAFactory<TRSAFactoryParams>>();
return services;
}
} }
} }

View File

@ -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<RSAFactory<RSAFactoryParams>> LazyInstance = new(() => new(Options.Create<RSAFactoryParams>(new())));
public static IRSAFactory RSAFactory => LazyInstance.Value;
}
}