{"ast":null,"code":"\"use strict\";\n\nvar _asyncToGenerator = require(\"/home/mleku/src/orly.dev/next/signer/node_modules/@babel/runtime/helpers/asyncToGenerator.js\").default;\nObject.defineProperty(exports, \"__esModule\", {\n  value: true\n});\nexports.pbkdf2 = pbkdf2;\nexports.pbkdf2Async = pbkdf2Async;\n/**\n * PBKDF (RFC 2898). Can be used to create a key from password and salt.\n * @module\n */\nconst hmac_ts_1 = require(\"./hmac.js\");\n// prettier-ignore\nconst utils_ts_1 = require(\"./utils.js\");\n// Common prologue and epilogue for sync/async functions\nfunction pbkdf2Init(hash, _password, _salt, _opts) {\n  (0, utils_ts_1.ahash)(hash);\n  const opts = (0, utils_ts_1.checkOpts)({\n    dkLen: 32,\n    asyncTick: 10\n  }, _opts);\n  const {\n    c,\n    dkLen,\n    asyncTick\n  } = opts;\n  (0, utils_ts_1.anumber)(c);\n  (0, utils_ts_1.anumber)(dkLen);\n  (0, utils_ts_1.anumber)(asyncTick);\n  if (c < 1) throw new Error('iterations (c) should be >= 1');\n  const password = (0, utils_ts_1.kdfInputToBytes)(_password);\n  const salt = (0, utils_ts_1.kdfInputToBytes)(_salt);\n  // DK = PBKDF2(PRF, Password, Salt, c, dkLen);\n  const DK = new Uint8Array(dkLen);\n  // U1 = PRF(Password, Salt + INT_32_BE(i))\n  const PRF = hmac_ts_1.hmac.create(hash, password);\n  const PRFSalt = PRF._cloneInto().update(salt);\n  return {\n    c,\n    dkLen,\n    asyncTick,\n    DK,\n    PRF,\n    PRFSalt\n  };\n}\nfunction pbkdf2Output(PRF, PRFSalt, DK, prfW, u) {\n  PRF.destroy();\n  PRFSalt.destroy();\n  if (prfW) prfW.destroy();\n  (0, utils_ts_1.clean)(u);\n  return DK;\n}\n/**\n * PBKDF2-HMAC: RFC 2898 key derivation function\n * @param hash - hash function that would be used e.g. sha256\n * @param password - password from which a derived key is generated\n * @param salt - cryptographic salt\n * @param opts - {c, dkLen} where c is work factor and dkLen is output message size\n * @example\n * const key = pbkdf2(sha256, 'password', 'salt', { dkLen: 32, c: Math.pow(2, 18) });\n */\nfunction pbkdf2(hash, password, salt, opts) {\n  const {\n    c,\n    dkLen,\n    DK,\n    PRF,\n    PRFSalt\n  } = pbkdf2Init(hash, password, salt, opts);\n  let prfW; // Working copy\n  const arr = new Uint8Array(4);\n  const view = (0, utils_ts_1.createView)(arr);\n  const u = new Uint8Array(PRF.outputLen);\n  // DK = T1 + T2 + ⋯ + Tdklen/hlen\n  for (let ti = 1, pos = 0; pos < dkLen; ti++, pos += PRF.outputLen) {\n    // Ti = F(Password, Salt, c, i)\n    const Ti = DK.subarray(pos, pos + PRF.outputLen);\n    view.setInt32(0, ti, false);\n    // F(Password, Salt, c, i) = U1 ^ U2 ^ ⋯ ^ Uc\n    // U1 = PRF(Password, Salt + INT_32_BE(i))\n    (prfW = PRFSalt._cloneInto(prfW)).update(arr).digestInto(u);\n    Ti.set(u.subarray(0, Ti.length));\n    for (let ui = 1; ui < c; ui++) {\n      // Uc = PRF(Password, Uc−1)\n      PRF._cloneInto(prfW).update(u).digestInto(u);\n      for (let i = 0; i < Ti.length; i++) Ti[i] ^= u[i];\n    }\n  }\n  return pbkdf2Output(PRF, PRFSalt, DK, prfW, u);\n}\n/**\n * PBKDF2-HMAC: RFC 2898 key derivation function. Async version.\n * @example\n * await pbkdf2Async(sha256, 'password', 'salt', { dkLen: 32, c: 500_000 });\n */\nfunction pbkdf2Async(_x, _x2, _x3, _x4) {\n  return _pbkdf2Async.apply(this, arguments);\n} //# sourceMappingURL=pbkdf2.js.map\nfunction _pbkdf2Async() {\n  _pbkdf2Async = _asyncToGenerator(function* (hash, password, salt, opts) {\n    const {\n      c,\n      dkLen,\n      asyncTick,\n      DK,\n      PRF,\n      PRFSalt\n    } = pbkdf2Init(hash, password, salt, opts);\n    let prfW; // Working copy\n    const arr = new Uint8Array(4);\n    const view = (0, utils_ts_1.createView)(arr);\n    const u = new Uint8Array(PRF.outputLen);\n    // DK = T1 + T2 + ⋯ + Tdklen/hlen\n    for (let ti = 1, pos = 0; pos < dkLen; ti++, pos += PRF.outputLen) {\n      // Ti = F(Password, Salt, c, i)\n      const Ti = DK.subarray(pos, pos + PRF.outputLen);\n      view.setInt32(0, ti, false);\n      // F(Password, Salt, c, i) = U1 ^ U2 ^ ⋯ ^ Uc\n      // U1 = PRF(Password, Salt + INT_32_BE(i))\n      (prfW = PRFSalt._cloneInto(prfW)).update(arr).digestInto(u);\n      Ti.set(u.subarray(0, Ti.length));\n      yield (0, utils_ts_1.asyncLoop)(c - 1, asyncTick, () => {\n        // Uc = PRF(Password, Uc−1)\n        PRF._cloneInto(prfW).update(u).digestInto(u);\n        for (let i = 0; i < Ti.length; i++) Ti[i] ^= u[i];\n      });\n    }\n    return pbkdf2Output(PRF, PRFSalt, DK, prfW, u);\n  });\n  return _pbkdf2Async.apply(this, arguments);\n}","map":null,"metadata":{},"sourceType":"script","externalDependencies":[]}