Cryptographic hashing
A one-way function mapping data to a fixed-size digest, infeasible to reverse or collide. Used for integrity, password storage (with salt), and content addressing. SHA-256 is the workhorse; bcrypt/argon2 for passwords.
- Password storage (argon2/bcrypt), integrity, content addressing.
- Anything needing one-way verification.
- Reversible encryption (use symmetric crypto).
- Fast lookups (use a hash map, not a crypto hash).
Definition
A hash function maps arbitrary input to a fixed-size digest deterministically, with avalanche (small input change → big digest change), one-wayness, and collision resistance. SHA-2/SHA-3 for general use; slow hashes (argon2, bcrypt) for passwords.
Why it exists
You need to verify integrity without storing the original, store passwords without storing them, and address content by its digest. Hashing enables all of these without reversible encryption.
How it works
For integrity, compare digests. For passwords, hash with a per-user salt and a slow, memory-hard function (argon2) to resist brute force. For addressing (git, CAS), the digest is the key. Hash maps use fast non-crypto hashes (xxHash).
Scaling limits
Slow password hashes add latency per login (intended). Collision resistance weakens over time (MD5/SHA-1 broken). Memory-hard hashes resist GPUs but need tuning.
Cost shape
Negligible for integrity. Password hashing cost is intentional (tune work factor). Blow-ups: too-slow hashes hurting login throughput.
Alternatives
- Encryption — Reversible, but needs key management.
- MAC/HMAC — Keyed integrity/authentication, not just hashing.
Failure modes
- Using fast hashes (MD5/SHA-1) for passwords.
- No salt (rainbow tables).
- Broken hash algorithms.