6

I have a PHP application that has a somewhat decent userbase. Now unfortunately, it has been using sha1($password . $salt) all these years and I really want to ditch that in favor of bcrypt. I have found some nice ways of getting a Blowfish hash, but I am still unsure about the conversion approach that I should use. Here are my options:

Option 1

Every time a user logs in, I check if the hash starts with $2. If not, I assume it is sha1, take the password entered by the user, get the bcrypt hash for it and replace the old hash in the database.

Option 2

I replace my auth class to do this:

$hash = password_hash("rasmuslerdorf", sha1($password . $salt));

That way, conversion is quicker.

But honestly, I don't really like either of the options. Both suggest that I still keep a legacy check in the codebase which I want to get rid of.

Any suggestions which of the above two are better from a coding standards point of view? Or does someone have a better solution?

5
  • 1
    possible duplicate of Most efficient way to change the hash type of a password (md5 to sha1)
    – Baba
    Jul 10, 2013 at 20:38
  • you'd have to keep SOME legacy code around until all your users are converted over, otherwise the legacy users would get locked out because they can't pass the hash validation.
    – Marc B
    Jul 10, 2013 at 20:41
  • @MarcB Well, that's the problem, I know a list of 20 sites that use my application. I don't know who maintains them though so I don't have a way to check if they migrated over completely. Jul 10, 2013 at 20:45
  • 2
    ah. so it's not one site with many users, it's multiple sites/servers with multiple copies of your app. I'd suggest you do like PHP it self - roll out the new hash as a feature in a new release, deprecate the old version. then after a couple versions, you remove the legacy hash stuff and too bad for any users still on the old system. A bit of extra work for a few versions, but at least you give a warning to your users.
    – Marc B
    Jul 10, 2013 at 20:54
  • That is a great idea! How can i mark it as the answer? :) Jul 10, 2013 at 22:39

2 Answers 2

2

Every password-storing-system must have the option to switch to a better hash algorithm, your problem is not a one-time migration problem as you may think. Good password hash algorithms like BCrypt have a cost factor, from time to time you have to increase this cost factor (because of faster hardware), then you need the same procedure as you need for the migration.

Your Option1 is a convenient approach, as long as the hashes are not terrible unsafe (unsalted or very weak algorithm). In PHP's new password API, you will even have a function password_needs_rehash() to determine whether an update is necessary.

I would recommend to let the fallback stay in the code, you will spare your customers the hassle of confront their users with an invalid password. As a user i don't like emails that demand to click a link and reenter my password, users are taught to ignore such email because of phishing. As said before, such fallbacks in the code are not bad, it is a necessary step to get a safe password handling.

1
  • Thanks, didn't know about password_needs_rehash() Jul 11, 2013 at 19:04
0

Add a new DB col, let's say [passNeedSystemUpdate], your default value will be 1. 1 = true or yes and 0 = na.

Process the user login as normal until you get to the end then run a check to see if the passNeedsSystemUpdate = 1. If the password needs updating then we take the users input from the password field and update the new password along with passNeedSystemUpdate which will now be 0.

NOTE: If in the event that your DB is compromised or has been, then users will have no other option but to create a completely new password. The above is just a bit of logic to use if you're changing your current encryption from sha256 to bycrypt or something else.

1
  • 1
    Why the overhead of adding a new column (which is one-time use only and will eventually become obsolete), when the same can be accomplished by looking at the format of the hash (which will also stay relevant forever)?
    – deceze
    Dec 7, 2016 at 14:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.