Hash functions are among the most heavily used tools in computing. They identify Git commits, verify downloads, sign certificates, protect stored passwords, and tie blockchains together. Yet "hash" is often confused with encryption, and the classic algorithms MD5 and SHA-1 still turn up long after they were broken. This guide explains what a cryptographic hash guarantees, how the SHA-2 family fits together, why MD5 and SHA-1 failed, and why passwords need a different kind of hash altogether. The examples use real test vectors you can reproduce in our hash generator.
- A cryptographic hash maps any input to a fixed-size digest. It is one-way and should make collisions infeasible to find.
- SHA-256, SHA-384 and SHA-512 (SHA-2) are secure today. MD5 and SHA-1 are broken for collision resistance.
- Hashing is not encryption (no key, no decryption) and not encoding (Base64 is reversible by anyone).
- Never store passwords with plain SHA-256. Use a salted, deliberately slow function such as Argon2id, scrypt, bcrypt or PBKDF2.
- For integrity with a secret key, use HMAC, not
hash(key + message).
What a Cryptographic Hash Function Is
A hash function takes input of any length and returns a fixed-length output called a digest. SHA-256 always returns 256 bits, whether the input is one byte or a 50 GB backup. Ordinary hash functions, like those behind hash tables, only need to spread values evenly. A cryptographic hash must also resist deliberate attack:
- Preimage resistance: given a digest, it's infeasible to find any input that produces it.
- Second-preimage resistance: given an input, it's infeasible to find a different input with the same digest.
- Collision resistance: it's infeasible to find any two different inputs with the same digest. Because of the birthday paradox, an ideal n-bit hash offers only about n/2 bits of collision resistance, which is why 128-bit MD5 was always marginal.
Good hashes also show an avalanche effect. SHA-256 of abc is ba7816bf…f20015ad, while SHA-256 of abd is a52d159f262b2c6ddb724a61840befc36eb30c88877a4030b65cbe86298449c9. Changing one letter flipped 122 of the 256 output bits, close to the 128 you'd expect by chance.
The SHA-2 Family and Its Relatives
NIST specifies SHA-1 and SHA-2 in FIPS 180-4 and SHA-3 in FIPS 202.
| Algorithm | Digest size | Hex length | Status |
|---|---|---|---|
| MD5 | 128 bits | 32 | Broken (collisions since 2004) |
| SHA-1 | 160 bits | 40 | Broken; NIST phase-out by 2030 |
| SHA-256 | 256 bits | 64 | Secure; the default choice |
| SHA-384 | 384 bits | 96 | Secure (truncated SHA-512) |
| SHA-512 | 512 bits | 128 | Secure; often faster on 64-bit CPUs |
| SHA3-256 | 256 bits | 64 | Secure; different internal design |
SHA-256 uses 32-bit arithmetic and SHA-512 uses 64-bit arithmetic, so on a 64-bit processor without dedicated SHA instructions, SHA-512 often processes data faster. SHA-384 is SHA-512 with different starting values and a truncated output. SHA-3 is built on a completely different design (Keccak). It exists as a backup in case SHA-2 is ever weakened, not because SHA-2 has been.
Worked Examples You Can Reproduce
These are standard test vectors. Type them into the hash generator, or run the commands shown, and you should get identical output.
| Input | Algorithm | Digest (hex) |
|---|---|---|
abc | SHA-256 | ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad |
abc | SHA-1 | a9993e364706816aba3e25717850c26c9cd0d89d |
abc | MD5 | 900150983cd24fb0d6963f7d28e17f72 |
| (empty) | SHA-256 | e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 |
abc + newline | SHA-256 | edeaaff3f1774ad2888673770c6d64097e391bc362d7d6fb34982ddf0efd18cb |
printf 'abc' | sha256sum # ba7816bf...f20015ad (Linux)
printf 'abc' | shasum -a 256 # same on macOS
echo 'abc' | sha256sum # edeaaff3...0efd18cb (echo adds "\n")
// Browser or Node.js 20+ (Web Crypto)
const buf = await crypto.subtle.digest('SHA-256', new TextEncoder().encode('abc'));
[...new Uint8Array(buf)].map(b => b.toString(16).padStart(2, '0')).join('');The last two rows of the table are the most common source of confusion. A hash covers bytes, not "text", so a trailing newline, Windows \r\n line endings, or a different character encoding gives a completely different digest. If you memorize one digest, make it e3b0c442…: seeing it means something hashed an empty input.
Why MD5 and SHA-1 Are Broken
In 2004 Xiaoyun Wang and colleagues published practical MD5 collisions. By 2008 researchers had used chosen-prefix MD5 collisions to forge a rogue certificate authority certificate, and in 2012 the Flame malware abused an MD5 collision to fake Microsoft code signing. RFC 6151 states that MD5 is no longer acceptable where collision resistance is required.
SHA-1 lasted longer. In February 2017, researchers from CWI Amsterdam and Google announced SHAttered, the first practical SHA-1 collision: two different PDF files with the same SHA-1 digest. Later work made chosen-prefix SHA-1 collisions practical too. NIST announced in December 2022 that SHA-1 should be phased out completely by December 31, 2030, and browsers stopped trusting SHA-1 certificates years ago.
Collisions don't mean these hashes are useless for everything. Finding a preimage for MD5 or SHA-1 is still impractical, and both still detect random corruption. The danger lies wherever an attacker can prepare two inputs in advance, such as certificates, signed documents, software packages and commit histories. For anything new, use SHA-256 or stronger.
Hashing vs. Encryption vs. Encoding
| Reversible? | Key? | Purpose | Examples | |
|---|---|---|---|---|
| Encoding | Yes, by anyone | No | Represent data in another format | Base64, URL encoding, hex |
| Encryption | Yes, with the key | Yes | Confidentiality | AES-GCM, RSA-OAEP |
| Hashing | No | No (HMAC adds one) | Fingerprints, integrity | SHA-256, SHA-3 |
"MD5 decrypt" websites don't decrypt anything. They store the digests of billions of known strings and look yours up. That works only when the input was guessable, which leads directly to passwords.
Passwords Need Slow, Salted Hashes
A fast hash is exactly what an attacker wants when cracking passwords. Commodity GPUs compute billions of SHA-256 hashes per second, and an unsalted hash lets one computation test a guess against every account at once. NIST SP 800-63B-4 requires verifiers to store passwords in a form that resists offline attack: salted and hashed with a suitable password hashing scheme. Two ingredients do the work:
- A unique random salt per password, stored alongside the hash. Identical passwords then produce different hashes, and precomputed lookup tables become useless.
- A deliberate work factor (iterations, and for the best schemes, memory). Each guess then costs milliseconds instead of nanoseconds.
The OWASP Password Storage Cheat Sheet recommends Argon2id first, then scrypt where Argon2id isn't available, bcrypt for legacy systems, and PBKDF2 where FIPS-140 compliance is required. It gives concrete minimum settings for each, such as 600,000 iterations for PBKDF2-HMAC-SHA-256. Use your platform's maintained library rather than writing your own. Never store passwords as MD5 or SHA-256, even with a salt. The other half of the defense is the password itself, covered in What Makes a Password Strong?.
HMAC: When You Need a Key
A bare hash proves only that data matches a digest; anyone can compute a new digest for altered data. To authenticate a message you need a key, and the standard construction is HMAC (RFC 2104). It hashes twice, with the key mixed in through inner and outer padding.
Don't take the shortcut of computing SHA-256(key + message). SHA-256 and SHA-512 use the Merkle–Damgård construction, so from that digest an attacker can compute a valid digest for key + message + padding + extra without knowing the key. This is called a length-extension attack, and HMAC isn't vulnerable to it.
HMAC-SHA-256 signs webhook payloads, AWS Signature Version 4 requests, and HS256 JSON Web Tokens. You can check an implementation against RFC 4231 test case 2: key Jefe and message what do ya want for nothing? must give 5bdcc146bf60754e6a042426089575c75a003f089d2739839dec58b964ec3843. Turn on HMAC mode in the hash generator to try it. When verifying an HMAC in server code, use a constant-time comparison such as crypto.timingSafeEqual or hmac.compare_digest.
Verifying a Download
Software publishers list a SHA-256 checksum so you can confirm that the file you received is the file they built:
sha256sum ubuntu.iso # Linux
shasum -a 256 ubuntu.iso # macOS
Get-FileHash .\ubuntu.iso -Algorithm SHA256 # Windows PowerShell
certutil -hashfile ubuntu.iso SHA256 # Windows cmdCompare the full value, not just the first few characters; the hash generator's compare box does this for you. A match rules out corruption and tampering in transit. It can't detect a compromised download page that serves a malicious file and its matching checksum. That is why serious projects also sign their checksum files with GPG or Sigstore, and why you should take the checksum from the publisher's HTTPS site rather than a mirror.
Frequently Asked Questions
Can SHA-256 be cracked?
No practical attack finds SHA-256 collisions or preimages. What gets "cracked" are hashes of guessable inputs such as short passwords, which attackers find by hashing guesses until one matches.
Is SHA-512 more secure than SHA-256?
It has a larger security margin, but both are far beyond any practical attack. SHA-256 is the common default for compatibility. SHA-512 can be faster on 64-bit machines.
Is it OK to use MD5 for checksums?
For catching accidental corruption, MD5 still works. For anything an attacker might tamper with, such as downloads, deduplication of untrusted files or signatures, use SHA-256.
Why do two tools give different hashes for the same text?
They are hashing different bytes. Check for a trailing newline, different line endings, a byte-order mark, or a different text encoding, and make sure both show the result in the same format, hex or Base64.
Should I add a salt to SHA-256 for passwords?
A salt is necessary but not sufficient. Salted SHA-256 is still far too fast to resist offline guessing. Use Argon2id, scrypt, bcrypt or PBKDF2, which handle the salt for you.
Sources: NIST FIPS 180-4; NIST: SHA-1 retirement (2022); SHAttered (2017); RFC 6151 (MD5 security considerations); RFC 2104 (HMAC); RFC 4231 (HMAC-SHA test vectors); NIST SP 800-63B-4; OWASP Password Storage Cheat Sheet.