The most common mistake in cybersecurity interviews: "I encrypted the password database." No, you didn't. You (hopefully) Hashed it. The fundamental difference is reversibility. Encryption is designed to be reversed (decrypted) if you have the key. Hashing is designed to be a one-way street.

Encryption

- Reversible: Key -> Plaintext. - Purpose: Confidentiality (Hiding data). - Output Size: Variable (Longer input = Longer output). - Example: AES, RSA.

Hashing

- One-Way: You cannot get the input back from the hash. - Purpose: Integrity (Verifying data). - Output Size: Fixed (SHA-256 is always 64 chars). - Example: SHA-256, MD5 (Broken), bcrypt.

1. Why Hash Passwords?

If a database is leaked:
- Encrypted: Hackers just need to better hack the server to find the key. Then they have all passwords.
- Hashed: Hackers have `a5d3...`. They cannot reverse it to "password123". They must guess "password123", hash it, and see if it matches. This is slow.

2. Collisions (The Pigeonhole Principle)

Since the input can be infinite (a whole book) and the output is fixed (256 bits), eventually, two different inputs MUST produce the same hash. This is a Collision.
MD5 is broken because it is now easy to generate collisions. SHA-256 is currently safe.

3. Salting & Peppering

To stop hackers from using Rainbow Tables (pre-calculated lists of hashes for common passwords), we add a Salt.
Hash("password123") = Common Hash (Bad).
Hash("password123" + "RandomSaltUser1") = Unique Hash (Good).
Even if two users have the same password, their hashes in the database will be different.