Blog

Moving password hashes off MD5 without forcing a reset

If your application stores passwords as MD5 or SHA-1, the usual advice is to rehash each one the next time its owner logs in. That is half a fix. It protects the users who come back, and leaves everyone else's password one fast hash away from a cracking rig for as long as the database and its backups exist.

The other half is to upgrade the hashes you already have, today, without knowing anyone's password. Symfony can do both. The second has a trap in it that locks out every user it touched, and the failure looks exactly like a wrong password.

Why MD5 is the problem

MD5 and SHA-1 were designed to be fast. That is right for checksums and wrong for passwords: OWASP's password storage guidance rules out fast hashes because they let an attacker make enormous numbers of guesses quickly. A password hash should be slow and memory-hungry on purpose. OWASP's first choice is Argon2id, with at least 19 MiB of memory, 2 iterations and 1 degree of parallelism.

A salt does not rescue a fast hash. It stops one precomputed table from cracking every account at once. It does nothing to slow down guessing a single one.

Step one: rehash at login

The plain-text password exists in your application at exactly one moment: when someone types it. So that is when you can hash it properly. Symfony builds this in. Name the new algorithm, list the old one under migrate_from, and have your user provider implement PasswordUpgraderInterface. For Doctrine users, that is an upgradePassword() method on the UserRepository that saves the new hash.

# config/packages/security.yaml
security:
    password_hashers:
        App\Entity\User:
            algorithm: argon2id
            migrate_from:
                - legacy_md5
        legacy_md5:
            algorithm: md5
            encode_as_base64: false
            iterations: 1

With one iteration and no base64, the legacy_md5 hasher computes the plain hexadecimal md5() most old PHP applications stored. If yours salted or iterated, match what it did. Name argon2id explicitly: the auto setting picks the best hasher available, which Symfony's documentation currently gives as bcrypt.

On a successful login, Symfony asks whether the stored hash needs rehashing. If it does, Symfony hashes the password it has just checked with the new algorithm and hands the result to upgradePassword() to save. The built-in form login does this for you. A custom authenticator has to carry a PasswordUpgradeBadge in its passport.

Outside Symfony the pattern is the same: password_verify() against the new hash, fall back to the old check, and on success store password_hash() of what the user typed. PHP's password_needs_rehash() tells you when a stored hash is not in the algorithm and options you now want.

Step two: wrap every hash you already have

Rehashing at login has a long tail. The users who never come back are the ones whose MD5 hashes sit in your database and backups indefinitely. OWASP describes two ways out. One is to expire the hashes of inactive users and make them reset their passwords. The other needs nobody to do anything: use the existing hashes as the input to a stronger algorithm.

You do not know your users' passwords, but you do have md5(password) for every one of them. Hash that with Argon2id and store the result in place of the MD5. Nobody gets back to the MD5 without cracking Argon2id first. At login you check Argon2id against md5() of what the user typed.

The migration is a one-off command that runs in batches over every hash that is still 32 hexadecimal characters and replaces it:

$user->setPassword('md5:'.password_hash($user->getPassword(), \PASSWORD_ARGON2ID));

Symfony then needs a hasher that understands the wrapped form:

namespace App\Security;

use Symfony\Component\PasswordHasher\Exception\InvalidPasswordException;
use Symfony\Component\PasswordHasher\Hasher\CheckPasswordLengthTrait;
use Symfony\Component\PasswordHasher\PasswordHasherInterface;

final class WrappedMd5PasswordHasher implements PasswordHasherInterface
{
    use CheckPasswordLengthTrait;

    private const PREFIX = 'md5:';

    public function hash(#[\SensitiveParameter] string $plainPassword): string
    {
        if ($this->isPasswordTooLong($plainPassword)) {
            throw new InvalidPasswordException();
        }

        return self::PREFIX.password_hash(md5($plainPassword), \PASSWORD_ARGON2ID);
    }

    public function verify(string $hashedPassword, #[\SensitiveParameter] string $plainPassword): bool
    {
        if (!str_starts_with($hashedPassword, self::PREFIX) || $this->isPasswordTooLong($plainPassword)) {
            return false;
        }

        return password_verify(md5($plainPassword), substr($hashedPassword, \strlen(self::PREFIX)));
    }

    public function needsRehash(string $hashedPassword): bool
    {
        // A wrapped hash is a bridge, never a destination.
        return true;
    }
}

Register it by service id, ahead of the plain MD5 hasher:

        App\Entity\User:
            algorithm: argon2id
            migrate_from:
                - wrapped_md5
                - legacy_md5
        wrapped_md5:
            id: App\Security\WrappedMd5PasswordHasher

Every user is now behind Argon2id from the day this ships, and each one moves to a direct Argon2id hash of their real password the next time they log in.

It does nothing for backups taken before the migration ran. Those still hold the plain MD5 hashes, so treat them as the exposure they are: know where they are, and let them expire on schedule rather than keeping them forever.

The trap: the prefix is not decoration

Leave out the md5: prefix and the wrapped hash is an ordinary $argon2id$ string with current parameters. That breaks two things.

Symfony's MigratingPasswordHasher tries the new hasher first. That check fails, because the stored hash is Argon2id of the MD5, not of the password. Before it falls back to the hashers under migrate_from, it asks the new hasher whether the stored hash needs rehashing. A current Argon2id hash does not, so it never tries them and the login fails. Every user you wrapped is locked out, and from the outside it looks like they all forgot their passwords on the same day.

The upgrade at login asks the same question, so even a hash that did verify would never be replaced.

With the prefix, the Argon2id hasher does not recognise the format and says it needs rehashing. Both checks send the hash to the legacy hashers, the login works, and the hash is replaced. I checked all four cases against Symfony's hasher before writing this: plain MD5, a prefixed wrap, a bare wrap and a direct Argon2id hash. Only the bare wrap fails.

Write that as a test. Log in once as a user with each kind of stored hash, and assert that every login succeeds and leaves a hash starting $argon2id$. It is a short test, and it is the one that catches this.

The bridge has an end

OWASP warns that layered hashes like this can be easier to crack than a direct hash, and says to replace them at the user's next login, which is what needsRehash() returning true does. A wrapped hash is far better than plain MD5, but it is still the weaker of the two. Some accounts will stay wrapped for years, so after a while decide what to do about them. Expiring them, so those users must reset their password, is OWASP's other option.

Then add the second factor

A properly stored password still gets reused, phished and guessed. Once passwords are on Argon2id, a second factor is the next step. On an application that already has users, most of that work is enrolment, recovery and rollout, not the codes themselves.

Sources

  • Password Storage Cheat Sheet, OWASP: Argon2id and its minimum parameters, why fast hashes such as MD5 and SHA-1 are unsuitable, and the two ways to upgrade legacy hashes: expiring inactive accounts, or using the old hash as input and replacing it at the next login.
  • Password Hashing and Verification, Symfony: migrate_from, PasswordUpgraderInterface, the legacy hasher options, the auto algorithm, and custom hashers.
  • password_needs_rehash() and password_hash(), PHP manual: when a hash needs rehashing, and that PASSWORD_ARGON2ID exists only when PHP is built with Argon2 support.

Moving password storage to Argon2id and adding two-factor authentication to a live PHP application is a fixed-scope package I offer: password and 2FA upgrade.