Password hashing
A stored password is a one-way hash, never the text the user typed, and PHP has one pair of calls for the whole job. Every example below hashes a password and checks it, so the numbers and booleans on the page are what the run printed.
password_hash
Function password_hash turns a plain password into a salted one way hash. Store only the hash in the database, never the original text.
Use the default algorithm unless you have a clear reason to pick another. The function generates a fresh random salt on every call.
<?php $hash = password_hash("secret123", PASSWORD_DEFAULT); echo password_get_info($hash)["algoName"], PHP_EOL; echo strlen($hash), PHP_EOL;
php hash.php
bcrypt
60
The hash names its own algorithm, and the stored string is 60 characters long, salt included. A second run of the same program gives a different 60 character string, because the salt is new every time.
password_verify
Function password_verify compares a login attempt against the stored hash. It handles salts and algorithm details for you.
It returns a plain boolean, so branch on true or false. Keep error messages generic to avoid leaking which accounts exist.
<?php $hash = password_hash("secret123", PASSWORD_DEFAULT); var_dump(password_verify("secret123", $hash)); var_dump(password_verify("wrong", $hash));
php verify.php
bool(true)
bool(false)
The same stored hash answers both attempts, and only the right password passes. Nothing in the comparison needs the salt, because the hash carries it.
needs_rehash
Algorithms and cost settings change over time. Function password_needs_rehash tells you when a stored hash no longer matches current policy.
Check right after a successful login, when the plain password is still available. Rehash and save the new value at once.
<?php $cheap = password_hash("secret123", PASSWORD_BCRYPT, ["cost" => 4]); var_dump(password_needs_rehash($cheap, PASSWORD_DEFAULT)); $current = password_hash("secret123", PASSWORD_DEFAULT); var_dump(password_needs_rehash($current, PASSWORD_DEFAULT));
php rehash.php
bool(true)
bool(false)
The cheap hash was made with a cost far below the default, so the first check asks for a new one. The second hash already matches the policy and needs nothing.
Algo upgrades
PHP ships several algorithms behind one stable API. The default has changed across versions while the function names stayed the same.
Probe for modern options with defined before using them. Fall back to the default when a constant is missing.
<?php echo PASSWORD_DEFAULT, PHP_EOL; echo PASSWORD_BCRYPT, PHP_EOL; if (defined("PASSWORD_ARGON2ID")) { echo "argon2 ready", PHP_EOL; } else { echo "no argon2", PHP_EOL; }
php algo.php
2y
2y
argon2 ready
The identifier 2y is the bcrypt marker, and this build carries the argon2id constant as well, so the probe takes the first branch.
Exercises
- Hash the same password twice and compare the two hashes with ===.
- Verify the second hash with the right password and explain why it still passes.
- Raise the cost of the cheap hash to the default and run the rehash check again.
- Print password_get_info($hash)["options"]["cost"] for a hash of your own.
Questions
A new set of questions in a new order every time. Choose one answer for each, then check.
-
What is stored for a password in the database?
Answer
A one-way hash with its salt
The hash cannot be turned back into the password, and the salt inside it makes equal passwords hash differently.
-
Why are two hashes of the same password different?
Answer
Each call draws a new random salt
The salt is generated per call and stored inside the hash, which is why the text differs while both still verify.
-
What does
password_verify()need to check a login attempt?Answer
The password and the stored hash
The stored hash already carries the algorithm, the cost and the salt, so the plain password is the only other input.
-
When does
password_needs_rehash()return true?Answer
When the stored hash was made with another algorithm or cost than the policy asks
The function compares the stored parameters with the ones you pass, and a difference means the hash should be replaced.
-
What does this program print?
<?php $hash = password_hash("secret123", PASSWORD_DEFAULT); var_dump(password_verify("secret123", $hash));Answer
bool(true)
The hash was made from that password, so the check passes and returns true.
-
What does this program print?
<?php $hash = password_hash("secret123", PASSWORD_DEFAULT); var_dump(password_verify("Secret123", $hash));Answer
bool(false)
Password comparison is case sensitive, so the capital S fails the check.
-
What does this program print?
<?php $hash = password_hash("secret123", PASSWORD_BCRYPT, ["cost" => 4]); var_dump(password_needs_rehash($hash, PASSWORD_DEFAULT));Answer
bool(true)
Cost 4 is below the default cost, so the function asks for a new hash.
-
What does this program print?
<?php $hash = password_hash("secret123", PASSWORD_DEFAULT); echo password_get_info($hash)["algoName"];Answer
bcryptThe info array names the algorithm in words, while
2yis the identifier written into the hash string. -
Why is
md5($password)the wrong tool for a password column?Answer
It is fast to compute and has no salt, so a stolen table falls to a lookup table
A fast hash without a salt can be reversed from precomputed tables, which is exactly what
password_hash()prevents. -
A stored hash needs a rehash, but the code has only the hash. What can it do?
Answer
Nothing until the user logs in again with the plain password
The new hash needs the plain password, so the moment right after a successful login is the only safe time.
The examples call password_hash(), password_verify(), password_needs_rehash() and password_get_info(). The programs on this page were run with PHP 8.5.13 (php -v) with the commands shown above.
Article author: Arthur Isaev