Calculates unsigned CRC32. Optimized Cycle Redundancy Check.


!npm type definitions

Cycle Redundancy Check 32 (CRC32).
Calculates unsigned 32 bit checksum for 0xEDB88320 polynomial.
Speed optimization is done through using pre-calculated values from lookup tables.
Performance totally depends on the hardware.
Results below are made on a very basic hardware with NodeJS v18.16.0, IntelCore i5 1.6 GHz CPU and DDR3 RAM.
| Processor | Rate | CRC32OPS | Input | Time |
| --- | --- | --- | --- | --- |
| CRC32 | 1.2047 Gbit/s | 147059 | 10K random 1024-Bytes items | 68 msec |
| CRC32Streams | 850.1594 Mbit/s | - | 100MB random data file | 941 msec |
``JavaScript`
class CRC32
// Browser and NodeJS compatible.
`JavaScript`
class CRC32Streams
// Depends on NodeJS "stream" module.
"@tsxper/crc32" package is originally written in TypeScript.
TypeScript type definitions are included.
UTF8 characters are fully supported.
`JavaScript`
crc32.calculate('español sí');
`JavaScript`
const crc32 = new CRC32();
crc32.calculate('crc32 test');
// or
crc32.forString('crc32 test');
`JavaScript`
const crc32 = new CRC32();
crc32.forBytes(new TextEncoder().encode('crc32 test'));
> Buffer extends Uint8Array.
`JavaScript`
const crc32 = new CRC32();
crc32.forBuffer(new TextEncoder().encode('crc32 test'));
`JavaScript`
const crc32 = new CRC32Streams();
const checksum = await crc32.forFile(filePath);
See "Compatibility".
HTML Copy/Paste Example
> This example also shows how to calculate CRC32 for ArrayBuffer.
` Calculating crc32 for text "hello crc32": CRC32 of the file: html`
For example, for sequence of events, converted into Uint8Array[] chunks.
`JavaScript
const chunks = [new TextEncoder().encode('text1 text2')];
const crc32 = new CRC32();
let crc = 0;
for (const chunk of chunks) {
crc = crc32.forBytes(chunk, crc);
}
`
`JavaScript0x${checksum10.toString(16)}
const crc32 = new CRC32();
const checksum10 = crc32.calculate("hi"); // 3633523372
const checksum16 = ; // 0xd8932aac`
Both, CommonJS and ESM modules are supported.
`JavaScript``
import { CRC32 } from '@tsxper/crc32';
// or
const { CRC32 } = require('@tsxper/crc32');
Supporting of the both module systems is done through "import", "require" and "default" conditions.
MIT License.