PHP: how should I hash passwords?
posée 3 months ago607 vuesen
An old project stores passwords with md5($password). What should I use instead, and how do I migrate existing users?
Diagnostic IA
Généré par IA. Ce n'est pas une réponse — la communauté ci-dessous le confirme ou le corrige. Vérifiez toujours avant de vous y fier.
1 réponse
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.
Connectez-vous pour dire si ça a marché.
Sign in and verify your email to post an answer.