Created
November 28, 2014 15:20
-
-
Save calvinmetcalf/91e8e84dc63c75f2aa53 to your computer and use it in GitHub Desktop.
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
function pbkdf2(password, salt, iterations, len, hashType) { | |
hashType = hashType || 'sha1'; | |
if (!Buffer.isBuffer(password)) { | |
password = new Buffer(password); | |
} | |
if (!Buffer.isBuffer(salt)) { | |
salt = new Buffer(salt); | |
} | |
var out = new Buffer(''); | |
var md, prev, i, j; | |
var num = 0; | |
var block = Buffer.concat([salt, new Buffer(4)]); | |
while (out.length < len) { | |
num++; | |
block.writeUInt32BE(num, salt.length); | |
prev = crypto.createHmac(hashType, password) | |
.update(block) | |
.digest(); | |
md = prev; | |
i = 0; | |
while (++i < iterations) { | |
prev = crypto.createHmac(hashType, password) | |
.update(prev) | |
.digest(); | |
j = -1; | |
while (++j < prev.length) { | |
md[j] ^= prev[j] | |
} | |
} | |
out = Buffer.concat([out, md]); | |
} | |
return out.slice(0, len); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Where do you define Buffer