diff --git a/CryptographyService.cs b/CryptographyService.cs new file mode 100644 index 0000000..3e0417f --- /dev/null +++ b/CryptographyService.cs @@ -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); + } + } +} \ No newline at end of file