Skip to content

Hashing

1.3.1 Compression, encryption and hashing · Paper 1 · 4 flashcards · 3 questions

Hashing is fundamentally different from encryption - it's a one-way process designed to create a unique "fingerprint" of data, not to hide it.

Definition
Hashing is a one-way function that converts input data of any size into a fixed-size string (the hash or digest). It cannot be reversed - you cannot recover the original data from the hash.
Key Point
Properties of a Good Hash Function:
• Deterministic - Same input always produces same hash
• Fast - Quick to compute the hash
• One-way - Cannot reverse to find original input
• Avalanche effect - Small input change = completely different hash
• Collision resistant - Hard to find two inputs with same hash

Use 1: Password Storage

Worked Example
Worked Example: Password Hashing

Why not store passwords directly?
If database is stolen, attacker sees all passwords in plain text!

Correct approach - store hashes:

Registration:
1. User creates password: "MyP@ssw0rd"
2. System hashes it: hash("MyP@ssw0rd") = "a7b9c3d2e1..."
3. Only the hash is stored in database

Login:
1. User enters password: "MyP@ssw0rd"
2. System hashes the attempt: hash("MyP@ssw0rd") = "a7b9c3d2e1..."
3. Compare with stored hash: Match! User authenticated.

If database is stolen:
• Attacker sees: "a7b9c3d2e1..."
• Cannot reverse to get "MyP@ssw0rd"
• Would need to try hashing millions of guesses

Use 2: Data Integrity

Worked Example
Worked Example: Verifying File Integrity

Scenario: You download a large software file

Step 1: Website publishes file AND its hash
• File: "software.exe" (500 MB)
• Published hash: "5d41402abc4b2a76..."

Step 2: You download the file

Step 3: You hash your downloaded file
• Your hash: "5d41402abc4b2a76..."

Step 4: Compare hashes
• Hashes match = file is intact, unchanged
• Hashes differ = file corrupted or tampered with

Use 3: Hash Tables

Definition
A hash table is a data structure that uses hashing to enable fast data retrieval. The hash of a key determines where its value is stored.
Key Point
Hash Table Performance:
• Average case: O(1) lookup, insert, delete
• Hash function maps key to array index
• Collisions occur when different keys hash to same index
• Collision handling: Chaining (linked lists) or Open Addressing (probe for empty slot)

Encryption vs Hashing

Key Point
Direction
• Encryption: Two-way (encrypt/decrypt)
• Hashing: One-way (cannot reverse)

Output Size
• Encryption: Variable (depends on input)
• Hashing: Fixed (always same length)

Purpose
• Encryption: Confidentiality (hide data)
• Hashing: Integrity/verification

Key Required
• Encryption: Yes
• Hashing: No

Can Recover Original
• Encryption: Yes (with key)
• Hashing: No (by design)
Exam Tip
Remember: Encryption is for HIDING data (confidentiality). Hashing is for VERIFYING data (integrity) and secure storage. They serve different purposes!