OPC
This commit is contained in:
22
lib/slug.ts
Normal file
22
lib/slug.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
export function generateSlug(): string {
|
||||
const cryptoApi = globalThis.crypto;
|
||||
if (!cryptoApi?.getRandomValues) {
|
||||
throw new Error("Web Crypto API is not available");
|
||||
}
|
||||
|
||||
const timestamp = Date.now().toString().padStart(13, "0").slice(-13);
|
||||
const prefixLength = 64 - 13;
|
||||
const bytes = new Uint8Array(Math.ceil(prefixLength / 8));
|
||||
cryptoApi.getRandomValues(bytes);
|
||||
|
||||
let prefix = "";
|
||||
for (const byte of bytes) {
|
||||
for (let bit = 0; bit < 8; bit += 1) {
|
||||
prefix += (byte >> bit) & 1 ? "O" : "o";
|
||||
if (prefix.length === prefixLength) break;
|
||||
}
|
||||
if (prefix.length === prefixLength) break;
|
||||
}
|
||||
|
||||
return `${prefix}${timestamp}`;
|
||||
}
|
||||
Reference in New Issue
Block a user