import { createHash, randomBytes } from 'crypto';

export function base64Url(buffer: Buffer): string {
  return buffer
    .toString('base64')
    .replace(/\+/g, '-')
    .replace(/\//g, '_')
    .replace(/=/g, '');
}

export function generateCodeVerifier(): string {
  return base64Url(randomBytes(64));
}

export function generateCodeChallenge(codeVerifier: string): string {
  return base64Url(createHash('sha256').update(codeVerifier).digest());
}

export function generateState(): string {
  return base64Url(randomBytes(32));
}
