/**
 * Minimal standard-base64 <-> bytes helpers for the DO host worker.
 *
 * Used by PasswordHashDO to encode the argon2id salt + peppered digest into the
 * PHC-style hash string. workerd provides global btoa/atob (binary strings).
 */

export function bytesToBase64(bytes: Uint8Array): string {
  let bin = '';
  for (let i = 0; i < bytes.length; i++) {
    bin += String.fromCharCode(bytes[i]!);
  }
  return btoa(bin);
}

/** Strict-ish decode: throws on input that is not valid base64. */
export function base64ToBytes(b64: string): Uint8Array {
  const bin = atob(b64);
  const out = new Uint8Array(bin.length);
  for (let i = 0; i < bin.length; i++) {
    out[i] = bin.charCodeAt(i);
  }
  return out;
}
