Lessons 1/4 · 25 min
The digest: a fixed-size fingerprint of content
How do you check 700 MB without rereading them?
Awa downloads a 700 MB program. The site says: "SHA-256: ba78…". How can she know the file received is exactly the one published, without comparing it byte by byte with an original she does not have? She computes a digest (also called a hash) of the file received and compares it to the published digest: 64 characters to compare instead of 700 million bytes.
What is a hash function?
A hash function takes data of any size (a sequence of bytes) and returns a value of fixed size. SHA-256, from the SHA-2 family defined by the FIPS 180-4 standard, always returns 256 bits, i.e. 32 bytes. They are written in hexadecimal (base 16: digits 0 to 9 then letters a to f), where each character represents 4 bits: 256 ÷ 4 = 64 characters.
| Input (text, UTF-8 encoded) | SHA-256 digest (64 hexadecimal characters) |
|---|---|
| (empty text) | e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 |
abc | ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad |
hello | 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 |
Type abc into the widget below: you should find the second row. A 700 MB file, too, gives 64 characters.
Two families of "hash" functions
In workshop 102, a hash table's hash function serves to spread keys over buckets: it just needs to be fast and well spread. A cryptographic hash function must also resist someone who is actively trying to break it. The first is used for data structures, the second for security. When we say "hash" without qualification in the rest of the curriculum, we mean the second.
A beginner's trap: what you really hash
You hash bytes. For a text, these are the bytes of its encoding (here UTF-8). An extra space or a hidden line break changes the digest:
printf 'hello' | sha256sum
# 2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824
echo hello | sha256sum
# 5891b5b522d5df086d0ff0b110fbd9d21bb4fc7163af34d08286a2e846f6be03The second command hashes hello followed by a line break (the echo command adds one). It is not the same data, hence not the same digest. When two people do not find the same digest for "the same text", it is almost always an invisible difference of this kind.
What is a digest for?
- Checking the integrity of a file downloaded or copied (lesson 4).
- Identifying content by its digest: the Git version-control system names each object this way.
- Committing to data without revealing it yet: publish the digest, reveal the data later.
- Linking blocks: each block contains the previous block's digest (lesson 4, then the blockchain workshops).
What this does not guarantee
- A digest alone does not say who produced the file: anyone can compute the SHA-256 of anything. Proving the author takes a signature (workshop 105).
- It does not necessarily hide the data (lesson 3).
- Two files with equal digests are, in practice, identical; the converse "different digests ⇒ different files" is, by contrast, absolutely certain.
Recap
Hash function: bytes of any size → fixed-size digest (SHA-256: 256 bits, 64 hexadecimal characters). It applies to bytes, to the character. The digest identifies content, but does not identify its author.
Interactive tool
SHA-256 digest
Type some text: the digest updates on every keystroke. Compare with a second text to see the avalanche effect.
Digest (64 hex characters)
…Digest of the second text
…Differing characters are underlined and bold. Text is UTF-8 encoded with no space or newline added.
