Developer

Hashing Explained: MD5, SHA-1 and SHA-256

A hash turns any data into a short, fixed-length fingerprint. Learn what MD5, SHA-1 and SHA-256 do, which ones are safe to use, and why passwords need more than a plain hash.

Hashing Explained: MD5, SHA-1 and SHA-256
Photo by Eduardo Rosas on Pexels

Key takeaways

  • A hash is a fixed-length fingerprint: the same input always gives the same output, and a tiny change gives a completely different one.
  • A hash cannot be reversed, and it is not encryption, because there is no key and nothing to decrypt.
  • MD5 and SHA-1 have known collision attacks, so do not use them for security. SHA-256 is a good choice for integrity checks.
  • Never store passwords as a plain SHA-256 hash. Use a slow, salted password hash such as bcrypt, scrypt or Argon2.
  • To check a download, hash the file and compare the result with the checksum the publisher gives you.

A hash is a short, fixed-length fingerprint made from any data. Feed the same text in and you always get the same fingerprint out. Change one letter and the fingerprint changes completely. You cannot turn a hash back into the original data. MD5, SHA-1 and SHA-256 are three well-known hash functions, and only SHA-256 is still a good choice for security work. The Hash Generator shows all of them at once.

What a hash is, in plain words

Think of a hash as a fingerprint for data. A fingerprint is small, but it is nearly unique to one person. A hash is small, and it is nearly unique to one piece of data.

A hash function takes input of any size. It could be one word, a whole book, or a 4 GB video. It always gives back output of the same length. The output is usually shown as a string of hexadecimal characters. Hex uses the digits 0 to 9 and the letters a to f.

Four properties make hashes useful:

  • Same input, same output. Hash the word "hello" today or next year, on any computer, and you get the same result.
  • Fixed length. A one-letter input and a million-word input give hashes of equal length.
  • Small changes, big differences. A tiny edit changes the whole hash. This is called the avalanche effect.
  • One way. You can go from data to hash. You cannot go from hash back to data.

The hash is also not a secret code. There is no key. Anyone can compute the hash of any text. That is a big difference from encryption, which we cover below.

A real example: hello and Hello

Here are real digests for two words that differ only in the first letter.

InputAlgorithmHash
helloMD55d41402abc4b2a76b9719d911017c592
HelloMD58b1a9953c4611296a827abf8c47804d7
helloSHA-1aaf4c61ddcc5e8a2dabede0f3b482cd9aea9434d
HelloSHA-1f7ff9e8b7bb2e09b70935a5d785e0cc5d9d0abf0
helloSHA-2562cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
HelloSHA-256185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969

Look at the two MD5 rows. Only one letter changed, from lower case h to upper case H. Yet the two hashes share almost nothing. You cannot guess one from the other. You can type these words into the Hash Generator and check the results yourself.

Spaces and line ends count too. "hello" and "hello " (with a trailing space) have different hashes. If two hashes do not match, look for a hidden space or a new line at the end. Our guide on invisible characters explains how these sneak in.

A laptop screen showing lines of code on a desk
Photo by Daniil Komov on Pexels

MD5, SHA-1, SHA-256 and others compared

Each hash function produces a different length. The length is counted in bits. Every 4 bits make one hex character, so you divide the bits by 4 to get the number of characters.

AlgorithmOutput sizeHex charactersSafe for security?
MD5128 bits32No. Collisions are easy to make.
SHA-1160 bits40No. Collisions have been shown.
SHA-256256 bits64Yes, for integrity and signatures
SHA-384384 bits96Yes
SHA-512512 bits128Yes
CRC3232 bits8No. It only catches accidental errors.

Check the sums: 128 ÷ 4 = 32, 160 ÷ 4 = 40, 256 ÷ 4 = 64, and 512 ÷ 4 = 128. These match the lengths in the hash examples above.

SHA-256, SHA-384 and SHA-512 belong to a group called SHA-2. CRC32 is not really a hash for security. It is a quick checksum used to catch accidental damage, such as a bad download or a faulty disk.

Why MD5 and SHA-1 are not safe

A hash should make it very hard to find two different inputs with the same output. Such a pair is called a collision. If someone can make collisions on purpose, they can build two files with the same hash, one harmless and one harmful.

That is what happened to MD5 and SHA-1. Researchers showed practical collision attacks on MD5 in the mid 2000s. In 2017, a research team showed the first real SHA-1 collision. Since then, browsers, certificate authorities and software makers have moved away from both.

This does not mean MD5 or SHA-1 are useless. They are still fine for jobs where nobody is attacking you. Examples include a quick check that a file copied correctly between two of your own folders, or a short ID for a cache. But do not use them to protect anything. If someone could gain by faking a match, pick SHA-256 or better.

Rule of thumb: use MD5 and SHA-1 only to catch accidents. Use SHA-256 or SHA-512 when someone might try to cheat.

A padlock displayed beside a laptop
Photo by Brett Sayles on Pexels

Hashing is not encryption

People often mix these up. The difference matters.

QuestionHashingEncryption
Can you get the data back?NoYes, with the right key
Is a key used?NoYes
Output lengthFixedGrows with the data
Main useChecking that data has not changedKeeping data secret

Encoding is a third thing. Base64, for example, only changes how data is written, and anyone can reverse it. Our article on Base64 images shows what that looks like in practice.

Why you must not store passwords as plain SHA-256

