Hashing vs Encryption Algos

A cryptographic hash function takes arbitrary input—like a password, message, or file—and produces a fixed-length digest. The key idea is that it’s a one-way function: easy to compute, but computationally infeasible to reverse.”

“From a security perspective, good hash functions are deterministic, collision-resistant, and have the avalanche effect—so even a one-character change completely changes the output.”

“In practice, older algorithms like MD5 and SHA-1 are broken due to collision attacks, so today we rely on SHA-256 or SHA-512 from the SHA-2 family.”

“One important distinction is that hashing is not encryption. Hashing is one-way and is used for integrity and authentication, while encryption is reversible and used for confidentiality.”

“For passwords specifically, we don’t use raw SHA-256 or SHA-512 because they’re too fast. Instead, we use dedicated password-hashing algorithms like bcrypt, scrypt, or Argon2, which are intentionally slow and salted to resist brute-force and rainbow-table attacks.”

“So overall, hashes are about verifying data hasn’t changed and proving knowledge of a secret, not about hiding data


Cryptographic Hash Functions — Senior Interview Notes

What is a cryptographic hash?

A cryptographic hash function maps any input (text, file, password) to a fixed-length output called a hash/digest.

Mental model:

👉 A one-way fingerprint for data.


Core properties (must-know)

A secure hash function is:

  1. Deterministic – same input → same hash

  2. Fixed output size – independent of input length

  3. Fast to compute – efficient for integrity checks

  4. Pre-image resistant – hash → original input is infeasible

  5. Collision resistant – hard to find two inputs with same hash

  6. Avalanche effect – tiny input change → totally different hash

Example:

"hello" → 2cf24dba...
"Hello" → 185f8db3...

Common hash algorithms (what to say in interviews)

MD5

SHA-1

SHA-256 (SHA-2)

SHA-512 (SHA-2)


Quick comparison

Algorithm Output Security Interview Status
MD5 128-bit ❌ Broken Never use
SHA-1 160-bit ❌ Broken Deprecated
SHA-256 256-bit ✅ Secure Recommended
SHA-512 512-bit ✅ Secure Recommended

Hashing vs Encryption (very important)

Hashing Encryption
One-way Two-way
Cannot be reversed Can be decrypted
Passwords, integrity Sensitive data

Examples:


Password hashing (senior-level clarity)

Never store passwords with raw SHA-256 / SHA-512

Use password-hashing algorithms instead:

Why?


One-line takeaway (perfect interview closer)

A cryptographic hash is a fast, one-way, collision-resistant fingerprint used for integrity and authentication—not secrecy.


🔐 JWT-Specific Explanation (Add-on Answer)

A JWT is Base64URL-encoded, which means it is readable by anyone who has the token.

“In JWTs, hashing is used as part of the digital signature, not for encrypting the payload.”

“A JWT has three parts: header, payload, and signature. The header and payload are Base64-encoded, not encrypted, meaning they’re readable.”

“The signature is created by hashing the header and payload together using an algorithm like HMAC-SHA256 (for symmetric keys) or RSA/ECDSA with SHA-256 (for asymmetric keys).”

“When the server receives a JWT, it recomputes the hash and compares it to the signature. If they match, we know the token hasn’t been tampered with and was issued by a trusted authority.”

“So hashing in JWTs provides integrity and authenticity, not confidentiality. If you need confidentiality, you’d use JWE instead of JWS.”


🎯 One-line JWT summary (great for closing)

JWT uses hashing for signature verification to ensure integrity and trust—not to hide data.