Cipher Utility

Hash Functions Explained: Why MD5 Isn't Secure

What a hash is for, and why MD5 no longer does the job.

What a hash function does

A hash function takes input of any size and turns it into a short output of fixed length, called a hash or digest. You can't run it backwards to get the input, and that is the main difference from encryption, which is designed to be reversed by anyone with the key.

A hash works like a fingerprint for data. The same input always gives the same hash, and changing a single character gives a completely different one:

MD5("Hello World")      b10a8db164e0754105b7a99be72e3fe5
MD5("Hello World!")     ed076287532e86365e841e92bfc50d8c
SHA-256("Hello World")  a591a6d40bf420404a011733cfb7b190d62c65bf0bcda32b57b277d9ad9f146e

You can reproduce these with the MD5 generator and the SHA-256 generator.

Properties of a cryptographic hash

  • It is deterministic: the same input always produces the same output.
  • The output has a fixed size. MD5 always gives 128 bits, whether you hash one letter or a 4 GB file.
  • A small change to the input changes about half of the output bits. This is called the avalanche effect.
  • It is one-way. Given a hash, there should be no faster way to find a matching input than guessing.
  • It is collision resistant. Finding two different inputs with the same hash should be practically impossible.

MD5 still has the first three properties. It fails the last one, and for passwords it is far too fast to be a useful one-way function.

Common hash functions

FunctionOutputPublishedStatus
MD5128 bits, 32 hex characters1992, Ronald RivestBroken. Fine only for non-security checksums.
SHA-1160 bits, 40 hex characters1995, NSADeprecated. Practical collisions shown in 2017.
SHA-256256 bits, 64 hex characters2001, NSASecure and widely used.
SHA-512512 bits, 128 hex characters2001, NSASecure, larger output than SHA-256.
SHA-3224 to 512 bits2015, NIST (Keccak)Secure, different internal design.

Bcrypt belongs in a different category. It is a password hashing function: deliberately slow, with a random salt built in and a cost setting you can raise as hardware gets faster.

Why MD5 isn't secure

Collisions

In 2004 a team led by Xiaoyun Wang published the first practical MD5 collisions: two different inputs with the same MD5 hash. Since then the attacks have become fast enough to run on a laptop. You can make two different files, for example two PDFs or two programs, that have identical MD5 hashes.

That matters wherever a hash stands in for the document itself. In 2008 researchers used an MD5 collision to create a rogue certificate authority certificate that browsers of the time would have trusted. Digital signatures, certificates and file verification all assume nobody can produce a second input with the same hash.

Speed

MD5 was designed to be fast, and on modern hardware it is extremely fast. A single current graphics card can compute tens of billions of MD5 hashes per second. For a file checksum that is a good thing. For stored passwords it is a disaster, because an attacker who steals a database of MD5 password hashes can test billions of guesses per second against it. Common passwords such as password123 fall instantly, and precomputed lookup tables (rainbow tables) exist for unsalted MD5 hashes of billions of common strings.

Where hash functions are used

Password storage

A site should store a hash of each password rather than the password itself, and the hash should come from a slow, salted password hashing function such as bcrypt. Here is a real bcrypt hash of mySecurePassword123 with a cost of 12:

$2y$12$UtKo0Em/kJqFAnNedGYVmOMfRYBpWr2nTbl8LJPr6p/I53WYvLEey

The string holds the algorithm version (2y), the cost (12), the random salt and the hash. Hash the same password again and you get a different string, because the salt changes. You can try this with the bcrypt generator, and Hashing passwords correctly covers the details.

Digital signatures and file integrity

A signature is computed over the hash of a document rather than the whole document, so any change to the document breaks the signature. Download pages often list a SHA-256 checksum so you can check that the file arrived intact. For reference, the SHA-256 hash of an empty input is:

e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855

Bitcoin

Bitcoin's proof of work uses SHA-256. Miners search for a block whose hash is below a target value, which shows up as a long run of leading zeros. The very first Bitcoin block has this hash:

000000000019d6689c085ae165831e934ff763ae46a2a6c172b3f1b60a8ce26f

Where MD5 is still acceptable

MD5 is still fine when nobody is trying to attack you and you only need to spot accidental changes or build an identifier:

  • checksums that catch a corrupted download or a bad disk copy
  • cache keys and ETags for HTTP caching
  • deduplication keys and identifiers in non-security code

What to use instead of MD5

JobUseWhy
Storing passwordsArgon2, bcrypt or scryptSlow on purpose, salted, and Argon2 and scrypt also use a lot of memory
Signatures and certificatesSHA-256 or SHA-512No known practical collisions
File integritySHA-256 or SHA-3Collision resistant
CryptocurrencySHA-256, Keccak-256What Bitcoin and Ethereum use

Salt every password

A salt is a random value stored next to each password hash and mixed into the input before hashing. Two users with the same password then get different hashes, and precomputed tables become useless.

Password:   password123
Salt:       randomSalt456
Hash input: password123randomSalt456

Bcrypt and Argon2 generate and store the salt for you, which is one more reason to use them instead of building this by hand.

Use a tested library

Don't write your own hash function or your own password scheme. Use what your language provides, such as hashlib.sha256(data).hexdigest() in Python, or password_hash() in PHP for passwords.

Quantum computers and SHA-3

Quantum computers threaten hash functions much less than they threaten public key encryption. Grover's algorithm would in theory give a square-root speedup on brute force search, which effectively halves the security level in bits, so a 256-bit hash would still offer about 128 bits of security against such an attack.

SHA-3, based on the Keccak algorithm, was standardized by NIST in 2015 as an alternative to SHA-2. It uses a sponge construction instead of the Merkle-Damgård design of MD5, SHA-1 and SHA-2, which makes it immune to length extension attacks. SHA-2 itself has no known practical weakness, so SHA-256 remains a sound default.

Try it on Cipher Utility

Further reading