Skip to content

Instantly share code, notes, and snippets.

@calvinmetcalf
Created November 28, 2014 15:20
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save calvinmetcalf/91e8e84dc63c75f2aa53 to your computer and use it in GitHub Desktop.
Save calvinmetcalf/91e8e84dc63c75f2aa53 to your computer and use it in GitHub Desktop.
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);
}
@MrQui3
Copy link

MrQui3 commented Nov 22, 2023

Where do you define Buffer

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment