#Why this matters
File downloads, Git commits, TLS certificates, and login systems all rely on cryptographic hashing. Confusing hashing with encryption or encoding, or storing passwords with raw SHA-256 or MD5, turns a single read-only database leak into rapid mass account takeover.
#Hash properties and how hashing differs from encryption
A cryptographic hash H(x) maps any input to a fixed-size digest without a key. Secure hashes satisfy four properties:
- Pre-image resistance (one-way): Given
h, findingxwithH(x) = his infeasible. - Second pre-image resistance: Given
x, findingx2 != xwithH(x2) = H(x)is infeasible. - Collision resistance: Finding any distinct
xandywithH(x) = H(y)is infeasible. - Avalanche effect: Flipping one input bit changes roughly 50% of the output bits.
| Property | Hash (SHA-256) | Encryption (AES) | Encoding (Base64) |
|---|---|---|---|
| Reversible? | No (one-way) | Yes (with key) | Yes (no key) |
| Key needed? | No | Yes | No |
| Output size | Fixed (256 bits) | Proportional | Proportional |
| Primary goal | Integrity | Confidentiality | Formatting |
Verify the avalanche effect at the bit level (using XOR, not hex character matching) in OpenSSL and Python 3:
$ printf 'hello' | openssl dgst -sha256
SHA2-256(stdin)= 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
$ printf 'Hello' | openssl dgst -sha256
SHA2-256(stdin)= 185f8db32271fe25f561a6fc938b2e264306ec304eda518007d1764826381969
$ python3 -c "import hashlib; a=hashlib.sha256(b'hello').digest(); b=hashlib.sha256(b'Hello').digest(); d=sum(bin(x^y).count('1') for x,y in zip(a,b)); print(f'Differing bits: {d}/256 ({d/256*100:.1f}%)')"
Differing bits: 125/256 (48.8%)#Choosing a hash algorithm in 2026
Both MD5 (collisions since 2004) and SHA-1 (SHAttered 2017, chosen-prefix 2020) are cryptographically broken and must never be used for security or adversarial integrity checks.
| Algorithm | Output | Hex length | 2026 Status | Recommended use |
|---|---|---|---|---|
| MD5 | 128 bits | 32 hex | Broken | Non-security legacy checksums only |
| SHA-1 | 160 bits | 40 hex | Broken / Deprecated | Never use for security |
| SHA-256 / SHA-512 | 256 / 512 bits | 64 / 128 hex | Secure | File integrity, TLS, signatures, HMAC |
| SHA-3 (Keccak) | 256 / 512 bits | 64 / 128 hex | Secure | Sponge-based alternative to SHA-2 |
| BLAKE2b / BLAKE2s | 256 / 512 bits | 64 / 128 hex | Secure | High-speed integrity, Argon2, WireGuard |
In CTF challenges, digest format identifies the algorithm: 32 hex chars = MD5, 40 hex = SHA-1, 64 hex = SHA-256, $2b$12$... = bcrypt, and $argon2id$... = Argon2id.
#Password storage: salts and memory-hard KDFs
Fast hashes like SHA-256 run at billions of guesses per second on GPUs. Without a unique random salt per user, identical passwords produce identical digests, enabling instant rainbow table lookups.
- Billions of hashes per second on GPUs
- No built-in salt; identical inputs match
- Built for file integrity, HMAC, signatures
- Deliberately slow and tunable (time / memory)
- Generates a unique random salt per user
- Built to resist GPU cracking and rainbow tables
- RegisterGenerate unique random salt for the user
- HashRun Argon2id or bcrypt over (password, salt)
- StoreSave encoded string with parameters, salt, and digest
- VerifyRe-hash login input with stored salt and compare in constant time
OWASP recommends Argon2id first because it combines time cost with memory hardness (e.g. memory_cost=65536 KiB, or 64 MB) to resist GPU and side-channel attacks. scrypt is also memory-hard; bcrypt remains acceptable for existing systems (4 KB memory, 72-byte input limit). Where FIPS modules are required, use PBKDF2-HMAC-SHA256 with at least 600,000 iterations per OWASP guidance.
import hmac, hashlib
from argon2 import PasswordHasher
ph = PasswordHasher(time_cost=3, memory_cost=65536, parallelism=2, hash_len=32)
stored_hash = ph.hash("correct-horse-battery-staple")
ph.verify(stored_hash, "correct-horse-battery-staple")
def verify_webhook(key: bytes, message: bytes, received_mac: bytes) -> bool:
expected = hmac.new(key, message, hashlib.sha256).digest()
return hmac.compare_digest(expected, received_mac)#HMAC and message authentication
A plain hash only checks accidental corruption. HMAC (RFC 2104) combines a hash with a secret key K: HMAC(K, M) = H((K XOR opad) || H((K XOR ipad) || M)). Without encrypting M, HMAC verifies both integrity and authenticity, and stops length-extension attacks that break naive SHA-256(secret || message). Always compare MACs with hmac.compare_digest rather than == to prevent timing attacks.
#Defending / Common mistakes
- Using MD5 or SHA-1 for security. Migrate all integrity checks and signatures to SHA-256, SHA-512, SHA-3, or BLAKE2.
- Storing passwords with fast hashes or a static salt. Use Argon2id, scrypt, or bcrypt so every user receives a unique random salt automatically.
- Building a custom MAC as
SHA-256(secret + message). Use standardhmac.new(key, message, hashlib.sha256)to avoid length-extension attacks. - Comparing MACs with
==. Always use constant-timehmac.compare_digest.
#Summary
- Cryptographic hashes are keyless, one-way, fixed-size functions with pre-image, second pre-image, collision resistance, and the avalanche effect.
- MD5 and SHA-1 are broken; use SHA-256, SHA-512, SHA-3, or BLAKE2 for integrity.
- Store passwords with salted, memory-hard KDFs like Argon2id (or scrypt/bcrypt), never fast hashes.
- Use HMAC with
hmac.compare_digestfor keyed integrity and authenticity without encryption.