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:
parent
36f75d003a
commit
7459f05748
@ -2,7 +2,7 @@
|
||||
|
||||
namespace DigitalData.Core.Abstractions.Security
|
||||
{
|
||||
public interface IRSAFactory<TParams>
|
||||
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<TParams> : IRSAFactory { }
|
||||
}
|
||||
@ -7,10 +7,6 @@ namespace DigitalData.Core.Security.Cryptographer
|
||||
{
|
||||
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;
|
||||
|
||||
public RSAFactory(IOptions<TRSAFactoryParams> options) => _params = options.Value;
|
||||
|
||||
@ -37,19 +37,49 @@ namespace DigitalData.Core.Security
|
||||
where TAsymCryptParams : AsymCryptParams
|
||||
=> services.AddSingleton(Options.Create(param)).AddAsymCryptService<TAsymCryptParams>();
|
||||
|
||||
private static IServiceCollection AddRSAFactory<TRSAFactoryParams>(this IServiceCollection services)
|
||||
where TRSAFactoryParams : RSAFactoryParams
|
||||
/// <summary>
|
||||
/// 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;
|
||||
}
|
||||
|
||||
/// <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)
|
||||
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
|
||||
=> 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;
|
||||
}
|
||||
}
|
||||
}
|
||||
14
DigitalData.Core.Security/Instance.cs
Normal file
14
DigitalData.Core.Security/Instance.cs
Normal 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;
|
||||
}
|
||||
}
|
||||
Loading…
x
Reference in New Issue
Block a user