UNPKG

pkcs-ts

Version:

PKCS #1 through #15 (Except #11) - Private and Public Keys, Certificates, and More

64 lines 2.14 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); const tslib_1 = require("tslib"); const asn1 = tslib_1.__importStar(require("asn1-ts")); const crypto = tslib_1.__importStar(require("crypto")); function generatePKCS12Key(password, salt, id, iter, n, hashAlgorithm, blockLength, digestLength) { let j, l; const u = digestLength; const v = blockLength; let result = Buffer.from([]); const passBuf = (() => { const el = new asn1.DERElement(); el.bmpString = password + "\0"; return el.value; })(); const p = passBuf.length; const s = salt.length; const D = new Uint8Array(v); D.fill(id); const Slen = v * Math.ceil(s / v); let S = new Uint8Array(Slen); for (l = 0; l < Slen; l++) { S[l] = salt[l % s]; } const Plen = v * Math.ceil(p / v); let P = new Uint8Array(Plen); for (l = 0; l < Plen; l++) { P[l] = passBuf[l % p]; } S = Buffer.concat([S, P]); let I = S; P = new Uint8Array(0); const c = Math.ceil(n / u); for (let i = 1; i <= c; i++) { let buf = Buffer.concat([D, I]); for (let round = 0; round < iter; round++) { const hasher = crypto.createHash(hashAlgorithm); hasher.update(buf); buf = hasher.digest(); } const B = new Uint8Array(v); for (l = 0; l < v; l++) { B[l] = buf[l % u]; } const k = Math.ceil(s / v) + Math.ceil(p / v); let Inew = Buffer.from([]); for (j = 0; j < k; j++) { const chunk = I.slice(0, v); I = I.slice(v); let x = 0x1ff; for (l = B.length - 1; l >= 0; l--) { x = x >> 8; x += B[l] + chunk[l]; chunk[l] = x & 0xff; } Inew = Buffer.concat([Inew, chunk]); } I = Inew; result = Buffer.concat([result, buf]); } return result.slice(0, n); } exports.default = generatePKCS12Key; //# sourceMappingURL=generatePKCS12Key.js.map