Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | 1x 1x 1x 16x 1x 6x 6x 4x 2x 1x 1x | function uuidFromRandomValues(randomValues: Uint8Array): string {
randomValues[6] = ((randomValues[6] ?? 0) & 0x0f) | 0x40;
randomValues[8] = ((randomValues[8] ?? 0) & 0x3f) | 0x80;
const hexadecimal = Array.from(
randomValues,
(value) => value.toString(16).padStart(2, "0"),
).join("");
return [
hexadecimal.slice(0, 8),
hexadecimal.slice(8, 12),
hexadecimal.slice(12, 16),
hexadecimal.slice(16, 20),
hexadecimal.slice(20),
].join("-");
}
/**
* Returns a cryptographically secure UUID v4 for client-visible command keys.
* Command and idempotency identities must never fall back to Math.random().
*/
export function secureRandomUUID(): string {
const webCrypto = globalThis.crypto;
if (typeof webCrypto?.randomUUID === "function") {
return webCrypto.randomUUID();
}
if (typeof webCrypto?.getRandomValues === "function") {
return uuidFromRandomValues(webCrypto.getRandomValues(new Uint8Array(16)));
}
throw new Error(
"当前环境不支持 Web Crypto,无法安全生成命令幂等键。",
);
}
|