MD5, SHA-1, SHA-256: Understanding Hash Functions
Learn what cryptographic hash functions are, how MD5, SHA-1, and SHA-256 differ, and when to use each algorithm in practice.
What Is a Hash Function?
A cryptographic hash function takes an input of any size — a single character, a sentence, an entire file — and produces a fixed-size output called a hash, digest, or checksum. The same input always produces the same output, but even a tiny change to the input produces a completely different hash.
For example, the SHA-256 hash of "hello" is:
``
2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
``
Change one letter to "Hello" and the hash becomes entirely different:
``
185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969
``
This property is called the avalanche effect and is fundamental to how hash functions provide security.
Key Properties
Deterministic
The same input always produces the same hash. This makes hashes useful for verification — if two files produce the same hash, they have identical content.
One-Way
It is computationally infeasible to reverse a hash — you cannot recover the original input from the hash output. You can only verify by hashing the candidate input and comparing.
Collision Resistant
A collision occurs when two different inputs produce the same hash. Good hash functions make collisions astronomically unlikely. Breaking collision resistance means the algorithm is considered compromised.
Fixed Output Size
Regardless of input size, the hash is always the same length. MD5 always produces 128 bits (32 hex characters), SHA-1 produces 160 bits (40 hex characters), and SHA-256 produces 256 bits (64 hex characters).
MD5: Fast but Broken
MD5 was designed in 1991 by Ronald Rivest and produces a 128-bit hash. For over a decade, it was the standard hash function for file integrity verification and password storage.
Why MD5 Is No Longer Secure
In 2004, researchers demonstrated practical collision attacks against MD5. By 2008, attackers could create forged SSL certificates using MD5 collisions. Today, generating an MD5 collision takes seconds on modern hardware.
When MD5 Is Still Acceptable
MD5 remains useful as a non-security checksum — for detecting accidental file corruption, deduplicating data, or generating cache keys. If the threat model does not include deliberate tampering, MD5's speed is an advantage.
SHA-1: Deprecated
SHA-1 (Secure Hash Algorithm 1) was designed by the NSA and published in 1995. It produces a 160-bit hash and was the successor to MD5 for security applications.
Why SHA-1 Is Deprecated
In 2017, Google and CWI Amsterdam published SHAttered, demonstrating the first practical SHA-1 collision. Two different PDF files were crafted to produce the same SHA-1 hash. While the attack required significant computational resources, it proved SHA-1 is fundamentally broken for security purposes.
Major browsers stopped accepting SHA-1 TLS certificates in 2017. Git, which uses SHA-1 for commit hashes, has been transitioning to SHA-256. NIST deprecated SHA-1 for digital signatures in 2011.
SHA-256: The Current Standard
SHA-256 is part of the SHA-2 family, designed by the NSA and published in 2001. It produces a 256-bit hash and is the most widely used secure hash function today.
Why SHA-256 Is Trusted
No practical or theoretical attacks have significantly weakened SHA-256. Its 256-bit output provides a vast collision space — finding a collision would require approximately 2^128 operations, which is beyond the capability of any foreseeable technology, including quantum computers using Grover's algorithm (which only halves the effective security to 128 bits).
Where SHA-256 Is Used
- TLS/SSL certificates. Modern certificates use SHA-256 for their signature hash.
- Bitcoin and blockchain. The Bitcoin proof-of-work system is based on SHA-256.
- Package managers. npm, pip, and other package managers verify downloads using SHA-256 checksums.
- Password hashing (as a building block). Algorithms like PBKDF2 can use SHA-256 internally.
SHA-3 and Beyond
SHA-3 (Keccak) was standardized in 2015 as an alternative to SHA-2, not a replacement. It uses a completely different internal structure (a sponge construction vs. SHA-2's Merkle-Damgard construction), providing algorithmic diversity. If a breakthrough ever compromises SHA-2, SHA-3 is unlikely to be affected by the same attack.
Hashing Is Not Encryption
A common confusion: hashing and encryption are fundamentally different. Encryption is reversible with the correct key — the ciphertext can be decrypted back to the plaintext. Hashing is one-way — you cannot recover the input from the hash. Use encryption when you need to retrieve the original data. Use hashing when you only need to verify or compare.
Password Hashing
Never store passwords as plain text or even as simple SHA-256 hashes. Dedicated password hashing algorithms like bcrypt, scrypt, and Argon2 add a salt (random data) and use deliberate slowness (key stretching) to make brute-force attacks impractical. These algorithms use hash functions internally but add critical layers of protection.
Generate Hashes Instantly
Our Hash Generator computes MD5, SHA-1, SHA-256, SHA-384, and SHA-512 hashes for any input text — all in your browser using the Web Crypto API. For verifying specific MD5 hashes, the MD5 Hash Checker provides a focused interface.
Summary
MD5 is fast but cryptographically broken — use it only for non-security checksums. SHA-1 is deprecated and should be replaced wherever it is still in use. SHA-256 is the current standard for security-sensitive hashing. For password storage, always use a dedicated algorithm like bcrypt or Argon2. Understanding these distinctions helps you choose the right tool and avoid security pitfalls.