What Is a Hash? SHA-256 vs SHA-1 vs MD5 and How to Verify a Download
How cryptographic hashes work, what makes MD5 and SHA-1 unsafe for security, why hashes are not encryption, and how to check a file checksum.
What a hash does
A hash function turns any input into a fixed-length fingerprint. The same input always gives the same output, a tiny change in the input changes the output completely, and you cannot work backwards from the fingerprint to the original.
The output length depends on the algorithm. SHA-1 produces 160 bits, shown as 40 hexadecimal characters. SHA-256 produces 256 bits, shown as 64 characters. SHA-512 produces 512 bits, shown as 128 characters.
Which algorithm to use
- SHA-256 and SHA-512 are safe for integrity checks and digital signatures, and SHA-256 is the usual default.
- SHA-1 has been broken for collision resistance since a practical attack was shown in 2017. Use it only to match an old system.
- MD5 has been broken for much longer. Two different files can be built with the same MD5 hash, so never use it to prove a file is untampered.
For simple error detection, such as checking that a copy finished, any of them works. For anything an attacker could try to fake, use SHA-256 or stronger.
Worked example: one letter changes everything
Hash the five-character text hello with SHA-256 and you get 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824. Change only the first letter to a capital, so the text is Hello, and the result is 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969. The two outputs share no visible pattern, even though the inputs differ by a single bit in one character.
This is called the avalanche effect. It is why a hash works as a fingerprint: you cannot tell how close two inputs are by comparing their hashes, and a one-bit corruption in a file is guaranteed to show up as a completely different value.
The hash of empty input is also well defined. SHA-256 of an empty string is e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855. If you ever see that value for a file you expected to have content, the file was empty when it was hashed.
Output sizes at a glance
| Algorithm | Bits | Hex characters | Digest of hello |
|---|---|---|---|
| MD5 | 128 | 32 | 5d41402abc4b2a76b9719d911017c592 |
| SHA-1 | 160 | 40 | aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d |
| SHA-256 | 256 | 64 | 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 |
Each hex character carries four bits, so the number of characters is always the bit length divided by four. If a checksum you are given has the wrong length for the algorithm it claims to be, it was copied incorrectly or truncated.
Verifying a download
Software projects often publish a checksum beside the download link. After downloading, compute the hash of your file and compare it to the published value. If every character matches, the file is identical to the one the publisher hashed.
Get the checksum from a different channel than the file if you can. If an attacker controls the page, they can replace both the file and the checksum.
Hashing is not encryption, and not password storage
Encryption can be reversed with a key. A hash cannot be reversed, but short or common inputs can still be found by trying guesses, which is how leaked unsalted password hashes get cracked.
Never store passwords with a plain SHA-256. Use a slow, salted password hashing function such as bcrypt, scrypt or Argon2, which is designed to make each guess expensive.
Common mistakes
- Hashing text with a trailing newline gives a different result from the same text without one.
- Different text encodings change the bytes, so the hash changes too. UTF-8 is the usual choice.
- Comparing hashes in uppercase against lowercase hex fails unless you normalize the case.
The Hash Generator computes SHA hashes in your browser with the Web Crypto API, so the text you paste is not uploaded.
Troubleshooting a hash that does not match
When your hash differs from the published one, work through the causes from cheapest to most expensive.
- Check the case first. Hex digits are the same value in upper and lower case, so lowercase both strings before comparing.
- Check for invisible characters. The text hello gives 2cf24dba... but hello followed by a newline gives 5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03. Editors and shell commands such as echo add that newline without showing it.
- Check the algorithm. A 40-character value is SHA-1 and a 64-character value is SHA-256 or another 256-bit hash, so confirm you picked the one the publisher used.
- Check the encoding. The text héllo produces 3c48591d8d098a4538f5e013dfcf406e948eac4d3277b10bf614e295d6068179 as UTF-8 bytes but c63c19ed1e0ee079f22ddb0d4112cfc799132c88684eb42324c62b9899b0c6c1 as Latin-1 bytes, because the accented letter is encoded differently.
- Re-download the file. If every other cause is ruled out, the download is probably damaged or incomplete.
Frequently asked questions
- Can a hash be reversed to get the original text? No. A hash throws information away, so there is nothing to reverse. Short or common inputs can be recovered only by guessing them and hashing each guess.
- Is SHA-256 encryption? No. Encryption is reversible with a key, while a hash has no key and no way back. It is a one-way fingerprint.
- Why do two different inputs never seem to share a hash? They can, because there are more possible inputs than outputs. With a strong algorithm such as SHA-256, finding such a pair is not practical, while for MD5 and SHA-1 it has been done.
- Is MD5 still fine for anything? It is acceptable for catching accidental corruption or for use as a cache key, where no attacker is involved. Do not use it where someone could benefit from forging a match.
- Should I hash a password with SHA-256 before storing it? No. SHA-256 is fast, which lets an attacker test billions of guesses. Use bcrypt, scrypt or Argon2 with a unique salt for each password.