37 lines
1.1 KiB
C#
37 lines
1.1 KiB
C#
using DigitalData.Core.Abstractions.Security;
|
|
using DigitalData.Core.Security.Config;
|
|
using DigitalData.Core.Security.Extensions;
|
|
|
|
namespace DigitalData.Core.Security.Cryptographer
|
|
{
|
|
public class RSAEncryptor : RSACryptographer, IRSAEncryptor, IRSACryptographer
|
|
{
|
|
public byte[] Encrypt(byte[] data) => RSA.Encrypt(data, Padding);
|
|
|
|
public string Encrypt(string data) => RSA.Encrypt(data.Base64ToByte(), Padding).BytesToString();
|
|
|
|
public bool Verify(string data, string signature) => Encrypt(data) == signature;
|
|
|
|
public override void Init()
|
|
{
|
|
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);
|
|
});
|
|
}
|
|
}
|
|
} |