From 76ce64691a4cc2ebf69cb956c87879df6ff19846 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Fri, 13 Dec 2024 10:29:49 +0100 Subject: [PATCH] refactor(RSACryptographer): Verzeichnis und Dateiname wurden entfernt. - Datei-Leseprozess in init-Methode entfernt. --- .../Security/IRSACryptographer.cs | 6 +--- .../Config/AsymCryptParams.cs | 20 ++++++------- .../Cryptographer/RSACryptographer.cs | 28 ++++--------------- .../Cryptographer/RSADecryptor.cs | 18 ------------ .../Cryptographer/RSAEncryptor.cs | 17 ----------- 5 files changed, 16 insertions(+), 73 deletions(-) diff --git a/DigitalData.Core.Abstractions/Security/IRSACryptographer.cs b/DigitalData.Core.Abstractions/Security/IRSACryptographer.cs index f2d29bd..bbbafa1 100644 --- a/DigitalData.Core.Abstractions/Security/IRSACryptographer.cs +++ b/DigitalData.Core.Abstractions/Security/IRSACryptographer.cs @@ -7,11 +7,7 @@ namespace DigitalData.Core.Abstractions.Security public string Pem { get; init; } public RSAEncryptionPadding Padding { get; init; } - - public string? Directory { get; set; } - - public string? FileName { get; set; } - + public string Issuer { get; init; } public string Audience { get; init; } diff --git a/DigitalData.Core.Security/Config/AsymCryptParams.cs b/DigitalData.Core.Security/Config/AsymCryptParams.cs index 5bbc93f..552fdbc 100644 --- a/DigitalData.Core.Security/Config/AsymCryptParams.cs +++ b/DigitalData.Core.Security/Config/AsymCryptParams.cs @@ -41,16 +41,16 @@ namespace DigitalData.Core.Security.Config foreach (var crypt in cryptographers) { // set default path - if (crypt.Pem is null) - { - crypt.Directory ??= Directory; - crypt.FileName ??= string.Format( - FileNameFormat, - crypt.Issuer, - crypt.Audience, - TypeTagOf(crypt), - Secrets.Version); - } + //if (crypt.Pem is null) + //{ + // crypt.Directory ??= Directory; + // crypt.FileName ??= string.Format( + // FileNameFormat, + // crypt.Issuer, + // crypt.Audience, + // TypeTagOf(crypt), + // Secrets.Version); + //} crypt.Init(); } diff --git a/DigitalData.Core.Security/Cryptographer/RSACryptographer.cs b/DigitalData.Core.Security/Cryptographer/RSACryptographer.cs index fa1de3d..9c93838 100644 --- a/DigitalData.Core.Security/Cryptographer/RSACryptographer.cs +++ b/DigitalData.Core.Security/Cryptographer/RSACryptographer.cs @@ -10,18 +10,14 @@ namespace DigitalData.Core.Security.Cryptographer public string Pem { get => _pem - ?? throw new InvalidOperationException($"Pem is not initialized. Please ensure that the PEM is set or properly loaded from the file. Issuer: {Issuer}, Audience: {Audience}."); + ?? throw PemIsNullException; init => _pem = value; } internal bool IsPemNull => _pem is null; - public string? PemPath => FileName is null ? null : Path.Combine(Directory ?? string.Empty, FileName); - - public string? Directory { get; set; } - - public string? FileName { get; set; } - + private InvalidOperationException PemIsNullException => new($"Pem is not initialized. Please ensure that the PEM is set or properly loaded from the file. Issuer: {Issuer}, Audience: {Audience}."); + public RSAEncryptionPadding Padding { get; init; } = RSAEncryptionPadding.OaepSHA256; protected virtual RSA RSA { get; } = RSA.Create(); @@ -34,24 +30,10 @@ namespace DigitalData.Core.Security.Cryptographer internal void SetPem(string pem) => _pem = pem; - public virtual void UnableToInitPemEvent() => throw new InvalidOperationException( - $"Pem is not initialized and pem file is null. Issuer is {Issuer} and audience {Audience}."); - - public virtual void FileNotFoundEvent() => throw new FileNotFoundException( - $"Pem is not initialized and pem file is not found in {PemPath}. Issuer is {Issuer} and audience {Audience}."); - - // TODO: make file read asynchronous, consider multiple routing public virtual void Init() { - if(_pem is null) - { - if(PemPath is null) - UnableToInitPemEvent(); - if (File.Exists(PemPath)) - _pem = File.ReadAllText(PemPath); - else - FileNotFoundEvent(); - } + if (_pem is null) + throw PemIsNullException; } } } \ No newline at end of file diff --git a/DigitalData.Core.Security/Cryptographer/RSADecryptor.cs b/DigitalData.Core.Security/Cryptographer/RSADecryptor.cs index f99ac5d..eb4cbec 100644 --- a/DigitalData.Core.Security/Cryptographer/RSADecryptor.cs +++ b/DigitalData.Core.Security/Cryptographer/RSADecryptor.cs @@ -1,5 +1,4 @@ using DigitalData.Core.Abstractions.Security; -using DigitalData.Core.Security.Config; using DigitalData.Core.Security.Extensions; using System.Security.Cryptography; @@ -34,22 +33,5 @@ namespace DigitalData.Core.Security.Cryptographer else RSA.ImportFromPem(Pem); } - - public override void FileNotFoundEvent() - { - var new_decryptor = new RSADecryptor() - { - Pem = RSAFactory.Static.CreateRSAPrivateKeyPem(), - Encrypt = Encrypt - }; - - _pem = new_decryptor.Pem; - - if (PemPath is not null) - Task.Run(async () => - { - await File.WriteAllTextAsync(_pem, PemPath); - }); - } } } \ No newline at end of file diff --git a/DigitalData.Core.Security/Cryptographer/RSAEncryptor.cs b/DigitalData.Core.Security/Cryptographer/RSAEncryptor.cs index be21823..f25f64e 100644 --- a/DigitalData.Core.Security/Cryptographer/RSAEncryptor.cs +++ b/DigitalData.Core.Security/Cryptographer/RSAEncryptor.cs @@ -1,5 +1,4 @@ using DigitalData.Core.Abstractions.Security; -using DigitalData.Core.Security.Config; using DigitalData.Core.Security.Extensions; namespace DigitalData.Core.Security.Cryptographer @@ -17,21 +16,5 @@ namespace DigitalData.Core.Security.Cryptographer base.Init(); RSA.ImportFromPem(base.Pem); } - - public override void FileNotFoundEvent() - { - var new_decryptor = new RSADecryptor() - { - Pem = RSAFactory.Static.CreateRSAPrivateKeyPem() - }; - - _pem = new_decryptor.Encryptor.Pem; - - if (PemPath is not null) - Task.Run(async () => - { - await File.WriteAllTextAsync(_pem, PemPath); - }); - } } } \ No newline at end of file