refactor(Privatekey): Die Klasse encryptor wurde erstellt und die Verschlüsselungsfunktionen wurden zur einfachen und sauberen Konfiguration dorthin verschoben.

This commit is contained in:
Developer 02
2025-01-08 20:03:25 +01:00
parent 9f0facc487
commit 7a938f0379
14 changed files with 67 additions and 33 deletions

View File

@@ -3,5 +3,7 @@
public interface IAsymmetricDecryptor : IAsymmetricPrivateKey
{
byte[] Decrypt(byte[] data);
IAsymmetricEncryptor Encryptor { get; }
}
}

View File

@@ -0,0 +1,7 @@
namespace DigitalData.Core.Abstractions.Security
{
public interface IAsymmetricEncryptor : IAsymmetricPublicKey
{
public byte[] Encrypt(byte[] data);
}
}

View File

@@ -18,6 +18,6 @@ namespace DigitalData.Core.Abstractions.Security
int? keySizeInBits = null,
string? password = null);
IAsymmetricPrivateKey CreatePrivateKey(string pem, string? issuer = null, string? audience = null, bool encrypt = false, RSAEncryptionPadding? padding = null);
public IAsymmetricDecryptor CreateDecryptor(string pem, string? issuer = null, string? audience = null, bool encrypt = false, RSAEncryptionPadding? padding = null);
}
}

View File

@@ -2,10 +2,5 @@
{
public interface IAsymmetricPublicKey : IAsymmetricKey
{
public byte[] Encrypt(byte[] data);
public string Encrypt(string data);
public bool Verify(string data, string signature) => Encrypt(data) == signature;
}
}

View File

@@ -6,6 +6,6 @@
IAsymmetricDecryptor VaultDecryptor { get; }
IEnumerable<IAsymmetricPublicKey> PublicKeys { get; }
IEnumerable<IAsymmetricEncryptor> Encryptors { get; }
}
}

View File

@@ -33,7 +33,13 @@ namespace DigitalData.Core.Abstractions.Security
internal static string BytesToString(this byte[] bytes) => Encoding.UTF8.GetString(bytes);
internal static string ToBase64String(this byte[] bytes) => Convert.ToBase64String(bytes);
internal static byte[] ToBytes(this string str) => System.Text.Encoding.UTF8.GetBytes(str);
public static string Decrypt(this IAsymmetricDecryptor decryptor, string data) => decryptor
.Decrypt(data.Base64ToByte()).BytesToString();
public static string Encrypt(this IAsymmetricEncryptor encryptor, string data) => encryptor.Encrypt(data.ToBytes()).ToBase64String();
}
}