A fingerprint for data
A cryptographic hash function takes any input, whether a single word, a photo or a whole library of files, and produces a fixed-length string of characters called a hash, digest or fingerprint. A widely used function such as SHA-256 always produces 256 bits of output, which is typically written as 64 hexadecimal characters, no matter how large the input is.
The result looks random, but it is not: the same input always gives exactly the same hash. That predictability is what makes a hash useful as an identifier for content.
The properties that matter
Good cryptographic hash functions are designed to have several properties.
- Deterministic. The same input always produces the same output.
- Fast to compute. Hashing even a large file takes little time on ordinary hardware.
- One-way. Given only a hash, there is no practical way to work out the original input, other than guessing inputs and checking.
- Avalanche effect. Changing one character, or even a single bit, of the input changes the output so completely that the new hash looks unrelated to the old one.
- Collision resistant. It should be practically impossible to find two different inputs that give the same hash. Because outputs are a fixed size and inputs are unlimited, collisions must exist in theory, but finding one should be infeasible for a well-designed function.
If a function loses these properties, it is considered broken. Some older functions, such as MD5 and SHA-1, have had practical weaknesses found and are no longer recommended for security purposes.
A simple picture
Imagine a machine that reads any document and prints a short code on a slip. Feed the same document in tomorrow and you get the same slip. Change a comma, and the slip is entirely different. Crucially, you cannot take the slip and rebuild the document. You can only use it to check whether a document matches.
Use 1: checking that data has not changed
Software download pages often publish a hash next to a file. After downloading, you compute the hash of your copy and compare. If they match, your copy is identical to the original; if not, the file was corrupted or altered along the way. Note that this only helps if the published hash itself comes from a trustworthy source.
The same idea underlies file integrity checks in backups, which relates to data backups, and in detecting duplicate files.
Use 2: protecting passwords
Well-run services avoid storing your actual password. Instead they store a hash of it. At sign-in, they hash what you typed and compare the results. If the database leaks in one of the data breaches in the news, the attackers get hashes rather than plain passwords.
This is not perfect protection. Attackers can guess common passwords, hash each guess, and compare. Careful systems therefore add a random value called a salt to each password and use deliberately slow, specialized password-hashing functions, rather than a plain fast hash. Using a long, unique password from a password manager makes guessing impractical.
Use 3: digital signatures
Signing a large document directly with a private key would be slow, so systems sign its hash instead. Anyone can recompute the hash and verify the signature, which proves the document is unchanged and came from the key holder. This is part of the picture in public key cryptography and in how HTTPS encryption certificates are checked.
Use 4: linking blocks in a blockchain
Hashes are the glue of a ledger. Each block contains the hash of the previous block. If any old data is altered, that block’s hash changes, which no longer matches what the next block recorded, and the break is visible to everyone. The mechanics are laid out in the step-by-step walkthrough of blockchain basics.
Hashes are also used to summarize all the transactions in a block. A structure called a Merkle tree combines transaction hashes in pairs, then hashes those pairs, and so on until one top hash remains. A single top-level hash can then represent thousands of transactions, and a person can prove that one particular transaction is included without downloading the whole block.
In proof of work, miners repeatedly hash block data with different trial numbers until the result meets a target, such as beginning with a certain number of zeros. Since hashes cannot be predicted, the only strategy is brute-force trying, which is what makes the work costly, a topic touched on in consensus mechanisms.
What hashes do not do
- A hash is not encryption. Encryption can be reversed with a key; hashing is not meant to be reversed.
- A hash cannot, on its own, prove who created a file. For that you need a signature.
- It does not hide short or guessable inputs. If someone hashes a small set of possible values, an attacker can hash all of them and compare.
- It does not prove that the underlying information is true, only that it is unchanged since it was hashed.
Related terms you may see
A checksum is a simpler fingerprint meant to catch accidental errors rather than deliberate tampering. A hash table is a programming structure that uses ordinary hash functions for quick lookup, in the area of database storage and indexing; it does not require cryptographic strength.
Overall, a cryptographic hash is a compact, tamper-revealing summary of data, and a surprising amount of everyday security depends on it.