Weak password hashing with MD5

Weak password hashing with MD5

Description

After a database leak, MD5 makes it cheap to compare candidate password hashes with the stored values. Without a unique salt, identical passwords have identical hashes, so the same calculations can be reused across users.

Potential impact

Someone who recovers a password may be able to sign in to the user’s account.

Remediation

Replace MD5 and SHA-1 with a maintained Argon2id or scrypt implementation. Use a fresh random salt per password. Let the library generate it when supported. With Node’s crypto.scrypt, generate and pass the salt yourself. Store it and the cost parameters with the hash. If keeping bcrypt for an existing system, handle its 72-byte input limit and benchmark its cost in your service.

Examples

Before

javascript
const crypto = require('crypto');

function setUserPassword(user, pwtext) {
  const hash = crypto.createHash('md5').update(pwtext).digest('hex');
  user.setPassword(hash);
}

After

javascript
const bcrypt = require('bcrypt');

function setUserPassword(user, pwtext) {
  const saltRounds = 12; // Example cost: tune for the service environment
  bcrypt.hash(pwtext, saltRounds, function(err, hash) {
    if (err) throw err;
    user.setPassword(hash);
  });
}

Here bcrypt generates the salt and hashes the password at cost 12. Route hashing failures through your application’s error handling.

References