Security

MD5 vs SHA-256: Which Hash Algorithm Should You Use?

·4 min read

Hash functions quietly protect a huge part of the internet. They verify downloads, sign certificates, power version control and sit behind many password systems. Yet developers still argue about which one to pick, and old advice keeps circulating. This article compares MD5, SHA-1 and SHA-256 in plain language and gives you a simple rule for choosing.

What is a hash function?

A cryptographic hash function takes any input, from a single word to a multi-gigabyte file, and produces a fixed-length fingerprint called a digest. Good hash functions share a few properties. The same input always gives the same output. A tiny change to the input produces a completely different output. It should be practically impossible to work backwards from the digest to the input, and practically impossible to find two different inputs that give the same digest. That last property is called collision resistance, and it is where older algorithms have failed.

MD5

MD5 was designed in 1991 and produces a 128-bit digest, written as 32 hexadecimal characters. It is very fast, which made it popular. Unfortunately, researchers showed that collisions can be created deliberately, and today this takes only seconds on an ordinary computer. That means an attacker can craft two different files with the same MD5 hash. MD5 must not be used for digital signatures, certificates or any situation where someone might try to forge data.

It is still acceptable for non-security checks, such as detecting accidental corruption of a file during a download or building a cache key, because random errors are extremely unlikely to collide.

SHA-1

SHA-1 produces a 160-bit digest (40 hexadecimal characters). It was the standard for many years, but practical collision attacks were demonstrated in 2017, and browsers and certificate authorities have stopped trusting it. Treat SHA-1 like MD5: acceptable only for legacy compatibility, not for new security designs.

SHA-256

SHA-256 belongs to the SHA-2 family and produces a 256-bit digest (64 hexadecimal characters). No practical attack on its collision or pre-image resistance is known. It is used in TLS certificates, Bitcoin, software signing and countless other systems. For almost every new project, SHA-256 is the sensible default. If you want extra margin, SHA-384 and SHA-512 are available, and the newer SHA-3 family offers a different internal design.

Quick comparison

  • MD5: 128-bit, very fast, collisions are easy. Use only for non-security checksums.
  • SHA-1: 160-bit, collisions demonstrated. Avoid in new work.
  • SHA-256: 256-bit, no known practical attacks. Recommended default.

The password mistake

The biggest misconception is that you should hash passwords with SHA-256 because it is strong. Speed is actually the problem. A modern graphics card can test billions of SHA-256 guesses per second, so a stolen database of plain SHA-256 password hashes can be cracked quickly, especially when users choose weak passwords. Passwords need a deliberately slow, salted algorithm designed for the job: Argon2, bcrypt or scrypt, or PBKDF2 with a high iteration count. A salt is a random value stored with each hash so that identical passwords produce different digests and precomputed tables become useless.

Using HMAC for messages

When you need to prove that a message was not changed and that it came from someone who holds a secret, such as a webhook from a payment provider, use HMAC. It combines a hash function with a secret key. HMAC-SHA256 is the common choice. Compare the received signature with your own calculation using a constant-time comparison to avoid timing attacks.

Which should you pick?

  1. Verifying a download against a published checksum: use whatever the publisher provides, and prefer SHA-256 when there is a choice.
  2. Signatures, integrity checks, tokens: SHA-256 or stronger, with HMAC when a secret is involved.
  3. Storing user passwords: Argon2, bcrypt or scrypt, never a plain fast hash.
  4. Legacy systems that require MD5 or SHA-1: keep them only where compatibility forces you and add other protections.

You can experiment with all of these algorithms using our Password & Hash Generator. Type a word, change one letter, and watch how completely the digest changes. It is the quickest way to understand why hashing is so useful.

Try the related tools

Related articles

Web hosting by Somee.com