A hash function compresses data of any size into a short fixed-length string called a hash, or digest. The original data cannot be recovered from the hash, and changing a single character of the input changes the result completely. That is why hashes are used wherever data must be checked quickly without comparing it in full: checksums, passwords, digital signatures, data structures.
How it works
The function splits the input into blocks and mixes the bits over several rounds to produce a result of a set length. NIST defines a cryptographic hash function by three properties:
- preimage resistance: it is infeasible to find an input from its hash;
- second preimage resistance: for a given input, it is infeasible to find a different input with the same hash;
- collision resistance: it is infeasible to find any pair of different inputs with the same hash.
“Infeasible” here means computationally infeasible: collisions always exist, because there are infinitely many inputs and a finite number of hashes, but in practice they cannot be found.
SHA-256: an example
One of the most widely used hash functions is SHA-256, from the SHA-2 family: a 256-bit digest, which is 64 hexadecimal characters. The input is processed in 512-bit blocks over 64 rounds of computation. Here is how the hash changes when one letter changes:
echo -n 'cat' | sha256sum77af778b51abd4a3c51c5ddd97204a9c3ae614ebccb75a606c3b6865aed6744e echo -n 'cats' | sha256sumd936608baaacc6b762c14b0c356026fba3b84e77d5b22e86f2fc29d3da09c675How hashing differs from encryption
Encryption is a reversible transformation: data is hidden so that it can later be decrypted with a key. Hashing is irreversible: the input cannot be recovered from the hash, and there is no key. So hash functions do not encrypt data, they verify it. A password is not stored; its hash is stored, and the hash of the entered password is compared with it. For passwords, use slow, salted functions such as Argon2id, bcrypt or PBKDF2: fast ones such as SHA-256 and MD5 are unsuitable because brute-forcing them is far too quick. A digital signature also relies on a hash: the document’s digest is signed, not the document itself.
Where hashes are used
- file and download integrity: a checksum, or in Docker and image registries a digest of the form
sha256:…; - password storage: a salted hash instead of the password;
- digital signatures and certificates: the hash of the message is signed;
- hash tables: dictionaries in Python, objects in JavaScript, database indexes;
- content identification: commits in Git, deduplication in storage systems.
Sources
the term is written from these pages- NIST FIPS 180-4 — Secure Hash Standardnvlpubs.nist.gov/nistpubs/FIPS/NIST.FIPS.180-4.pdf(opens in a new tab)
- NIST CSRC — Glossary: Hash functioncsrc.nist.gov/glossary/term/hash_function(opens in a new tab)
- OWASP — Password Storage Cheat Sheetcheatsheetseries.owasp.org/cheatsheets/Password_Storage_Cheat_Sheet.html(opens in a new tab)
Verified 9 Nov