GameParticles
Developer

MD5 vs SHA-256: Hash Sizes, Security and When to Use Each

MD5 vs SHA-256 compared: digest sizes, which algorithms have known collisions, what MD5 and SHA-1 are still fine for, and why none should store passwords.

Updated

MD5 and SHA-256 are both hash functions: they turn any input into a short fixed-length fingerprint. MD5 produces a 128-bit digest and is broken for security purposes, because attackers can create two different inputs with the same MD5 hash in seconds. SHA-256 produces a 256-bit digest, has no known practical attacks, and is the standard choice today for signatures, certificates and file verification.

What is a hash?

A hash function takes input of any size, a password, a file, an entire disk image, and returns a fixed-size value called a digest or simply a hash. A cryptographic hash function is designed to have these properties:

  • Deterministic. The same input always produces the same digest.
  • Fixed length. One byte or ten gigabytes in, the same number of bits out.
  • Avalanche effect. Changing a single character of input changes about half the output bits.
  • One-way. Given a digest, there is no feasible way to work out an input that produces it.
  • Collision resistant. It should be infeasible to find any two different inputs with the same digest.

Here is the word hello through three algorithms:

  • MD5: 5d41402abc4b2a76b9719d911017c592
  • SHA-1: aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
  • SHA-256: 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824

Capitalize one letter, Hello, and the MD5 becomes 8b1a9953c4611296a827abf8c47804d7. Nothing about the two digests suggests the inputs were nearly identical.

Hashing vs encryption

The two are often confused because both produce unreadable output. They do opposite jobs:

AspectHashingEncryption
ReversibleNoYes, with the key
Uses a keyNoYes
Output sizeFixedGrows with the input
PurposeVerify that data has not changedKeep data secret
ExamplesMD5, SHA-1, SHA-256AES, RSA, ChaCha20

There is no such thing as "decrypting" a hash. Sites that appear to reverse MD5 are looking the digest up in a table of previously hashed common inputs. Encoding is a third, separate thing: Base64 is neither hashing nor encryption, just a reversible change of representation, as Base64 encoding explained describes.

MD5, SHA-1 and SHA-256 compared

PropertyMD5SHA-1SHA-256
Digest size128 bits160 bits256 bits
Hex length32 characters40 characters64 characters
Introduced1991, by Ron Rivest1995, by NIST2001, by NIST, as part of SHA-2
Collisions foundYes, 2004Yes, 2017None known
Safe for signatures and certificatesNoNoYes
Relative speedFastestFastSlower, still very fast

SHA-256 is one member of the SHA-2 family, alongside SHA-384 and SHA-512, which give longer digests of 96 and 128 hex characters.

What "broken" means

A collision is two different inputs with the same digest. For an ideal hash with an n-bit output, finding one by brute force takes about 2 to the power n/2 attempts: 2^64 for MD5, 2^80 for SHA-1, 2^128 for SHA-256. An algorithm is considered broken when someone finds a shortcut well below that.

  • MD5. Researchers led by Xiaoyun Wang published collisions in 2004. Attacks improved quickly, and collisions can now be generated in seconds on an ordinary computer. In 2008 a team used MD5 collisions to create a rogue certificate authority certificate, and in 2012 the Flame malware used one to forge a Microsoft code-signing certificate.
  • SHA-1. In 2017 Google and CWI Amsterdam announced SHAttered, two different PDF files with the same SHA-1 digest. Major browsers stopped trusting SHA-1 certificates that same year, and NIST has said SHA-1 should be phased out entirely by the end of 2030.
  • SHA-256. No collision has been found, and no attack on the full algorithm comes anywhere near practical.

Collisions matter wherever a hash stands in for a document someone else supplied. If a signature covers only the digest, an attacker who can prepare two files with the same digest gets a harmless one signed and then substitutes the malicious one.

Note the limit of these attacks. They let an attacker create two colliding inputs of their own choosing. Finding an input that matches a given existing digest, a preimage attack, is still impractical even for MD5. That distinction is why MD5 remains usable in some settings.

What each is still fine for

MD5 is acceptable for:

  • Detecting accidental corruption in a download or copy, where nobody is trying to fool you.
  • Deduplication, cache keys, ETags and hash-based partitioning in systems you control.
  • Matching against legacy systems that publish only MD5 checksums.

SHA-1 is in the same position. It survives in older protocols, and Git still identifies objects by SHA-1 by default, hardened with collision detection, with SHA-256 repositories available as an option. Do not choose it for anything new.

Use SHA-256, or another SHA-2 or SHA-3 function, for:

  • Verifying downloads against a publisher's checksum when tampering is a concern.
  • Digital signatures, TLS certificates and code signing.
  • HMAC message authentication, such as the HS256 signatures in JWTs covered in how to decode a JWT.

Why none of them should store passwords

