/** * Generate a new API key in format: ak_live_<32 hex chars> */ export function generateApiKey(): string { const array = new Uint8Array(16); crypto.getRandomValues(array); const hex = Array.from(array) .map((b) => b.toString(16).padStart(2, "0")) .join(""); return `ak_live_${hex}`; } /** * Hash API key using SHA-256 */ export async function hashApiKey(apiKey: string): Promise { const encoder = new TextEncoder(); const data = encoder.encode(apiKey); const hashBuffer = await crypto.subtle.digest("SHA-256", data); const hashArray = Array.from(new Uint8Array(hashBuffer)); return hashArray.map((b) => b.toString(16).padStart(2, "0")).join(""); }