LazyTransform.prototype);
ObjectSetPrototypeOf(Hmac, LazyTransform);

Hmac.prototype.update = Hash.prototype.update;

Hmac.prototype.digest = function digest(outputEncoding) {
  const state = this[kState];
  outputEncoding = outputEncoding || getDefaultEncoding();

  if (state[kFinalized]) {
    const buf = Buffer.from('');
    return outputEncoding === 'buffer' ? buf : buf.toString(outputEncoding);
  }

  // Explicit conversion for backward compatibility.
  const ret = this[kHandle].digest(`${outputEncoding}`);
  state[kFinalized] = true;
  return ret;
};

Hmac.prototype._flush = Hash.prototype._flush;
Hmac.prototype._transform = Hash.prototype._transform;

// Implementation for WebCrypto subtle.digest()

async function asyncDigest(algorithm, data) {
  algorithm = normalizeAlgorithm(algorithm);
  data = getArrayBufferOrView(data, 'data');
  validateMaxBufferLength(data, 'data');

  if (algorithm.length !== undefined)
    validateUint32(algorithm.length, 'algorithm.length');

  return jobPromise(new HashJob(
    kCryptoJobAsync,
    normalizeHashName(algorithm.name),
    data,
    algorithm.length));
}

module.exports = {
  Hash,
  Hmac,
  asyncDigest,
};
