import { base64UrlEncode } from './utils';

export function generateState(): string {
  return base64UrlEncode(window.crypto.getRandomValues(new Uint8Array(32)));
}

export function generateCodeVerifier(): string {
  // RFC 7636 recommends a length between 43 and 128 characters.
  return base64UrlEncode(window.crypto.getRandomValues(new Uint8Array(64)));
}

export async function generateCodeChallenge(verifier: string): Promise<string> {
  const data = new TextEncoder().encode(verifier);
  const digest = await window.crypto.subtle.digest('SHA-256', data);
  return base64UrlEncode(new Uint8Array(digest));
}
