How to store and verify a password?
On May 21,2022 by Tom RoutleyIf you develop an application that manages passwords, it is important to do so on pain of risking piracy of your system and compromise your data users.
There is good practice to store a password.
The right way to store
Store the login and hash (login + password + salt)
Pseudo-code: passwordHash = MD5MD5( login + "zo5pro$1pvkhj6*cz4a8ùtvb#ui4oeuio" + password)
Store and passwordHash login. Do not store password.
Why hash?
We should never store the password in clear text.
Risk: If someone breaks into your database of passwords, it can retrieve and use them directly.
If someone breaks into your database of passwords, it can retrieve and use them directly. Protection: The hash used to calculate a fingerprint of the password. As the algorithm is not reversible, it can not immediately find the password from the hash.
The hash used to calculate a fingerprint of the password. As the algorithm is not reversible, it can not immediately find the password from the hash. This is why we use a cryptographic hash. Typically MD5, SHA-1 or other (SHA-256, SHA-512 ...)
Never use a CRC or CRC32.
Why using salt?
The rainbow-tables are large tables containing hash (MD5, and others) precomputed.
This allows you to quickly find the password that gave a specific hash.
Risk: If you just use MD5 (password), the rainbow-tables allows to recover the password to MD5 in minutes, even seconds.
Protection: Using a salt, this makes the rainbow-tables completely unnecessary.
The salt is an arbitrary value, the length of your choice. Set it as a constant in your application.
Why add the login?
If you just store MD5 (salt + password) or MD5 (password), this means that two users with the same password will have the same hash.
Risk: You can easily identify the users with the same password. If a user is compromised, it allows immediate access to other users (who may have greater rights).
Protection: By adding the login before hasher, the MD5 result will be different for each user, even if they have the same password.
Verification
When you receive login and password, you simply repeat the same calculation:
Look in your base, for the hash for this login
Compare the hash value (login + salt + password)
If the two hashes are identical, the password entered is correct.
Thanks to sebsauvage for this tip.
Article Recommendations
Latest articles
Popular Articles
Archives
- November 2024
- October 2024
- September 2024
- August 2024
- July 2024
- June 2024
- May 2024
- April 2024
- March 2024
- February 2024
- January 2024
- December 2023
- November 2023
- October 2023
- September 2023
- August 2023
- July 2023
- June 2023
- May 2023
- April 2023
- March 2023
- February 2023
- January 2023
- December 2022
- November 2022
- October 2022
- September 2022
- August 2022
- July 2022
- June 2022
- May 2022
- April 2022
- March 2022
- February 2022
- January 2022
- December 2021
- November 2021
- October 2021
- September 2021
- August 2021
- July 2021
- January 2021
Leave a Reply