Bookmarks

Hash function

A function that maps data of any length to a fixed-length string so that the input cannot be recovered from the result, and the smallest change in the data changes the result completely.

Also: hash algorithm, cryptographic hash function, hashing function

checked 9 Nov

All terms

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:

bashOne letter of input, a completely different hash
echo -n 'cat'  | sha256sum77af778b51abd4a3c51c5ddd97204a9c3ae614ebccb75a606c3b6865aed6744e echo -n 'cats' | sha256sumd936608baaacc6b762c14b0c356026fba3b84e77d5b22e86f2fc29d3da09c675

How 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

Verified 9 Nov

Not to be confused with

what it is mixed up with and how it differs

  • UTF-8UTF-8 encoding

    UTF-8 is an encoding: it converts text to bytes and back, without loss. A hash function converts bytes into a short fingerprint from which the original text cannot be recovered.

  • E2EEend-to-end encryption

    In end-to-end encryption, data can be decrypted with a key; a hash can never be decrypted, since it is used only for verification.

In depth in the references

where the topic is covered in detail