using DigitalData.Core.Security.Cryptographer; namespace DigitalData.Core.Security.Config { public class AsymCryptParams : RSAFactoryParams { public string PemDirectory { get; init; } = string.Empty; /// /// Represents the separator used to concatenate the components of a file-related token string. /// /// /// The resulting file-related token string is constructed as follows: /// string.Join(FileNameSeparator, Issuer, Audience, Secret_version). /// If Secret_version is not null, it will be included in the concatenation. /// /// /// For example, if FileNameSeparator = "_-_", the output might look like: /// "Issuer_-_Audience_-_Secret_version". /// public string FileNameSeparator { get; init; } = "_-_"; /// /// Represents the separator used to concatenate the components of a key-related token string. /// /// /// The resulting key-related token string is constructed as follows: /// string.Join(KeyNameSeparator, Issuer, Audience, Secret_version). /// If Secret_version is not null, it will be included in the concatenation. /// /// /// For example, if KeyNameSeparator = ":", the output might look like: /// "Issuer:Audience:Secret_version". /// public string KeyNameSeparator { get; init; } = ":"; public IEnumerable Decryptors { get; init; } = new List(); public RSADecryptor? Vault { get; init; } public override void OnDeserialized() { base.OnDeserialized(); // Create root folder if it does not exist if (!Directory.Exists(PemDirectory)) Directory.CreateDirectory(PemDirectory); foreach (var decryptor in Decryptors) { // set default path if (decryptor.IsPemNull) { var file_name_params = new List { decryptor.Issuer, decryptor.Audience }; if (decryptor.IsEncrypted) 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 ? 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)); } } } } } }