using System.Security.Cryptography;
public static bool Login(string password, Tuple<string, string> user)
if (!VerifyPasswordHash(password, Convert.FromBase64String(user.Item1),
Convert.FromBase64String(user.Item2)))
private static bool VerifyPasswordHash(string password, byte[] passwordHash, byte[] passwordSalt)
using (var hmac = new System.Security.Cryptography.HMACSHA512(passwordSalt))
byte[] computedHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
for (int i = 0; i < computedHash.Length; i++)
if (computedHash[i] != passwordHash[i])
public static Tuple<string, string> Register(string password)
byte[] passwordHash, passwordSalt;
CreatePasswordHash(password, out passwordHash, out passwordSalt);
return new Tuple<string, string>( Convert.ToBase64String(passwordHash), Convert.ToBase64String(passwordSalt) );
private static void CreatePasswordHash(string password, out byte[] passwordHash, out byte[] passwordSalt)
using (var hmac = new System.Security.Cryptography.HMACSHA512())
passwordHash = hmac.ComputeHash(System.Text.Encoding.UTF8.GetBytes(password));
public static void Main()
Tuple<string, string> r = Register( password );
Console.WriteLine( r.Item1 );
Console.WriteLine( r.Item2 );
Console.WriteLine( Login( password, r ));