How to securely store user passwords ?, Secure storage of user passwords?
Original article link: click it
This article only captures a small part of the knowledge. Read the original article.
I. Basic Knowledge: Hashing with Salt)
We already know how fast a malicious attacker can use the query table and rainbow table to crack normal hash encryption. We have
We have learned that using random salt hashing can solve this problem. However, how can we change the value of salt?
Mixing in the password?
The salt value should use the encrypted Secure Pseudo-Random number generator (Cryptographically Secure Pseudo-Random)
Number Generator, CSPRNG. CSPRNG is very different from a common pseudo-random number generator,
For example, the rand () function in "C" language. As the name suggests, CSPRNG is designed for encryption security, which means it can
Random numbers that are highly random and completely unpredictable. We do not want the salt value to be predicted, so CSPRNG must be used.
The following table lists the CSPRNG methods of some mainstream programming platforms.
| Platform |
CSPRNG |
| PHP |
Mcrypt_create_iv, openssl_random_pseudo _ bytes |
| Java |
Java. security. SecureRandom |
| Dot NET (C #, VB) |
System. Security. Cryptography. RNGCryptoServiceProvider |
| Ruby |
SecureRandom |
| Python |
OS. urandom |
| Perl |
Math: Random: Secure |
| C/C ++ (Windows API) |
CryptGenRandom |
| Any language on GNU/Linux or Unix |
Read from/dev/random or/dev/urandom
|
Each user's password must use a unique salt value. Each time a user creates an account or changes the password, the password should use a new random salt value.
Never reuse a salt value. This salt value should also be long enough so that there are enough salt values for hash encryption. An empirical rule is that salt
The value must be at least as long as the output of the hash function. The salt should be stored in the user account table together with the password hash.
Steps for storing the password:
-
Use CSPRNG to generate a random salt value long enough.
-
Mix the salt value into the password and use the standard password hash function for encryption, such as Argon2, bcrypt, scrypt, or PBKDF2.
-
Store the salt value and the corresponding hash value in the user database.
Steps for password verification:
-
The user's salt value and corresponding hash value are retrieved from the database.
-
Mix the salt value with the password entered by the user and encrypt it using a common hash function.
-
Compare the result of the previous step to check whether the hash value is the same as the hash value stored in the database. If they are the same, the password is correct. Otherwise, the password is incorrect.