Sub topic 1
What a hash function does
Prerequisites
This chapter assumes you can read basic Python, and that you know what binary and hexadecimal notation are. You should also know what an array is and have used a dictionary (a map from keys to values). Bitwise operations and the modulo operator appear in later chapters, not in this one.
The definition
A hash function takes input of any length and returns a value of fixed size. The input is often called the key or message. The output is called the hash value, digest, or hash code.
Python's built-in hashlib shows the idea. Short and long inputs both produce 256 bits.
import hashlib
for text in [b"a", b"hello world", b"x" * 1_000_000]:
digest = hashlib.sha256(text).hexdigest()
print(len(text), "bytes in ->", len(digest) * 4, "bits out")The output is always 256 bits, whether the input is one byte or a million.
The core guarantee: determinism
A hash function must be deterministic. The same input always gives the same output. Everything else in this tutorial depends on this. A hash table looks up a key by recomputing its hash. A file checker compares a stored hash with a fresh one. If the function returned different values for the same input, both would fail.
import hashlib
a = hashlib.sha256(b"hello").hexdigest()
b = hashlib.sha256(b"hello").hexdigest()
print(a == b) # True, on every run and every machineDeterminism has a scope. It holds for a given function with the same settings. Some hashes are deliberately seeded with a random value per process, so they are deterministic within one run but differ between runs. Python's built-in hash() for strings works this way. A later chapter explains why.
print(hash("hello")) # changes each time you start PythonDo not store the result of hash() in a file or database and expect to match it later.
The flow of data
The diagram shows the shape of the idea. Many possible inputs go in, and a small fixed set of outputs comes out.
What a hash function does not promise
Determinism is the only guarantee every hash function makes. Other properties are goals that depend on the job:
| Property | Needed for |
|---|---|
| Even spread of outputs | Hash tables |
| Speed | Hash tables, checksums |
| Hard to reverse | Security uses |
| Hard to find two inputs with the same output | Security uses |
A function that returns 0 for every input is deterministic and has a fixed-size output, so it technically qualifies as a hash function. It is useless in practice, which is why later chapters define what makes a hash good.
def constant_hash(data: bytes) -> int:
return 0
print(constant_hash(b"anything"), constant_hash(b"something else"))Common errors
- Assuming equal hashes mean equal inputs. Different inputs can share a hash, as the chapter on collisions shows. Equal hashes only suggest equal inputs.
- Assuming different hashes might still mean equal inputs. This cannot happen. If the hashes differ, the inputs differ, because the function is deterministic.
- Relying on a hash value that changes between runs, such as Python's
hash()on strings.
The second point is the useful one. A hash can prove two inputs are different, but it can only suggest that they are the same.
Exercise
Two files produce different SHA-256 digests. Can they have identical contents? Two other files produce identical digests. Can you be sure their contents are identical?
Show answer
No to the first, because a deterministic function gives identical contents the same digest, so different digests prove different contents. Not strictly to the second, because different inputs can collide. With SHA-256, a collision is so unlikely that identical digests are treated as identical contents in practice.