@technobuddha/library
Version:
A large library of useful functions
30 lines (23 loc) • 664 B
text/typescript
import isEmpty from 'lodash/isEmpty';
const crcTable: number[] = [];
/**
* Compute the CRC32 checksum for a string
*
* @param input The string
* @return the CRC32 checksum
*/
export function crc32(input: string): number {
if(isEmpty(crcTable)) {
for(let n = 0; n < 256; ++n) {
let c = n;
for(let k = 0; k < 8; ++k)
c = ((c & 1) ? (0xEDB88320 ^ (c >>> 1)) : (c >>> 1));
crcTable[n] = c;
}
}
let crc = -1;
for(let i = 0; i < input.length; ++i)
crc = (crc >>> 8) ^ crcTable[(crc ^ input.charCodeAt(i)) & 0xFF];
return (crc ^ -1) >>> 0;
}
export default crc32;