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

Commit 0cbe95f

Browse files
committedOct 12, 2017
fix(scram): cache salted data, not the original data
NODE-1161
1 parent 7bfdc71 commit 0cbe95f

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed
 

‎lib/auth/scram.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -76,28 +76,30 @@ var xor = function(a, b) {
7676
return new Buffer(res);
7777
};
7878

79-
// hiCache stores previous salt creations so it's not regenerated per-pool member
80-
var _hiCache = {},
81-
_hiCacheCount = 0;
82-
79+
var _hiCache = {};
80+
var _hiCacheCount = 0;
8381
var _hiCachePurge = function() {
8482
_hiCache = {};
8583
_hiCacheCount = 0;
8684
};
8785

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

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

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

101+
_hiCache[key] = saltedData;
102+
_hiCacheCount += 1;
101103
return saltedData;
102104
};
103105

0 commit comments

Comments
 (0)
This repository has been archived.