Cryptography

Password Hashing Guide

8 min read

Password hashing is critical for protecting user credentials. Unlike encryption, hashing is one-way—passwords cannot be recovered, only verified.

Algorithm Comparison

AlgorithmMemorySpeedRecommendation
Argon2idConfigurableSlowest✅ Best choice
bcrypt4KBSlow✅ Excellent
PBKDF2MinimalModerate⚠️ OK if required
SHA-256 (raw)MinimalFast❌ Never use alone
MD5MinimalVery fast❌ Broken

Implementation

# Python - Argon2 (recommended)
from argon2 import PasswordHasher
ph = PasswordHasher()
hash = ph.hash("password123")
ph.verify(hash, "password123")  # Returns True or raises

# Python - bcrypt
import bcrypt
hash = bcrypt.hashpw(password.encode(), bcrypt.gensalt(rounds=12))
bcrypt.checkpw(password.encode(), hash)  # Returns True/False

# Node.js - bcrypt
const bcrypt = require('bcrypt');
const hash = await bcrypt.hash(password, 12);
await bcrypt.compare(password, hash);
Common Mistakes
Best Practices

December 2024