Key points
- password_needs_rehash() tells you when a hash no longer matches the algorithm or cost you want.
- You can only compute a new hash at login, the one moment you have the plain-text password.
- Old MD5 hashes can be protected right away by wrapping them in bcrypt, without waiting for anyone to log in.
While auditing a PHP application that was around ten years old, we found three generations of passwords in one table: unsalted MD5, bcrypt at cost 10, and a few recent accounts done properly. Forcing everyone to reset would have buried the support team. PHP already gives you everything you need to migrate gradually, as long as you understand when you’re able to act.
Upgrade the hash when the user logs in
You can’t convert a hash; you need the plain-text password to compute a new one. The only time your application has it is right after a successful login, and that’s where password_needs_rehash() comes in:
const PASSWORD_OPTIONS = ['cost' => 12];
// Any bcrypt hash, generated once with password_hash() and pasted here
const DUMMY_HASH = '$2y$12$...';
function attemptLogin(PDO $db, string $email, string $password): ?int
{
$stmt = $db->prepare('SELECT id, password_hash FROM users WHERE email = ?');
$stmt->execute([$email]);
$user = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$user) {
// Same work as a real check, so response time doesn't reveal unknown emails
password_verify($password, DUMMY_HASH);
return null;
}
if (!password_verify($password, $user['password_hash'])) {
return null;
}
// The password is known to be correct here: upgrade the hash if needed
if (password_needs_rehash($user['password_hash'], PASSWORD_DEFAULT, PASSWORD_OPTIONS)) {
$db->prepare('UPDATE users SET password_hash = ? WHERE id = ?')
->execute([password_hash($password, PASSWORD_DEFAULT, PASSWORD_OPTIONS), $user['id']]);
}
return (int) $user['id'];
}
Setting the cost explicitly keeps the rule visible and stable across PHP versions; PHP 8.4 raised bcrypt’s default cost from 10 to 12, for instance. DUMMY_HASH is any hash generated once. Without it, an unknown email gets a noticeably faster response than a real one, which lets an attacker work out which accounts exist.
Protect old MD5 hashes immediately
Waiting for every user to log in again leaves dormant accounts on MD5 for years. Our approach is to wrap every MD5 in bcrypt straight away, then replace the wrapper with a normal hash at the next login.
// One-off migration: wrap every old MD5 hash in bcrypt
foreach ($db->query("SELECT id, password_hash FROM users WHERE hash_scheme = 'md5'") as $row) {
$db->prepare("UPDATE users SET password_hash = ?, hash_scheme = 'bcrypt-md5' WHERE id = ?")
->execute([password_hash($row['password_hash'], PASSWORD_DEFAULT, PASSWORD_OPTIONS), $row['id']]);
}
// At login, for users still on the wrapped scheme
if ($user['hash_scheme'] === 'bcrypt-md5' && password_verify(md5($password), $user['password_hash'])) {
// correct password: store a normal hash and drop the wrapper
$db->prepare("UPDATE users SET password_hash = ?, hash_scheme = 'bcrypt' WHERE id = ?")
->execute([password_hash($password, PASSWORD_DEFAULT, PASSWORD_OPTIONS), $user['id']]);
}
A hash_scheme column makes each account’s state explicit and lets you track progress. Once every MD5 is wrapped, a leaked database no longer hands out hashes that crack in minutes.
Details that derail a migration
- A column that’s too short. The PHP manual recommends 255 characters for
password_hash()output. VARCHAR(60) works for bcrypt but will block a later move to Argon2. - bcrypt’s 72-byte limit. Anything beyond is ignored. That’s rarely an issue in practice, but don’t pre-hash to raw binary to get around it: a null byte would truncate the string.
- A cost set too high. Each step doubles the work. Measure on your production server; around a tenth of a second per check is a sensible target.
- Several PHP servers on different versions. If one doesn’t support the algorithm you picked, logins routed there will fail. Align versions before switching algorithms.
What about WordPress?
WordPress takes care of this itself: since version 6.8 it hashes passwords with bcrypt through password_hash() and upgrades old hashes at login. If a plugin or some legacy custom code keeps its own passwords in a separate table, that’s what you need to audit.
FAQ
Should I use Argon2id instead of bcrypt?
Argon2id is an excellent choice if your PHP build supports it, which not every host does. bcrypt with a suitable cost is still safe. What matters most is using password_hash() rather than anything home-made.
Can I force the migration without waiting for logins?
Not for a full replacement. The bcrypt wrapper protects old hashes immediately, though, and after a few months you can ask only the accounts still on the wrapper to reset their password.
Is your PHP application still storing passwords with an outdated algorithm? We audit and secure existing PHP and WordPress applications.



