Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 7 additions & 10 deletions src/cmap/auth/scram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -229,19 +229,16 @@ function passwordDigest(username: string, password: string) {
throw new MongoInvalidArgumentError('Password cannot be empty');
}

let md5: crypto.Hash;
// Use PBKDF2 with SHA-256, 100,000 iterations, and a salt derived from username
const salt = Buffer.from(username + ':mongo', 'utf8');
const iterations = 100000;
const keylen = 32; // 256 bits
try {
md5 = crypto.createHash('md5');
const derivedKey = crypto.pbkdf2Sync(password, salt, iterations, keylen, 'sha256');
return derivedKey.toString('hex');
} catch (err) {
if (crypto.getFips()) {
// This error is (slightly) more helpful than what comes from OpenSSL directly, e.g.
// 'Error: error:060800C8:digital envelope routines:EVP_DigestInit_ex:disabled for FIPS'
throw new Error('Auth mechanism SCRAM-SHA-1 is not supported in FIPS mode');
}
throw err;
throw new MongoRuntimeError('Error hashing password with PBKDF2: ' + err.message);
}
md5.update(`${username}:mongo:${password}`, 'utf8');
return md5.digest('hex');
}

// XOR two buffers
Expand Down