Last active
June 14, 2021 20:01
-
-
Save jaames/1f0a68a2575a287eeabe1710d8eb2ad4 to your computer and use it in GitHub Desktop.
Tiny Typescript implementation of CRC32
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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