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:
- retrieve user data database login (for example,
getuser(login)
). user class should contains login, hashed password , salt. - if there no user login, fail authentication. else execute
calculatehash()
password
, salt user class retrieved on previous step. - compare 2 strings: first hashed password user class , second
calculatehash()
method. if hashes equals user authenticated.
Comments
Post a Comment