Core\Password::verify
Core\Password::verify(string $password, string $hash): boolCore\Password::verify( string $password, string $hash, ): bool
Checks whether a password matches a stored hash.
You give it the password somebody typed and the hash you stored. It returns true when they match and false when they do not. It uses the settings saved inside the hash, so a hash made with older settings still works.
It reads two kinds of hash: the $argon2id$ strings from Core\Password::hash, and bcrypt hashes, which start with $2y$, $2a$ or $2b$.
When the stored value is not a password hash at all, verify throws a LogicError. A wrong password returns false, but a broken database value is a bug that somebody should see.
Good to know: a stored hash that asks for a huge amount of memory or work throws a RuntimeError. It does not run.
The examples below check a login, read a bcrypt hash, and write a login function that handles a broken stored value.
Parameters
$passwordstringThe password somebody typed. It may be asecretstring.$hashstringThe stored hash: a string fromhash, or a bcrypt hash that PHP stored.
Return value
Returns bool. true when $password matches $hash, false when it does not.
Errors
LogicError$hashis not a password hash this class can read. It does not parse, it uses another algorithm, or it starts with$2x$. This usually means the stored value is broken, so it is an error and notfalse.RuntimeError$hashneeds too much work: more than 1 GiB of memory, too many passes over that memory, or a bcrypt cost above 17. It is also thrown when the request cannot use the memory the hash needs.
Examples
Check a login
<?nvs// Shows how to check a password somebody typed against the stored hash.
string $stored = Core\Password::hash('correct horse battery staple');
// The right password returns `true`. This prints `welcome`.secret string $typed = 'correct horse battery staple';echo Core\Password::verify($typed, $stored) ? 'welcome' : 'wrong password', "\n";
// A wrong password returns `false`. This prints `wrong password`.secret string $guess = 'Correct horse battery staple';echo Core\Password::verify($guess, $stored) ? 'welcome' : 'wrong password', "\n";welcomewrong passwordA hash that php made
<?nvs// Shows that `verify` reads a bcrypt hash that a PHP application stored with// `password_hash`. The user table does not need to change.
// PHP wrote this hash for the password "correct horse battery staple".string $fromPhp = '$2y$10$PE6UB/yJ1bk1dIwtgHee0es/SxguHDHJgvcdKauDX66xu84voItOi';
// This prints `true`, then `false`.secret string $right = 'correct horse battery staple';secret string $wrong = 'hunter2';echo Core\Password::verify($right, $fromPhp) ? 'true' : 'false', "\n";echo Core\Password::verify($wrong, $fromPhp) ? 'true' : 'false', "\n";truefalseA login function
<?nvs// Shows a login function as a web application writes it. A wrong password is// a normal result. A broken stored value is a bug, so it is logged.
class Login{ public static function check(secret string $typed, string $stored): string { try { if (Core\Password::verify($typed, $stored)) { return 'logged in'; } return 'wrong email or password'; } catch (LogicError $broken) { // The stored value is not a password hash. The user sees the same // message, but the error is written to the log for a developer. Core\Log::write(Core\Log::Level::Error, 'the stored password hash cannot be read'); return 'wrong email or password'; } }}
string $stored = Core\Password::hash('my first pet was a cat');secret string $right = 'my first pet was a cat';secret string $wrong = 'my first pet was a dog';
// This prints `logged in`, then `wrong email or password`.echo Login::check($right, $stored), "\n";echo Login::check($wrong, $stored), "\n";
// This stored value is damaged, so `verify` throws a `LogicError`. The function// returns `wrong email or password` and writes a log record to standard error.echo Login::check($right, 'not-a-hash'), "\n";// proof: with-stderrlogged inwrong email or passwordwrong email or password{"level":"error","msg":"the stored password hash cannot be read","source":{"file":"docs/examples/core/Password/verify/03-a-login-function.nvs","line":17,"member":"Login::check"}}