From 70ccec9fef229cbc55a24ac0c2d427d967723dc2 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Fri, 22 Nov 2024 09:06:58 +0100 Subject: [PATCH] =?UTF-8?q?feat(RSAFactory.ReadRSADecryptorAsync):=20ReadR?= =?UTF-8?q?SADecryptorAsync-Methode=20hinzugef=C3=BCgt,=20um=20die=20pem-D?= =?UTF-8?q?atei=20zu=20lesen=20und=20den=20Decryptor=20asynchron=20zu=20er?= =?UTF-8?q?stellen?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Security/IRSADecryptor.cs | 2 +- DigitalData.Core.Security/RSADecryptor.cs | 6 ++--- DigitalData.Core.Security/RSAFactory.cs | 24 ++++++++++++++++++- 3 files changed, 27 insertions(+), 5 deletions(-) diff --git a/DigitalData.Core.Abstractions/Security/IRSADecryptor.cs b/DigitalData.Core.Abstractions/Security/IRSADecryptor.cs index 91a906b..376684e 100644 --- a/DigitalData.Core.Abstractions/Security/IRSADecryptor.cs +++ b/DigitalData.Core.Abstractions/Security/IRSADecryptor.cs @@ -2,7 +2,7 @@ { public interface IRSADecryptor : IRSACryptographer { - (string Value, Version Version) VersionedPassword { init; } + (string Value, Version Version)? VersionedPassword { init; } Version? PasswordVersion { get; } diff --git a/DigitalData.Core.Security/RSADecryptor.cs b/DigitalData.Core.Security/RSADecryptor.cs index 4e2ac03..a527fe5 100644 --- a/DigitalData.Core.Security/RSADecryptor.cs +++ b/DigitalData.Core.Security/RSADecryptor.cs @@ -6,12 +6,12 @@ namespace DigitalData.Core.Security { public class RSADecryptor : RSACryptographer, IRSADecryptor, IRSACryptographer { - public (string Value, Version Version) VersionedPassword + public (string Value, Version Version)? VersionedPassword { init { - _password = value.Value; - PasswordVersion = value.Version; + _password = value?.Value; + PasswordVersion = value?.Version; } } diff --git a/DigitalData.Core.Security/RSAFactory.cs b/DigitalData.Core.Security/RSAFactory.cs index f7a51a5..9425dbb 100644 --- a/DigitalData.Core.Security/RSAFactory.cs +++ b/DigitalData.Core.Security/RSAFactory.cs @@ -1,4 +1,5 @@ -using System.Security.Cryptography; +using DigitalData.Core.Abstractions.Security; +using System.Security.Cryptography; using System.Text; namespace DigitalData.Core.Security @@ -106,5 +107,26 @@ namespace DigitalData.Core.Security return new string(pemChars); } + + public async Task ReadRSADecryptorAsync(string path, Version? version = null, CancellationToken cancellationToken = default) + { + var pem = await File.ReadAllTextAsync(path, cancellationToken); + + (string Value, Version Version)? versionedPassword = null; + + if(version is not null) + { + if (version != Secrets.Version) + throw new InvalidOperationException($"The provided version {version} does not match the expected version {Secrets.Version}."); + + versionedPassword = (Secrets.PBE_PASSWORD, Secrets.Version); + } + + return new RSADecryptor() + { + Pem = pem, + VersionedPassword = versionedPassword + }; + } } } \ No newline at end of file