/** * Pure JavaScript SHA-256 implementation * Conforms to the crypto-js API for drop-in replacement */ /* eslint-disable no-bitwise */ // SHA-256 constants (first 32 bits of the fractional parts of the cube roots of the first 64 primes) const K = [ 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, ] // Initial hash values (first 32 bits of the fractional parts of the square roots of the first 8 primes) const H = [ 0x6a09e667, 0xbb67ae85, 0x3c6ef372, 0xa54ff53a, 0x510e527f, 0x9b05688c, 0x1f83d9ab, 0x5be0cd19, ] /** Right rotate 32-bit integer */ function rotr(n: number, x: number): number { return (x >>> n) | (x << (32 - n)) } /** SHA-256 choice function */ function ch(x: number, y: number, z: number): number { return (x & y) ^ (~x & z) } /** SHA-256 majority function */ function maj(x: number, y: number, z: number): number { return (x & y) ^ (x & z) ^ (y & z) } /** SHA-256 Σ0 function */ function sigma0(x: number): number { return rotr(2, x) ^ rotr(13, x) ^ rotr(22, x) } /** SHA-256 Σ1 function */ function sigma1(x: number): number { return rotr(6, x) ^ rotr(11, x) ^ rotr(25, x) } /** SHA-256 σ0 function */ function gamma0(x: number): number { return rotr(7, x) ^ rotr(18, x) ^ (x >>> 3) } /** SHA-256 σ1 function */ function gamma1(x: number): number { return rotr(17, x) ^ rotr(19, x) ^ (x >>> 10) } /** Convert string to UTF-8 byte array */ function stringToBytes(str: string): number[] { const bytes: number[] = [] for (let i = 0; i < str.length; i++) { let charCode = str.charCodeAt(i) if (charCode < 0x80) { bytes.push(charCode) } else if (charCode < 0x800) { bytes.push(0xc0 | (charCode >> 6), 0x80 | (charCode & 0x3f)) } else if (charCode < 0xd800 || charCode >= 0xe000) { bytes.push(0xe0 | (charCode >> 12), 0x80 | ((charCode >> 6) & 0x3f), 0x80 | (charCode & 0x3f)) } else { // Surrogate pair i++ charCode = 0x10000 + (((charCode & 0x3ff) << 10) | (str.charCodeAt(i) & 0x3ff)) bytes.push( 0xf0 | (charCode >> 18), 0x80 | ((charCode >> 12) & 0x3f), 0x80 | ((charCode >> 6) & 0x3f), 0x80 | (charCode & 0x3f) ) } } return bytes } /** Preprocess message: pad and convert to 512-bit blocks */ function preprocessMessage(message: number[]): Uint32Array { const messageLength = message.length const bitLength = messageLength * 8 // Append the '1' bit (plus zero padding to make 8 bits) message.push(0x80) // Append zeros until message length ≡ 448 (mod 512) // This leaves 64 bits for the length while (message.length % 64 !== 56) { message.push(0x00) } // Append original message length as 64-bit big-endian integer // JavaScript numbers are 53-bit safe, so we split into two 32-bit parts const lengthHigh = Math.floor(bitLength / 0x100000000) const lengthLow = bitLength >>> 0 for (let i = 24; i >= 0; i -= 8) { message.push((lengthHigh >>> i) & 0xff) } for (let i = 24; i >= 0; i -= 8) { message.push((lengthLow >>> i) & 0xff) } // Convert byte array to 32-bit word array const words = new Uint32Array(message.length / 4) for (let i = 0; i < message.length; i += 4) { words[i / 4] = (message[i]! << 24) | (message[i + 1]! << 16) | (message[i + 2]! << 8) | message[i + 3]! } return words } /** Process a single 512-bit chunk */ function processChunk(chunk: Uint32Array, hash: number[]): void { const W = new Uint32Array(64) // Prepare message schedule for (let t = 0; t < 16; t++) { W[t] = chunk[t]! } for (let t = 16; t < 64; t++) { W[t] = (gamma1(W[t - 2]!) + W[t - 7]! + gamma0(W[t - 15]!) + W[t - 16]!) >>> 0 } // Initialize working variables let a = hash[0]! let b = hash[1]! let c = hash[2]! let d = hash[3]! let e = hash[4]! let f = hash[5]! let g = hash[6]! let h = hash[7]! // Main loop for (let t = 0; t < 64; t++) { const T1 = (h + sigma1(e) + ch(e, f, g) + K[t]! + W[t]!) >>> 0 const T2 = (sigma0(a) + maj(a, b, c)) >>> 0 h = g g = f f = e e = (d + T1) >>> 0 d = c c = b b = a a = (T1 + T2) >>> 0 } // Add compressed chunk to current hash value hash[0] = (hash[0]! + a) >>> 0 hash[1] = (hash[1]! + b) >>> 0 hash[2] = (hash[2]! + c) >>> 0 hash[3] = (hash[3]! + d) >>> 0 hash[4] = (hash[4]! + e) >>> 0 hash[5] = (hash[5]! + f) >>> 0 hash[6] = (hash[6]! + g) >>> 0 hash[7] = (hash[7]! + h) >>> 0 } /** Compute SHA-256 hash and return as byte array */ function sha256Raw(message: string): number[] { const bytes = stringToBytes(message) const preprocessed = preprocessMessage(bytes) const hash = [...H] // Copy initial hash values // Process each 512-bit chunk for (let i = 0; i < preprocessed.length; i += 16) { const chunk = preprocessed.subarray(i, i + 16) processChunk(chunk, hash) } // Convert hash to byte array const result: number[] = [] for (let i = 0; i < 8; i++) { result.push((hash[i]! >>> 24) & 0xff) result.push((hash[i]! >>> 16) & 0xff) result.push((hash[i]! >>> 8) & 0xff) result.push(hash[i]! & 0xff) } return result } /** Hash result object with toString methods */ class HashResult { private bytes: number[] constructor(bytes: number[]) { this.bytes = bytes } /** Convert hash to hexadecimal string */ toString(encoding?: { stringify: (bytes: number[]) => string }): string { if (encoding === Hex) { return this.bytes.map(b => b.toString(16).padStart(2, '0')).join('') } // Default to hex if no encoding specified return this.bytes.map(b => b.toString(16).padStart(2, '0')).join('') } } /** Hex encoding object (mimics crypto-js enc.Hex) */ export const Hex = { stringify: (bytes: number[]) => bytes.map(b => b.toString(16).padStart(2, '0')).join(''), } /** * Compute SHA-256 hash of a string * @param message - The message to hash * @returns HashResult object with toString method */ export function SHA256(message: string): HashResult { const hash = sha256Raw(message) return new HashResult(hash) } /** Encoding utilities (mimics crypto-js enc) */ export const enc = { Hex, }