Why should you never use md5() or sha1() for password hashing, and what is the recommended alternative in modern PHP?

Last updated 15 June 2026

You should never use md5() or sha1() for passwords because they are extremely fast and were built for integrity, not secrecy; the modern alternative is PHP's password hashing API (password_hash() / password_verify()).

  • The problem with fast hashes:

    • Speed is a liability for passwords: attackers can brute-force or use precomputed rainbow tables against billions of candidates per second.

    • They have no built-in salt and known collisions, making them unfit even with manual salting.

  • The recommended approach:

    • password_hash() defaults to bcrypt (with PASSWORD_ARGON2ID available), automatically salting and applying a configurable work factor.

    • Verify with password_verify(), which reads the salt and cost from the stored hash itself.

  • Future-proofing: Use password_needs_rehash() on login to transparently upgrade hashes when you raise the cost or change the algorithm.

  • https://en.wikipedia.org/wiki/PHP
  • https://lemon.io/for-developers/php-developer-jobs/
  • https://lan-portal.uob.edu.ly/visit/BOOK/529X432U02/modern-php_new__features_and_good-practices.pdf
  • https://lan-portal.uob.edu.ly/upload/MD/D57953S372/modern-php__new_features_and__good__practices.pdf
  • https://news.ycombinator.com/item?id=30786927
  • https://kvnc-inc.medium.com/solid-principles-in-php-1c41069e9af6
  • https://www.shkodenko.com/
  • https://confoo.ca/en/2024/sessions
All PHP interview questions