feat(RSAParams): Merged CryptoFactoryParams and RSAFactoryParams

This commit is contained in:
Developer 02
2025-03-14 11:14:18 +01:00
parent b8de148c52
commit 7dd8271f4a
7 changed files with 109 additions and 121 deletions

View File

@@ -9,11 +9,11 @@ namespace DigitalData.Core.Security.Services;
public class PemFileInitalizer : BackgroundService
{
private readonly CryptoFactoryParams _factoryParams;
private readonly RSAParams _factoryParams;
private readonly ILogger<PemFileInitalizer> _logger;
public PemFileInitalizer(IOptions<CryptoFactoryParams> factoryParamsOptions, ILogger<PemFileInitalizer> logger)
public PemFileInitalizer(IOptions<RSAParams> factoryParamsOptions, ILogger<PemFileInitalizer> logger)
{
_factoryParams = factoryParamsOptions.Value;
_logger = logger;

View File

@@ -11,7 +11,7 @@ public class RSAFactory : IAsymmetricKeyFactory
public string CreatePrivateKeyPem(int? keySizeInBits = null, bool encrypt = false) => encrypt
? CreateEncryptedPrivateKeyPem(keySizeInBits: keySizeInBits)
: RSA.Create(keySizeInBits ?? RSAFactoryParams.Default.KeySizeInBits).ExportRSAPrivateKeyPem();
: RSA.Create(keySizeInBits ?? RSAParams.Default.KeySizeInBits).ExportRSAPrivateKeyPem();
public string CreateEncryptedPrivateKeyPem(
PbeEncryptionAlgorithm? pbeEncryptionAlgorithm = null,
@@ -20,16 +20,16 @@ public class RSAFactory : IAsymmetricKeyFactory
int? keySizeInBits = null,
string? password = null)
{
password ??= RSAFactoryParams.Default.PbePassword;
password ??= RSAParams.Default.PbePassword;
var pbeParameters = new PbeParameters(
pbeEncryptionAlgorithm ?? RSAFactoryParams.Default.PbeEncryptionAlgorithm,
hashAlgorithmName ?? RSAFactoryParams.Default.PbeHashAlgorithm,
iterationCount ?? RSAFactoryParams.Default.PbeIterationCount);
pbeEncryptionAlgorithm ?? RSAParams.Default.PbeEncryptionAlgorithm,
hashAlgorithmName ?? RSAParams.Default.PbeHashAlgorithm,
iterationCount ?? RSAParams.Default.PbeIterationCount);
var encryptedPrivateKey = RSA.Create(keySizeInBits ?? RSAFactoryParams.Default.KeySizeInBits).ExportEncryptedPkcs8PrivateKey(password.AsSpan(), pbeParameters);
var encryptedPrivateKey = RSA.Create(keySizeInBits ?? RSAParams.Default.KeySizeInBits).ExportEncryptedPkcs8PrivateKey(password.AsSpan(), pbeParameters);
var pemChars = PemEncoding.Write(RSAFactoryParams.Default.EncryptedPrivateKeyPemLabel, encryptedPrivateKey);
var pemChars = PemEncoding.Write(RSAParams.Default.EncryptedPrivateKeyPemLabel, encryptedPrivateKey);
return new string(pemChars);
}
@@ -39,11 +39,11 @@ public class RSAFactory : IAsymmetricKeyFactory
int? keySizeInBits = null,
string? password = null)
{
password ??= RSAFactoryParams.Default.PbePassword;
password ??= RSAParams.Default.PbePassword;
var encryptedPrivateKey = RSA.Create(keySizeInBits ?? RSAFactoryParams.Default.KeySizeInBits).ExportEncryptedPkcs8PrivateKey(password.AsSpan(), pbeParameters);
var encryptedPrivateKey = RSA.Create(keySizeInBits ?? RSAParams.Default.KeySizeInBits).ExportEncryptedPkcs8PrivateKey(password.AsSpan(), pbeParameters);
var pemChars = PemEncoding.Write(RSAFactoryParams.Default.EncryptedPrivateKeyPemLabel, encryptedPrivateKey);
var pemChars = PemEncoding.Write(RSAParams.Default.EncryptedPrivateKeyPemLabel, encryptedPrivateKey);
return new string(pemChars);
}

View File

@@ -7,7 +7,7 @@ namespace DigitalData.Core.Security.Services;
public class RSAPool : RSAFactory, IAsymmetricKeyPool, IAsymmetricKeyFactory
{
private readonly CryptoFactoryParams _params;
private readonly RSAParams _params;
public IEnumerable<IAsymmetricDecryptor> Decryptors { get; }
@@ -18,7 +18,7 @@ public class RSAPool : RSAFactory, IAsymmetricKeyPool, IAsymmetricKeyFactory
public IEnumerable<IAsymmetricTokenDescriptor> TokenDescriptors { get; init; } = new List<IAsymmetricTokenDescriptor>();
public RSAPool(IOptions<CryptoFactoryParams> cryptoFactoryParamsOptions, ILogger<RSAPool>? logger = null)
public RSAPool(IOptions<RSAParams> cryptoFactoryParamsOptions, ILogger<RSAPool>? logger = null)
{
_params = cryptoFactoryParamsOptions.Value;