Skip to content

Commit

Permalink
pbkdf2: use pbkdf2-compat 2.0.0
Browse files Browse the repository at this point in the history
  • Loading branch information
dcousens committed Sep 24, 2014
1 parent a5cf35b commit 5dbe10a
Show file tree
Hide file tree
Showing 3 changed files with 7 additions and 77 deletions.
3 changes: 1 addition & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,9 @@ function each(a, f) {

exports.getHashes = function () {
return ['sha1', 'sha256', 'sha512', 'md5', 'rmd160']

}

var p = require('./pbkdf2')(exports.createHmac)
var p = require('./pbkdf2')(exports)
exports.pbkdf2 = p.pbkdf2
exports.pbkdf2Sync = p.pbkdf2Sync

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
"node": "*"
},
"dependencies": {
"pbkdf2-compat": "2.0.0",
"ripemd160": "0.2.0",
"sha.js": "2.2.6"
},
Expand Down
80 changes: 5 additions & 75 deletions pbkdf2.js
Original file line number Diff line number Diff line change
@@ -1,82 +1,12 @@
// JavaScript PBKDF2 Implementation
// Based on http://git.io/qsv2zw
// Licensed under LGPL v3
// Copyright (c) 2013 jduncanator
var pbkdf2Export = require('pbkdf2-compat').__pbkdf2Export

var blocksize = 64
var zeroBuffer = new Buffer(blocksize); zeroBuffer.fill(0)

module.exports = function (createHmac, exports) {
module.exports = function (crypto, exports) {
exports = exports || {}

exports.pbkdf2 = function(password, salt, iterations, keylen, cb) {
if('function' !== typeof cb)
throw new Error('No callback provided to pbkdf2');
setTimeout(function () {
cb(null, exports.pbkdf2Sync(password, salt, iterations, keylen))
})
}

exports.pbkdf2Sync = function(key, salt, iterations, keylen) {
if('number' !== typeof iterations)
throw new TypeError('Iterations not a number')
if(iterations < 0)
throw new TypeError('Bad iterations')
if('number' !== typeof keylen)
throw new TypeError('Key length not a number')
if(keylen < 0)
throw new TypeError('Bad key length')

//stretch key to the correct length that hmac wants it,
//otherwise this will happen every time hmac is called
//twice per iteration.
var key = !Buffer.isBuffer(key) ? new Buffer(key) : key

if(key.length > blocksize) {
key = createHash(alg).update(key).digest()
} else if(key.length < blocksize) {
key = Buffer.concat([key, zeroBuffer], blocksize)
}

var HMAC;
var cplen, p = 0, i = 1, itmp = new Buffer(4), digtmp;
var out = new Buffer(keylen);
out.fill(0);
while(keylen) {
if(keylen > 20)
cplen = 20;
else
cplen = keylen;

/* We are unlikely to ever use more than 256 blocks (5120 bits!)
* but just in case...
*/
itmp[0] = (i >> 24) & 0xff;
itmp[1] = (i >> 16) & 0xff;
itmp[2] = (i >> 8) & 0xff;
itmp[3] = i & 0xff;

HMAC = createHmac('sha1', key);
HMAC.update(salt)
HMAC.update(itmp);
digtmp = HMAC.digest();
digtmp.copy(out, p, 0, cplen);

for(var j = 1; j < iterations; j++) {
HMAC = createHmac('sha1', key);
HMAC.update(digtmp);
digtmp = HMAC.digest();
for(var k = 0; k < cplen; k++) {
out[k] ^= digtmp[k];
}
}
keylen -= cplen;
i++;
p += cplen;
}
var exported = pbkdf2Export(crypto)

return out;
}
exports.pbkdf2 = exported.pbkdf2
exports.pbkdf2Sync = exported.pbkdf2Sync

return exports
}

0 comments on commit 5dbe10a

Please sign in to comment.