/*! noble-hashes - MIT License (c) 2021 Paul Miller (paulmillr.com) */
// https://github.com/paulmillr/noble-hashes/pull/13
import { hmac } from "./hmac.js"; // prettier-ignore

import { createView, toBytes, assertNumber, assertHash, checkOpts, asyncLoop } from "./utils.js"; // PBKDF (RFC 2898)

// Common prologue and epilogue for sync/async functions
function pbkdf2Init(hash, _password, _salt, _opts) {
  assertHash(hash);
  const opts = checkOpts({
    dkLen: 32,
    asyncTick: 10
  }, _opts);
  const {
    c,
    dkLen,
    asyncTick
  } = opts;
  assertNumber(c);
  assertNumber(dkLen);
  assertNumber(asyncTick);
  if (c < 1) throw new Error('PBKDF2: iterations (c) should be >= 1');
  const password = toBytes(_password);
  const salt = toBytes(_salt); // DK = PBKDF2(PRF, Password, Salt, c, dkLen);

  const DK = new Uint8Array(dkLen); // U1 = PRF(Password, Salt + INT_32_BE(i))

  const PRF = hmac.init(hash, password);

  const PRFSalt = PRF._cloneInto().update(salt);

  return {
    c,
    dkLen,
    asyncTick,
    DK,
    PRF,
    PRFSalt
  };
}

function pbkdf2Output(PRF, PRFSalt, DK, prfW, u) {
  PRF.destroy();
  PRFSalt.destroy();
  if (prfW) prfW.destroy();
  u.fill(0);
  return DK;
}

export function pbkdf2(hash, password, salt, _opts) {
  const {
    c,
    dkLen,
    DK,
    PRF,
    PRFSalt
  } = pbkdf2Init(hash, password, salt, _opts);
  let prfW; // Working copy

  const arr = new Uint8Array(4);
  const view = createView(arr);
  const u = new Uint8Array(PRF.outputLen); // DK = T1 + T2 + ⋯ + Tdklen/hlen

  for (let ti = 1, pos = 0; pos < dkLen; ti++, pos += PRF.outputLen) {
    // Ti = F(Password, Salt, c, i)
    const Ti = DK.subarray(pos, pos + PRF.outputLen);
    view.setInt32(0, ti, false); // F(Password, Salt, c, i) = U1 ^ U2 ^ ⋯ ^ Uc
    // U1 = PRF(Password, Salt + INT_32_BE(i))

    (prfW = PRFSalt._cloneInto(prfW)).update(arr).digestInto(u);

    Ti.set(u.subarray(0, Ti.length));

    for (let ui = 1; ui < c; ui++) {
      // Uc = PRF(Password, Uc−1)
      PRF._cloneInto(prfW).update(u).digestInto(u);

      for (let i = 0; i < Ti.length; i++) Ti[i] ^= u[i];
    }
  }

  return pbkdf2Output(PRF, PRFSalt, DK, prfW, u);
}
export async function pbkdf2Async(hash, password, salt, _opts) {
  const {
    c,
    dkLen,
    asyncTick,
    DK,
    PRF,
    PRFSalt
  } = pbkdf2Init(hash, password, salt, _opts);
  let prfW; // Working copy

  const arr = new Uint8Array(4);
  const view = createView(arr);
  const u = new Uint8Array(PRF.outputLen); // DK = T1 + T2 + ⋯ + Tdklen/hlen

  for (let ti = 1, pos = 0; pos < dkLen; ti++, pos += PRF.outputLen) {
    // Ti = F(Password, Salt, c, i)
    const Ti = DK.subarray(pos, pos + PRF.outputLen);
    view.setInt32(0, ti, false); // F(Password, Salt, c, i) = U1 ^ U2 ^ ⋯ ^ Uc
    // U1 = PRF(Password, Salt + INT_32_BE(i))

    (prfW = PRFSalt._cloneInto(prfW)).update(arr).digestInto(u);

    Ti.set(u.subarray(0, Ti.length));
    await asyncLoop(c - 1, asyncTick, i => {
      // Uc = PRF(Password, Uc−1)
      PRF._cloneInto(prfW).update(u).digestInto(u);

      for (let i = 0; i < Ti.length; i++) Ti[i] ^= u[i];
    });
  }

  return pbkdf2Output(PRF, PRFSalt, DK, prfW, u);
}