0

I would like if it's possible to change the hashing method for an already hashed password. For example:

$password_input = '123456789';
$hashed_password = md5($password_input);
// The output would be 25f9e794323b453885f5181f1b624d0b

The result was made with the following online tool: https://helloacm.com/md5/

The next step would be insert the hashed password into the database. When I do this the given hashed password will be in the users table. If I select that password, can I change the md5 hash by a sha-256? For example:

$md5_password = '25f9e794323b453885f5181f1b624d0b';
$sha256_password = hash('sha256', $md5_password);

If this would be possible, would it break the login function? I mean if I use password_verify method, will it return true?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
Álvaro
  • 385
  • 5
  • 15
  • 2
    You can't unhash a password, that's the whole point, and hashing the hash will give a result that won't match the password. Typically what you do when you change the hashing method is re-hash as part of login flow; once you've validated the user's input against the old hash you know that's their password in plain text, so you can hash it with the new settings. – jonrsharpe Feb 09 '23 at 09:33
  • Yeah... I supposed that. I wanted to know if a 'rehash' wouldn't break the login flow. Because it has to be done by a PHP script, to improve security in an older project – Álvaro Feb 09 '23 at 09:35
  • 1
    You can't do it as a migration, for the same reason. You either rehash as users log in, or (assuming you have a way to do so) delete/expire all the passwords and force the users to reset them when they want to log in. – jonrsharpe Feb 09 '23 at 09:37
  • 1
    Or this? https://stackoverflow.com/questions/16863775/most-efficient-way-to-change-the-hash-type-of-a-password-md5-to-sha1 – Nico Haase Feb 09 '23 at 09:39
  • More importantly, your method does not improve security. All the weaknesses of MD5 remain. For example once an attacker finds a collision on MD5, the outputs from MD5 are identical, so hashing those outputs with better algorithms still maintains the collision. The same holds true for entropy considerations. – President James K. Polk Feb 09 '23 at 13:59

1 Answers1

2

You will not get password back from md5, you can't unhash one way hash algorithms.

What we do - incorporate re-hashing in login flow.

  1. User logins to your system with old hash password
  2. You detect, that this user needs re-hash
  3. While still having sent plain text password you hash it with new algorithm and save to database
  4. Next time user logins with newly hashed password without problems
Justinas
  • 41,402
  • 5
  • 66
  • 96
  • What if the user never logins again? The user uses the account once and never again. But I want to 'improve' the security changing the password. It's a old project, where the developers used `MD5` – Álvaro Feb 09 '23 at 09:37
  • 1
    @ÁlvaroPérezDíaz If user will never login again, why you worry about him? There is no way you can get back what was hashed with `MD5` or any other one way algo. – Justinas Feb 09 '23 at 09:39
  • You're right, I guess. It won't be a potential danger even if the user doesn't login. I mean, it's a password, and if this password is used by the user in every place he / she register... I don't know if I explain myself – Álvaro Feb 09 '23 at 09:46
  • @ÁlvaroPérezDíaz the risk there is that if your DB is compromised, having an insecure hash means it's easier for the attacker to get the plaintext passwords. The only way to avoid that from your end is get rid of hashes with the old algorithm and force a reset. – jonrsharpe Feb 09 '23 at 10:51
  • @Álvaro - In the case of an unsalted MD5 you should indeed protect the user passwords as soon as possible, without waiting for a next login, which maybe never happen. This can be done with double hashing as described in this [answer](https://stackoverflow.com/a/56626811/575765). – martinstoeckli Feb 26 '23 at 13:30
  • @martinstoeckli Fortunately, there isn't any unsalted password, but I'll check it. Thanks – Álvaro Feb 27 '23 at 07:51