export type PasswordHashDeriveRequest = {
  plainB64: string
  saltB64: string
  iterations: number
  hash: string
  keyBytes: number
}

export type PasswordHashVerifyRequest = {
  plainB64: string
  storedHash: string
}

export type PasswordHashHashRequest = {
  plainB64: string
}

export type PasswordHashVerifyResponse = {
  ok: boolean
  needsRehash: boolean
  rehashHash?: string
}

export function decodeBase64Bytes(b64: string): Uint8Array | null {
  try {
    return new Uint8Array(atob(b64).split('').map((c) => c.charCodeAt(0)))
  } catch {
    return null
  }
}

export function encodeBase64Bytes(bytes: Uint8Array | ArrayBuffer): string {
  const arr = bytes instanceof Uint8Array ? bytes : new Uint8Array(bytes)
  return btoa(String.fromCharCode(...arr))
}
