refactor(RSACryptographer): Verzeichnis und Dateiname wurden entfernt.
- Datei-Leseprozess in init-Methode entfernt.
This commit is contained in:
@@ -7,11 +7,7 @@ namespace DigitalData.Core.Abstractions.Security
|
||||
public string Pem { get; init; }
|
||||
|
||||
public RSAEncryptionPadding Padding { get; init; }
|
||||
|
||||
public string? Directory { get; set; }
|
||||
|
||||
public string? FileName { get; set; }
|
||||
|
||||
|
||||
public string Issuer { get; init; }
|
||||
|
||||
public string Audience { get; init; }
|
||||
|
||||
@@ -41,16 +41,16 @@ namespace DigitalData.Core.Security.Config
|
||||
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);
|
||||
}
|
||||
//if (crypt.Pem is null)
|
||||
//{
|
||||
// crypt.Directory ??= Directory;
|
||||
// crypt.FileName ??= string.Format(
|
||||
// FileNameFormat,
|
||||
// crypt.Issuer,
|
||||
// crypt.Audience,
|
||||
// TypeTagOf(crypt),
|
||||
// Secrets.Version);
|
||||
//}
|
||||
|
||||
crypt.Init();
|
||||
}
|
||||
|
||||
@@ -10,18 +10,14 @@ namespace DigitalData.Core.Security.Cryptographer
|
||||
public string Pem
|
||||
{
|
||||
get => _pem
|
||||
?? throw new InvalidOperationException($"Pem is not initialized. Please ensure that the PEM is set or properly loaded from the file. Issuer: {Issuer}, Audience: {Audience}.");
|
||||
?? throw PemIsNullException;
|
||||
init => _pem = value;
|
||||
}
|
||||
|
||||
internal bool IsPemNull => _pem is null;
|
||||
|
||||
public string? PemPath => FileName is null ? null : Path.Combine(Directory ?? string.Empty, FileName);
|
||||
|
||||
public string? Directory { get; set; }
|
||||
|
||||
public string? FileName { get; set; }
|
||||
|
||||
private InvalidOperationException PemIsNullException => new($"Pem is not initialized. Please ensure that the PEM is set or properly loaded from the file. Issuer: {Issuer}, Audience: {Audience}.");
|
||||
|
||||
public RSAEncryptionPadding Padding { get; init; } = RSAEncryptionPadding.OaepSHA256;
|
||||
|
||||
protected virtual RSA RSA { get; } = RSA.Create();
|
||||
@@ -34,24 +30,10 @@ namespace DigitalData.Core.Security.Cryptographer
|
||||
|
||||
internal void SetPem(string pem) => _pem = pem;
|
||||
|
||||
public virtual void UnableToInitPemEvent() => throw new InvalidOperationException(
|
||||
$"Pem is not initialized and pem file is null. Issuer is {Issuer} and audience {Audience}.");
|
||||
|
||||
public virtual void FileNotFoundEvent() => throw new FileNotFoundException(
|
||||
$"Pem is not initialized and pem file is not found in {PemPath}. Issuer is {Issuer} and audience {Audience}.");
|
||||
|
||||
// TODO: make file read asynchronous, consider multiple routing
|
||||
public virtual void Init()
|
||||
{
|
||||
if(_pem is null)
|
||||
{
|
||||
if(PemPath is null)
|
||||
UnableToInitPemEvent();
|
||||
if (File.Exists(PemPath))
|
||||
_pem = File.ReadAllText(PemPath);
|
||||
else
|
||||
FileNotFoundEvent();
|
||||
}
|
||||
if (_pem is null)
|
||||
throw PemIsNullException;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using DigitalData.Core.Abstractions.Security;
|
||||
using DigitalData.Core.Security.Config;
|
||||
using DigitalData.Core.Security.Extensions;
|
||||
using System.Security.Cryptography;
|
||||
|
||||
@@ -34,22 +33,5 @@ namespace DigitalData.Core.Security.Cryptographer
|
||||
else
|
||||
RSA.ImportFromPem(Pem);
|
||||
}
|
||||
|
||||
public override void FileNotFoundEvent()
|
||||
{
|
||||
var new_decryptor = new RSADecryptor()
|
||||
{
|
||||
Pem = RSAFactory<RSAFactoryParams>.Static.CreateRSAPrivateKeyPem(),
|
||||
Encrypt = Encrypt
|
||||
};
|
||||
|
||||
_pem = new_decryptor.Pem;
|
||||
|
||||
if (PemPath is not null)
|
||||
Task.Run(async () =>
|
||||
{
|
||||
await File.WriteAllTextAsync(_pem, PemPath);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,5 +1,4 @@
|
||||
using DigitalData.Core.Abstractions.Security;
|
||||
using DigitalData.Core.Security.Config;
|
||||
using DigitalData.Core.Security.Extensions;
|
||||
|
||||
namespace DigitalData.Core.Security.Cryptographer
|
||||
@@ -17,21 +16,5 @@ namespace DigitalData.Core.Security.Cryptographer
|
||||
base.Init();
|
||||
RSA.ImportFromPem(base.Pem);
|
||||
}
|
||||
|
||||
public override void FileNotFoundEvent()
|
||||
{
|
||||
var new_decryptor = new RSADecryptor()
|
||||
{
|
||||
Pem = RSAFactory<RSAFactoryParams>.Static.CreateRSAPrivateKeyPem()
|
||||
};
|
||||
|
||||
_pem = new_decryptor.Encryptor.Pem;
|
||||
|
||||
if (PemPath is not null)
|
||||
Task.Run(async () =>
|
||||
{
|
||||
await File.WriteAllTextAsync(_pem, PemPath);
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user