TeleTok/CryptographyService.cs

46 lines
1.2 KiB
C#
Raw Permalink Normal View History

2023-02-16 17:03:01 -05:00
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);
}
}
}