3

I just downloaded my best friends script he used many years ago and all the password is using MD5 hash which I'd like to change. Now I'm not so good in coding so I ask you guys where to start. Do I convert this in the database or in the script? I want from now on everyone who signup have another hash, I'm thinking of SHA-2.

So I guess there should be something that's saying md5 in the code which I'm going to change to SHA-2? Am I right? If anyone here could tell me what the MD5 hash looks like in the code I can just search for that in every file until I find it. Also please tell me how to replace it to SHA-2.

Thank you.

Elliot S
  • 41
  • 7
  • 2
    You can't "convert" one hash value to another; and if you believe that SHA2 is more secure than MD5, think again, replacing one leaky bucket with another leaky bucket doesn't make much difference .... use a proper approach to passwords, such as PHP's built-in [password_hash()](http://www.php.net/manual/en/function.password-hash.php)/[password_verify()](http://www.php.net/manual/en/function.password-verify.php) – Mark Baker May 19 '16 at 12:52
  • In general - you cannot. http://md5db.net/api - here you can try to "descrypt" and then to convert original values into another hash. Will not work on 100%. – mitkosoft May 19 '16 at 12:56
  • 1
    As others have said, you can't migrate the existing hashes, but there's nothing stopping you using a more secure method for any future users. There's a decent guide about migrating here: https://paragonie.com/blog/2016/02/how-safely-store-password-in-2016 – iainn May 19 '16 at 13:00
  • Write a code, which will calculate and save hash on each user login(where you have access to plain pass). Over month or two - analyze how much `sha-2` you have. If this number is enough(ex: 90%) - you can to migrate your login system to new hash with announcment to inactive part of users. You can to leave fallback to md5 method for several time also. – vp_arth May 19 '16 at 13:15

3 Answers3

6

Because you cannot decrypt MD5 you can handle the migration like this and make it invisible to the user:

Once you have decided the hashing method to be employed (I recommend using PHP's built-in functions to handle password security.):

  1. Add a new column to the user database for the new hash, based on the hashing method to be employed.
  2. On user login check the table to see if they have an MD5 password (and it matches) and see if the new hash has been entered.

    a. If they do not have a new hash, create one in the new column based on the password they logged in with.

    b. If they do have a new hash verify their password against the new hash.

At a certain point every user, by virtue of logging in, will update their own hashes.


In addition make sure that you don't escape passwords or use any other cleansing mechanism on them before hashing. Doing so changes the password and causes unnecessary additional coding.

Community
  • 1
  • 1
Jay Blanchard
  • 32,731
  • 15
  • 70
  • 112
1

I like Jay's answer but there's an even better way to do this.

You'll need another field in your DB. We'll call it legacy for this example. It's a simple Yes/No and defaults to No. What you do is take all the MD5 hashes you have and you push them into password_hash. Then you write the new hash over the MD5 and set legacy to Yes

When you go to login, you pull their record (based on the username) and you get the password and legacy fields. Then you do something like this

$password = $_POST['password'];
// $result is the result from your query. I like arrays so we'll use that
if($result['legacy'] == 'Yes') $password = md5($password);
if(password_verify($password, $result['password']))

This does two things

  1. You don't need to know what's in the MD5 hashes first
  2. You're still using password_hash regardless

Just remember that if they update their password, set the legacy field to No

Machavity
  • 28,730
  • 25
  • 78
  • 91
0

The md5 hashing function is called md5("my password") or hash("md5", "my password"). You could replace either with hash("sha256", "my password") or hash("sha512", "my password").

But...

These are not very safe either. Use the suggested password_hash() function. This function is just as simple: password_hash(string $password , integer $algorithm), where the algorithm is one of the PASSWORD_* constants.

Read the notes on the function page, and read these notes for the algorithm.

Community
  • 1
  • 1
Byson
  • 530
  • 4
  • 18