36 lines
828 B
C#
36 lines
828 B
C#
using System.Security.Cryptography;
|
|
using ldap_cesi.Services.Interfaces;
|
|
|
|
namespace ldap_cesi.Services;
|
|
|
|
public class RsaKeyService : IRsaKeyService
|
|
{
|
|
private readonly RSA _rsa;
|
|
private readonly string _keyPath;
|
|
|
|
public RsaKeyService(IConfiguration configuration)
|
|
{
|
|
_rsa = RSA.Create();
|
|
_keyPath = configuration["Jwt:KeyPath"] ?? "cle.bin";
|
|
LoadOrCreateRsaKey();
|
|
}
|
|
|
|
private void LoadOrCreateRsaKey()
|
|
{
|
|
if (File.Exists(_keyPath))
|
|
{
|
|
var keyBytes = File.ReadAllBytes(_keyPath);
|
|
_rsa.ImportRSAPrivateKey(keyBytes, out _);
|
|
}
|
|
else
|
|
{
|
|
var keyBytes = _rsa.ExportRSAPrivateKey();
|
|
File.WriteAllBytes(_keyPath, keyBytes);
|
|
}
|
|
}
|
|
|
|
public RSA GetRsaKey()
|
|
{
|
|
return _rsa;
|
|
}
|
|
} |