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,40 +38,41 @@ namespace DigitalData.Core.Security.Config
|
|||||||
|
|
||||||
public RSADecryptor? Vault { get; init; }
|
public RSADecryptor? Vault { get; init; }
|
||||||
|
|
||||||
public override void OnDeserialized()
|
public AsymCryptParams()
|
||||||
{
|
{
|
||||||
base.OnDeserialized();
|
AfterCreate += () =>
|
||||||
|
|
||||||
// Create root folder if it does not exist
|
|
||||||
if (!Directory.Exists(PemDirectory))
|
|
||||||
Directory.CreateDirectory(PemDirectory);
|
|
||||||
|
|
||||||
foreach (var decryptor in Decryptors)
|
|
||||||
{
|
{
|
||||||
// set default path
|
// Create root folder if it does not exist
|
||||||
if (decryptor.IsPemNull)
|
if (!Directory.Exists(PemDirectory))
|
||||||
|
Directory.CreateDirectory(PemDirectory);
|
||||||
|
|
||||||
|
foreach (var decryptor in Decryptors)
|
||||||
{
|
{
|
||||||
var file_name_params = new List<object> { decryptor.Issuer, decryptor.Audience };
|
// set default path
|
||||||
if (decryptor.IsEncrypted)
|
if (decryptor.IsPemNull)
|
||||||
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
|
var file_name_params = new List<object> { decryptor.Issuer, decryptor.Audience };
|
||||||
? Instance.RSAFactory.CreateEncryptedPrivateKeyPem(pbeParameters: PbeParameters, keySizeInBits: KeySizeInBits, password: Secrets.PBE_PASSWORD)
|
if (decryptor.IsEncrypted)
|
||||||
: Instance.RSAFactory.CreatePrivateKeyPem(keySizeInBits: KeySizeInBits);
|
file_name_params.Add(Secrets.Version);
|
||||||
|
|
||||||
decryptor.SetPem(File.ReadAllText(pem));
|
var path = Path.Combine(PemDirectory, string.Join(FileNameSeparator, file_name_params));
|
||||||
|
|
||||||
// Save file in background
|
if (File.Exists(path))
|
||||||
Task.Run(async () => await File.WriteAllTextAsync(path: path, pem));
|
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));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
};
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -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