using System.Security.Cryptography;
public static void Main(){
var pass = HashPassword("1234567");
for (int i = 0; i < pass.Hash.Length; i++)
Console.WriteLine("----");
for (int i = 0; i < pass.Salt.Length; i++)
byte[] newpass = Hash("1234567",pass.Salt);
Console.WriteLine(SlowEquals(newpass,pass.Hash));
public byte[] Hash { get; set; }
public byte[] Salt { get; set; }
internal static byte[] Hash(string plaintext, byte[] salt)
SHA512Cng hashFunc = new SHA512Cng();
byte[] plainBytes = System.Text.Encoding.ASCII.GetBytes(plaintext);
byte[] toHash = new byte[plainBytes.Length + salt.Length];
plainBytes.CopyTo(toHash, 0);
salt.CopyTo(toHash, plainBytes.Length);
return hashFunc.ComputeHash(toHash);
private static byte[] GenerateSalt()
RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();
byte[] salt = new byte[256];
internal static bool SlowEquals(byte[] a, byte[] b)
int diff = a.Length ^ b.Length;
for (int i = 0; i < a.Length && i < b.Length; i++)
internal static Password HashPassword(string clearPassword)
var salt = GenerateSalt();
return new Password { Salt = salt, Hash = Hash(clearPassword, salt) };