Skip to content

Commit 5dbe10a

Browse files
committed
pbkdf2: use pbkdf2-compat 2.0.0
1 parent a5cf35b commit 5dbe10a

File tree

3 files changed

+7
-77
lines changed

3 files changed

+7
-77
lines changed

index.js

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,10 +30,9 @@ function each(a, f) {
3030

3131
exports.getHashes = function () {
3232
return ['sha1', 'sha256', 'sha512', 'md5', 'rmd160']
33-
3433
}
3534

36-
var p = require('./pbkdf2')(exports.createHmac)
35+
var p = require('./pbkdf2')(exports)
3736
exports.pbkdf2 = p.pbkdf2
3837
exports.pbkdf2Sync = p.pbkdf2Sync
3938

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
"node": "*"
1616
},
1717
"dependencies": {
18+
"pbkdf2-compat": "2.0.0",
1819
"ripemd160": "0.2.0",
1920
"sha.js": "2.2.6"
2021
},

pbkdf2.js

Lines changed: 5 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -1,82 +1,12 @@
1-
// JavaScript PBKDF2 Implementation
2-
// Based on http://git.io/qsv2zw
3-
// Licensed under LGPL v3
4-
// Copyright (c) 2013 jduncanator
1+
var pbkdf2Export = require('pbkdf2-compat').__pbkdf2Export
52

6-
var blocksize = 64
7-
var zeroBuffer = new Buffer(blocksize); zeroBuffer.fill(0)
8-
9-
module.exports = function (createHmac, exports) {
3+
module.exports = function (crypto, exports) {
104
exports = exports || {}
115

12-
exports.pbkdf2 = function(password, salt, iterations, keylen, cb) {
13-
if('function' !== typeof cb)
14-
throw new Error('No callback provided to pbkdf2');
15-
setTimeout(function () {
16-
cb(null, exports.pbkdf2Sync(password, salt, iterations, keylen))
17-
})
18-
}
19-
20-
exports.pbkdf2Sync = function(key, salt, iterations, keylen) {
21-
if('number' !== typeof iterations)
22-
throw new TypeError('Iterations not a number')
23-
if(iterations < 0)
24-
throw new TypeError('Bad iterations')
25-
if('number' !== typeof keylen)
26-
throw new TypeError('Key length not a number')
27-
if(keylen < 0)
28-
throw new TypeError('Bad key length')
29-
30-
//stretch key to the correct length that hmac wants it,
31-
//otherwise this will happen every time hmac is called
32-
//twice per iteration.
33-
var key = !Buffer.isBuffer(key) ? new Buffer(key) : key
34-
35-
if(key.length > blocksize) {
36-
key = createHash(alg).update(key).digest()
37-
} else if(key.length < blocksize) {
38-
key = Buffer.concat([key, zeroBuffer], blocksize)
39-
}
40-
41-
var HMAC;
42-
var cplen, p = 0, i = 1, itmp = new Buffer(4), digtmp;
43-
var out = new Buffer(keylen);
44-
out.fill(0);
45-
while(keylen) {
46-
if(keylen > 20)
47-
cplen = 20;
48-
else
49-
cplen = keylen;
50-
51-
/* We are unlikely to ever use more than 256 blocks (5120 bits!)
52-
* but just in case...
53-
*/
54-
itmp[0] = (i >> 24) & 0xff;
55-
itmp[1] = (i >> 16) & 0xff;
56-
itmp[2] = (i >> 8) & 0xff;
57-
itmp[3] = i & 0xff;
58-
59-
HMAC = createHmac('sha1', key);
60-
HMAC.update(salt)
61-
HMAC.update(itmp);
62-
digtmp = HMAC.digest();
63-
digtmp.copy(out, p, 0, cplen);
64-
65-
for(var j = 1; j < iterations; j++) {
66-
HMAC = createHmac('sha1', key);
67-
HMAC.update(digtmp);
68-
digtmp = HMAC.digest();
69-
for(var k = 0; k < cplen; k++) {
70-
out[k] ^= digtmp[k];
71-
}
72-
}
73-
keylen -= cplen;
74-
i++;
75-
p += cplen;
76-
}
6+
var exported = pbkdf2Export(crypto)
777

78-
return out;
79-
}
8+
exports.pbkdf2 = exported.pbkdf2
9+
exports.pbkdf2Sync = exported.pbkdf2Sync
8010

8111
return exports
8212
}

0 commit comments

Comments
 (0)