← Back to Blog

Password Hashing: Why Bcrypt Beats MD5 and SHA-256

Share:

Developers like fast code. In one place, though, fast is dangerous: storing passwords. This post explains why general hashes such as MD5 and SHA-256 are the wrong choice, and what to use instead.

Why fast hashes are a problem

MD5, SHA-1 and SHA-256 were built to check data quickly, for example to verify a file. They are very fast on purpose.

That speed helps attackers. Suppose someone steals your database of password hashes. They can then guess passwords offline, with no login limits and no network delay. Modern graphics cards can test billions of fast hashes every second. If passwords are short or common, many can be cracked in hours or less.

Two ideas that fix it

  • Salt. A salt is random text added to each password before hashing. Two users with the same password then get different hashes, and pre-built lookup tables (rainbow tables) become useless.
  • A work factor. Password algorithms such as bcrypt and Argon2 are deliberately slow, and you can make them slower as computers improve. The cost setting controls how much work each hash takes.

How bcrypt works

bcrypt was published in 1999 by Niels Provos and David Mazieres. It is based on the Blowfish cipher. Each hash includes a random salt and a cost setting.

A bcrypt hash is a single string. It starts with a marker such as $2y$, then a two-digit cost, then the salt and the hash itself. Because everything is in one string, you do not need extra columns for the salt or settings.

Hashing passwords in PHP

PHP has built-in functions, so you should not write your own scheme.

  1. To store a password: use password_hash($password, PASSWORD_DEFAULT) or PASSWORD_BCRYPT. PHP creates the salt for you.
  2. To check a login: use password_verify($password, $storedHash).
  3. To upgrade old hashes: use password_needs_rehash(). If it says yes, hash the password again at the next successful login and save the new hash.

A limit to know: 72 bytes

bcrypt only uses the first 72 bytes of a password. Anything after that is ignored. Long passphrases, or text with many multi-byte characters, can hit this limit. Some developers handle it by pre-hashing, and others choose Argon2id. If you are starting a new project, check what your language and framework recommend.

Mistakes to avoid

  • Inventing your own scheme. Hashing MD5 twice does not make it safe, and home-made schemes often have flaws.
  • A cost that is too high. Hashing should be slow, but not so slow that a busy login page overloads your server. A common target is roughly 100 to 250 milliseconds per hash on your server.
  • Keeping passwords in plain text. Never store them, write them to logs, or send them over unencrypted connections.

A note on online hashing tools

Our Bcrypt Generator is useful for seeing what a bcrypt hash looks like, or for testing. It sends what you type to our server over HTTPS to do the hashing. So never enter a real password from a live account. Use test values only.

A short checklist for your login system

Hashing is only one part of keeping passwords safe. When you build or review a login system, check these points as well.

  • Hash on the server. Never trust a browser to do the hashing for you.
  • Compare with the right function. Use password_verify and not a simple string comparison.
  • Allow long passphrases. Do not cut passwords to a short length. Remember the 72 byte limit of bcrypt and decide how you will handle it.
  • Use HTTPS everywhere. A strong hash does not help if the password travels over an open connection.
  • Limit login attempts. Slow down or block repeated failed tries from the same address or account.
  • Make resets safe. You cannot read a password back from a hash, so send a reset link that works for a short time and only once.

Argon2id as an option

Argon2id is another password algorithm and it is recommended by many security guides. PHP can use it through PASSWORD_ARGON2ID when your PHP build supports it. Check your server before you rely on it, and keep using password_needs_rehash() so that you can move users to a stronger setting over time.

The goal is to make stolen hashes too expensive to crack. Use bcrypt or Argon2, let your language handle the salt, and never use a fast hash for passwords.

M
Written by Muhammad Faraz

Co-founder and developer of AioGalaxy. Builds and maintains the Eaglefly, AIVoice and free tools platforms, and writes the blog.

Enjoyed this? Explore the AioGalaxy toolset

Everything mentioned here — and a lot more — is free to try right now, no sign-up walls.

Explore AioGalaxy →

Comments

No comments yet — be the first to share your thoughts.