- Kombiniert `Encryptors` und `Decryptors` in `cryptographers` für eine vereinfachte Initialisierung in `OnDeserialized`.
59 lines
2.0 KiB
C#
59 lines
2.0 KiB
C#
using DigitalData.Core.Abstractions.Security;
|
|
|
|
namespace DigitalData.Core.Security.Config
|
|
{
|
|
public class AsymCryptParams : RSAFactoryParams
|
|
{
|
|
public string Directory { get; init; } = string.Empty;
|
|
|
|
/// <summary>
|
|
/// 0: Issuer - 1: Audience - 2: Type tag - 3: Version
|
|
/// </summary>
|
|
public string FileNameFormat { get; init; } = "{0}_-_{1}_-_{2}_-_{3}.pem";
|
|
|
|
public string EncryptorTag { get; init; } = "public";
|
|
|
|
public string DecryptorTag { get; init; } = "private";
|
|
|
|
public string EncryptedDecryptorTag { get; init; } = "enc-private";
|
|
|
|
public IEnumerable<IRSADecryptor> Decryptors { get; init; } = new List<IRSADecryptor>();
|
|
|
|
public IEnumerable<IRSAEncryptor> Encryptors { get; init; } = new List<IRSAEncryptor>();
|
|
|
|
private string TypeTagOf(IRSACryptographer crypt)
|
|
{
|
|
if (crypt is IRSAEncryptor)
|
|
return EncryptorTag;
|
|
else if (crypt is IRSADecryptor decryptor)
|
|
return decryptor.Encrypt ? EncryptedDecryptorTag : DecryptorTag;
|
|
else
|
|
throw new InvalidOperationException(
|
|
"Unknown cryptographer type. The crypt parameter must be either IRSAEncryptor or IRSADecryptor.");
|
|
}
|
|
|
|
public override void OnDeserialized()
|
|
{
|
|
base.OnDeserialized();
|
|
|
|
var cryptographers = Encryptors.Cast<IRSACryptographer>().Concat(Decryptors.Cast<IRSACryptographer>());
|
|
|
|
foreach (var crypt in cryptographers)
|
|
{
|
|
// set default path
|
|
if (crypt.Pem is null)
|
|
{
|
|
crypt.Directory ??= Directory;
|
|
crypt.FileName ??= string.Format(
|
|
FileNameFormat,
|
|
crypt.Issuer,
|
|
crypt.Audience,
|
|
TypeTagOf(crypt),
|
|
Secrets.Version);
|
|
}
|
|
|
|
crypt.Init();
|
|
}
|
|
}
|
|
}
|
|
} |