Hashing is fundamentally different from encryption - it's a one-way process designed to create a unique "fingerprint" of data, not to hide it.
• 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
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
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
• 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
• 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)