Files
Auths/RsaKeyLoader/KeyLoader.cs
2024-10-13 14:56:22 +07:00

35 lines
862 B
C#

using System.Security.Cryptography;
namespace RsaKeyLoader;
public class KeyLoader
{
public static string PathString =
Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "auth-key");
public static RSA GetGeneratedKey()
{
if (File.Exists(PathString))
{
return LoadKey(PathString);
}
else
{
Generate(PathString);
return LoadKey(PathString);
}
}
public static void Generate(string path)
{
var rsaKey = RSA.Create();
var privateKey = rsaKey.ExportRSAPrivateKey();
File.WriteAllBytes(path, privateKey);
}
public static RSA LoadKey(string path)
{
var rsaKey = RSA.Create();
rsaKey.ImportRSAPrivateKey(File.ReadAllBytes(path), out _);
return rsaKey;
}
}