refactor(Privatekey): Die Klasse encryptor wurde erstellt und die Verschlüsselungsfunktionen wurden zur einfachen und sauberen Konfiguration dorthin verschoben.
This commit is contained in:
parent
9f0facc487
commit
7a938f0379
@ -3,5 +3,7 @@
|
|||||||
public interface IAsymmetricDecryptor : IAsymmetricPrivateKey
|
public interface IAsymmetricDecryptor : IAsymmetricPrivateKey
|
||||||
{
|
{
|
||||||
byte[] Decrypt(byte[] data);
|
byte[] Decrypt(byte[] data);
|
||||||
|
|
||||||
|
IAsymmetricEncryptor Encryptor { get; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -0,0 +1,7 @@
|
|||||||
|
namespace DigitalData.Core.Abstractions.Security
|
||||||
|
{
|
||||||
|
public interface IAsymmetricEncryptor : IAsymmetricPublicKey
|
||||||
|
{
|
||||||
|
public byte[] Encrypt(byte[] data);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -18,6 +18,6 @@ namespace DigitalData.Core.Abstractions.Security
|
|||||||
int? keySizeInBits = null,
|
int? keySizeInBits = null,
|
||||||
string? password = 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);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -2,10 +2,5 @@
|
|||||||
{
|
{
|
||||||
public interface IAsymmetricPublicKey : IAsymmetricKey
|
public interface IAsymmetricPublicKey : IAsymmetricKey
|
||||||
{
|
{
|
||||||
public byte[] Encrypt(byte[] data);
|
|
||||||
|
|
||||||
public string Encrypt(string data);
|
|
||||||
|
|
||||||
public bool Verify(string data, string signature) => Encrypt(data) == signature;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -6,6 +6,6 @@
|
|||||||
|
|
||||||
IAsymmetricDecryptor VaultDecryptor { get; }
|
IAsymmetricDecryptor VaultDecryptor { get; }
|
||||||
|
|
||||||
IEnumerable<IAsymmetricPublicKey> PublicKeys { get; }
|
IEnumerable<IAsymmetricEncryptor> Encryptors { get; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -33,7 +33,13 @@ namespace DigitalData.Core.Abstractions.Security
|
|||||||
|
|
||||||
internal static string BytesToString(this byte[] bytes) => Encoding.UTF8.GetString(bytes);
|
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
|
public static string Decrypt(this IAsymmetricDecryptor decryptor, string data) => decryptor
|
||||||
.Decrypt(data.Base64ToByte()).BytesToString();
|
.Decrypt(data.Base64ToByte()).BytesToString();
|
||||||
|
|
||||||
|
public static string Encrypt(this IAsymmetricEncryptor encryptor, string data) => encryptor.Encrypt(data.ToBytes()).ToBase64String();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -15,9 +15,9 @@ namespace DigitalData.Core.Security
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public IAsymmetricDecryptor VaultDecryptor { get; }
|
public IAsymmetricDecryptor VaultDecryptor { get; }
|
||||||
|
|
||||||
private readonly Lazy<IEnumerable<IAsymmetricPublicKey>> _lazyPublicKeys;
|
private readonly Lazy<IEnumerable<IAsymmetricEncryptor>> _lazyEncryptors;
|
||||||
|
|
||||||
public IEnumerable<IAsymmetricPublicKey> PublicKeys => _lazyPublicKeys.Value;
|
public IEnumerable<IAsymmetricEncryptor> Encryptors => _lazyEncryptors.Value;
|
||||||
|
|
||||||
public IEnumerable<PrivateKeyTokenDescriptor> TokenDescriptions { get; init; } = new List<PrivateKeyTokenDescriptor>();
|
public IEnumerable<PrivateKeyTokenDescriptor> TokenDescriptions { get; init; } = new List<PrivateKeyTokenDescriptor>();
|
||||||
|
|
||||||
@ -36,7 +36,7 @@ namespace DigitalData.Core.Security
|
|||||||
|
|
||||||
VaultDecryptor = _params.VaultDecryptor ?? Decryptors.First();
|
VaultDecryptor = _params.VaultDecryptor ?? Decryptors.First();
|
||||||
|
|
||||||
_lazyPublicKeys = new(Decryptors.Select(decryptor => decryptor.PublicKey));
|
_lazyEncryptors = new(Decryptors.Select(decryptor => decryptor.Encryptor));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -6,10 +6,6 @@ namespace DigitalData.Core.Security
|
|||||||
{
|
{
|
||||||
internal static class Extension
|
internal static class Extension
|
||||||
{
|
{
|
||||||
internal static string ToBase64String(this byte[] bytes) => Convert.ToBase64String(bytes);
|
|
||||||
|
|
||||||
internal static byte[] ToBytes(this string str) => System.Text.Encoding.UTF8.GetBytes(str);
|
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Converts a <see cref="DateTime"/> to a formatted string based on the specified format string.
|
/// Converts a <see cref="DateTime"/> to a formatted string based on the specified format string.
|
||||||
/// <br />
|
/// <br />
|
||||||
|
|||||||
@ -1,9 +1,33 @@
|
|||||||
using DigitalData.Core.Abstractions.Security;
|
using DigitalData.Core.Abstractions.Security;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
namespace DigitalData.Core.Security.RSAKey
|
namespace DigitalData.Core.Security.RSAKey
|
||||||
{
|
{
|
||||||
public class RSADecryptor : RSAPrivateKey, IAsymmetricDecryptor
|
public class RSADecryptor : RSAPrivateKey, IAsymmetricDecryptor
|
||||||
{
|
{
|
||||||
|
public RSAEncryptionPadding Padding { get; init; } = RSAEncryptionPadding.OaepSHA256;
|
||||||
|
|
||||||
|
// TODO: add as json converter to IConfigurIConfiguration.Config
|
||||||
|
public string PaddingName
|
||||||
|
{
|
||||||
|
get => Padding.ToString();
|
||||||
|
init => Padding = typeof(RSAEncryptionPadding).GetProperty(value, BindingFlags.Public | BindingFlags.Static)?.GetValue(null) as RSAEncryptionPadding ?? throw new ArgumentException($"Padding '{value}' not found.");
|
||||||
|
}
|
||||||
|
|
||||||
public byte[] Decrypt(byte[] data) => RSA.Decrypt(data, Padding);
|
public byte[] Decrypt(byte[] data) => RSA.Decrypt(data, Padding);
|
||||||
|
|
||||||
|
private readonly Lazy<IAsymmetricEncryptor> _lazyEncryptor;
|
||||||
|
|
||||||
|
public IAsymmetricEncryptor Encryptor => _lazyEncryptor.Value;
|
||||||
|
|
||||||
|
public RSADecryptor()
|
||||||
|
{
|
||||||
|
_lazyEncryptor = new(() => new RSAEncryptor()
|
||||||
|
{
|
||||||
|
Content = RSA.ExportRSAPublicKeyPem(),
|
||||||
|
Padding = Padding
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
20
DigitalData.Core.Security/RSAKey/RSAEncryptor.cs
Normal file
20
DigitalData.Core.Security/RSAKey/RSAEncryptor.cs
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
using DigitalData.Core.Abstractions.Security;
|
||||||
|
using System.Reflection;
|
||||||
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
|
namespace DigitalData.Core.Security.RSAKey
|
||||||
|
{
|
||||||
|
public class RSAEncryptor : RSAPublicKey, IAsymmetricEncryptor
|
||||||
|
{
|
||||||
|
public RSAEncryptionPadding Padding { get; init; } = RSAEncryptionPadding.OaepSHA256;
|
||||||
|
|
||||||
|
// TODO: add as json converter to IConfigurIConfiguration.Config
|
||||||
|
public string PaddingName
|
||||||
|
{
|
||||||
|
get => Padding.ToString();
|
||||||
|
init => Padding = typeof(RSAEncryptionPadding).GetProperty(value, BindingFlags.Public | BindingFlags.Static)?.GetValue(null) as RSAEncryptionPadding ?? throw new ArgumentException($"Padding '{value}' not found.");
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] Encrypt(byte[] data) => RSA.Encrypt(data, Padding);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -56,7 +56,7 @@ namespace DigitalData.Core.Security.RSAKey
|
|||||||
return new string(pemChars);
|
return new string(pemChars);
|
||||||
}
|
}
|
||||||
|
|
||||||
public IAsymmetricPrivateKey CreatePrivateKey(string pem, string? issuer = null, string? audience = null, bool encrypt = false, RSAEncryptionPadding? padding = null) => new RSAPrivateKey()
|
public IAsymmetricDecryptor CreateDecryptor(string pem, string? issuer = null, string? audience = null, bool encrypt = false, RSAEncryptionPadding? padding = null) => new RSADecryptor()
|
||||||
{
|
{
|
||||||
Content = pem,
|
Content = pem,
|
||||||
Issuer = issuer ?? string.Empty,
|
Issuer = issuer ?? string.Empty,
|
||||||
|
|||||||
@ -8,16 +8,7 @@ namespace DigitalData.Core.Security.RSAKey
|
|||||||
public class RSAKeyBase : IAsymmetricKey
|
public class RSAKeyBase : IAsymmetricKey
|
||||||
{
|
{
|
||||||
public virtual string Content { get; init; }
|
public virtual string Content { get; init; }
|
||||||
|
|
||||||
public RSAEncryptionPadding Padding { get; init; } = RSAEncryptionPadding.OaepSHA256;
|
|
||||||
|
|
||||||
// TODO: add as json converter to IConfigurIConfiguration.Config
|
|
||||||
public string PaddingName
|
|
||||||
{
|
|
||||||
get => Padding.ToString();
|
|
||||||
init => Padding = typeof(RSAEncryptionPadding).GetProperty(value, BindingFlags.Public | BindingFlags.Static)?.GetValue(null) as RSAEncryptionPadding ?? throw new ArgumentException($"Padding '{value}' not found.");
|
|
||||||
}
|
|
||||||
|
|
||||||
protected virtual RSA RSA { get; } = RSA.Create();
|
protected virtual RSA RSA { get; } = RSA.Create();
|
||||||
|
|
||||||
public string Issuer { get; init; } = string.Empty;
|
public string Issuer { get; init; } = string.Empty;
|
||||||
|
|||||||
@ -38,8 +38,7 @@ namespace DigitalData.Core.Security.RSAKey
|
|||||||
{
|
{
|
||||||
_lazyPublicKey = new(() => new RSAPublicKey()
|
_lazyPublicKey = new(() => new RSAPublicKey()
|
||||||
{
|
{
|
||||||
Content = RSA.ExportRSAPublicKeyPem(),
|
Content = RSA.ExportRSAPublicKeyPem()
|
||||||
Padding = Padding
|
|
||||||
});
|
});
|
||||||
|
|
||||||
_descriptorInitiator = new(() =>
|
_descriptorInitiator = new(() =>
|
||||||
|
|||||||
@ -13,11 +13,5 @@ namespace DigitalData.Core.Security.RSAKey
|
|||||||
RSA.ImportFromPem(value);
|
RSA.ImportFromPem(value);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public byte[] Encrypt(byte[] data) => RSA.Encrypt(data, Padding);
|
|
||||||
|
|
||||||
public string Encrypt(string data) => RSA.Encrypt(data.ToBytes(), Padding).ToBase64String();
|
|
||||||
|
|
||||||
public bool Verify(string data, string signature) => Encrypt(data) == signature;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
Loading…
x
Reference in New Issue
Block a user