import { createHash, randomBytes } from "crypto";

const TOKEN_PREFIX = "chj_";

export function hashServiceToken(rawToken: string): string {
  return createHash("sha256").update(rawToken, "utf8").digest("hex");
}

export function generateServiceToken(): string {
  return `${TOKEN_PREFIX}${randomBytes(24).toString("base64url")}`;
}

export function tokenKeyPrefix(rawToken: string): string {
  return rawToken.slice(0, Math.min(12, rawToken.length));
}
