feat (CryptFactory.RSADecryptorKeyFormatter): aktualisiert, um die erforderlichen Parameter als Eingabe zu nehmen, anstatt IRSADecryptor direkt als Eingabe zu nehmen
This commit is contained in:
parent
49b49271f3
commit
0ff0de8159
@ -18,7 +18,19 @@ namespace DigitalData.Core.Abstractions.Security
|
|||||||
|
|
||||||
string EncryptedPrivateKeyPemLabel { get; init; }
|
string EncryptedPrivateKeyPemLabel { get; init; }
|
||||||
|
|
||||||
Func<IRSADecryptor, string, string, string, string> RSADecryptorKeyFormatter { get; }
|
/// <summary>
|
||||||
|
/// Gets the formatter function for generating RSA key names.
|
||||||
|
/// This formatter takes an issuer, audience, isPrivate, and optional version and separator
|
||||||
|
/// to produce a formatted string used for the key naming convention.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="issuer">A string representing the issuer of the key. It should not contain invalid file name characters or the separator.</param>
|
||||||
|
/// <param name="audience">A string representing the audience for which the key is intended. It should not contain invalid file name characters or the separator.</param>
|
||||||
|
/// <param name="isPrivate">An bool to check if the key is private.</param>
|
||||||
|
/// <param name="version">An instance of the <see cref="Version?"/> interface, which is used to keep the version of Pbe password.</param>
|
||||||
|
/// <param name="separator">An optional string separator used to separate the issuer and audience. The default is "-_-". It should not be included in the issuer or audience strings.</param>
|
||||||
|
/// <returns>A formatted string combining the issuer, audience, and separator, which adheres to valid file naming rules.</returns>
|
||||||
|
/// <exception cref="ArgumentException">Thrown when the issuer, audience, or separator contains invalid characters or when the separator is present within the issuer or audience.</exception>
|
||||||
|
Func<string, string, bool, Version?, string, string> RSAKeyNameFormatter { get; }
|
||||||
|
|
||||||
string CreateRSAPrivateKeyPem(int? keySizeInBits = null);
|
string CreateRSAPrivateKeyPem(int? keySizeInBits = null);
|
||||||
|
|
||||||
|
|||||||
@ -10,13 +10,13 @@ namespace DigitalData.Core.Security
|
|||||||
|
|
||||||
public IRSADecryptor this[string key] { get => _decryptors[key]; set => _decryptors[key] = value; }
|
public IRSADecryptor this[string key] { get => _decryptors[key]; set => _decryptors[key] = value; }
|
||||||
|
|
||||||
public Func<IRSADecryptor, string, string, string, string> RSADecryptorKeyFormatter { get; }
|
public Func<string, string, bool, Version?, string, string> RSAKeyNameFormatter { get; }
|
||||||
|
|
||||||
public CryptFactory(ILogger<CryptFactory> logger, IDictionary<string, IRSADecryptor> decryptors, Func<IRSADecryptor, string, string, string, string> rsaDecryptorKeyFormatter) : base()
|
public CryptFactory(ILogger<CryptFactory> logger, IDictionary<string, IRSADecryptor> decryptors, Func<string, string, bool, Version?, string, string> rsaKeyNameFormatter) : base()
|
||||||
{
|
{
|
||||||
_decryptors = decryptors ?? new Dictionary<string, IRSADecryptor>();
|
_decryptors = decryptors ?? new Dictionary<string, IRSADecryptor>();
|
||||||
|
|
||||||
RSADecryptorKeyFormatter = rsaDecryptorKeyFormatter;
|
RSAKeyNameFormatter = rsaKeyNameFormatter;
|
||||||
|
|
||||||
logger?.LogInformation("Core.Secrets version: {Version}, Created on: {CreationDate}.", Secrets.Version, Secrets.CreationDate.ToString("dd.MM.yyyy"));
|
logger?.LogInformation("Core.Secrets version: {Version}, Created on: {CreationDate}.", Secrets.Version, Secrets.CreationDate.ToString("dd.MM.yyyy"));
|
||||||
}
|
}
|
||||||
@ -31,10 +31,12 @@ namespace DigitalData.Core.Security
|
|||||||
|
|
||||||
public static readonly IEnumerable<string> KeyFileTags = new string[] { DefaultEncryptedPrivateKeyFileTag, DefaultPrivateKeyFileTag, DefaultPublicKeyFileTag };
|
public static readonly IEnumerable<string> KeyFileTags = new string[] { DefaultEncryptedPrivateKeyFileTag, DefaultPrivateKeyFileTag, DefaultPublicKeyFileTag };
|
||||||
|
|
||||||
|
public static readonly string PEMFileExtension = ".pem";
|
||||||
|
|
||||||
private static readonly Lazy<IEnumerable<string>> LazyLowerFileTags = new(() => KeyFileTags.Select(tag => tag.ToLower()));
|
private static readonly Lazy<IEnumerable<string>> LazyLowerFileTags = new(() => KeyFileTags.Select(tag => tag.ToLower()));
|
||||||
|
|
||||||
//TODO: make the validation using regex
|
//TODO: make the validation using regex
|
||||||
public static string DefaultRSADecryptorKeyFormatter(IRSADecryptor decryptor, string issuer, string audience, string separator = "-_-")
|
public static string DefaultRSAKeyNameFormatter(string issuer, string audience, bool isPrivate = true, Version? passwordVersion = null, string separator = "-_-")
|
||||||
{
|
{
|
||||||
void ValidateForbidden(string value, string paramName)
|
void ValidateForbidden(string value, string paramName)
|
||||||
{
|
{
|
||||||
@ -58,12 +60,16 @@ namespace DigitalData.Core.Security
|
|||||||
var sb = new StringBuilder(issuer.Length + audience.Length + separator.Length * 2 + 20);
|
var sb = new StringBuilder(issuer.Length + audience.Length + separator.Length * 2 + 20);
|
||||||
sb.Append(issuer).Append(separator).Append(audience).Append(separator);
|
sb.Append(issuer).Append(separator).Append(audience).Append(separator);
|
||||||
|
|
||||||
if (decryptor.HasEncryptedPem)
|
if (passwordVersion is null && isPrivate)
|
||||||
sb.Append("e-private").Append(separator).Append(decryptor.PasswordVersion);
|
sb.Append(DefaultPrivateKeyFileTag);
|
||||||
|
else if(isPrivate)
|
||||||
|
sb.Append(DefaultEncryptedPrivateKeyFileTag).Append(separator).Append(passwordVersion);
|
||||||
|
else if(passwordVersion is null)
|
||||||
|
sb.Append(DefaultPublicKeyFileTag);
|
||||||
else
|
else
|
||||||
sb.Append("private");
|
sb.Append(DefaultPublicKeyFileTag).Append(separator).Append(passwordVersion);
|
||||||
|
|
||||||
var rsaKey = sb.Append(".pem").ToString();
|
var rsaKey = sb.Append(PEMFileExtension).ToString();
|
||||||
|
|
||||||
return rsaKey;
|
return rsaKey;
|
||||||
}
|
}
|
||||||
|
|||||||
@ -17,7 +17,7 @@ namespace DigitalData.Core.Security
|
|||||||
|
|
||||||
private string? _password;
|
private string? _password;
|
||||||
|
|
||||||
public Version? PasswordVersion { get; private init; }
|
public Version? PasswordVersion { get; private init; } = null;
|
||||||
|
|
||||||
public bool HasEncryptedPem => _password is not null;
|
public bool HasEncryptedPem => _password is not null;
|
||||||
|
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user