Skip to content
This repository has been archived by the owner on Feb 4, 2022. It is now read-only.

Commit

Permalink
fix(scram): cache salted data, not the original data
Browse files Browse the repository at this point in the history
NODE-1161
  • Loading branch information
mbroadst committed Oct 12, 2017
1 parent 7bfdc71 commit 0cbe95f
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions lib/auth/scram.js
Original file line number Diff line number Diff line change
Expand Up @@ -76,28 +76,30 @@ var xor = function(a, b) {
return new Buffer(res);
};

// hiCache stores previous salt creations so it's not regenerated per-pool member
var _hiCache = {},
_hiCacheCount = 0;

var _hiCache = {};
var _hiCacheCount = 0;
var _hiCachePurge = function() {
_hiCache = {};
_hiCacheCount = 0;
};

var hi = function(data, salt, iterations) {
// omit the work if already generated
var key = data + '_' + salt.toString('base64') + '_' + iterations;
if (_hiCache[key] !== undefined) return _hiCache[key];
var key = [data, salt.toString('base64'), iterations].join('_');
if (_hiCache[key] !== undefined) {
return _hiCache[key];
}

// generate the salt
var saltedData = crypto.pbkdf2Sync(data, salt, iterations, 20, 'sha1');
var saltedData = crypto.pbkdf2Sync(data, salt, iterations, 20, "sha1");

// cache a copy to speed up the next lookup, but prevent unbounded cache growth
if (_hiCacheCount >= 200) _hiCachePurge();
_hiCache[key] = data;
_hiCacheCount += 1;
if (_hiCacheCount >= 200) {
_hiCachePurge();
}

_hiCache[key] = saltedData;
_hiCacheCount += 1;
return saltedData;
};

Expand Down

0 comments on commit 0cbe95f

Please sign in to comment.