-
-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
3 changed files
with
7 additions
and
77 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |