Download src/shared/crypto.ts from xjackkx/s2: direct link, hf CLI and curl.
- Browser
- Download file 922 Bytes
-
https://huggingface.co/spaces/xjackkx/s2/resolve/main/src/shared/crypto.ts
- Command line
-
hf download hf://spaces/xjackkx/s2/src/shared/crypto.ts
-
curl -L -o crypto.ts https://huggingface.co/spaces/xjackkx/s2/resolve/main/src/shared/crypto.ts
922 Bytes
| export function randomBytes(length: number): Uint8Array { | |
| const bytes = new Uint8Array(length); | |
| crypto.getRandomValues(bytes); | |
| return bytes; | |
| } | |
| export function bytesToHex(bytes: Uint8Array): string { | |
| const nativeToHex = (bytes as Uint8Array & { toHex?: () => string }).toHex; | |
| if (typeof nativeToHex === "function") return nativeToHex.call(bytes); | |
| return Array.from(bytes) | |
| .map((byte) => byte.toString(16).padStart(2, "0")) | |
| .join(""); | |
| } | |
| export function randHex(length: number): string { | |
| return bytesToHex(randomBytes(Math.ceil(length / 2))).slice(0, length); | |
| } | |
| export function uuid(): string { | |
| return crypto.randomUUID(); | |
| } | |
| export function timingSafeStringEqual(a: string, b: string): boolean { | |
| const max = Math.max(a.length, b.length); | |
| let diff = a.length ^ b.length; | |
| for (let index = 0; index < max; index++) { | |
| diff |= (a.charCodeAt(index) || 0) ^ (b.charCodeAt(index) || 0); | |
| } | |
| return diff === 0; | |
| } | |