Full answer
Why a slow, memory-hard hash
- Password databases leak. Attackers then try billions of guesses. A fast hash (SHA-256) allows billions per second on a GPU. Password hashes are slow on purpose and have a unique salt per password.
- Argon2id uses a lot of memory, which makes GPU and ASIC attacks expensive. bcrypt (cost factor, 72 byte limit) and scrypt (built into node:crypto) are acceptable alternatives. PBKDF2 is the compatible choice for rules that demand it.
Parameters (argon2 npm package)
- memoryCost in KiB (65536 means 64 MiB), timeCost (iterations), parallelism (lanes). Defaults are reasonable. OWASP lists values such as 19 MiB with 2 iterations or 46 MiB with 1 iteration as minimum levels, and higher is better.
- Measure on your production hardware. Aim for roughly 100 to 500 ms per hash. Remember that login bursts run many hashes at once and use memory (threads in the libuv pool run them, default 4 at a time).
Storing and verifying
- argon2.hash(password) returns a PHC string such as $argon2id$v=19$m=65536,t=3,p=4$salt$hash. Store the whole string in one column.
- argon2.verify(hash, password) reads the parameters from the string and compares in constant time.
- argon2.needsRehash(hash, options) tells whether the stored hash uses weaker parameters than your current policy.
Upgrade flow
- User logs in, verify succeeds.
- If needsRehash is true, hash the password again with the new options and update the row. You only have the plain password at this moment.
- Over time active users move to the new settings. Inactive users keep old hashes until they return.
Also: never log passwords, limit login attempts, compare reset tokens with timingSafeEqual, and consider a pepper kept outside the database.
const argon2 = require('argon2');
const oldPolicy = { type: argon2.argon2id, memoryCost: 4096, timeCost: 2, parallelism: 1 }; // small values keep the demo fast
const newPolicy = { type: argon2.argon2id, memoryCost: 16384, timeCost: 3, parallelism: 1 };
// A tiny user table with a pretend login flow.
const users = new Map();
async function register(name, password) { users.set(name, await argon2.hash(password, oldPolicy)); }
async function login(name, password) {
const stored = users.get(name);
if (!(await argon2.verify(stored, password))) return 'wrong password';
if (argon2.needsRehash(stored, newPolicy)) {
users.set(name, await argon2.hash(password, newPolicy)); // upgrade while we have the password
return 'ok (hash upgraded)';
}
return 'ok';
}
(async () => {
await register('asha', 'correct horse battery');
const before = users.get('asha');
console.log('stored hash parameters:', before.split('$')[3]);
console.log('same password hashed twice differs (salt):', before !== await argon2.hash('correct horse battery', oldPolicy));
console.log('wrong password ->', await login('asha', 'guess'));
console.log('right password ->', await login('asha', 'correct horse battery'));
console.log('hash parameters after login:', users.get('asha').split('$')[3]);
console.log('second login ->', await login('asha', 'correct horse battery'));
console.log('needsRehash with the new policy now:', argon2.needsRehash(users.get('asha'), newPolicy));
})();The first login after a policy change replaces the stored hash with one made with stronger parameters. The parameters used here are small so that the demo runs fast, so do not copy them into production. Install first with npm install argon2 (run here with argon2 0.45.2). Save it as app.js and run it with node app.js. Output (Node.js v24.22.1, run once): stored hash parameters: m=4096,t=2,p=1 same password hashed twice differs (salt): true wrong password -> wrong password right password -> ok (hash upgraded) hash parameters after login: m=16384,t=3,p=1 second login -> ok needsRehash with the new policy now: false
Key points
- Slow, salted, memory-hard hashes limit offline guessing.
- The hash string stores algorithm, parameters and salt.
- Tune cost to about 100 to 500 ms on your hardware.
- needsRehash plus a successful login upgrades hashes in place.
- Rate limit logins and never log passwords.
Common mistakes
- Using SHA-256 or MD5 for passwords.
- Copying the demo parameters into production.
- Never upgrading old hashes.
- Running hashing on a tiny server with no memory headroom.
- Truncating long passwords (bcrypt's 72 bytes) without noticing.
Follow-ups
- How do you choose between argon2id, bcrypt and scrypt?
- What is a pepper, and where do you keep it?
- How do you protect the login endpoint from guessing?
Tips
- Explain why slow hashing helps.
- Mention the PHC string format.
- Describe the upgrade flow in three steps.