It is tempting to conclude that SHA-256 fixes password storage. It does not. MD5, SHA-1 and SHA-256 are all designed to be fast, and speed is the wrong property here. Someone who steals a database of password hashes can try billions of guesses per second on graphics cards, and most human-chosen passwords fall quickly.

Passwords need a dedicated password hashing function: Argon2id, bcrypt, scrypt or PBKDF2. These are deliberately slow and tunable, some also demand a lot of memory, and each uses a salt, a random value stored with every password so that identical passwords produce different hashes and precomputed tables are useless. Use your framework's built-in password hashing rather than assembling one from a general-purpose hash. For the user's side of the problem, see how to create a strong password.

Fast hashes are for data. Slow, salted hashes are for passwords. Using the first kind for the second job is the mistake, whichever algorithm you pick.

Computing and comparing hashes

To verify a downloaded file, hash the file itself with a command-line tool and compare the result with the published value: sha256sum file.iso on Linux, shasum -a 256 file.iso on macOS, and Get-FileHash file.iso in PowerShell, which defaults to SHA-256. Replace the algorithm name to get MD5 or SHA-1.

For text, the Hash Generator computes MD5, SHA-1, SHA-256, SHA-384 and SHA-512 for whatever you type, all at once, so you can compare digest lengths directly and copy the one you need. It hashes the text you enter; for files, use the commands above. Remember that a trailing newline or a different character encoding changes the input and therefore the digest, which is the usual reason two hashes of "the same" string disagree.

Try it

Type hello into the Hash Generator and check the three digests against the ones on this page. Then change a single letter and watch every hash change completely: that is the avalanche effect, and it is why a hash is such a reliable fingerprint.

Tools in this guide

Frequently asked questions

What is a hash?

A hash is a fixed-length fingerprint computed from data by a hash function. The same input always gives the same output, a tiny change in the input gives a completely different output, and the original data cannot be reconstructed from the hash.

What is the difference between hashing and encryption?

Encryption is reversible: with the right key, ciphertext is turned back into the original data. Hashing is one-way and uses no key, so there is nothing to decrypt. Encryption protects confidentiality; hashing verifies integrity and identity of data.

What is the difference between SHA-1 and SHA-256?

SHA-1 produces a 160-bit digest, shown as 40 hex characters, and has had practical collisions since 2017. SHA-256 belongs to the newer SHA-2 family, produces a 256-bit digest of 64 hex characters, and has no known practical attacks.

Is MD5 still safe to use?

Not for anything security related. Collisions can be produced in seconds, so MD5 must not be used for signatures, certificates or verifying files from untrusted sources. It is still acceptable for detecting accidental corruption and for non-security uses such as cache keys.

More developer guides

JSON Formatter
Developer · Format, validate and minify JSON with syntax highlighting.
↵
Base64 Encode / Decode
Developer · Encode text to Base64 or decode Base64 back to plain text.
↵
URL Encoder / Decoder
Developer · Encode or decode URL components and query strings.
↵
Hash Generator
Developer · Generate MD5, SHA-1, SHA-256 and SHA-512 hashes.
↵
UUID Generator
Developer · Generate UUIDs v4 (random) in bulk with one click.
↵
Regex Tester
Developer · Test regular expressions against text with live match highlighting.
↵
Word Counter
Text · Count words, characters, sentences, paragraphs and reading time.
↵
Case Converter
Text · Convert text between UPPER, lower, Title, camelCase, snake_case and more.
↵
List Sorter
Text · Sort, reverse, deduplicate and shuffle lists of text.
↵
Markdown Preview
Text · Write Markdown and see a live rendered preview side by side.
↵
Percentage Calculator
Numbers · Calculate percentages, increases, decreases and what % one number is of another.
↵
Color Converter
Colors · Convert colors between HEX, RGB, HSL and HSV instantly.
↵
Snake
Arcade · Eat food, grow longer, don't hit the walls. A timeless classic.
↵
2048
Puzzle · Slide tiles and combine them to reach the legendary 2048 tile.
↵
Word Grid
Puzzle · Guess the hidden five letter word in six tries using color clues.
↵
Sudoku
Puzzle · Fill the 9x9 grid with logic. Four difficulties from easy to expert.
↵
Minesweeper
Strategy · Use number clues to clear the minefield. Classic logic, three levels.
↵
Breakout
Arcade · Smash every brick with paddle and ball. Power-ups, combos, endless levels.
↵
Typing Practice
Casual · Build your WPM and accuracy with timed drills from 30 seconds to 5 minutes. Find your weak keys.
↵
TypeRunner
Arcade · Multiplayer typing race. Race friends by link, beat computer runners, or chase your own ghost.
↵
Typing Blocks
Arcade · Type each word before its wooden block lands. Miss, and the pile climbs toward the line.
↵
Bubble Shooter
Arcade · Aim, bank shots off the walls, and pop groups of three or more bubbles.
↵
Road Rush
Arcade · Race seven rivals through traffic and punch your way to the front.
↵