Home » Php » php – Upgrading user passwords from salted SHA1 to bcrypt

php – Upgrading user passwords from salted SHA1 to bcrypt

Posted by: admin July 12, 2020 Leave a comment


I’ve been hired to rebuild a actively used application that was built on CodeIgniter 1.7.3 (on a PHP 4.2 server) using Laravel 4 on a new PHP 5.3 server.

The system has about ~500 users whose passwords are encrypted with a salted SHA-1 hash. I’d like to use bcrypt to increase the application’s security as well as to integrate with Laravel 4’s authentication system.

How would you suggest migrating these users passwords?

How to&Answers:

The whole point of a hash is that you can’t recover the original password.

You have three options:

  • Store bcrypt hashes of the SHA1 hashes, then SHA1 hash each password before bcrypting it on every login.
    This may not be a good idea.

  • Upgrade each hash next time that user logs in. (so that you have the plain text to hash)
    This is the best option, but you need to keep your SHA1 hashes and transition code until every single user logs in

  • Reset every user to a random bcrypted password and force them all to use Forgot Password to change it back.
    You probably don’t want to do this


  • Add a column to your database that tells the system which hashing algorithm has been used
  • On login, check the credentials as normal
  • If they’re using the old one and login is successful – bcrypt the password they entered and update their password and algorithm in the database.


You could create a random password for each user and send out a notification email to everyone with their new password. But this will result in confusion if a user doesn’t see the email.

I recommend that you add another db field for the bcrypt value and then create an entry when a user logs in for the first time after the change. You can use either a separate field or delete the old hash to keep track.

When your active users have migrated, feel free to use the random password approach for the rest of your userbase to finish the migration.