refactor(IRSAFactory): Erstellt, um die Funktionalität von RSAFactory zu trennen

This commit is contained in:
Developer 02 2024-12-02 15:10:51 +01:00
parent a4b96c2f3e
commit f720ea9cd6
5 changed files with 38 additions and 30 deletions

View File

@ -1,23 +1,7 @@
using System.Security.Cryptography;
namespace DigitalData.Core.Abstractions.Security
namespace DigitalData.Core.Abstractions.Security
{
public interface IAsymCryptService
public interface IAsymCryptService : IRSAFactory
{
int KeySizeInBits { get; init; }
string PbePassword { init; }
PbeEncryptionAlgorithm PbeEncryptionAlgorithm { get; init; }
HashAlgorithmName PbeHashAlgorithmName { get; init; }
int PbeIterationCount { get; init; }
PbeParameters PbeParameters { get; }
string EncryptedPrivateKeyPemLabel { get; init; }
/// <summary>
/// Gets the formatter function for generating RSA key names.
/// This formatter takes an issuer, audience, isPrivate, and optional version and separator
@ -31,15 +15,6 @@ namespace DigitalData.Core.Abstractions.Security
/// <returns>A formatted string combining the issuer, audience, and separator, which adheres to valid file naming rules.</returns>
/// <exception cref="ArgumentException">Thrown when the issuer, audience, or separator contains invalid characters or when the separator is present within the issuer or audience.</exception>
Func<string, string, bool, Version?, string?, string> RSAKeyNameFormatter { get; }
string CreateRSAPrivateKeyPem(int? keySizeInBits = null);
string CreateEncryptedPrivateKeyPem(
int? keySizeInBits = null,
string? password = null,
PbeEncryptionAlgorithm? pbeEncryptionAlgorithm = null,
HashAlgorithmName? hashAlgorithmName = null,
int? iterationCount = null);
IRSADecryptor this[string key] { get; }

View File

@ -0,0 +1,32 @@
using System.Security.Cryptography;
namespace DigitalData.Core.Abstractions.Security
{
public interface IRSAFactory
{
int KeySizeInBits { get; init; }
string PbePassword { init; }
PbeEncryptionAlgorithm PbeEncryptionAlgorithm { get; init; }
HashAlgorithmName PbeHashAlgorithmName { get; init; }
int PbeIterationCount { get; init; }
PbeParameters PbeParameters { get; }
string EncryptedPrivateKeyPemLabel { get; init; }
string CreateRSAPrivateKeyPem(int? keySizeInBits = null);
string CreateEncryptedPrivateKeyPem(
int? keySizeInBits = null,
string? password = null,
PbeEncryptionAlgorithm? pbeEncryptionAlgorithm = null,
HashAlgorithmName? hashAlgorithmName = null,
int? iterationCount = null);
Task<IRSADecryptor> ReadRSADecryptorAsync(string path, Version? version = null, CancellationToken cancellationToken = default);
}
}

View File

@ -3,7 +3,7 @@ using Microsoft.Extensions.Logging;
namespace DigitalData.Core.Security
{
public class AsymCryptService : RSAFactory, IAsymCryptService
public class AsymCryptService : RSAFactory, IAsymCryptService, IRSAFactory
{
private readonly IDictionary<string, IRSADecryptor> _decryptors;
@ -11,7 +11,7 @@ namespace DigitalData.Core.Security
public Func<string, string, bool, Version?, string?, string> RSAKeyNameFormatter { get; }
public AsymCryptService(ILogger<AsymCryptService> logger, IDictionary<string, IRSADecryptor> decryptors, Func<string, string, bool, Version?, string?, string> rsaKeyNameFormatter) : base()
public AsymCryptService(IDictionary<string, IRSADecryptor> decryptors, Func<string, string, bool, Version?, string?, string> rsaKeyNameFormatter, ILogger<AsymCryptService>? logger = null) : base()
{
_decryptors = decryptors ?? new Dictionary<string, IRSADecryptor>();

View File

@ -8,6 +8,7 @@ namespace DigitalData.Core.Security
{
public static IServiceCollection AddSecurity(this IServiceCollection services)
{
services.TryAddScoped<IRSAFactory>(sp => RSAFactory.Static);
services.TryAddScoped<IAsymCryptService, AsymCryptService>();
return services;

View File

@ -4,7 +4,7 @@ using System.Text;
namespace DigitalData.Core.Security
{
public class RSAFactory
public class RSAFactory : IRSAFactory
{
private static readonly Lazy<RSAFactory> LazyInstance = new(() => new());