using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Security.Cryptography; namespace hashing { class Program { static void Main(string[] args) { RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider(); byte[] salt = new byte[0x10]; rng.GetBytes(salt); Rfc2898DeriveBytes deriveBytes = new Rfc2898DeriveBytes("the password", salt, /*iterations*/ 5); byte[] passHash = deriveBytes.GetBytes(0x100); byte[] result = Merge(passHash, salt); byte[] salt2 = ExtractSalt(result, 0x10); bool same = Compare(salt, salt2); } private static bool Compare(byte[] left, byte[] right) { if (left == null) { return right == null; } if (left.Length != right.Length) { return false; } for (int i = 0; i < left.Length; i++) { if (left[i] != right[i]) { return false; } } return true; } private static byte[] Merge(byte[] first, byte[] second) { byte[] result = new byte[first.Length + second.Length]; Buffer.BlockCopy(first, 0, result, 0, first.Length); Buffer.BlockCopy(second, 0, result, first.Length, second.Length); return result; } private static byte[] ExtractSalt(byte[] hash, int length) { byte[] salt = new byte[length]; Buffer.BlockCopy(hash, hash.Length - length, salt, 0, length); return salt; } } }