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

Commit 31ef03a

Browse files
andrasqmbroadst
authored andcommitted
fix(auth-scram): cache the ScramSHA1 salted passwords up to 200 entries
* cache the ScramSHA1 salted passwords, capping the cache at 200 entries * rename var to fix lint error
1 parent 35c5ea2 commit 31ef03a

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

lib/auth/scram.js

+18-10
Original file line numberDiff line numberDiff line change
@@ -77,20 +77,28 @@ var xor = function(a, b) {
7777
};
7878

7979
// hiCache stores previous salt creations so it's not regenerated per-pool member
80-
var _hiCache = {};
80+
var _hiCache = {},
81+
_hiCacheCount = 0;
82+
83+
var _hiCachePurge = function() {
84+
_hiCache = {};
85+
_hiCacheCount = 0;
86+
};
8187

8288
var hi = function(data, salt, iterations) {
83-
var key = [data, salt.toString('base64'), iterations].join('_');
84-
// check if we've already generated this salt
85-
if (_hiCache[key] !== undefined) {
86-
return _hiCache[key];
87-
}
89+
// omit the work if already generated
90+
var key = data + '_' + salt.toString('base64') + '_' + iterations;
91+
if (_hiCache[key] !== undefined) return _hiCache[key];
92+
93+
// generate the salt
94+
var saltedData = crypto.pbkdf2Sync(data, salt, iterations, 20, 'sha1');
8895

89-
// generate the salt and store it in the cache for the next worker
90-
var result = crypto.pbkdf2Sync(data, salt, iterations, 20, 'sha1');
91-
_hiCache[key] = result;
96+
// 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;
92100

93-
return result;
101+
return saltedData;
94102
};
95103

96104
/**

0 commit comments

Comments
 (0)