Files
ai-drama-platform/server/api-client-secrets.mjs
T

26 lines
658 B
JavaScript

import { createHash, randomBytes } from "node:crypto";
const KEY_PREFIX = "local-";
export function hashApiClientKey(value) {
return createHash("sha256").update(String(value || "")).digest("hex");
}
export function issueApiClientKey() {
const secret = `${KEY_PREFIX}${randomBytes(24).toString("base64url")}`;
return {
secret,
hash: hashApiClientKey(secret),
prefix: secret.slice(0, 12)
};
}
export function apiClientStorageMarker(hash) {
return `hash:${String(hash || "")}`;
}
export function apiClientPreview(prefix) {
const value = String(prefix || "");
return value ? `${value}****` : "仅创建或轮换时显示一次";
}