ldap-cesi/ldap-cesi/Services/RsaKeyService.cs
BuzzLeclair 8901f921c7 - Ajout d'automapper pour convertir les dto en entité et inversement
- Ajout des méthodes nécessaire pour la gestion de JWT
2025-03-02 16:50:53 +01:00

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;
}
}