Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public static class StringHasher
- {
- public static string GenerateHash(string value)
- {
- string res = "";
- byte[] salt;
- new RNGCryptoServiceProvider().GetBytes(salt = new byte[16]);
- var pbkdf2 = new Rfc2898DeriveBytes(value, salt, 10);
- byte[] hash = pbkdf2.GetBytes(20);
- byte[] hashBytes = new byte[36];
- Array.Copy(salt, 0, hashBytes, 0, 16);
- Array.Copy(hash, 0, hashBytes, 16, 20);
- res = Convert.ToBase64String(hashBytes);
- return res;
- }
- public static bool ValidateHash(string rawValue, string hashedValue)
- {
- byte[] hashBytes = Convert.FromBase64String(hashedValue);
- /* Get the salt */
- byte[] salt = new byte[16];
- Array.Copy(hashBytes, 0, salt, 0, 16);
- /* Compute the hash on the password the user entered */
- var pbkdf2 = new Rfc2898DeriveBytes(rawValue, salt, 10);
- byte[] hash = pbkdf2.GetBytes(20);
- /* Compare the results */
- for (int i = 0; i < 20; i++)
- if (hashBytes[i + 16] != hash[i])
- return false;
- return true;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement