2026-10-23 · Q&A guide

Hashing vs Encryption: Core Differences & Practical Use Cases

Hashing vs encryption are distinct cryptographic primitives; hashing produces a fixed‑size digest for integrity and authentication, while encryption hides data and can be reversed with a key.

Definitions & Basic Properties

A hash function maps an arbitrary input to a fixed‑length output, called a digest. The mapping is deterministic and one‑way: recovering the input from the digest is computationally infeasible. Encryption, in contrast, is a bijective transformation that requires a secret key to revert the ciphertext back to plaintext. The process is designed to be reversible when the key is known.

Mathematical Foundations

Hash algorithms rely on properties such as pre‑image resistance, second‑preimage resistance, and collision resistance. They use compression functions and iterative chaining to produce a digest that appears random. Encryption algorithms are built around invertible operations, often using modular arithmetic, substitution–permutation networks, or block ciphers. Their security hinges on the secrecy of the key and the hardness of key recovery.

When to Use Which

Use hashing when you need to verify data integrity, store passwords securely, or generate unique identifiers. The irreversible nature of a hash guarantees that the original data cannot be reconstructed. Use encryption when you need to protect confidentiality of data at rest or in transit. The reversible property allows authorized parties to recover the original content.

Common Pitfalls

Treating a hash as a reversible cipher leads to vulnerabilities; attackers can perform dictionary or rainbow‑table attacks against weak hashes. Always salt and stretch password hashes with algorithms like Argon2 or PBKDF2. Using encryption for data integrity without a MAC or authenticated encryption mode (e.g., AES‑GCM) exposes you to malleability attacks. Prefer AEAD schemes that combine confidentiality and integrity.

Quick Code Demo (Python)

Below is a minimal example that demonstrates hashing with SHA‑256 and encryption with AES‑GCM using the cryptography library.

from cryptography.hazmat.primitives import hashes, serialization
from cryptography.hazmat.primitives.kdf.pbkdf2 import PBKDF2HMAC
from cryptography.hazmat.primitives.ciphers.aead import AESGCM
import os

# Hashing
message = b"Secret data"
hasher = hashes.Hash(hashes.SHA256())
hasher.update(message)
digest = hasher.finalize()
print("SHA‑256 digest:", digest.hex())

# Encryption
password = b"strong_password"
salt = os.urandom(16)
# Derive a 256‑bit key
kdf = PBKDF2HMAC(algorithm=hashes.SHA256(), length=32, salt=salt, iterations=100000)
key = kdf.derive(password)

aesgcm = AESGCM(key)
nonce = os.urandom(12)
ct = aesgcm.encrypt(nonce, message, None)
print("AES‑GCM ciphertext:", ct.hex())

# Decryption
pt = aesgcm.decrypt(nonce, ct, None)
print("Recovered plaintext:", pt.decode())

Takeaway: Hashing is a one‑way digest for integrity; encryption is a reversible transformation for confidentiality.

People also ask

Can I use a hash to verify a password?

Yes, but always combine it with a unique salt and a slow KDF like Argon2 to defend against brute‑force attacks.

Is AES‑GCM a hash?

No, AES‑GCM is an authenticated encryption mode; it provides confidentiality and integrity, not a one‑way digest.

What is the difference between a collision and a pre‑image in hashing?

A collision is two distinct inputs producing the same digest; a pre‑image is finding any input that maps to a given digest.

Inspired by a public discussion on Stack Overflow. This article is an original explanation for learners.

← All posts