PHP: how should I hash passwords?
asked 3 months ago604 viewsen
An old project stores passwords with md5($password). What should I use instead, and how do I migrate existing users?
AI diagnosis
AI-generated. Not an answer — the community below confirms or corrects it. Always verify before relying on it.
1 Answer
Use PHP's built-in password API. It picks a strong algorithm (bcrypt by default, Argon2id available) and handles salts for you:
$hash = password_hash($password, PASSWORD_DEFAULT);
// or: password_hash($password, PASSWORD_ARGON2ID);
if (password_verify($input, $hash)) { /* ok */ }
Migrating from MD5 without forcing resets: 1. Immediately wrap existing hashes: store password_hash(md5_hash, ...) and mark those rows as legacy. 2. At login, for legacy rows verify password_verify(md5($input), $stored). On success, re-hash with password_hash($input, ...) and clear the flag. 3. Also use password_needs_rehash() on every login so hashes upgrade automatically when you raise the cost.
MD5 hashes can be cracked in seconds, so do step 1 today.
Sign in to tell others whether this worked.
Sign in and verify your email to post an answer.