From f720ea9cd611c5d9629afefac7fb2a906773e66e Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Mon, 2 Dec 2024 15:10:51 +0100 Subject: [PATCH] =?UTF-8?q?refactor(IRSAFactory):=20Erstellt,=20um=20die?= =?UTF-8?q?=20Funktionalit=C3=A4t=20von=20RSAFactory=20zu=20trennen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Security/IAsymCryptService.cs | 29 ++--------------- .../Security/IRSAFactory.cs | 32 +++++++++++++++++++ DigitalData.Core.Security/AsymCryptService.cs | 4 +-- DigitalData.Core.Security/DIExtensions.cs | 1 + DigitalData.Core.Security/RSAFactory.cs | 2 +- 5 files changed, 38 insertions(+), 30 deletions(-) create mode 100644 DigitalData.Core.Abstractions/Security/IRSAFactory.cs diff --git a/DigitalData.Core.Abstractions/Security/IAsymCryptService.cs b/DigitalData.Core.Abstractions/Security/IAsymCryptService.cs index e4bfc5a..ef8ecbc 100644 --- a/DigitalData.Core.Abstractions/Security/IAsymCryptService.cs +++ b/DigitalData.Core.Abstractions/Security/IAsymCryptService.cs @@ -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; } - /// /// 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 /// A formatted string combining the issuer, audience, and separator, which adheres to valid file naming rules. /// Thrown when the issuer, audience, or separator contains invalid characters or when the separator is present within the issuer or audience. Func 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; } diff --git a/DigitalData.Core.Abstractions/Security/IRSAFactory.cs b/DigitalData.Core.Abstractions/Security/IRSAFactory.cs new file mode 100644 index 0000000..6293b66 --- /dev/null +++ b/DigitalData.Core.Abstractions/Security/IRSAFactory.cs @@ -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 ReadRSADecryptorAsync(string path, Version? version = null, CancellationToken cancellationToken = default); + } +} \ No newline at end of file diff --git a/DigitalData.Core.Security/AsymCryptService.cs b/DigitalData.Core.Security/AsymCryptService.cs index c2bc1ea..2b867a4 100644 --- a/DigitalData.Core.Security/AsymCryptService.cs +++ b/DigitalData.Core.Security/AsymCryptService.cs @@ -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 _decryptors; @@ -11,7 +11,7 @@ namespace DigitalData.Core.Security public Func RSAKeyNameFormatter { get; } - public AsymCryptService(ILogger logger, IDictionary decryptors, Func rsaKeyNameFormatter) : base() + public AsymCryptService(IDictionary decryptors, Func rsaKeyNameFormatter, ILogger? logger = null) : base() { _decryptors = decryptors ?? new Dictionary(); diff --git a/DigitalData.Core.Security/DIExtensions.cs b/DigitalData.Core.Security/DIExtensions.cs index 012ff26..28f4596 100644 --- a/DigitalData.Core.Security/DIExtensions.cs +++ b/DigitalData.Core.Security/DIExtensions.cs @@ -8,6 +8,7 @@ namespace DigitalData.Core.Security { public static IServiceCollection AddSecurity(this IServiceCollection services) { + services.TryAddScoped(sp => RSAFactory.Static); services.TryAddScoped(); return services; diff --git a/DigitalData.Core.Security/RSAFactory.cs b/DigitalData.Core.Security/RSAFactory.cs index 9425dbb..846f3a5 100644 --- a/DigitalData.Core.Security/RSAFactory.cs +++ b/DigitalData.Core.Security/RSAFactory.cs @@ -4,7 +4,7 @@ using System.Text; namespace DigitalData.Core.Security { - public class RSAFactory + public class RSAFactory : IRSAFactory { private static readonly Lazy LazyInstance = new(() => new());