Hash Functions Explained — MD5, SHA-256, and When to Use Each
Understand hash functions: what they are, how they work, when to use MD5 vs SHA-256, and common use cases like checksums, password hashing, and data integrity.
What Is a Hash Function?
A hash function takes input of any size — a single word, a 10GB file, or anything in between — and produces a fixed-size output called a hash (also called a digest or checksum). The same input always produces the same hash. Even a tiny change in the input produces a completely different hash.
For example, the SHA-256 hash of "hello" is 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824, but the hash of "Hello" (capital H) is 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969 — completely different despite a single character change.
This property — called the "avalanche effect" — is what makes hash functions useful. They act as fingerprints for data. If two files produce the same hash, they're identical. If the hashes differ, the files are different. You can verify data integrity without comparing entire files byte by byte.
MD5 vs. SHA Family
Several hash algorithms are widely used, each with different properties and appropriate use cases:
MD5 (128-bit output) — The oldest commonly-used hash function. Fast and widely supported, but cryptographically broken since 2004. Researchers have demonstrated collision attacks where two different inputs produce the same MD5 hash. This means MD5 cannot be trusted for security-sensitive purposes.
SHA-1 (160-bit output) — Stronger than MD5 but also broken. Google demonstrated a practical collision attack in 2017. Most modern systems have deprecated SHA-1 for security purposes.
SHA-256 (256-bit output) — Part of the SHA-2 family. Currently considered secure with no known practical attacks. The standard choice for security-sensitive hashing: certificate signing, blockchain, digital signatures, and data integrity verification.
SHA-512 (512-bit output) — Same family as SHA-256 with a larger output. Slightly slower on 32-bit systems but faster on 64-bit systems. Provides a larger security margin but SHA-256 is already more than sufficient for current threats.
SHA-3 (variable output) — The newest standard, based on a completely different algorithm (Keccak). Exists as a backup in case SHA-2 is ever broken. Not widely adopted yet since SHA-256 remains secure.
Common Use Cases
Hash functions serve several distinct purposes in software development and IT:
File integrity (checksums) — When you download software, the publisher often provides an SHA-256 hash. After downloading, you compute the hash of your file and compare. If they match, your download wasn't corrupted or tampered with. This is why package managers and security-conscious projects always publish hashes.
Password storage — Databases should never store passwords in plain text. Instead, they store the hash. When you log in, the system hashes your input and compares it to the stored hash. Even if the database is stolen, attackers get hashes, not passwords. Note: for passwords, use purpose-built functions like bcrypt, scrypt, or Argon2 — not raw SHA-256 — because they're designed to be slow (making brute-force attacks impractical).
Data deduplication — Storage systems hash files to detect duplicates. If two files have the same hash, they're the same file and only one copy needs to be stored. Cloud storage services use this to save petabytes of disk space.
Git version control — Every commit, file, and tree object in Git is identified by its SHA-1 hash (migration to SHA-256 is in progress). This is how Git detects changes and ensures repository integrity.
API authentication — HMAC (Hash-based Message Authentication Code) combines a hash function with a secret key to sign API requests, ensuring they haven't been modified in transit.
When NOT to Use MD5
Despite being broken for security purposes, MD5 is still used in many legacy systems. Here's a clear breakdown of when MD5 is acceptable and when it's not:
Still acceptable:
- Non-security checksums — Verifying file downloads from trusted sources where the hash is delivered over a secure channel. The risk is accidental corruption, not adversarial tampering.
- Cache keys and hash tables — When you just need to distribute data evenly across buckets and there's no security implication.
- Data deduplication in trusted environments — Detecting duplicate files in your own storage where no one is trying to create collisions.
Never acceptable:
- Password hashing — MD5 is too fast (billions of hashes per second on modern GPUs) and collision-vulnerable. Use bcrypt, Argon2, or scrypt instead.
- Digital signatures — A collision attack means someone could create a different document with the same hash, undermining the entire purpose of the signature.
- Certificate verification — All major certificate authorities and browsers have dropped MD5.
- Integrity verification in adversarial settings — If someone could tamper with the file and the hash independently, MD5 doesn't protect you.
When in doubt, use SHA-256. It's only marginally slower than MD5 and eliminates the security concerns entirely.
Frequently Asked Questions
›Can you reverse a hash to get the original input?
No. Hash functions are one-way by design. You can't mathematically reverse a hash. Attackers use brute force (trying inputs until one matches) or rainbow tables (pre-computed hash-to-input mappings) to crack weak hashes.
›What is a hash collision?
A collision is when two different inputs produce the same hash output. For MD5 and SHA-1, researchers have found practical methods to create collisions intentionally. For SHA-256, no collision has ever been found.
›Is SHA-256 or SHA-512 better?
Both are secure. SHA-512 is actually faster on 64-bit systems due to how it processes data. SHA-256 has wider adoption and is the standard choice. Use SHA-512 if you want a larger security margin.
›Why not use SHA-256 for password hashing?
SHA-256 is too fast. A modern GPU can compute billions of SHA-256 hashes per second, making brute-force attacks practical. Password hashing algorithms like bcrypt are intentionally slow (configurable rounds) to make brute force infeasible.
No signup. Runs in your browser.