Skip to content

Instantly share code, notes, and snippets.

@jaames
Last active Jun 14, 2021
Embed
What would you like to do?
Tiny Typescript implementation of CRC32
const POLYNOMIAL = -306674912;
let crc32_table: Int32Array = undefined;
export function Crc32(bytes: Uint8Array, crc=0xFFFFFFFF) {
if (crc32_table === undefined)
calcTable();
for (let i = 0; i < bytes.length; ++i)
crc = crc32_table[(crc ^ bytes[i]) & 0xff] ^ (crc >>> 8);
return (crc ^ -1) >>> 0;
}
function calcTable() {
crc32_table = new Int32Array(256);
for(let i = 0; i < 256; i++) {
let r = i;
for (let bit = 8; bit > 0; --bit)
r = ((r & 1) ? ((r >>> 1) ^ POLYNOMIAL) : (r >>> 1));
crc32_table[i] = r;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment