A hash function turns input of any length into a fixed-length fingerprint. The same input always produces the same output, but the output cannot practically be turned back into the input. That one-way property makes hashes useful for integrity checks, password storage and message authentication — provided you choose the right construction for the job.
Properties that matter
A cryptographic hash is expected to have:
- Determinism. The same bytes produce the same digest.
- Preimage resistance. Given a digest, finding any input that produces it is impractical.
- Second-preimage resistance. Given one input, finding a different input with the same digest is impractical.
- Collision resistance. Finding any two inputs with the same digest is impractical.
- Avalanche behaviour. A one-bit input change should change roughly half the output bits.
A useful consequence of determinism is that hashes can detect accidental changes. It is not a consequence that hashes prove who created a file. For that, you need a signature or a MAC with a secret key.
Common algorithms
| Algorithm | Output | Status |
|---|---|---|
| MD5 | 128 bits | Broken for security; collisions are easy to generate |
| SHA-1 | 160 bits | Broken for collision resistance |
| SHA-256 | 256 bits | Recommended for integrity and signatures |
| SHA-512 | 512 bits | Recommended, faster on some 64-bit platforms |
| SHA-3 | 224–512 bits | Standardized alternative construction |
| BLAKE3 | 256 bits | Fast modern hash, less universally available |
MD5 and SHA-1 still appear in old checksum files and non-security contexts. Do not use them where an attacker benefits from a collision, and do not use either for password storage.
Checksums versus authenticity
A checksum answers “did these bytes change?” It does not answer “who produced these bytes?” If an attacker can replace both the file and the checksum, a matching hash proves nothing.
For downloads, publish the digest over a channel you trust, or sign it. For internal integrity checks, compare the full digest rather than a truncated prefix. A short prefix is fine for human spot checks but weak against deliberate substitution.
Password hashing is different
A fast hash such as SHA-256 is designed to process data quickly. That is exactly the wrong property for passwords, because an attacker can try billions of guesses per second on a GPU.
Password storage needs a deliberately slow, memory-hard function with a unique salt per password:
- Argon2id is a modern default where available.
- scrypt is a solid memory-hard option.
- bcrypt is widely supported and still acceptable when configured with a sufficient cost.
- PBKDF2-HMAC-SHA-256 is acceptable where required by compliance, with a high iteration count.
A salt prevents identical passwords from producing identical stored values and defeats precomputed tables. It does not make a weak password strong. Neither does pepper, although a secret pepper stored outside the database adds defence in depth.
Never store a password encrypted or hashed with a general-purpose fast hash. Never compare password hashes with a normal string comparison in production code; use the library’s constant-time verification function.
HMAC and signatures
A hash by itself has no key. An HMAC combines a hash with a secret key to produce a message authentication code: anyone with the key can verify that the message was not modified.
HMAC is appropriate for:
- Verifying webhook payloads.
- Signing API requests with a shared secret.
- Deriving keys in a protocol.
HMAC is not a digital signature. A signature uses a private key to sign and a public key to verify, which allows verification without sharing the signing secret. JWT libraries support both HMAC (HS256) and public-key algorithms (RS256, ES256); the choice changes the trust model. See the JWT guide for the verification pitfalls.
Encoding the digest
Digests are bytes. They are usually displayed as hexadecimal or Base64:
- Hex is longer but easy to read and compare.
- Base64 is shorter but case-sensitive and can include
+and/. - Base64URL replaces those characters so the value can travel in a URL or token.
When comparing digests, compare the same encoding and the same algorithm. A 64-character hex string is usually SHA-256; a 32-character hex string is usually MD5. Do not assume from length alone.
Practical uses
- File integrity. Generate a SHA-256 digest and compare it after transfer.
- Cache keys. Hash a long input to a fixed-size key, accepting that collisions must be handled.
- Content addressing. Name a blob by its digest so identical content deduplicates.
- Data anonymization. Hashing an email address produces a stable pseudonym, but low-entropy inputs can be brute-forced. Treat it as pseudonymization, not anonymity.
A short checklist
- Use SHA-256 or stronger for integrity and signatures.
- Use Argon2id, scrypt or bcrypt for passwords.
- Salt every password and use a constant-time verifier.
- Use HMAC when both sides share a secret; use signatures when they do not.
- Compare full digests, not truncated prefixes, for security decisions.
- Remember that a hash is not encryption: there is no key and no way back.
The hash generator computes MD5, SHA-1, SHA-256 and SHA-512 locally, including for files. To create a strong random password rather than a hash, use the password generator.
Further reading: NIST FIPS 180-4 for SHA-2, NIST SP 800-63B for password handling, and the OWASP Password Storage Cheat Sheet.