How to hash passwords in Node.js and Python
Updated 8 November 2026 · 6 min read
If your app stores passwords, it should store a slow, salted hash of each one, never the password itself and never a fast hash like MD5 or SHA-1. This guide shows why, which algorithm to pick, and the code for Node.js and Python.
Why MD5 and SHA-1 are the wrong tool
MD5, SHA-1 and SHA-256 are built to be fast. That is good for checking a file download and bad for passwords. If your database leaks, an attacker can try billions of guesses per second against fast hashes on ordinary graphics cards, and common passwords fall in minutes.
A password hashing function is built to be slow and to use memory, so each guess costs real time. It also adds a random salt to every password, so two users with the same password get different hashes and precomputed tables don’t help.
// Don't do this
const hash = md5(user.password);
const hash2 = crypto.createHash("sha256").update(user.password).digest("hex");
Which algorithm to use
The OWASP Password Storage Cheat Sheet recommends, in order:
- Argon2id, the winner of the Password Hashing Competition. Use it for new projects.
- scrypt, if Argon2id isn’t available.
- bcrypt, still fine and very widely supported. It only uses the first 72 bytes of a password, so very long passphrases are cut.
All three handle the salt for you and store it inside the hash string, so you keep one value per user.
Node.js
With Argon2id (the argon2 package):
import argon2 from "argon2";
// Sign-up
const hash = await argon2.hash(password, { type: argon2.argon2id });
await db.users.update(userId, { passwordHash: hash });
// Login
const ok = await argon2.verify(user.passwordHash, password);
With bcrypt (the bcrypt package):
import bcrypt from "bcrypt";
const hash = await bcrypt.hash(password, 12); // 12 is the cost factor
const ok = await bcrypt.compare(password, user.passwordHash);
Node also has scrypt built in, with no extra package:
import { scrypt, randomBytes, timingSafeEqual } from "node:crypto";
import { promisify } from "node:util";
const scryptAsync = promisify(scrypt);
const salt = randomBytes(16);
const key = (await scryptAsync(password, salt, 64)) as Buffer;
const stored = `${salt.toString("hex")}:${key.toString("hex")}`;
When you check a scrypt hash yourself, compare with timingSafeEqual, not ===, so the comparison takes the same time whether the first byte matches or not.
Python
With Argon2id (the argon2-cffi package):
from argon2 import PasswordHasher
ph = PasswordHasher() # Argon2id with sensible defaults
hash = ph.hash(password)
try:
ph.verify(user.password_hash, password)
except Exception:
raise InvalidLogin()
if ph.check_needs_rehash(user.password_hash):
user.password_hash = ph.hash(password) # upgrade old hashes at login
With bcrypt:
import bcrypt
hash = bcrypt.hashpw(password.encode(), bcrypt.gensalt(rounds=12))
ok = bcrypt.checkpw(password.encode(), user.password_hash)
Django and most frameworks already hash passwords properly by default. If you use one, use its built-in user model and password functions rather than writing your own.
Moving away from MD5 or SHA-1
You can’t turn old hashes into new ones, because you don’t have the passwords. Two common ways out:
- Rehash at login. When a user logs in successfully against the old hash, hash the password they just typed with Argon2id and replace the old value. Users who never log in again keep weak hashes, so set a cut-off date and force a reset after it.
- Wrap the old hash. Hash the existing MD5 value with Argon2id straight away, so every stored value is protected today, and mark those rows so login checks
argon2(md5(password)). Then rehash cleanly at the next login.
Why it matters for audits
Weak password storage is a common finding in security reviews. It touches GDPR Art. 32 (security of processing), SOC 2 CC6.1 (logical access) and ISO 27001 A.8.24 (use of cryptography). It is also one of the first things a customer’s security questionnaire asks about.
What Lintsure checks
The passwords hashed with a weak or fast algorithm rule flags lines that use MD5 or SHA-1 near a password field, and leaves alone MD5 used for file checksums or cache keys. Through a coding agent it proposes the change and waits for your approval.
Check your own code
Lintsure runs these checks on your machine and never uploads your code.
npx lintsure scanThis guide is our own reading of public texts, written for software teams. It is not legal advice and it doesn't cover your whole obligation, only the parts code can show. Ask a qualified person before any audit or decision. Disclaimer