Developer 02 155eb563d1 feat: Lazy-Initialisierung für threadsichere RSAFactoryParams-Initialisierung hinzugefügt
- Lazy-Initialisierungsmechanismus für threadsichere Handhabung der _pbeParameters eingeführt.
- IsInitialized-Eigenschaft hinzugefügt, um den Initialisierungsstatus zu verfolgen.
- Konstruktor geändert, um das Lazy-Objekt zu initialisieren und das AfterCreate-Ereignis auszulösen.
- Sichergestellt, dass die OnDeserialized-Methode Init aufruft, um das Objekt korrekt zu initialisieren.
2024-12-16 16:37:48 +01:00

78 lines
3.3 KiB
C#

using DigitalData.Core.Security.Cryptographer;
namespace DigitalData.Core.Security.Config
{
public class AsymCryptParams : RSAFactoryParams
{
public string PemDirectory { get; init; } = string.Empty;
/// <summary>
/// Represents the separator used to concatenate the components of a file-related token string.
/// </summary>
/// <remarks>
/// The resulting file-related token string is constructed as follows:
/// <c>string.Join(FileNameSeparator, 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>FileNameSeparator = "_-_"</c>, the output might look like:
/// <c>"Issuer_-_Audience_-_Secret_version"</c>.
/// </example>
public string FileNameSeparator { get; init; } = "_-_";
/// <summary>
/// Represents the separator used to concatenate the components of a key-related token string.
/// </summary>
/// <remarks>
/// The resulting key-related token string is constructed as follows:
/// <c>string.Join(KeyNameSeparator, 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>KeyNameSeparator = ":"</c>, the output might look like:
/// <c>"Issuer:Audience:Secret_version"</c>.
/// </example>
public string KeyNameSeparator { get; init; } = ":";
public IEnumerable<RSADecryptor> Decryptors { get; init; } = new List<RSADecryptor>();
public RSADecryptor? Vault { get; init; }
public AsymCryptParams()
{
AfterCreate += () =>
{
// Create root folder if it does not exist
if (!Directory.Exists(PemDirectory))
Directory.CreateDirectory(PemDirectory);
foreach (var decryptor in Decryptors)
{
// set default path
if (decryptor.IsPemNull)
{
var file_name_params = new List<object> { decryptor.Issuer, decryptor.Audience };
if (decryptor.IsEncrypted)
file_name_params.Add(Secrets.Version);
var path = Path.Combine(PemDirectory, string.Join(FileNameSeparator, file_name_params));
if (File.Exists(path))
decryptor.SetPem(File.ReadAllText(path));
else
{
var pem = decryptor.IsEncrypted
? Instance.RSAFactory.CreateEncryptedPrivateKeyPem(pbeParameters: PbeParameters, keySizeInBits: KeySizeInBits, password: Secrets.PBE_PASSWORD)
: Instance.RSAFactory.CreatePrivateKeyPem(keySizeInBits: KeySizeInBits);
decryptor.SetPem(File.ReadAllText(pem));
// Save file in background
Task.Run(async () => await File.WriteAllTextAsync(path: path, pem));
}
}
}
};
}
}
}