Added crypto file
This commit is contained in:
		
							
								
								
									
										46
									
								
								CryptographyService.cs
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										46
									
								
								CryptographyService.cs
									
									
									
									
									
										Normal 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);
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
}
 | 
			
		||||
		Reference in New Issue
	
	Block a user