Of course, it is a good idea to switch to a more secure hash algorithm. There is a password_hash () function that you can use to create BCrypt hashes:
// Hash a new password for storing in the database. // The function automatically generates a cryptographically safe salt. $hashToStoreInDb = password_hash($password, PASSWORD_DEFAULT); // Check if the hash of the entered login password, matches the stored hash. // The salt and the cost factor will be extracted from $existingHashFromDb. $isPasswordCorrect = password_verify($password, $existingHashFromDb);
From your answer, I assume that you used the unsalted MD5 value, so double hashing might be a good solution here. Just pass the MD5 hash to password_hash (), it will generate a safe salt on its own.
// Migrating the old MD5 hashes to MD5-BCrypt $hashToStoreInDb = password_hash($existingMd5Hash, PASSWORD_DEFAULT);
To verify, first check the double hash , and then check the password accordingly.
if (checkIfDoubleHash($existingHashFromDb)) { $isPasswordCorrect = password_verify(MD5($password), $existingHashFromDb); // Update database with pure BCrypt hash if ($isPasswordCorrect) $hashToStoreInDb = password_hash($password, PASSWORD_DEFAULT); } else { $isPasswordCorrect = password_verify($password, $existingHashFromDb) }
Stored hashes can be recognized by the leading value of $ or a separate db field, for example, BCrypt hashing always starts with the $ character, the MD5 hash does not.
The salt should not be delayed from other parameters and should be unique for each password. The password_hash () function will take care of this. Because a rainbow must be built for each salt, an attacker will have to create a rainbow for each password. For more information, you can see my tutorial on secure password storage .