57 lines
2.2 KiB
C#
57 lines
2.2 KiB
C#
using DigitalData.Core.Security.Cryptographer;
|
|
|
|
namespace DigitalData.Core.Security.Config
|
|
{
|
|
public class AsymCryptParams : RSAFactoryParams
|
|
{
|
|
public string PemDirectory { get; init; } = string.Empty;
|
|
|
|
public string Separator { get; init; } = "_-_";
|
|
|
|
public IEnumerable<RSADecryptor> Decryptors { get; init; } = new List<RSADecryptor>();
|
|
|
|
/// <summary>
|
|
/// 0: Issuer - 1: Audience - 2: Secret version (if is encrypted)
|
|
/// </summary>
|
|
private string CreateFileName(params object[] objs) => string.Join(Separator, objs);
|
|
|
|
private string CreatePem(bool isEncrypted) => isEncrypted
|
|
? Instance.RSAFactory.CreateEncryptedPrivateKeyPem(keySizeInBits: KeySizeInBits, password: Secrets.PBE_PASSWORD,
|
|
pbeEncryptionAlgorithm: PbeEncryptionAlgorithm, hashAlgorithmName: PbeHashAlgorithmName, iterationCount: PbeIterationCount)
|
|
: Instance.RSAFactory.CreateRSAPrivateKeyPem(keySizeInBits: KeySizeInBits);
|
|
|
|
public override void OnDeserialized()
|
|
{
|
|
base.OnDeserialized();
|
|
|
|
// Create root folder if it does not exist
|
|
if (!Directory.Exists(PemDirectory))
|
|
Directory.CreateDirectory(PemDirectory);
|
|
|
|
foreach (var crypt in Decryptors)
|
|
{
|
|
// set default path
|
|
if (crypt.IsPemNull)
|
|
{
|
|
var file_name_params = new List<object> { crypt.Issuer, crypt.Audience };
|
|
if (crypt.Encrypt)
|
|
file_name_params.Add(Secrets.Version);
|
|
|
|
var file_name = CreateFileName(file_name_params);
|
|
var path = Path.Combine(PemDirectory, file_name);
|
|
|
|
if (File.Exists(path))
|
|
crypt.SetPem(File.ReadAllText(path));
|
|
else
|
|
{
|
|
var pem = CreatePem(crypt.Encrypt);
|
|
crypt.SetPem(File.ReadAllText(pem));
|
|
Task.Run(async () => await File.WriteAllTextAsync(path: path, pem));
|
|
}
|
|
}
|
|
|
|
crypt.Init();
|
|
}
|
|
}
|
|
}
|
|
} |