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 token string. /// /// /// The resulting token string is constructed as follows: /// string.Join(Separator, Issuer, Audience, Secret_version). /// If Secret_version is not null, it will be included in the concatenation. /// /// /// For example, if Separator = "_-_", the output might look like: /// "Issuer_-_Audience_-_Secret_version". /// public string Separator { get; init; } = "_-_"; public IEnumerable Decryptors { get; init; } = new List(); 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 { crypt.Issuer, crypt.Audience }; if (crypt.Encrypt) file_name_params.Add(Secrets.Version); var path = Path.Combine(PemDirectory, string.Join(Separator, file_name_params)); if (File.Exists(path)) crypt.SetPem(File.ReadAllText(path)); else { var pem = crypt.Encrypt ? Instance.RSAFactory.CreateEncryptedPrivateKeyPem(pbeParameters: PbeParameters, keySizeInBits: KeySizeInBits, password: Secrets.PBE_PASSWORD) : Instance.RSAFactory.CreatePrivateKeyPem(keySizeInBits: KeySizeInBits); crypt.SetPem(File.ReadAllText(pem)); // Save file in background Task.Run(async () => await File.WriteAllTextAsync(path: path, pem)); } } crypt.Init(); } } } }