c# - How do I compare two hashes using SHA256Managed? -


i can hash user-entered password, i'm unable find out how compare stored hash , new hash user-entered password.

this hashing code:

public static string calculatehash(string cleartextpassword, string salt) {     //convert salted password byte array     byte[] saltedhashbytes = encoding.utf8.getbytes(cleartextpassword + salt);      //use hash algorithm calulate hash     hashalgorithm algorithm = new sha256managed();     byte[] hash = algorithm.computehash(saltedhashbytes);      //return hash base64 encoded string compared , stored     return convert.tobase64string(hash); } 

how compare 2 hashes validate password?

first, should store salt hashed value.

next, when user trying authenticate login , password can use next scenario:

  1. retrieve user data database login (for example, getuser(login)). user class should contains login, hashed password , salt.
  2. if there no user login, fail authentication. else execute calculatehash() password , salt user class retrieved on previous step.
  3. compare 2 strings: first hashed password user class , second calculatehash() method. if hashes equals user authenticated.

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -