From 600d17ef40a1ed5092ba3bde0c22c03f825ae1fb Mon Sep 17 00:00:00 2001 From: Developer 02 Date: Thu, 5 Dec 2024 23:08:13 +0100 Subject: [PATCH] Revert "refactor(RSACryptographer): Entfernte nullbare Eigenschaft von Issuer und Audience." This reverts commit 16565eca4d0d92182306e72081c73caa19f944c7. --- .../Security/IRSACryptographer.cs | 4 +- .../Cryptographer/RSACryptographer.cs | 46 ++----------------- .../Cryptographer/RSAEncryptor.cs | 4 +- 3 files changed, 8 insertions(+), 46 deletions(-) diff --git a/DigitalData.Core.Abstractions/Security/IRSACryptographer.cs b/DigitalData.Core.Abstractions/Security/IRSACryptographer.cs index 9bbeb98..406efa8 100644 --- a/DigitalData.Core.Abstractions/Security/IRSACryptographer.cs +++ b/DigitalData.Core.Abstractions/Security/IRSACryptographer.cs @@ -8,8 +8,8 @@ namespace DigitalData.Core.Abstractions.Security public RSAEncryptionPadding Padding { get; init; } - public string Issuer { get; init; } + public string? Issuer { get; init; } - public string Audience { get; init; } + public string? Audience { get; init; } } } \ No newline at end of file diff --git a/DigitalData.Core.Security/Cryptographer/RSACryptographer.cs b/DigitalData.Core.Security/Cryptographer/RSACryptographer.cs index 3e08d47..a128c62 100644 --- a/DigitalData.Core.Security/Cryptographer/RSACryptographer.cs +++ b/DigitalData.Core.Security/Cryptographer/RSACryptographer.cs @@ -1,58 +1,20 @@ using DigitalData.Core.Abstractions.Security; using System.Security.Cryptography; -using System.Text.Json.Serialization; namespace DigitalData.Core.Security.Cryptographer { - public class RSACryptographer : IRSACryptographer, IJsonOnDeserialized + public class RSACryptographer : IRSACryptographer { - private string? _pem; - - private string? _pemPath; - - public virtual string Pem - { - get => _pem!; - init - { - ValidatePemInit(); - _pem = value; - } - } - - public string? PemPath - { - get => _pemPath; - init - { - _pemPath = value; - if (value is null) - return; - ValidatePemInit(); - _pem = File.ReadAllText(value); - } - } + public required virtual string Pem { get; init; } public RSAEncryptionPadding Padding { get; init; } = RSAEncryptionPadding.OaepSHA256; protected virtual RSA RSA { get; } = RSA.Create(); - public string Issuer { get; init; } = string.Empty; + public string? Issuer { get; init; } - public string Audience { get; init; } = string.Empty; + public string? Audience { get; init; } internal RSACryptographer() { } - - public void OnDeserialized() - { - if (Pem is null) - throw new InvalidOperationException($"Pem must be initialized. Issuer: {Issuer} and Audience: {Audience}"); - } - - private void ValidatePemInit() - { - if (_pem is not null) - throw new InvalidOperationException($"Pem can only be initilized once. Remove one of the Pem or Pem file initilizations. Issuer: {Issuer} and Audience: {Audience}"); - } } } \ No newline at end of file diff --git a/DigitalData.Core.Security/Cryptographer/RSAEncryptor.cs b/DigitalData.Core.Security/Cryptographer/RSAEncryptor.cs index 055a6bc..2445b30 100644 --- a/DigitalData.Core.Security/Cryptographer/RSAEncryptor.cs +++ b/DigitalData.Core.Security/Cryptographer/RSAEncryptor.cs @@ -5,7 +5,7 @@ namespace DigitalData.Core.Security.Cryptographer { public class RSAEncryptor : RSACryptographer, IRSAEncryptor, IRSACryptographer { - public override string Pem + public override required string Pem { get => base.Pem; init @@ -14,7 +14,7 @@ namespace DigitalData.Core.Security.Cryptographer base.Pem = value; } } - + public byte[] Encrypt(byte[] data) => RSA.Encrypt(data, Padding); public string Encrypt(string data) => RSA.Encrypt(data.Base64ToByte(), Padding).BytesToString();