crypto: optimize hkdf and pbkdf2.

This commit is contained in:
Christopher Jeffrey 2016-07-27 07:02:53 -07:00
parent 4e977810ce
commit 28c7ee69e4
No known key found for this signature in database
GPG Key ID: 8962AB9DE6666BBD

View File

@ -312,7 +312,7 @@ crypto.decipher = function decipher(data, key, iv) {
crypto._pbkdf2 = function pbkdf2(key, salt, iter, len, alg) {
var size = crypto.hash(alg, new Buffer(0)).length;
var blocks = Math.ceil(len / size);
var out = new Buffer(blocks * size);
var out = new Buffer(len);
var buf = new Buffer(salt.length + 4);
var block = new Buffer(size);
var pos = 0;
@ -333,7 +333,7 @@ crypto._pbkdf2 = function pbkdf2(key, salt, iter, len, alg) {
pos += size;
}
return out.slice(0, len);
return out;
};
/**
@ -365,7 +365,7 @@ crypto.hkdfExpand = function hkdfExpand(prk, info, len, alg) {
if (blocks > 255)
throw new Error('Too many blocks.');
okm = new Buffer(0);
okm = new Buffer(len);
if (blocks === 0)
return okm;
@ -376,16 +376,16 @@ crypto.hkdfExpand = function hkdfExpand(prk, info, len, alg) {
info.copy(buf, size);
buf[buf.length - 1] = 1;
out = crypto.hmac(alg, buf.slice(size), prk);
okm = out;
out.copy(okm, 0);
for (i = 1; i < blocks; i++) {
out.copy(buf, 0);
buf[buf.length - 1]++;
out = crypto.hmac(alg, buf, prk);
okm = Buffer.concat([okm, out]);
out.copy(okm, i * size);
}
return okm.slice(0, len);
return okm;
};
/**