File size: 641 Bytes
c09f67c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
import { randomBytes } from "node:crypto";

/**
 * Generates a new API key with the format mid_{random_string}
 * @returns A new API key string
 */
export function generateApiKey(): string {
  // Generate 32 random bytes and convert to hex
  const randomString = randomBytes(32).toString("hex");
  return `mid_${randomString}`;
}

/**
 * Validates if a string is a valid API key format
 * @param key The key to validate
 * @returns True if the key starts with 'mid-' and has the correct length
 */
export function isValidApiKeyFormat(key: string): boolean {
  return key.startsWith("mid_") && key.length === 68; // mid_ (4) + 64 hex chars
}