- 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.
49 lines
1.5 KiB
C#
49 lines
1.5 KiB
C#
using System.Security.Cryptography;
|
|
using System.Text.Json.Serialization;
|
|
|
|
namespace DigitalData.Core.Security.Config
|
|
{
|
|
public class RSAFactoryParams : IJsonOnDeserialized
|
|
{
|
|
public int KeySizeInBits { get; init; } = 2048;
|
|
|
|
public string PbePassword { internal get; init; } = Secrets.PBE_PASSWORD;
|
|
|
|
public PbeEncryptionAlgorithm PbeEncryptionAlgorithm { get; init; } = PbeEncryptionAlgorithm.Aes256Cbc;
|
|
|
|
public HashAlgorithmName PbeHashAlgorithmName { get; init; } = HashAlgorithmName.SHA256;
|
|
|
|
public int PbeIterationCount { get; init; } = 100_000;
|
|
|
|
public string EncryptedPrivateKeyPemLabel { get; init; } = "ENCRYPTED PRIVATE KEY";
|
|
|
|
private PbeParameters? _pbeParameters;
|
|
|
|
[JsonIgnore]
|
|
public PbeParameters PbeParameters => _pbeParameters!;
|
|
|
|
/// <summary>
|
|
/// Provides a thread-safe initialization mechanism using Lazy initialization.
|
|
/// </summary>
|
|
private readonly Lazy<bool> _lazyInitializer;
|
|
|
|
public bool IsInitialized => _lazyInitializer.IsValueCreated;
|
|
|
|
public RSAFactoryParams()
|
|
{
|
|
_lazyInitializer = new(() =>
|
|
{
|
|
AfterCreate?.Invoke();
|
|
return true;
|
|
});
|
|
|
|
AfterCreate += () => _pbeParameters = new PbeParameters(PbeEncryptionAlgorithm, PbeHashAlgorithmName, PbeIterationCount);
|
|
}
|
|
|
|
protected event Action AfterCreate;
|
|
|
|
public void Init() => _ = _lazyInitializer.Value;
|
|
|
|
public void OnDeserialized() => Init();
|
|
}
|
|
} |