This is the most common mistake in the whole topic. A site should never keep your password in plain form. Many people then think, "I will store a SHA-256 hash." That is better than plain text, but it is still not good enough.

There are two problems.

  1. Hashes like SHA-256 are fast. A modern computer can test a huge number of guesses every second. An attacker who steals a database can try common passwords and short ones very quickly.
  2. Same password, same hash. The SHA-256 hash of "password" is always 5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8. Attackers keep large tables of hashes for common passwords. If your database holds that value, they see the password at once, for every user who chose it.

What a salt does

A salt is a random string added to each password before it is hashed. Each user gets their own salt, and the salt is stored next to the hash. Two users with the same password now get different hashes. Pre-made tables stop working, because the attacker would need a new table for every salt.

Say Ana and Ben both choose the password "sunshine". Without a salt, both rows in the database hold the same hash, and cracking one cracks both. With a salt, Ana's row is the hash of her random salt plus "sunshine", and Ben's row uses a different random salt. The two stored values look unrelated, so the attacker must attack each account on its own.

Use a slow password hash

Passwords need a hash that is slow on purpose, and that includes a salt. The usual choices are:

  • bcrypt: old, widely supported, and a good default in many frameworks.
  • scrypt: slow and also hungry for memory, which makes attacks costly.
  • Argon2: a newer design that won a public password hashing competition in 2015.

In real projects, use the password functions that come with your language or framework. Do not write your own. Strong passwords still matter, because a slow hash only buys time. Read password strength explained for the reasons, and use the Strong Password Generator to make long random ones.

How to check a download with a checksum

Software sites often publish a SHA-256 checksum next to a download. It lets you confirm the file is complete and has not been changed. Here are the steps.

  1. Download the file. Copy the checksum from the publisher's page.
  2. Compute the SHA-256 of your file with a command on your computer.
  3. Compare the two strings, character by character.

Common commands:

  • Windows (PowerShell): Get-FileHash file.zip. It uses SHA-256 by default.
  • Windows (Command Prompt): certutil -hashfile file.zip SHA256
  • macOS: shasum -a 256 file.zip
  • Linux: sha256sum file.zip

If the strings match, the file is the same as the one the publisher hashed. If they do not match, delete the file and download it again. A mismatch can mean a broken download or a changed file.

The ToolsWish Hash Generator works on text that you type or paste, and does it in your browser. It is good for short strings, such as test values. For large files, use the commands above.

A checksum only helps if you got it from a place you trust. If a hacker changed both the file and the checksum on the same page, the check proves nothing. Prefer a checksum from a separate, official source.

Other uses and common mistakes

Hashes show up in many places: Git commit IDs, file de-duplication, digital signatures, caches and data integrity checks. They are not the same as unique IDs. If you only need a unique label for a record, a UUID is a better fit. See UUIDs: versions, uniqueness and when to use them, or make one with the UUID Generator.

Avoid these mistakes:

  1. Using MD5 or SHA-1 for security. Pick SHA-256 or stronger.
  2. Hashing passwords with plain SHA-256. Use bcrypt, scrypt or Argon2 with a salt.
  3. Thinking a hash is encryption. You cannot decrypt a hash, and you cannot hide data with one.
  4. Comparing hashes with different cases. A hash written in capitals has the same meaning as one in lower case. But a plain text comparison sees them as different. Change both to lower case before you compare.
  5. Hashing the wrong text. A trailing space, a new line or a different spelling gives a different hash. Check the exact input before you decide two hashes should match.
  6. Trusting a checksum from an untrusted page. It must come from a source you trust.

Where to go next

Try the Hash Generator with the words "hello" and "Hello" and compare your results with the table above. Use the UUID Generator when you need a unique ID and the Strong Password Generator when you need a good password. For more reading, see password strength explained and UUIDs and when to use them.

Frequently asked questions

What is a hash?

A hash is a short, fixed-length fingerprint made from data by a hash function. The same input always gives the same hash, and a tiny change gives a completely different one. You cannot reverse a hash to get the data back. People use hashes to check that files have not changed, to build signatures and to store passwords safely, when combined with a salt.

Is MD5 still safe to use?

Not for security. Researchers have shown that two different inputs can be made to give the same MD5 hash, which breaks its main promise. It is still fine to catch accidental errors, such as a bad copy of a file you made yourself. If anyone could gain by faking a match, use SHA-256 or SHA-512.

How long is a SHA-256 hash?

A SHA-256 hash is 256 bits long. Written in hexadecimal, that is 64 characters, because each hex character stands for 4 bits and 256 divided by 4 is 64. Other lengths are 32 characters for MD5, 40 for SHA-1, and 128 for SHA-512. The length stays the same however large the input is.

Can I store passwords with SHA-256?

You should not use plain SHA-256 for passwords. It is very fast, so attackers can test huge numbers of guesses, and the same password always gives the same hash. Use a slow, salted password hash such as bcrypt, scrypt or Argon2, and prefer the functions built into your framework instead of writing your own.

Is hashing the same as encryption?

No. Encryption uses a key and can be reversed by someone who holds the right key. Hashing has no key and cannot be reversed. Encryption keeps data secret. Hashing checks that data has not changed or compares values without storing them. Base64 is a third thing, an encoding that anyone can undo, so it hides nothing.

Try the tools from this guide

Keep reading

Related guides

All guides