refactor(AsymCryptParams): Unnötige Methoden entfernt

This commit is contained in:
Developer 02 2024-12-13 15:57:17 +01:00
parent a9ebc406f3
commit 7ec85b4e30

View File

@ -6,19 +6,22 @@ namespace DigitalData.Core.Security.Config
{
public string PemDirectory { get; init; } = string.Empty;
/// <summary>
/// Represents the separator used to concatenate the components of a token string.
/// </summary>
/// <remarks>
/// The resulting token string is constructed as follows:
/// <c>string.Join(Separator, Issuer, Audience, Secret_version)</c>.
/// If <c>Secret_version</c> is not null, it will be included in the concatenation.
/// </remarks>
/// <example>
/// For example, if <c>Separator = "_-_"</c>, the output might look like:
/// <c>"Issuer_-_Audience_-_Secret_version"</c>.
/// </example>
public string Separator { get; init; } = "_-_";
public IEnumerable<RSADecryptor> Decryptors { get; init; } = new List<RSADecryptor>();
/// <summary>
/// 0: Issuer - 1: Audience - 2: Secret version (if is encrypted)
/// </summary>
private string CreateFileName(params object[] objs) => string.Join(Separator, objs);
private string CreatePem(bool isEncrypted) => isEncrypted
? Instance.RSAFactory.CreateEncryptedPrivateKeyPem(pbeParameters: PbeParameters, keySizeInBits: KeySizeInBits, password: Secrets.PBE_PASSWORD)
: Instance.RSAFactory.CreatePrivateKeyPem(keySizeInBits: KeySizeInBits);
public override void OnDeserialized()
{
base.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));
}
}