Password Verification - How to securely check if entered password is correct

空扰寡人 提交于 2019-12-04 20:42:46
Anti-weakpasswords
  • Do not use a single pass of any hashing function to store passwords.
  • Do not fail to use a random salt in the 8-16 byte range.
  • Do not use reversible encryption to store passwords.
  • Do not use the password precisely as entered as your encryption key.

Instead, when the user is selecting a keyword/passphrase

  • Generate a cryptographically random 8-16 byte salt
  • Use PBKDF2, BCrypt, or SCrypt with said salt and as large an iteration count/work factor as your processors can handle to create a password hash
    • If you use PBKDF2 in specific, do not request a larger output than the native hash size (SHA-1 = 20 bytes, SHA-256 is 32 bytes, SHA-384 is 48 bytes, and SHA-512 is 64 bytes), or you increase the comparative advantage an attacker has over you, the defender.

Then in your database, you store that user's particular:

  • Salt in the clear
  • Iteration count/work factor
    • So you can easily change/upgrade it later
  • Resulting password hash
  • Version of authentication protocol - this would be 2, probably, or 1.
    • So you can easily change/upgrade it later if you move from this method to NewWellKnownMethod later

When the user wants to authenticate to your system, you:

  • Retrieve their version, salt, iteration count/work factor, and resulting hash from the database
  • Hash whatever keyword/password they just entered with the salt and iteration count/work factor from the database.
  • Compare the result you just got with what was in the database; if they're the same, let them in.
    • Advanced: use a constant time compare, so it doesn't just quit trying if the first byte is different, to reduce the vulnerability to timing attacks.

Please read How to securely hash passwords?, of which Thomas Porrin's answer is currently the most commonly referred to Stackexchange treatise on password hashing, and certainly the best I've seen so far.

Greg

It's not good way of doing that. You should use one way hash algorithm to hash the password (you won't be able to decrypt it). After you hash the password you save it to database after the user provide password you have to hash it and compare it (the hash value) with the hash you store in database. If it matches it means it's the same if not the authentication failed.

In that way even if someone gain access to the database the data will be secured, he won't do anything with that, it's not password stored there.

Most of the authentication is made that way.

//Extended

You should use some hash algorithm made for this kind of job.

Check out SHA or MD5

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!