refactor(RSAFactoryParams): Eigenschaft PbeParameters hinzugefügt

This commit is contained in:
Developer 02 2024-12-05 00:43:42 +01:00
parent c895d2df0e
commit 65989b23b3
3 changed files with 11 additions and 10 deletions

View File

@ -13,10 +13,10 @@ namespace DigitalData.Core.Security.Extensions
return rsa; return rsa;
} }
public static IRSADecryptor GetRSADecryptor(this IAsymCryptService factory, string issuer, string audience, Version? version = null, string? seperator = null) public static IRSADecryptor GetRSADecryptor<TParams>(this IAsymCryptService<TParams> factory, string issuer, string audience, Version? version = null, string? seperator = null)
=> factory[factory.RSAKeyNameFormatter(issuer, audience, true, version, seperator)]; => factory[factory.RSAKeyNameFormatter(issuer, audience, true, version, seperator)];
public static bool TryGetRSADecryptor(this IAsymCryptService factory, string issuer, string audience, out IRSADecryptor? decryptor, Version? version = null, string? seperator = null) public static bool TryGetRSADecryptor<TParams>(this IAsymCryptService<TParams> factory, string issuer, string audience, out IRSADecryptor? decryptor, Version? version = null, string? seperator = null)
=> factory.TryGetRSADecryptor(factory.RSAKeyNameFormatter(issuer, audience, true, version, seperator), out decryptor); => factory.TryGetRSADecryptor(factory.RSAKeyNameFormatter(issuer, audience, true, version, seperator), out decryptor);
private static string CreatePath(string filename, string? directory = null) private static string CreatePath(string filename, string? directory = null)

View File

@ -12,13 +12,7 @@ namespace DigitalData.Core.Security
protected readonly TRSAFactoryParams _params; protected readonly TRSAFactoryParams _params;
private readonly PbeParameters _pbeParameters; public RSAFactory(IOptions<TRSAFactoryParams> options) => _params = options.Value;
public RSAFactory(IOptions<TRSAFactoryParams> options)
{
_params = options.Value;
_pbeParameters = new PbeParameters(_params.PbeEncryptionAlgorithm, _params.PbeHashAlgorithmName, _params.PbeIterationCount);
}
public string CreateRSAPrivateKeyPem(int? keySizeInBits = null) public string CreateRSAPrivateKeyPem(int? keySizeInBits = null)
=> RSA.Create(keySizeInBits ?? _params.KeySizeInBits).ExportRSAPrivateKeyPem(); => RSA.Create(keySizeInBits ?? _params.KeySizeInBits).ExportRSAPrivateKeyPem();
@ -37,7 +31,7 @@ namespace DigitalData.Core.Security
pbeEncryptionAlgorithm ?? _params.PbeEncryptionAlgorithm, pbeEncryptionAlgorithm ?? _params.PbeEncryptionAlgorithm,
hashAlgorithmName ?? _params.PbeHashAlgorithmName, hashAlgorithmName ?? _params.PbeHashAlgorithmName,
iterationCount ?? _params.PbeIterationCount) iterationCount ?? _params.PbeIterationCount)
: _pbeParameters; : _params.PbeParameters;
var encryptedPrivateKey = RSA.Create(keySizeInBits ?? _params.KeySizeInBits).ExportEncryptedPkcs8PrivateKey(password.AsSpan(), pbeParameters); var encryptedPrivateKey = RSA.Create(keySizeInBits ?? _params.KeySizeInBits).ExportEncryptedPkcs8PrivateKey(password.AsSpan(), pbeParameters);

View File

@ -23,5 +23,12 @@ namespace DigitalData.Core.Security
public int PbeIterationCount { get; init; } = 100_000; public int PbeIterationCount { get; init; } = 100_000;
public string EncryptedPrivateKeyPemLabel { get; init; } = "ENCRYPTED PRIVATE KEY"; public string EncryptedPrivateKeyPemLabel { get; init; } = "ENCRYPTED PRIVATE KEY";
private readonly Lazy<PbeParameters> _lazyPbeParameters;
public PbeParameters PbeParameters => _lazyPbeParameters.Value;
public RSAFactoryParams()
=> _lazyPbeParameters = new(() => new PbeParameters(PbeEncryptionAlgorithm, PbeHashAlgorithmName, PbeIterationCount));
} }
} }