Description
Fast hashes such as MD5 and SHA-1, or password hashes with too little work or memory, make it cheap to check guesses after a database or backup leak. Without salts, the same calculations work across accounts.
Potential impact
A recovered password can expose sensitive data or administrative functions within the account’s permissions. Reuse can put other accounts at risk. Weak storage may also undermine compliance and customer trust.
Remediation
- Prefer Argon2id for new systems, or use scrypt if Argon2id is unavailable. Keep bcrypt for compatibility or use PBKDF2 when requirements call for it.
- Benchmark the settings for your deployment. Example baselines are Argon2id
m≥19 MiB, t≥2, p=1, scryptN≥2^17, r=8, p=1, bcryptcost≥10, and PBKDF2-HMAC-SHA256 with at least600,000iterations. - Use a fresh random salt per password. Let the library generate and store it when supported. If you add a pepper, keep it in a secret store or HSM separate from the database.
- After a successful login against an old hash, rehash the supplied password with the current algorithm and cost. Also enforce a strong password policy and limit login attempts.
Examples
This example retains bcrypt for an existing system. bcrypt uses only the first 72 bytes of UTF-8 input. Apply the same length policy at registration and login to prevent silent truncation.
Before
// Store passwords with a fast general-purpose hash (MD5/SHA-1)
const crypto = require("crypto");
async function saveUserWeak(username, plainPassword, db) {
const digest = crypto
.createHash("md5")
.update(plainPassword, "utf8")
.digest("hex");
await db.users.insert({ username, passwordHash: digest });
}
After
// Use bcrypt with unique salts and an appropriate cost
const bcrypt = require("bcrypt");
async function saveUserSecure(username, plainPassword, db) {
const cost = 12; // Example cost factor; benchmark in the deployment environment
const hash = await bcrypt.hash(plainPassword, cost); // Includes a randomly generated salt
await db.users.insert({ username, passwordHash: hash });
}
async function verifyLogin(username, inputPassword, db) {
const user = await db.users.findOne({ username });
if (!user) return false;
return bcrypt.compare(inputPassword, user.passwordHash);
}
bcrypt.hash uses cost 12 and a random salt. bcrypt.compare checks the supplied password against the stored hash.