refactor(RSACryptographer): Verzeichnis und Dateiname wurden entfernt.
- Datei-Leseprozess in init-Methode entfernt.
This commit is contained in:
@@ -8,10 +8,6 @@ namespace DigitalData.Core.Abstractions.Security
|
|||||||
|
|
||||||
public RSAEncryptionPadding Padding { get; init; }
|
public RSAEncryptionPadding Padding { get; init; }
|
||||||
|
|
||||||
public string? Directory { get; set; }
|
|
||||||
|
|
||||||
public string? FileName { get; set; }
|
|
||||||
|
|
||||||
public string Issuer { get; init; }
|
public string Issuer { get; init; }
|
||||||
|
|
||||||
public string Audience { get; init; }
|
public string Audience { get; init; }
|
||||||
|
|||||||
@@ -41,16 +41,16 @@ namespace DigitalData.Core.Security.Config
|
|||||||
foreach (var crypt in cryptographers)
|
foreach (var crypt in cryptographers)
|
||||||
{
|
{
|
||||||
// set default path
|
// set default path
|
||||||
if (crypt.Pem is null)
|
//if (crypt.Pem is null)
|
||||||
{
|
//{
|
||||||
crypt.Directory ??= Directory;
|
// crypt.Directory ??= Directory;
|
||||||
crypt.FileName ??= string.Format(
|
// crypt.FileName ??= string.Format(
|
||||||
FileNameFormat,
|
// FileNameFormat,
|
||||||
crypt.Issuer,
|
// crypt.Issuer,
|
||||||
crypt.Audience,
|
// crypt.Audience,
|
||||||
TypeTagOf(crypt),
|
// TypeTagOf(crypt),
|
||||||
Secrets.Version);
|
// Secrets.Version);
|
||||||
}
|
//}
|
||||||
|
|
||||||
crypt.Init();
|
crypt.Init();
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -10,17 +10,13 @@ namespace DigitalData.Core.Security.Cryptographer
|
|||||||
public string Pem
|
public string Pem
|
||||||
{
|
{
|
||||||
get => _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;
|
init => _pem = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
internal bool IsPemNull => _pem is null;
|
internal bool IsPemNull => _pem is null;
|
||||||
|
|
||||||
public string? PemPath => FileName is null ? null : Path.Combine(Directory ?? string.Empty, FileName);
|
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 string? Directory { get; set; }
|
|
||||||
|
|
||||||
public string? FileName { get; set; }
|
|
||||||
|
|
||||||
public RSAEncryptionPadding Padding { get; init; } = RSAEncryptionPadding.OaepSHA256;
|
public RSAEncryptionPadding Padding { get; init; } = RSAEncryptionPadding.OaepSHA256;
|
||||||
|
|
||||||
@@ -34,24 +30,10 @@ namespace DigitalData.Core.Security.Cryptographer
|
|||||||
|
|
||||||
internal void SetPem(string pem) => _pem = pem;
|
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()
|
public virtual void Init()
|
||||||
{
|
{
|
||||||
if(_pem is null)
|
if (_pem is null)
|
||||||
{
|
throw PemIsNullException;
|
||||||
if(PemPath is null)
|
|
||||||
UnableToInitPemEvent();
|
|
||||||
if (File.Exists(PemPath))
|
|
||||||
_pem = File.ReadAllText(PemPath);
|
|
||||||
else
|
|
||||||
FileNotFoundEvent();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -1,5 +1,4 @@
|
|||||||
using DigitalData.Core.Abstractions.Security;
|
using DigitalData.Core.Abstractions.Security;
|
||||||
using DigitalData.Core.Security.Config;
|
|
||||||
using DigitalData.Core.Security.Extensions;
|
using DigitalData.Core.Security.Extensions;
|
||||||
using System.Security.Cryptography;
|
using System.Security.Cryptography;
|
||||||
|
|
||||||
@@ -34,22 +33,5 @@ namespace DigitalData.Core.Security.Cryptographer
|
|||||||
else
|
else
|
||||||
RSA.ImportFromPem(Pem);
|
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.Abstractions.Security;
|
||||||
using DigitalData.Core.Security.Config;
|
|
||||||
using DigitalData.Core.Security.Extensions;
|
using DigitalData.Core.Security.Extensions;
|
||||||
|
|
||||||
namespace DigitalData.Core.Security.Cryptographer
|
namespace DigitalData.Core.Security.Cryptographer
|
||||||
@@ -17,21 +16,5 @@ namespace DigitalData.Core.Security.Cryptographer
|
|||||||
base.Init();
|
base.Init();
|
||||||
RSA.ImportFromPem(base.Pem);
|
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