What is the current best practice for hashing passwords in PHP, and why are md5() or sha1() no longer acceptable?

Last updated 15 June 2026

The current best practice is to use PHP's built-in password_hash() with PASSWORD_DEFAULT (currently bcrypt, with Argon2 available) and verify with password_verify(). These use slow, salted, adaptive algorithms designed specifically for passwords, unlike fast general-purpose hashes.

  • Why password_hash() is right:

    • Generates a unique random salt automatically and embeds it in the output (no separate salt column needed).

    • Adaptive cost: you can raise the work factor as hardware improves.

    • password_needs_rehash() lets you upgrade old hashes transparently on login.

  • Why md5() and sha1() are unacceptable:

    • They are extremely fast, so attackers can compute billions of guesses per second (brute force / GPU cracking).

    • No built-in salting, making rainbow-table attacks trivial.

    • Both have known collision weaknesses and were never designed for password storage.

[lang:php] $hash = password_hash($password, PASSWORD_DEFAULT); if (password_verify($input, $hash)) { if (password_needs_rehash($hash, PASSWORD_DEFAULT)) { $hash = password_hash($input, PASSWORD_DEFAULT); // upgrade } }

  • https://www.interviewbit.com/php-interview-questions/
  • https://www.geeksforgeeks.org/wordpress/what-type-of-hashing-does-wordpress-use/
All PHP interview questions