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.
This commit is contained in:
parent
4aacc3f650
commit
155eb563d1
@ -38,10 +38,10 @@ namespace DigitalData.Core.Security.Config
|
|||||||
|
|
||||||
public RSADecryptor? Vault { get; init; }
|
public RSADecryptor? Vault { get; init; }
|
||||||
|
|
||||||
public override void OnDeserialized()
|
public AsymCryptParams()
|
||||||
|
{
|
||||||
|
AfterCreate += () =>
|
||||||
{
|
{
|
||||||
base.OnDeserialized();
|
|
||||||
|
|
||||||
// Create root folder if it does not exist
|
// Create root folder if it does not exist
|
||||||
if (!Directory.Exists(PemDirectory))
|
if (!Directory.Exists(PemDirectory))
|
||||||
Directory.CreateDirectory(PemDirectory);
|
Directory.CreateDirectory(PemDirectory);
|
||||||
@ -72,6 +72,7 @@ namespace DigitalData.Core.Security.Config
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -22,6 +22,28 @@ namespace DigitalData.Core.Security.Config
|
|||||||
[JsonIgnore]
|
[JsonIgnore]
|
||||||
public PbeParameters PbeParameters => _pbeParameters!;
|
public PbeParameters PbeParameters => _pbeParameters!;
|
||||||
|
|
||||||
public virtual void OnDeserialized() => _pbeParameters = new PbeParameters(PbeEncryptionAlgorithm, PbeHashAlgorithmName, PbeIterationCount);
|
/// <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();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -9,7 +9,11 @@ namespace DigitalData.Core.Security.Cryptographer
|
|||||||
{
|
{
|
||||||
protected readonly TRSAFactoryParams _params;
|
protected readonly TRSAFactoryParams _params;
|
||||||
|
|
||||||
public RSAFactory(IOptions<TRSAFactoryParams> options) => _params = options.Value;
|
public RSAFactory(IOptions<TRSAFactoryParams> options)
|
||||||
|
{
|
||||||
|
options.Value.Init();
|
||||||
|
_params = options.Value;
|
||||||
|
}
|
||||||
|
|
||||||
public string CreatePrivateKeyPem(int? keySizeInBits = null, bool encrypt = false) => encrypt
|
public string CreatePrivateKeyPem(int? keySizeInBits = null, bool encrypt = false) => encrypt
|
||||||
? CreateEncryptedPrivateKeyPem(keySizeInBits: keySizeInBits)
|
? CreateEncryptedPrivateKeyPem(keySizeInBits: keySizeInBits)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user