OCR H446, section 1.3.1 (d)
What is hashing?
A hash function takes data of any size (a password, a file, a message) and produces a fixed-size value called a hash (or digest, or hash value).
Whatever the input's length, SHA-256 always gives a 256-bit hash (64 hexadecimal digits). Changing a single letter, from hello to Hello, gives a completely different hash.
Properties of a good hash function
- Deterministic: the same input always gives the same hash.
- Fixed length output, however large the input is.
- Fast to calculate.
- One-way: it is practically impossible to work backwards from the hash to the original data.
- Small change, big difference: a tiny change in the input changes the hash completely (the avalanche effect).
- Few collisions: it should be very hard to find two different inputs with the same hash. A collision is when two inputs give the same hash.
Hashing is not encryption. Encryption is reversible with the key, so the original can be recovered. Hashing is one-way, and the original can't be recovered from the hash.
Uses of hashing
1. Storing passwords
A website should never store passwords as plain text. Instead it stores the hash.
- When you register, the system hashes your password and stores only the hash.
- When you log in, it hashes what you typed, and compares it with the stored hash. If they match, the password is right.
- If the database is stolen, the attacker only gets hashes, not the passwords, and can't reverse them.
Salting: a random value called a salt is added to each password before hashing. Then two users with the same password get different hashes, and attackers can't use ready-made tables of hashes of common passwords ("rainbow tables").
2. Checking the integrity of data
A checksum or hash of a file can be published with it. After downloading, you hash the file and compare with the published value.
- If they are the same, the file hasn't been changed or corrupted in transit.
- If they differ, the file has been altered or damaged.
This is used for software downloads, backups and detecting tampering.
3. Digital signatures
To sign a message, the sender hashes the message, then encrypts the hash with their private key. The receiver decrypts it with the public key, hashes the message again, and compares. A match shows the sender's identity and that the message is unchanged. A hash is used because it is short, so only a small value needs the slow asymmetric encryption.
4. Hash tables (fast lookup)
A hash table uses a hash function to turn a key into a position in an array, so that data can be stored and found directly, with no searching (see 1.4.2).
If two keys give the same position, that is a collision, and it must be handled (for example by using the next free slot). Notice that cat and act give the same hash with this simple function, which shows why a better hash function matters.
5. Other uses
- Identifying and comparing files: two files with the same hash are almost certainly identical, which is used to find duplicates.
- Blockchain and cryptocurrencies: each block contains the hash of the previous one, so changing any block would break the chain.
- Caching and data deduplication.
Worked example
Task: a website stores the SHA-256 hash of each password. A user forgets their password. Why can't the site send them their old password, and what should it do?
- The site only has the hash, which is one-way, so it can't recover the original password. (That is deliberate: it means that a stolen database doesn't reveal the passwords.)
- It should let the user set a new password, hash it (with a salt), and store the new hash.
- If a site can email you your password, it is storing it in a way that can be read, which is a security weakness.
Exam tip: stress that hashing is one-way, gives a fixed-length output, and the same input always gives the same hash. Give specific uses (passwords, integrity checks, signatures, hash tables), and mention salting for passwords. Don't confuse hashing with encryption.
Check yourself
What is a hash function?
A function that takes data of any size and produces a fixed-size value (the hash). The same input always gives the same hash, and it can't practically be reversed.
Why are passwords stored as hashes and not as plain text?
If the database is stolen, the attacker only gets hashes, which can't be reversed to reveal the passwords.
What is the difference between hashing and encryption?
Encryption is reversible: the key can recover the original data. Hashing is one-way, and the original can't be recovered from the hash.
How can a hash show that a downloaded file has not been changed?
Calculate the hash of the downloaded file and compare it with the hash published by the source. If they match, the file is unchanged.