From 7ec85b4e303f02007402ace89ab73ed6febac5b1 Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Fri, 13 Dec 2024 15:57:17 +0100 Subject: [PATCH] =?UTF-8?q?refactor(AsymCryptParams):=20Unn=C3=B6tige=20Me?= =?UTF-8?q?thoden=20entfernt?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../Config/AsymCryptParams.cs | 31 ++++++++++++------- 1 file changed, 19 insertions(+), 12 deletions(-) diff --git a/DigitalData.Core.Security/Config/AsymCryptParams.cs b/DigitalData.Core.Security/Config/AsymCryptParams.cs index a2a2a24..e991cc1 100644 --- a/DigitalData.Core.Security/Config/AsymCryptParams.cs +++ b/DigitalData.Core.Security/Config/AsymCryptParams.cs @@ -6,18 +6,21 @@ namespace DigitalData.Core.Security.Config { public string PemDirectory { get; init; } = string.Empty; - public string Separator { get; init; } = "_-_"; - - public IEnumerable Decryptors { get; init; } = new List(); - /// - /// 0: Issuer - 1: Audience - 2: Secret version (if is encrypted) + /// Represents the separator used to concatenate the components of a token string. /// - private string CreateFileName(params object[] objs) => string.Join(Separator, objs); + /// + /// The resulting token string is constructed as follows: + /// string.Join(Separator, Issuer, Audience, Secret_version). + /// If Secret_version is not null, it will be included in the concatenation. + /// + /// + /// For example, if Separator = "_-_", the output might look like: + /// "Issuer_-_Audience_-_Secret_version". + /// + public string Separator { get; init; } = "_-_"; - private string CreatePem(bool isEncrypted) => isEncrypted - ? Instance.RSAFactory.CreateEncryptedPrivateKeyPem(pbeParameters: PbeParameters, keySizeInBits: KeySizeInBits, password: Secrets.PBE_PASSWORD) - : Instance.RSAFactory.CreatePrivateKeyPem(keySizeInBits: KeySizeInBits); + public IEnumerable Decryptors { get; init; } = new List(); public override void OnDeserialized() { @@ -36,15 +39,19 @@ namespace DigitalData.Core.Security.Config if (crypt.Encrypt) file_name_params.Add(Secrets.Version); - var file_name = CreateFileName(file_name_params); - var path = Path.Combine(PemDirectory, file_name); + var path = Path.Combine(PemDirectory, string.Join(Separator, file_name_params)); if (File.Exists(path)) crypt.SetPem(File.ReadAllText(path)); else { - var pem = CreatePem(crypt.Encrypt); + var pem = crypt.Encrypt + ? Instance.RSAFactory.CreateEncryptedPrivateKeyPem(pbeParameters: PbeParameters, keySizeInBits: KeySizeInBits, password: Secrets.PBE_PASSWORD) + : Instance.RSAFactory.CreatePrivateKeyPem(keySizeInBits: KeySizeInBits); + crypt.SetPem(File.ReadAllText(pem)); + + // Save file in background Task.Run(async () => await File.WriteAllTextAsync(path: path, pem)); } }