Hashing vs. Encryption

Difficulty: Beginner

Question

What is the difference between hashing and encryption, and when would you use each?

Answer

Hashing is a one-way function that turns any input into a fixed-size digest; you cannot get the original back, only check whether another input produces the same digest. Encryption is two-way: data is scrambled with a key and can be decrypted by someone holding the right key. Use hashing when you only need to verify something (passwords, file integrity) and encryption when you need the original data back later (card details, Aadhaar numbers, messages).

Side-by-side

AspectHashingEncryption
Reversible?NoYes, with the key
Key involved?No (HMAC uses a key for authentication)Yes
Output sizeFixed (SHA-256 is always 256 bits)Roughly the size of the input
Main goalIntegrity and verificationConfidentiality
ExamplesSHA-256, SHA-3, BLAKE3; for passwords Argon2id, bcrypt, scryptAES-256-GCM, ChaCha20-Poly1305, RSA, ECIES
Typical useStoring passwords, checksums, deduplication, digital signaturesHTTPS traffic, encrypting PII at rest, disk encryption

Properties of a good cryptographic hash

  • Deterministic: same input, same output.
  • Pre-image resistant: given a hash, you cannot find an input that produces it.
  • Collision resistant: you cannot find two inputs with the same hash. MD5 and SHA-1 have practical collisions and should not be used for security.
  • Avalanche effect: changing one bit of input changes about half the output bits.

Symmetric vs asymmetric encryption

  • Symmetric (AES, ChaCha20): one shared key encrypts and decrypts. Fast, used for bulk data.
  • Asymmetric (RSA, elliptic curve): a public key encrypts or verifies, a private key decrypts or signs. Slower, used for key exchange and signatures.

TLS uses both: asymmetric cryptography to agree on a key, then symmetric encryption for the actual traffic. Prefer authenticated encryption modes like AES-GCM, which detect tampering, over older modes like AES-CBC without a MAC. Never use ECB mode.

Where people go wrong

  • Encrypting passwords. If the key leaks, every password is exposed. Passwords should be hashed with a slow, salted algorithm like Argon2id or bcrypt.
  • Hashing passwords with SHA-256. It is designed to be fast, which makes offline guessing cheap. General-purpose hashes are for integrity, not passwords.
  • Confusing encoding with either. Base64 is encoding; it provides zero security. A JWT payload is Base64URL-encoded and readable by anyone.
  • Hashing data you later need. If support staff need to see the last four digits of a card, store those separately or encrypt the field; a hash will not help.
  • Key management. Encryption is only as strong as key storage. Keep keys in a KMS (AWS KMS, GCP KMS, HashiCorp Vault), not in the same database as the encrypted data.

Code examples

Hash vs encrypt in Node.js

import crypto from 'node:crypto';

// Hashing: one-way, fixed length
const digest = crypto.createHash('sha256').update('hello').digest('hex');
console.log(digest.length); // 64 hex chars = 256 bits

// Encryption: AES-256-GCM, reversible with the key
const key = crypto.randomBytes(32);   // in production: from a KMS
const iv = crypto.randomBytes(12);    // unique per message
const cipher = crypto.createCipheriv('aes-256-gcm', key, iv);
const ct = Buffer.concat([cipher.update('4111-xxxx', 'utf8'), cipher.final()]);
const tag = cipher.getAuthTag();      // detects tampering

const decipher = crypto.createDecipheriv('aes-256-gcm', key, iv);
decipher.setAuthTag(tag);
const pt = Buffer.concat([decipher.update(ct), decipher.final()]).toString();
console.log(pt); // '4111-xxxx'

The hash cannot be reversed, while the GCM ciphertext decrypts back to the original and the auth tag fails decryption if anything was modified.

Key points

  • Hashing is one-way and fixed-size; encryption is reversible with a key.
  • Use hashing for verification and integrity, encryption for confidentiality.
  • Passwords need slow, salted hashes (Argon2id, bcrypt), not encryption and not plain SHA-256.
  • Base64 is encoding, not security.
  • Prefer authenticated encryption like AES-GCM and store keys in a KMS.

Common follow-up questions

  • Why should passwords be hashed rather than encrypted?
  • What is an HMAC and how is it different from a plain hash?
  • How do symmetric and asymmetric encryption work together in TLS?
  • Why are MD5 and SHA-1 considered broken?

Interview tips

  • Open with the one-line distinction (one-way vs reversible with a key), then give one use case each.
  • Mention encoding as a third, separate concept; interviewers often probe this confusion.
  • Naming Argon2id or bcrypt for passwords and AES-GCM for data shows practical knowledge.

Concepts covered

Hashing, Symmetric Encryption, Asymmetric Encryption, AES-GCM, Argon2, Encoding

More Web Security (OWASP, JWT, XSS, CSRF) questions