@serialpilot/crc
v0.1.0
Published
Pure-function, table-driven CRC kit (CRC-8, CRC-16/Modbus, CRC-16/CCITT, CRC-16/XMODEM, CRC-32, CRC-32C). Browser-clean — no node:* imports.
Maintainers
Readme
@serialpilot/crc
Pure-function, table-driven CRC kit. Zero dependencies, zero allocation in the hot path, lookup tables computed once at module load, and no node:* imports — runs unchanged in browsers and Web Serial.
Install
npm install @serialpilot/crcUse
import {
crc8,
crc16Modbus,
crc16Ccitt,
crc16Xmodem,
crc32,
crc32c,
CrcStream,
} from '@serialpilot/crc'
const data = new TextEncoder().encode('123456789')
crc16Modbus(data).toString(16) // '4b37'
crc16Ccitt(data).toString(16) // '29b1'
crc16Xmodem(data).toString(16) // '31c3'
crc32(data).toString(16) // 'cbf43926'
crc32c(data).toString(16) // 'e3069283'
crc8(data).toString(16) // 'f4' (CRC-8/SMBUS preset)
// Streaming form for inputs you can't hold in one buffer:
const s = new CrcStream('crc32')
for (const chunk of chunks) s.update(chunk)
s.digest()Presets
| Function | Width | Poly | Init | RefIn | RefOut | XorOut | Check ('123456789') |
| ------------ | ----- | ------------ | ---------- | ----- | ------ | ---------- | --------------------- |
| crc8 | 8 | 0x07 | 0x00 | false | false | 0x00 | 0xF4 |
| crc16Modbus| 16 | 0x8005 | 0xFFFF | true | true | 0x0000 | 0x4B37 |
| crc16Ccitt | 16 | 0x1021 | 0xFFFF | false | false | 0x0000 | 0x29B1 |
| crc16Xmodem| 16 | 0x1021 | 0x0000 | false | false | 0x0000 | 0x31C3 |
| crc32 | 32 | 0x04C11DB7 | 0xFFFFFFFF| true | true | 0xFFFFFFFF| 0xCBF43926 |
| crc32c | 32 | 0x1EDC6F41 | 0xFFFFFFFF| true | true | 0xFFFFFFFF| 0xE3069283 |
crc8 accepts an opts object to override the default SMBUS preset ({ poly, init, refIn, refOut, xorOut }).
License
MIT.
