MD5 vs SHA-256: What Hashes Are and When to Use Each
How hash functions work, the difference between MD5, SHA-1, SHA-256 and SHA-512, how to verify a download with a checksum, and why hashes alone are not enough for passwords.
UpdatedDownload pages often list a long string of letters and numbers labelled “SHA-256” or “MD5” next to a file. That string is a hash, and it lets you confirm the file you received is exactly the one the publisher released. Hashes are also used in passwords, version control and databases, so it is worth knowing what they can and cannot do.
What a hash function does
A hash function takes any input, from one letter to a multi-gigabyte video, and produces a fixed-length output called a hash, digest or checksum. Good hash functions have four properties:
- Deterministic: the same input always gives the same hash.
- Fixed size: SHA-256 always gives 256 bits (64 hex characters), whatever the input size.
- Avalanche effect: changing one character changes the hash completely, not just a little.
- One-way: you can’t work backwards from a hash to the original input.
Try it in the Hash Generator (MD5, SHA-256): hash the word hello, then Hello. The two results share nothing in common, even though only one letter changed.
The common algorithms
- MD5 (128 bits, 32 hex characters). Fast and still widely published, but broken for security: researchers can create two different files with the same MD5 hash. Fine for catching accidental corruption, not for proving a file wasn’t tampered with.
- SHA-1 (160 bits, 40 characters). Also broken for security since practical collisions were shown in 2017. Git historically used SHA-1 for commit IDs and is moving to SHA-256.
- SHA-256 (256 bits, 64 characters). Part of the SHA-2 family and the current standard choice. No practical attacks are known. Used in TLS certificates, software signing and Bitcoin.
- SHA-384 and SHA-512. Longer members of the SHA-2 family. SHA-512 can be faster than SHA-256 on 64-bit processors and gives a larger safety margin.
If you get to choose, use SHA-256. Use MD5 or SHA-1 only when you need to match a checksum that someone else already published in that format.
How to verify a download
- Download the file and copy the checksum from the publisher’s official page, ideally from a different source than the download link, such as their website rather than a mirror.
- Open the Hash Generator (MD5, SHA-256) and choose the file. It is hashed locally, so even a large file is never uploaded.
- Paste the published checksum into the compare box. If they match, the file is intact. If they don’t, delete it and download again from the official source.
Hex hashes are not case-sensitive, so A1B2 and a1b2 are the same value.
Hashes and passwords
Websites should never store your password itself. Instead they store a value derived from it and compare that value when you log in. But a plain SHA-256 of a password is not safe enough. SHA-256 is designed to be fast, and attackers with graphics cards can test billions of guesses per second against a stolen list of fast hashes. They also use precomputed tables of common passwords.
Proper password storage uses slow, salted algorithms built for the job: Argon2, bcrypt, scrypt or PBKDF2. A salt is a random value stored next to each password so that two users with the same password get different results. If you are building a login system, use your framework’s password hashing functions rather than calling SHA-256 directly.
Hashes are not encryption
Encryption is two-way: with the right key you get the original data back. Hashing is one-way: there is no key, and the original can’t be recovered. Encoding, such as Base64, is different again: it changes the format of data so it can travel safely, and anyone can reverse it. Mixing these up leads to real security mistakes, such as “protecting” data with Base64.
Hashes versus random IDs
Because a hash always gives the same output for the same input, it is a poor choice when you need a unique identifier for something new, such as a database row or an order. For that, use a random identifier like a UUID. The UUID Generator creates version 4 UUIDs from your browser’s secure random number generator; the chance of two ever colliding is negligible.
Summary
- A hash is a fixed-length fingerprint of any data.
- SHA-256 is the safe default; MD5 and SHA-1 are only for matching old checksums.
- Compare checksums to check a download hasn’t been corrupted or altered.
- For passwords, use Argon2, bcrypt or scrypt, not a plain hash.
Tools in this guide
More guides
- How to Extract Audio from a Video on Any Device2 MIN READThree free ways to pull the sound out of an MP4, MOV or WebM video on a phone or computer, with no app to install. Includes tips for speech and music.
- WebP vs PNG vs JPG: Which Image Format Should You Use?2 MIN READA plain comparison of JPG, PNG and WebP: file size, quality, transparency and compatibility, with a simple rule for picking the right format.
- How to Reduce Image File Size Without Losing Quality2 MIN READMake photos and screenshots smaller for email, uploads and websites. The three levers that matter: dimensions, format and quality, with recommended settings.
- What Is Base64 Encoding? A Plain-English Guide2 MIN READWhat Base64 is, how it turns bytes into text step by step, why it makes data 33% bigger, and where you meet it: data URIs, email, JWTs and APIs.