or run

npx @tessl/cli init
Log in

Version

Tile

Overview

Evals

Files

docs

crc1.mdcrc16-algorithms.mdcrc24.mdcrc32-algorithms.mdcrc8-algorithms.mdindex.md
tile.json

tessl/npm-crc

Comprehensive Cyclic Redundancy Check (CRC) calculation library supporting 13 different CRC algorithms for Node.js and browser environments.

Workspace
tessl
Visibility
Public
Created
Last updated
Describes
npmpkg:npm/crc@4.3.x

To install, run

npx @tessl/cli install tessl/npm-crc@4.3.0

index.mddocs/

CRC

Comprehensive Cyclic Redundancy Check (CRC) calculation library supporting 13 different CRC algorithms for Node.js and browser environments.

Overview

The CRC package provides a comprehensive set of CRC calculation functions with full TypeScript support, pure JavaScript implementation (no native dependencies), and support for both ESM and CommonJS module systems. It implements 13 different CRC algorithms including CRC1, CRC8 variants, CRC16 variants, CRC24, CRC32 variants, and CRCJAM.

Key features:

  • Written in TypeScript with full type definitions
  • Pure JavaScript implementation, no native dependencies
  • Supports ESM and CommonJS
  • Optimized for both bundle size and performance
  • Incremental CRC calculation support
  • Browser and Node.js compatible

Package Information

  • Package Name: crc
  • Package Type: npm
  • Language: TypeScript/JavaScript
  • Installation: npm install crc

Core Imports

ESM (Recommended)

import { crc32, crc16, crc8, type CRCModule } from "crc";
import crc32 from "crc/crc32";
import crc32Calculator from "crc/calculators/crc32";

CommonJS

const { crc32, crc16, crc8 } = require("crc");
const crc32 = require("crc/crc32");
const crc32Calculator = require("crc/calculators/crc32");

Core Types

/**
 * Input types accepted by CRC functions
 */
type BufferInput = string | ArrayBuffer | Buffer;

/**
 * Generic CRC calculator function interface
 */
interface CRCCalculator<T = BufferInput | Uint8Array> {
  (value: T, previous?: number): number;
}

/**
 * Main CRC function interface with additional properties
 */
interface CRCModule extends CRCCalculator<BufferInput> {
  /** Returns signed CRC value */
  signed: CRCCalculator<BufferInput>;
  /** Returns unsigned CRC value (same as main function) */
  unsigned: CRCCalculator<BufferInput>;
  /** CRC model identifier string */
  model: string;
}

Basic Usage

Simple CRC Calculation

import crc32 from "crc/crc32";

// Calculate CRC32 of a string
const result = crc32("hello");
console.log(result.toString(16)); // "3610a686"

// Calculate CRC32 of a buffer
import fs from "fs";
const fileBuffer = fs.readFileSync("data.txt");
const fileCrc = crc32(fileBuffer);

Incremental CRC Calculation

import crc32 from "crc/crc32";

// Calculate CRC incrementally for large data
let crcValue = crc32("chunk1");
crcValue = crc32("chunk2", crcValue);
crcValue = crc32("chunk3", crcValue);
console.log(crcValue.toString(16));

Using Multiple Algorithms

import { crc32, crc16, crc8 } from "crc";

const data = "test data";
const crc32Result = crc32(data);
const crc16Result = crc16(data);  
const crc8Result = crc8(data);

CRC Algorithm Capabilities

The CRC package provides 13 different CRC algorithms organized by bit width:

1-Bit CRC Algorithms

  • CRC1: Simple checksum algorithm
  • Complete CRC1 Documentation

8-Bit CRC Algorithms

  • CRC8: Standard 8-bit CRC
  • CRC8 1-Wire: CRC8 variant optimized for 1-Wire protocol
  • CRC8 DVB-S2: CRC8 variant used in DVB-S2 standard
  • Complete 8-Bit CRC Documentation

16-Bit CRC Algorithms

  • CRC16: Standard 16-bit CRC
  • CRC16 CCITT: CRC16 variant commonly used in telecommunications
  • CRC16 Modbus: CRC16 variant used in Modbus protocol
  • CRC16 XModem: CRC16 variant used in XModem protocol
  • CRC16 Kermit: CRC16 variant used in Kermit protocol
  • Complete 16-Bit CRC Documentation

24-Bit CRC Algorithms

32-Bit CRC Algorithms

Import Strategies

The package supports multiple import strategies for different use cases:

Main Module Import (All Algorithms)

import crc from "crc";
const result = crc.crc32("data");

Named Imports (Specific Algorithms)

import { crc32, crc16ccitt } from "crc";

Direct Algorithm Import (Tree-Shaking Optimized)

import crc32 from "crc/crc32";
import crc16ccitt from "crc/crc16ccitt";

Direct Calculator Import (Minimal Bundle Size)

import crc32Calculator from "crc/calculators/crc32";
const buffer = new Uint8Array([104, 101, 108, 108, 111]);
const result = crc32Calculator(buffer);

Performance Optimization

Bundle Size Optimization

Use direct imports for minimal bundle size:

// Only imports CRC32 algorithm
import crc32 from "crc/crc32";

// Even smaller - only the raw calculator
import crc32Calculator from "crc/calculators/crc32";

Performance Optimization

Use raw calculators when working with Uint8Array data:

import crc32Calculator from "crc/calculators/crc32";

// Convert string to Uint8Array manually for better performance
const encoder = new TextEncoder();
const data = encoder.encode("hello");
const result = crc32Calculator(data);

Error Handling

CRC functions are designed to be robust and handle various input types. Common considerations:

  • Buffer Dependency: The package uses the buffer polyfill for browser compatibility
  • Input Validation: Functions accept string, ArrayBuffer, or Buffer inputs
  • Type Safety: TypeScript definitions ensure compile-time type checking
  • Cross-Platform: Works consistently across Node.js and browser environments