// Copyright 2024 Signal Messenger, LLC
// SPDX-License-Identifier: AGPL-3.0-only

import { createHmac } from 'node:crypto';
import { Transform } from 'node:stream';
import type { Duplex } from 'node:stream';

import { HashType } from '../types/Crypto.std.ts';

/** @testexport */
export const MAC_KEY_SIZE = 32;

/** @testexport */
export const MAC_SIZE = 32;

export function appendMacStream(
  macKey: Uint8Array<ArrayBuffer>,
  onMac?: (mac: Uint8Array<ArrayBuffer>) => undefined
): Duplex {
  if (macKey.byteLength !== MAC_KEY_SIZE) {
    throw new Error('appendMacStream: invalid macKey length');
  }

  const hmac = createHmac(HashType.size256, macKey);
  return new Transform({
    transform(chunk, _encoding, callback) {
      try {
        hmac.update(chunk);
        callback(null, chunk);
      } catch (error) {
        callback(error);
      }
    },
    flush(callback) {
      try {
        const mac = hmac.digest();
        onMac?.(mac);
        callback(null, mac);
      } catch (error) {
        callback(error);
      }
    },
  });
}
