Update password hash from md5 to bcrypt

It has been discussed here before, but there seems to be no conclusion.

Ideally, you do not want to maintain state (updated / not updated) in the database, etc., here is what I think:

bcrypt MD5'd password and use "username + something else" as the salt.

  • Does this circuit make sense?
  • Also, is it generally useful to use a username in salt? I read somewhere that adding different salt to each hash makes it safer. Is this correct (especially in the context of bcrypt)?
+6
source share
1 answer

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 .

+6
source

Source: https://habr.com/ru/post/976038/


All Articles