Added crypto file

This commit is contained in:
Chris Plaatjes 2023-02-16 17:03:01 -05:00
parent 4b6bb780d4
commit a4ff706a28

46
CryptographyService.cs Normal file
View File

@ -0,0 +1,46 @@
using System;
using Sodium;
namespace TeleTok
{
public class CryptographyService
{
public string ToHexString(byte[] input)
{
var hexString = BitConverter.ToString(input);
string result = hexString.Replace("-", "");
return result.ToLower();
}
public byte[] GenerateLoginDigest()
{
long now = DateTimeOffset.UtcNow.ToUnixTimeSeconds() * 1000;
var message = $"login:{now / 1000 / (5 * 60)}";
return GenericHash.Hash(message, (byte[]?) null, 32);
}
public KeyPair GenerateEd25519KeyPair(string seed)
{
byte[] hash = GenericHash.Hash(seed, (byte[]?) null, 32);
return PublicKeyAuth.GenerateKeyPair(hash);
}
public string GenerateHexSignature(byte[] loginDigest, byte[] secretKey)
{
byte[] signature = PublicKeyAuth.SignDetached(loginDigest, secretKey);
return ToHexString(signature);
}
public string GenerateHexId(byte[] publicKey)
{
byte[] hash = GenericHash.Hash(publicKey, null, publicKey.Length);
return ToHexString(hash);
}
}
}