Skip to content

Core\Password::needsRehash

Core\Password::needsRehash(string $hash): boolCore\Password::needsRehash(
    string $hash,
): bool

Checks whether a stored password hash is older or weaker than a hash made today.

It returns true when the hash uses another algorithm, or less memory or fewer passes than Core\Password::hash uses now. Every bcrypt hash returns true. It returns false for a hash that is as strong as a new one, or stronger.

You cannot turn an old hash into a new one without the password. So the moment to update it is at login: verify has just returned true, and you have the password. If needsRehash returns true, you call hash and store the new string. Over time, every active user's hash is upgraded.

When the value is not a password hash at all, needsRehash throws a LogicError.

The examples below check a new hash and a weaker one, check a bcrypt hash, and upgrade a stored hash when a user logs in.

Parameters

  • $hashstringThe stored hash to check.

Return value

Returns bool. true when $hash uses another algorithm or version, or less memory or fewer passes than hash uses now. Every bcrypt hash returns true. false when $hash is as strong as a new hash, or stronger.

Errors

  • LogicError$hash is not a password hash at all. A hash that uses another algorithm does not throw: it returns true.

Examples

A new hash and an old one

<?nvs
// Shows which hashes need to be made again. A new hash does not. A hash made
// with less memory does.
// This prints `false`: a hash made now uses the current settings.
string $fresh = Core\Password::hash('correct horse battery staple');
echo Core\Password::needsRehash($fresh) ? 'true' : 'false', "\n";
// This hash was made with 4 MiB of memory (`m=4096`). A new hash uses 19 MiB,
// so this one is weaker. This prints `true`.
string $old = '$argon2id$v=19$m=4096,t=2,p=1$jJ+hokoSJRsAzYsgfwhV6g$BGyWXoH11l0/GF4ezLqvtQgPY0T/4fv4DukErq9R0cI';
echo Core\Password::needsRehash($old) ? 'true' : 'false', "\n";
// This hash uses more memory than a new one. Hashing it again would make it
// weaker, so this prints `false`.
string $stronger = '$argon2id$v=19$m=65536,t=3,p=1$jJ+hokoSJRsAzYsgfwhV6g$BGyWXoH11l0/GF4ezLqvtQgPY0T/4fv4DukErq9R0cI';
echo Core\Password::needsRehash($stronger) ? 'true' : 'false', "\n";
Output
false
true
false

A hash that php made

<?nvs
// Shows that every bcrypt hash from PHP needs to be made again. It still
// works with `verify`, but it uses another algorithm.
string $fromPhp = '$2y$10$PE6UB/yJ1bk1dIwtgHee0es/SxguHDHJgvcdKauDX66xu84voItOi';
// This prints `true`.
echo Core\Password::needsRehash($fromPhp) ? 'true' : 'false', "\n";
// A hash made by `hash` today does not need it. This prints `false`.
string $fresh = Core\Password::hash('correct horse battery staple');
echo Core\Password::needsRehash($fresh) ? 'true' : 'false', "\n";
Output
true
false

Upgrade a hash at login

<?nvs
// Shows how a login upgrades an old stored hash. This is the only moment the
// program has the password, so it is the moment to hash it again.
class Account
{
public string $passwordHash = '';
}
class Login
{
public static function attempt(Account $account, secret string $typed): bool
{
if (!Core\Password::verify($typed, $account->passwordHash)) {
return false;
}
// The password is correct. If the stored hash is old, replace it.
if (Core\Password::needsRehash($account->passwordHash)) {
$account->passwordHash = Core\Password::hash($typed);
echo "the stored hash was upgraded\n";
}
return true;
}
}
// This account was moved from a PHP application, so its hash is bcrypt.
Account $account = new Account();
$account->passwordHash = '$2y$10$PE6UB/yJ1bk1dIwtgHee0es/SxguHDHJgvcdKauDX66xu84voItOi';
secret string $typed = 'correct horse battery staple';
// The first login upgrades the hash. The second login finds it current.
// This prints `the stored hash was upgraded`, then `logged in` twice.
echo Login::attempt($account, $typed) ? 'logged in' : 'wrong password', "\n";
echo Login::attempt($account, $typed) ? 'logged in' : 'wrong password', "\n";
// The account now has a new Argon2id hash. This prints `$argon2id$`.
echo Core\Str::slice($account->passwordHash, 0, 10), "\n";
Output
the stored hash was upgraded
logged in
logged in
$argon2id$