ethers.js/packages/pbkdf2/lib/browser-pbkdf2.js

47 lines
1.8 KiB
JavaScript
Raw Permalink Normal View History

2019-05-14 18:48:48 -04:00
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
2021-03-07 18:24:04 -05:00
exports.pbkdf2 = void 0;
2019-05-14 18:48:48 -04:00
var bytes_1 = require("@ethersproject/bytes");
var sha2_1 = require("@ethersproject/sha2");
function pbkdf2(password, salt, iterations, keylen, hashAlgorithm) {
2021-10-16 02:29:27 -04:00
password = (0, bytes_1.arrayify)(password);
salt = (0, bytes_1.arrayify)(salt);
2019-05-14 18:48:48 -04:00
var hLen;
var l = 1;
var DK = new Uint8Array(keylen);
var block1 = new Uint8Array(salt.length + 4);
block1.set(salt);
//salt.copy(block1, 0, 0, salt.length)
var r;
var T;
for (var i = 1; i <= l; i++) {
//block1.writeUInt32BE(i, salt.length)
block1[salt.length] = (i >> 24) & 0xff;
block1[salt.length + 1] = (i >> 16) & 0xff;
block1[salt.length + 2] = (i >> 8) & 0xff;
block1[salt.length + 3] = i & 0xff;
//let U = createHmac(password).update(block1).digest();
2021-10-16 02:29:27 -04:00
var U = (0, bytes_1.arrayify)((0, sha2_1.computeHmac)(hashAlgorithm, password, block1));
2019-05-14 18:48:48 -04:00
if (!hLen) {
hLen = U.length;
T = new Uint8Array(hLen);
l = Math.ceil(keylen / hLen);
r = keylen - (l - 1) * hLen;
}
//U.copy(T, 0, 0, hLen)
T.set(U);
for (var j = 1; j < iterations; j++) {
//U = createHmac(password).update(U).digest();
2021-10-16 02:29:27 -04:00
U = (0, bytes_1.arrayify)((0, sha2_1.computeHmac)(hashAlgorithm, password, U));
2019-05-14 18:48:48 -04:00
for (var k = 0; k < hLen; k++)
T[k] ^= U[k];
}
var destPos = (i - 1) * hLen;
var len = (i === l ? r : hLen);
//T.copy(DK, destPos, 0, len)
2021-10-16 02:29:27 -04:00
DK.set((0, bytes_1.arrayify)(T).slice(0, len), destPos);
2019-05-14 18:48:48 -04:00
}
2021-10-16 02:29:27 -04:00
return (0, bytes_1.hexlify)(DK);
2019-05-14 18:48:48 -04:00
}
exports.pbkdf2 = pbkdf2;
2020-11-16 23:07:24 -05:00
//# sourceMappingURL=browser-pbkdf2.js.map