Pr0gramer

PASSWORD HASH SALT

Sep 29th, 2018
387
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.68 KB | None | 0 0
  1. Does salt need to be random to secure a password hash?
  2. private static byte[] HashPassword(string password)
  3. {
  4.    using (var deriveBytes = new Rfc2898DeriveBytes(password, 10))
  5.    {
  6.       byte[] salt = deriveBytes.Salt;
  7.       byte[] key  = deriveBytes.GetBytes(20);
  8.  
  9.       return salt.Concat(key).ToArray();  //Return Salt+Key
  10.    }
  11. }
  12.    
  13. var salt = //1st  10 bytes stored in the DB
  14. var key  = //Next 20 bytes stored in the DB
  15. using (var deriveBytes = new Rfc2898DeriveBytes(password, salt))
  16. {
  17.    byte[] newKey = deriveBytes.GetBytes(20);
  18.  
  19.    if (newKey.SequenceEqual(key) == false)  //Check if keys match
  20.    {
  21.       return "No Match";
  22.    }
  23.    else { return "Passwords match"; }
Advertisement
Add Comment
Please, Sign In to add comment