import * as nodeCrypto from "node:crypto"; // Node 20+ globally available Crypto Module -OR- Browser Crypto Module const globalCrypto = (globalThis.crypto)?.webcrypto || globalThis.crypto; // Globally available crypto (Node or Browser) -OR- Node Web Crypto (We can get rid of this in April 2025 after Node 18 is EOL) // biome-ignore lint/style/noRestrictedImports: This is where we re-export our version of the module const crypto = globalCrypto || nodeCrypto.webcrypto; const { subtle, getRandomValues, randomUUID } = crypto; export { subtle, getRandomValues, randomUUID }; export default crypto; export async function computeContentHash( data: string | Uint8Array, ): Promise { const bytes: Uint8Array = typeof data === "string" ? new TextEncoder().encode(data) : new Uint8Array(data); const hashBuffer = await subtle.digest("SHA-256", bytes); return Array.from(new Uint8Array(hashBuffer)) .map((b) => b.toString(16).padStart(2, "0")) .join("") .slice(0, 8); }