Asp.net MVC - How to hash password

前端 未结 3 637
时光说笑
时光说笑 2020-12-16 06:00

How do I hash an users input(password) to database and then later read the hashed password during login?

I believe the solution is to hash the password upon registe

3条回答
  •  鱼传尺愫
    2020-12-16 06:32

    You should never need to unhash a password. A cryptographic hash function is supposed to be a one-way operation.

    (And that's precisely why it is called hashing and not encrypting. If unhashing passwords was to be a normal procedure in your flow of operations, then it would not be hashing and unhashing, it would be encrypting and decrypting. So, hashing is a different thing from encryption, precisely because unhashing is not supposed to ever happen.)

    Hashing provides security, because nobody can steal your user's passwords even if they manage to view the contents of your database.

    • When the user registers, compute the hash of their password, store the hash in the database, and forget the password forever.

    • When the user logs in, compute the hash of the password they entered, (forget that password too,) and see if the hash matches the hash stored in the database.

    This is the mechanism used by most websites out there, and that's precisely why if you successfully go through the "I forgot my password" procedure, they will still not show you your password: they don't have it; they cannot retrieve it even if they wanted to. Instead, they send you a password reset link.

    As for how to compute a hash from a string, the interwebz abound with answers to that question, for example: MD5 (MSDN); SHA-256 (MSDN); SHA-512 (MSDN)

提交回复
热议问题