MD5, SHA-256 and bcrypt: what hashing is and when to use each

Hashes check files and protect stored passwords, but not with the same algorithm. Which one fits which job, and why MD5 is retired.

Cover image for MD5, SHA-256 and bcrypt: what hashing is and when to use each

A hash function turns any input, from a single word to a whole file, into a fixed-length string of characters. The same input always gives the same hash, a tiny change gives a completely different one, and there's no way to work back from the hash to the input:

Hello  ->  185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969
hello  ->  2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

(Both are SHA-256.) Those properties make hashes useful for two quite different jobs, and the two need different algorithms.

Job one: checking that data hasn't changed

When you download software, the site often publishes a checksum: the file's hash. Hash your download and compare. If they match, the file is exactly what was published, neither corrupted nor tampered with. The same idea is used to find duplicate files, to cache content, and inside Git and other version control systems.

For this job you want a fast hash that resists collisions, meaning nobody can create two different inputs with the same hash.

  • SHA-256, part of the SHA-2 family, is the standard choice. SHA-384 and SHA-512 are longer versions.
  • SHA-3 is a newer family with a different design, a good alternative when you need one.
  • MD5 and SHA-1 are broken: researchers can create collisions with ordinary computers. They still catch accidental corruption, but shouldn't be trusted where someone might tamper with the data on purpose.

Job two: storing passwords

A website should never store your actual password. It stores a hash, and when you sign in, it hashes what you typed and compares the two. If the database is stolen, the attacker gets hashes, not passwords.

But a fast hash is exactly wrong here. Attackers guess passwords, hash each guess and look for a match. A fast hash such as SHA-256 lets them try billions of guesses a second. Password hashing has to be deliberately slow, and salted.

  • A salt is a random value added to each password before hashing, and stored with the hash. Two people with the same password get different hashes, so attackers can't use precomputed tables or crack everyone at once.
  • Slow means the algorithm can be tuned to take a noticeable fraction of a second. That's nothing when you sign in, and crippling for someone making billions of guesses.

The algorithms built for this are Argon2id, bcrypt and scrypt. Argon2id is the current recommendation for new systems; bcrypt is older, supported everywhere and still a sound choice. Laravel, for example, hashes passwords with bcrypt by default.

A bcrypt hash looks like this:

$2y$12$m/FMWEHO1gq.jRZ8MOKMv.tZLKgzPXzhmyJOCskjTYQni8kWBZPNG

2y is the version and 12 is the cost: each step up doubles the work. The rest holds the salt followed by the hash. One limit to know: bcrypt only uses the first 72 bytes of a password.

Which to use

Job Use Avoid
File checksums and integrity SHA-256, SHA-3 MD5 and SHA-1, wherever tampering matters
Storing passwords Argon2id, bcrypt, scrypt MD5, SHA-1, SHA-256, anything unsalted
Signing messages, such as webhooks HMAC with SHA-256 A plain hash of a secret and the message

Try them

Generate hashes with the SHA-256 generator or the MD5 generator, and create or check a password hash with the Bcrypt generator.