UNPKG

node-csfd-api

Version:

ČSFD API in JavaScript. Amazing NPM library for scrapping csfd.cz :)

167 lines (166 loc) 3.59 kB
//#region src/anubis/sha256.ts const K = new Uint32Array([ 1116352408, 1899447441, 3049323471, 3921009573, 961987163, 1508970993, 2453635748, 2870763221, 3624381080, 310598401, 607225278, 1426881987, 1925078388, 2162078206, 2614888103, 3248222580, 3835390401, 4022224774, 264347078, 604807628, 770255983, 1249150122, 1555081692, 1996064986, 2554220882, 2821834349, 2952996808, 3210313671, 3336571891, 3584528711, 113926993, 338241895, 666307205, 773529912, 1294757372, 1396182291, 1695183700, 1986661051, 2177026350, 2456956037, 2730485921, 2820302411, 3259730800, 3345764771, 3516065817, 3600352804, 4094571909, 275423344, 430227734, 506948616, 659060556, 883997877, 958139571, 1322822218, 1537002063, 1747873779, 1955562222, 2024104815, 2227730452, 2361852424, 2428436474, 2756734187, 3204031479, 3329325298 ]); const rotr = (x, n) => x >>> n | x << 32 - n; const HEX = "0123456789abcdef"; /** SHA-256 digest of `text` (UTF-8) as 32 raw bytes. */ const sha256 = (text) => { const bytes = new TextEncoder().encode(text); const length = bytes.length; const withOne = length + 1; const total = withOne + (56 - withOne % 64 + 64) % 64 + 8; const message = new Uint8Array(total); message.set(bytes); message[length] = 128; const bitLengthHi = Math.floor(length / 536870912); const bitLengthLo = length * 8 >>> 0; message[total - 8] = bitLengthHi >>> 24 & 255; message[total - 7] = bitLengthHi >>> 16 & 255; message[total - 6] = bitLengthHi >>> 8 & 255; message[total - 5] = bitLengthHi & 255; message[total - 4] = bitLengthLo >>> 24 & 255; message[total - 3] = bitLengthLo >>> 16 & 255; message[total - 2] = bitLengthLo >>> 8 & 255; message[total - 1] = bitLengthLo & 255; let h0 = 1779033703; let h1 = 3144134277; let h2 = 1013904242; let h3 = 2773480762; let h4 = 1359893119; let h5 = 2600822924; let h6 = 528734635; let h7 = 1541459225; const w = /* @__PURE__ */ new Uint32Array(64); for (let offset = 0; offset < total; offset += 64) { for (let i = 0; i < 16; i++) { const j = offset + i * 4; w[i] = message[j] << 24 | message[j + 1] << 16 | message[j + 2] << 8 | message[j + 3]; } 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 = h0; let b = h1; let c = h2; let d = h3; let e = h4; let f = h5; let g = h6; let h = h7; 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 t1 = h + S1 + ch + K[i] + w[i] | 0; const t2 = (rotr(a, 2) ^ rotr(a, 13) ^ rotr(a, 22)) + (a & b ^ a & c ^ b & c) | 0; h = g; g = f; f = e; e = d + t1 | 0; d = c; c = b; b = a; a = t1 + t2 | 0; } h0 = h0 + a | 0; h1 = h1 + b | 0; h2 = h2 + c | 0; h3 = h3 + d | 0; h4 = h4 + e | 0; h5 = h5 + f | 0; h6 = h6 + g | 0; h7 = h7 + h | 0; } const digest = /* @__PURE__ */ new Uint8Array(32); const words = [ h0, h1, h2, h3, h4, h5, h6, h7 ]; for (let i = 0; i < 8; i++) { const v = words[i]; digest[i * 4] = v >>> 24 & 255; digest[i * 4 + 1] = v >>> 16 & 255; digest[i * 4 + 2] = v >>> 8 & 255; digest[i * 4 + 3] = v & 255; } return digest; }; const toHex = (bytes) => { let hex = ""; for (let i = 0; i < bytes.length; i++) hex += HEX[bytes[i] >> 4] + HEX[bytes[i] & 15]; return hex; }; //#endregion export { sha256, toHex }; //# sourceMappingURL=sha256.js.map