Advertisement
Guest User

Untitled

a guest
Jul 25th, 2017
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.23 KB | None | 0 0
  1. public static class StringHasher
  2. {
  3. public static string GenerateHash(string value)
  4. {
  5. string res = "";
  6.  
  7. byte[] salt;
  8. new RNGCryptoServiceProvider().GetBytes(salt = new byte[16]);
  9.  
  10. var pbkdf2 = new Rfc2898DeriveBytes(value, salt, 10);
  11. byte[] hash = pbkdf2.GetBytes(20);
  12.  
  13. byte[] hashBytes = new byte[36];
  14. Array.Copy(salt, 0, hashBytes, 0, 16);
  15. Array.Copy(hash, 0, hashBytes, 16, 20);
  16.  
  17. res = Convert.ToBase64String(hashBytes);
  18. return res;
  19. }
  20.  
  21. public static bool ValidateHash(string rawValue, string hashedValue)
  22. {
  23. byte[] hashBytes = Convert.FromBase64String(hashedValue);
  24.  
  25. /* Get the salt */
  26. byte[] salt = new byte[16];
  27. Array.Copy(hashBytes, 0, salt, 0, 16);
  28.  
  29. /* Compute the hash on the password the user entered */
  30. var pbkdf2 = new Rfc2898DeriveBytes(rawValue, salt, 10);
  31. byte[] hash = pbkdf2.GetBytes(20);
  32.  
  33. /* Compare the results */
  34. for (int i = 0; i < 20; i++)
  35. if (hashBytes[i + 16] != hash[i])
  36. return false;
  37.  
  38. return true;
  39. }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement