2023-07-10 07:35:05 -04:00
|
|
|
using System;
|
2023-08-21 10:58:17 -04:00
|
|
|
using System.Net.Http;
|
2023-07-10 07:35:05 -04:00
|
|
|
using System.Security.Cryptography;
|
|
|
|
using JacobTechEncryption;
|
2023-08-21 10:58:17 -04:00
|
|
|
using JacobTechEncryption.Enums;
|
2023-07-10 14:19:03 -04:00
|
|
|
using Luski.net.Enums;
|
2023-08-21 10:58:17 -04:00
|
|
|
using Luski.net.Structures;
|
2023-07-10 07:35:05 -04:00
|
|
|
|
|
|
|
namespace Luski.net;
|
|
|
|
|
|
|
|
public class ServerEncryption
|
|
|
|
{
|
|
|
|
internal bool Generating, Generated;
|
|
|
|
internal string ServerPublicKey = "", MyPublicKey = "", myPrivateKey = "", OfflinePrivateKey = "", OfflinePublicKey = "";
|
2023-08-25 12:07:36 -04:00
|
|
|
|
2023-07-10 07:35:05 -04:00
|
|
|
internal byte[] Hash = default!;
|
2023-08-25 12:07:36 -04:00
|
|
|
internal ServerEncryption(ServerStorage Storage)
|
2023-07-10 07:35:05 -04:00
|
|
|
{
|
2023-07-10 14:19:03 -04:00
|
|
|
this.Storage = Storage;
|
2023-07-10 07:35:05 -04:00
|
|
|
}
|
2023-07-10 14:19:03 -04:00
|
|
|
|
|
|
|
public string GetChannelKey(long Channel)
|
|
|
|
{
|
2023-08-21 10:58:17 -04:00
|
|
|
return Storage.GetResourceKeyRaw(StorageDirectory.ChannelKeys, Channel.ToString(), Hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
public LocalKeyInfo GetKey(long Key)
|
|
|
|
{
|
|
|
|
return Storage.GetResourceKey(StorageDirectory.ServerKeys, Key.ToString(), Hash);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void SetKey(long Key, LocalKeyInfo Info)
|
|
|
|
{
|
|
|
|
Storage.SetResourceKey(StorageDirectory.ServerKeys, Key.ToString(), Info, Hash);
|
2023-07-10 14:19:03 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
public void SetChannelKey(long Channel, string Key)
|
|
|
|
{
|
|
|
|
Storage.SetResourceKey(StorageDirectory.ChannelKeys, Channel.ToString(), Hash, Key);
|
|
|
|
}
|
|
|
|
|
|
|
|
private ServerStorage Storage { get; }
|
2023-07-10 07:35:05 -04:00
|
|
|
|
|
|
|
internal int PasswordVersion = 0;
|
|
|
|
internal byte[] LocalPasswordEncrypt(byte[] Password) => LocalPasswordEncrypt(Password, PasswordVersion);
|
|
|
|
internal string RemotePasswordEncrypt(byte[] Password) => RemotePasswordEncrypt(Password, PasswordVersion);
|
|
|
|
|
|
|
|
internal byte[] LocalPasswordEncrypt(byte[] Password, int PasswordVersion)
|
|
|
|
{
|
|
|
|
return PasswordVersion switch
|
|
|
|
{
|
|
|
|
0 => SHA256.Create().ComputeHash(Password),
|
|
|
|
_ => throw new ArgumentException("The value provided was not accepted", nameof(PasswordVersion)),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
internal string RemotePasswordEncrypt(byte[] Password, int PasswordVersion)
|
|
|
|
{
|
|
|
|
return PasswordVersion switch
|
|
|
|
{
|
|
|
|
0 => Convert.ToBase64String(Encryption.RSA.Encrypt(LocalPasswordEncrypt(Password, PasswordVersion), ServerPublicKey)),
|
|
|
|
_ => throw new ArgumentException("The value provided was not accepted", nameof(PasswordVersion)),
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
public void GenerateKeys()
|
|
|
|
{
|
|
|
|
if (!Generating)
|
|
|
|
{
|
|
|
|
Generating = true;
|
|
|
|
GenerateNewKeys(out MyPublicKey, out myPrivateKey);
|
|
|
|
GenerateNewKeys(out OfflinePublicKey, out OfflinePrivateKey);
|
|
|
|
Generated = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public static void GenerateNewKeys(out string Public, out string Private, int KeySize = 4096)
|
|
|
|
{
|
|
|
|
using RSACryptoServiceProvider r = new(KeySize);
|
|
|
|
Private = r.ToXmlString(true);
|
|
|
|
Public = r.ToXmlString(false);
|
|
|
|
}
|
|
|
|
}
|