Conclusion: Pick by Job, Not by Prestige
The same word "hash" implies opposite requirements depending on the job: verification wants speed, password storage wants deliberate slowness. Getting it wrong is not a style problem — it means signatures can be forged or passwords cracked in seconds.
| Job | Use | Why | Never |
|---|---|---|---|
| File integrity check (non-adversarial) | SHA-256; MD5 only for legacy compatibility | Fast, deterministic, reproducible; the goal is detecting damage | — |
| Data fingerprint / dedup / cache key | SHA-256 | Negligible collision probability, stable output | Truncated fingerprints that are too short |
| Tamper protection / API signatures | HMAC-SHA256 | A key must participate, and string concatenation will not do | hash(secret + msg) |
| Password storage | Argon2id, then bcrypt | Slow hash plus a per-user salt, built to resist enumeration | Any bare SHA / MD5 |
Four Properties Define What It Can Do
A hash maps arbitrary-length input to a fixed-length digest. Each cryptographic property maps to one capability — and to one class of misuse:
| Property | Meaning | Capability |
|---|---|---|
| Deterministic | Same input, same output | Reproducible verification |
| Avalanche | One flipped input bit changes about half the output bits | You cannot infer input similarity from digest similarity |
| Preimage-resistant | Recovering input from a digest is infeasible | One-wayness |
| Collision-resistant | Finding two inputs with the same digest is hard | Forgery prevention |
The avalanche effect is easy to check (real SHA-256 values):
echo -n "hello" | sha256sum # 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
echo -n "hellp" | sha256sum # fdd7585e08c4e2afd71dcabdb4636c89d557a3f42db9e2040c8bbd1708aa4ce7
One letter differs and not a single one of the 64 hex characters matches. That is why digest comparison can never tell you whether two documents are similar — a digest keeps no similarity information at all.
"One-way" is not "unbreakable", though: short passwords and common strings fall to rainbow tables or brute force. This is exactly why passwords need salt and a slow hash, and why general-purpose hash tools do not salt — they must stay reproducible.
Another frequent misuse is treating a hash as a randomness source: its output is predictable, so random values must come from a CSPRNG such as crypto.randomBytes.
Collision Strength Is 2^(n/2), Not 2^n
| Algorithm | Output bits | Collision strength | Status |
|---|---|---|---|
| MD5 | 128 | 2⁶⁴ | Constructible collisions exist |
| SHA-1 | 160 | 2⁸⁰ | Constructible collisions exist |
| SHA-256 | 256 | 2¹²⁸ | No known practical attack |
| SHA-512 | 512 | 2²⁵⁶ | No known practical attack |
The reason fits in one sentence: an attacker does not need to hit a specific digest, only any two inputs that collide, so the work is about 2^(n/2) — the birthday bound. At 256 bits that is roughly 2¹²⁸, far beyond feasible compute; at 128 bits it is only 2⁶⁴, which dedicated hardware can reach. MD5 is not a theoretical risk: collisions are constructible on demand, and SHA-1's SHAttered attack produced two different PDFs with the same digest.
The derivation, a runnable collision experiment, and how many bits a truncated fingerprint needs are all in hash collisions explained. This article keeps only the selection outcome.
MD5 / SHA-1: Non-Adversarial Use Only
One question decides it: would an attacker benefit from crafting input for you?
- File dedup, cache keys, local comparisons: no adversary, so MD5 remains fast and usable.
- Digital signatures, certificates, security identifiers, any tamper protection: out — including "just this once".
SHA-2: The Default, Plus One Gotcha
SHA-256 and SHA-512 both have no known practical collision attack. The difference is cost and platform: SHA-256 emits 256 bits and is cheaper on 32-bit platforms, mobile devices, and high-throughput verification; SHA-512 emits 512 bits, runs faster on 64-bit CPUs, and is marginally stronger against length-extension attacks. SHA-256 is the right default.
Length Extension: Why hash(secret + msg) Is Forgeable
SHA-2 uses the Merkle–Damgård construction: the digest is the final value of the internal compression state. Given H(secret || msg) and the length of msg, an attacker can treat that state as the starting point and keep compressing, producing H(secret || msg || padding || extra) — without ever knowing secret.
The consequence is not academic: in webhook verification, an attacker appends content to the message and the signature still validates.
// ❌ Dangerous: forgeable via length extension
const sig = sha256(SECRET + body);
// ✅ Correct: HMAC mixes the key into the initial state
const sig = hmacSha256(SECRET, body);
Generate and verify HMACs with the HMAC generator. Switching to SHA-3 or BLAKE3 also removes the property, because sponge constructions are not vulnerable.
Password Storage: Slow Hashes Are Not Optional
SHA was designed for speed, which is precisely fatal here: one modern GPU computes billions of hashes per second, so weak passwords fall instantly. Password storage needs a deliberately slow hash plus a per-user salt:
| Scheme | Traits | Key parameters |
|---|---|---|
| BCrypt | Adaptive cost factor, salts automatically | cost ≥ 12 (each +1 doubles the work) |
| Argon2id | 2015 Password Hashing Competition winner, memory-hard | memory ≥ 19 MB, iterations ≥ 2, parallelism 1–4 |
| SCrypt | Memory-hard, resists hardware acceleration | N ≥ 2¹⁴, r = 8, p = 1 |
| PBKDF2 | Best compatibility, but not memory-hard | iterations ≥ 600,000 (OWASP 2023) |
Memory hardness is what breaks GPU economics: GPUs have many cores and little memory per core, so forcing each guess to occupy megabytes pushes concurrency back against memory capacity and bandwidth.
hash-text is a general-purpose one-way hash tool for checksums, fingerprints, and signature checks. For password storage use the bcrypt hash tool; parameter tuning and legacy migration are covered in the password hashing guide.
Hashing ≠ Encryption
| Encryption (AES, etc.) | Hash (SHA/MD5/BCrypt) | |
|---|---|---|
| Reversible | ✅ With the key | ❌ One-way |
| Output length | Related to plaintext | Fixed |
| Purpose | Protect confidentiality | Verify integrity / fingerprint |
| Classic misuse | — | Treating a hash as encryption and expecting decryption |
Need the data back? Use AES encryption; irreversibility is a hash's design goal, not a defect.
Verifying Files in Practice
# Linux / macOS
sha256sum ubuntu-24.04.iso
sha256sum -c SHA256SUMS # batch-check when the vendor publishes a .sha256/.sums file
# Windows PowerShell
Get-FileHash .\ubuntu-24.04.iso -Algorithm SHA256
# Windows cmd
certutil -hashfile ubuntu-24.04.iso SHA256
The critical rule: compare against the digest the vendor publishes over HTTPS, not a value pasted beside the download link on some mirror. If the digest and the file come from the same hijacked source, the comparison fails consistently in the wrong direction. For the full workflow and triage steps when values differ, see file integrity checks.
Selection Decision Tree
Need a hash?
├── Must the original data come back?
│ └── ❌ Not possible with a hash — use AES encryption
├── Proving content was not changed?
│ ├── Shared secret available? → HMAC-SHA256
│ └── No key, integrity only? → SHA-256 (MD5 fine when non-adversarial)
├── Fingerprint / dedup / cache key?
│ └── SHA-256 (BLAKE3 if raw speed matters and no adversary exists)
├── Storing passwords?
│ └── Argon2id first, then BCrypt (cost ≥ 12); never SHA/MD5
└── Signing something?
└── Use a real signature scheme (Ed25519 / RS256); do not assemble hashes