Definition
A cryptographic hash function is a public algorithm §14.1 that turns an input of any length into a short, fixed-length output called the digest. It is designed so that finding an input for a given digest, or two different inputs with the same digest, would take more computation than anyone can perform. The same input always gives the same digest. The function uses no secret key, so anyone can compute it. SHA-256 §5.2 (Secure Hash Algorithm with a 256-bit output), defined by NIST in the standard FIPS 180-4, is an example.
The idea
You download a 2 GiB installation file and want to know that it is exactly the file its publisher released. Comparing it byte by byte would need a second copy that you already trust. A cryptographic hash function replaces the second copy with a short digest, 32 bytes for SHA-256. The publisher computes the digest of the file once and makes it available through a channel you trust. You compute the digest of your download and compare the two short values.
The picture is a fingerprint. A fingerprint is small, it is easy to take, and in practice it picks out one person. A digest is small, it is fast to compute, and in practice it picks out one input. The picture stops being accurate in two places. First, a digest is not unique: there are far more possible inputs than possible digests, so many different inputs must share a digest. The function is built so that nobody can find two of them. Second, a fingerprint belongs to a person, but a digest belongs only to the bytes. It says nothing about who produced them.
How it works
The parts have fixed names.
The input, often called the message, is any sequence of bytes: a word, a file, a whole disk image.
The digest, also called the hash value or simply the hash, is the output. Its length is fixed by the function, for example 256 bits (32 bytes) for SHA-256, whatever the length of the input.
The function is public, uses no key and involves no randomness. Anyone who has the input can compute the digest, and everyone computes the same one.
What makes a hash function cryptographic is that three tasks are out of reach: finding an input for a given digest; finding, for a given input, a different input with the same digest; and finding any two different inputs with the same digest. To put them out of reach, the function is built so that every bit of the digest depends on every bit of the input in a complicated way. For SHA-256, changing one bit of the input changes about half of the digest bits on average, with no usable pattern.
The standard use is an integrity §1.9 check, a check that data has not been changed.
The producer computes the digest of the object. Call this value the reference digest.
The reference digest reaches the receiver through a channel the attacker cannot change, for example built into software that the receiver already trusts.
The receiver obtains the object through any channel and computes the digest of what arrived.
The receiver compares the two digests. If they differ, the object is not the original. If they are equal, the object is the original, unless someone has managed one of the three tasks.
A cryptographic hash function is also a component of larger constructions. A message authentication code §5.7 (MAC) is a tag computed from a message and a secret key, and the MAC named HMAC §5.8 (hash-based message authentication code) is built from a hash function. A digital signature §5.14 is a value that only the holder of a private key can produce and that anyone can check; most signature schemes compute it over the digest of a message, not over the message itself. A Merkle tree §5.43 hashes digests together so that one digest covers many objects. The security of each construction comes from the whole construction, not from the hash function alone.
Worked example
One bit changes everything. The three ASCII characters abc are the bytes 0x61, 0x62, 0x63. Their SHA-256 digest, written as 64 hexadecimal digits, is
ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
Change the input to abb. The last byte goes from 0x63 to 0x62, a change of one bit. The digest becomes
715edf8ba8729420cd4d1ce85ed61954a9f531f8c548df728c407effe839296d
The two digests differ in 124 of their 256 bits, close to half. Nothing in the second digest shows how small the change was.
Collisions must exist. A toy function with a 2-bit digest has possible digests: 00, 01, 10 and 11. Five different inputs cannot all receive different digests when only four exist, so at least two of the five share one. SHA-256 has possible digests, about , and accepts far more inputs than that, so it has collisions too. What protects it is the cost of finding one.
A check that proves nothing. A server offers a file and, next to it, a small text file that holds the SHA-256 digest of the file. An attacker who controls the server replaces the file with a modified one. The attacker then computes the digest of the modified file, which anyone can do because the function is public and has no key, and replaces the text file as well. A visitor downloads both, computes the digest of the file and finds that it equals the published digest. The check passes and the file is the attacker’s. Nothing about SHA-256 was broken. The reference digest came from the attacker.
Stated precisely
Let be a function that maps every input to a digest of bits. For SHA-256, . is a cryptographic hash function if it is fast to compute and is designed to have three properties.
Preimage resistance §5.3: given a digest that was computed from an input the attacker cannot guess, it is infeasible to find any input with .
Second-preimage resistance §5.4: given an input , it is infeasible to find an input with .
Collision resistance §5.5: it is infeasible to find any two inputs with .
Infeasible means that the best known method needs more computation than any attacker can perform. The amount is stated as a security strength in bits: a strength of bits means about operations. When nothing better than trying inputs is known, a digest of bits gives a strength of bits for the first two properties and only bits for collision resistance. The halving is the birthday bound §5.6: among many digests, any two may be the equal pair. For SHA-256, NIST Special Publication 800-107 Revision 1 (2012) lists 256 bits for preimage resistance, 201 to 256 bits for second-preimage resistance depending on the length of the message, and 128 bits for collision resistance [310].
The three properties are design goals. They are not mathematically proven: for SHA-256 and the other standard functions, confidence rests on the failure of public attacks. Collision resistance implies second-preimage resistance, because an input together with a second preimage of it is a collision.
The integrity check therefore supports exactly one statement. If the reference digest reached the receiver unchanged, and the digest of the received object equals , then the received object is identical, byte for byte, to the object from which was computed, unless an attacker has found a second preimage of that object. If the attacker was able to influence the original object before was computed, the statement also relies on collision resistance.
Limits and common mistakes
A digest is not a secret and not a proof of origin. Whoever can change the object can compute a new, matching digest, as the third example shows. To tie data to the holder of a key, use a message authentication code or a digital signature.
A digest does not hide an input that can be guessed. A 4-digit code has only 10,000 possible values, and an attacker recovers the code from its digest by hashing all 10,000. A hash function adds no unpredictability to its input.
A general-purpose function such as SHA-256 is fast, which makes it the wrong tool for storing passwords. Password storage uses a deliberately slow password-hashing function §15.10 such as Argon2, together with a salt §5.19, a random value stored with each password so that equal passwords do not give equal digests.
Not every hash function is cryptographic. The simple hash functions commonly used inside a hash table §14.7, a data structure for fast lookup, are built for speed, and a checksum §4.32 such as a cyclic redundancy check (CRC) is built to catch accidental errors. Neither is designed to resist someone who constructs colliding inputs on purpose.
The properties can fail as attacks improve. Practical collisions are known for MD5 (Message Digest 5) and for SHA-1 (Secure Hash Algorithm 1), two older functions with 128-bit and 160-bit digests. Neither should be used where collision resistance is needed, for example under a digital signature.
SHA-256 processes its input block by block and outputs its whole final working state as the digest. For SHA-256 and every other function built this way, the digest of a message lets anyone who knows the length of the message compute the digest of a longer message that begins with the original one, without knowing the original. This is the length-extension attack §5.11. Because of it, the digest of a secret key followed by a message is not a safe message authentication code. HMAC exists to do that job correctly.
Questions answered
What does a matching digest establish when an attacker can replace both object and digest?
A matching digest then establishes only that the object and the digest that arrived belong together: the digest was computed from that object. It establishes nothing about whether the object is the original one. A digest is the short fixed-length value that a cryptographic hash function computes from an object. The function is public and has no key, so an attacker who replaces the object can compute the digest of the replacement and replace the digest too. The comparison then succeeds although no weakness of the hash function was used. The match still excludes accidental damage in practice, because random damage to the object or to the digest makes the two disagree.
A matching digest protects against deliberate change only when the reference digest comes from a source the attacker cannot alter: a value stored in software or hardware that is already trusted, a digest protected by a digital signature or a message authentication code, which an attacker without the key cannot produce, or a digest fetched from the publisher over an authenticated connection. With such a reference digest, a match shows that the object is byte for byte the one from which the digest was computed, as long as nobody can find a second input with the same digest. Even then the match does not show that the object is the newest version or that its use is permitted. Those questions need separate checks.
Related entries: Preimage resistance (§5.3), Second-preimage resistance (§5.4), Collision resistance (§5.5), HMAC (§5.8), Merkle trees and authenticated data structures (§5.43), SHA-256 (§5.2), Birthday bound (§5.6), Message authentication code (MAC) (§5.7), Length-extension attack (§5.11), Digital signature (§5.14). Further reading: [7, 310].