From a4ff706a2848568380849709c90217d78ac24413 Mon Sep 17 00:00:00 2001 From: Chris Plaatjes Date: Thu, 16 Feb 2023 17:03:01 -0500 Subject: [PATCH] Added crypto file --- CryptographyService.cs | 46 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) create mode 100644 CryptographyService.cs 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