/** * Isomorphic SHA-256 implementation that works in both Node.js and the browser. * * Avoids importing `node:crypto`, which Vite externalizes for browser builds, * causing runtime errors when this module is pulled into client bundles. * * Uses the same SHA-256 algorithm as `node:crypto`, producing identical hex output. */ // SHA-256 constants: first 32 bits of the fractional parts of the cube roots of the first 64 primes const K = new Uint32Array([ 0x428a2f98, 0x71374491, 0xb5c0fbcf, 0xe9b5dba5, 0x3956c25b, 0x59f111f1, 0x923f82a4, 0xab1c5ed5, 0xd807aa98, 0x12835b01, 0x243185be, 0x550c7dc3, 0x72be5d74, 0x80deb1fe, 0x9bdc06a7, 0xc19bf174, 0xe49b69c1, 0xefbe4786, 0x0fc19dc6, 0x240ca1cc, 0x2de92c6f, 0x4a7484aa, 0x5cb0a9dc, 0x76f988da, 0x983e5152, 0xa831c66d, 0xb00327c8, 0xbf597fc7, 0xc6e00bf3, 0xd5a79147, 0x06ca6351, 0x14292967, 0x27b70a85, 0x2e1b2138, 0x4d2c6dfc, 0x53380d13, 0x650a7354, 0x766a0abb, 0x81c2c92e, 0x92722c85, 0xa2bfe8a1, 0xa81a664b, 0xc24b8b70, 0xc76c51a3, 0xd192e819, 0xd6990624, 0xf40e3585, 0x106aa070, 0x19a4c116, 0x1e376c08, 0x2748774c, 0x34b0bcb5, 0x391c0cb3, 0x4ed8aa4a, 0x5b9cca4f, 0x682e6ff3, 0x748f82ee, 0x78a5636f, 0x84c87814, 0x8cc70208, 0x90befffa, 0xa4506ceb, 0xbef9a3f7, 0xc67178f2, ]); const rotr = (x: number, n: number): number => (x >>> n) | (x << (32 - n)); const sha256Compress = (state: Uint32Array, block: Uint32Array): void => { const w = new Uint32Array(64); for (let i = 0; i < 16; i++) w[i] = block[i]; for (let i = 16; i < 64; i++) { const s0 = rotr(w[i - 15], 7) ^ rotr(w[i - 15], 18) ^ (w[i - 15] >>> 3); const s1 = rotr(w[i - 2], 17) ^ rotr(w[i - 2], 19) ^ (w[i - 2] >>> 10); w[i] = (w[i - 16] + s0 + w[i - 7] + s1) | 0; } let a = state[0], b = state[1], c = state[2], d = state[3]; let e = state[4], f = state[5], g = state[6], h = state[7]; for (let i = 0; i < 64; i++) { const S1 = rotr(e, 6) ^ rotr(e, 11) ^ rotr(e, 25); const ch = (e & f) ^ (~e & g); const temp1 = (h + S1 + ch + K[i] + w[i]) | 0; const S0 = rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22); const maj = (a & b) ^ (a & c) ^ (b & c); const temp2 = (S0 + maj) | 0; h = g; g = f; f = e; e = (d + temp1) | 0; d = c; c = b; b = a; a = (temp1 + temp2) | 0; } state[0] = (state[0] + a) | 0; state[1] = (state[1] + b) | 0; state[2] = (state[2] + c) | 0; state[3] = (state[3] + d) | 0; state[4] = (state[4] + e) | 0; state[5] = (state[5] + f) | 0; state[6] = (state[6] + g) | 0; state[7] = (state[7] + h) | 0; }; const utf8Encode = (str: string): Uint8Array => { if (typeof TextEncoder !== "undefined") { return new TextEncoder().encode(str); } // Fallback for environments without TextEncoder const bytes: number[] = []; for (let i = 0; i < str.length; i++) { let c = str.charCodeAt(i); if (c < 0x80) { bytes.push(c); } else if (c < 0x800) { bytes.push(0xc0 | (c >> 6), 0x80 | (c & 0x3f)); } else if (c >= 0xd800 && c < 0xdc00 && i + 1 < str.length) { const next = str.charCodeAt(++i); c = 0x10000 + ((c - 0xd800) << 10) + (next - 0xdc00); bytes.push(0xf0 | (c >> 18), 0x80 | ((c >> 12) & 0x3f), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f)); } else { bytes.push(0xe0 | (c >> 12), 0x80 | ((c >> 6) & 0x3f), 0x80 | (c & 0x3f)); } } return new Uint8Array(bytes); }; const sha256 = (data: Uint8Array): Uint8Array => { const state = new Uint32Array([ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19, ]); const bitLen = data.length * 8; // Padding: append 1 bit, then zeros, then 64-bit big-endian length const padded = new Uint8Array(Math.ceil((data.length + 9) / 64) * 64); padded.set(data); padded[data.length] = 0x80; // Write the 64-bit big-endian bit length at the end const view = new DataView(padded.buffer); // For strings up to ~256 MB, the high 32 bits of the bit length are zero view.setUint32(padded.length - 4, bitLen, false); if (bitLen > 0xffffffff) { view.setUint32(padded.length - 8, Math.floor(bitLen / 0x100000000), false); } // Process each 64-byte block const block = new Uint32Array(16); for (let offset = 0; offset < padded.length; offset += 64) { for (let i = 0; i < 16; i++) { block[i] = view.getUint32(offset + i * 4, false); } sha256Compress(state, block); } const result = new Uint8Array(32); const resultView = new DataView(result.buffer); for (let i = 0; i < 8; i++) { resultView.setUint32(i * 4, state[i], false); } return result; }; const hexFromBytes = (bytes: Uint8Array): string => { let hex = ""; for (let i = 0; i < bytes.length; i++) { hex += bytes[i].toString(16).padStart(2, "0"); } return hex; }; /** * Compute a SHA-256 hex digest of a UTF-8 string. * Drop-in replacement for `createHash("sha256").update(value).digest("hex")`. */ export const sha256Hex = (value: string): string => hexFromBytes(sha256(utf8Encode(value)));