CRC (Cyclic Redundancy Check) class for computing various CRC algorithms.

This class provides a comprehensive implementation of CRC calculation algorithms commonly used in automotive diagnostics, communication protocols, and data integrity verification. It supports 8-bit, 16-bit, and 32-bit CRC calculations with configurable parameters including polynomial, initial value, final XOR value, and reflection settings.

The class includes a comprehensive set of predefined CRC algorithms and allows custom CRC configurations for specific use cases.

// Using predefined CRC algorithms
const crc16Modbus = CRC.default('CRC16_MODBUS')
if (crc16Modbus) {
const data = Buffer.from([0x01, 0x02, 0x03, 0x04])
const crcValue = crc16Modbus.compute(data)
console.log(`CRC16-MODBUS: 0x${crcValue.toString(16).padStart(4, '0')}`)
}

// Creating custom CRC configuration (like in bootloader.ts)
const customCrc = new CRC('self', 16, 0x3d65, 0, 0xffff, true, true)
const fileContent = await fs.readFile('firmware.bin')
const crcResult = customCrc.compute(fileContent)

// Computing CRC for different data types
const crc32 = CRC.default('CRC32')
if (crc32) {
// From number array
const arrayData = [0x48, 0x65, 0x6c, 0x6c, 0x6f] // "Hello"
const crcFromArray = crc32.compute(arrayData)

// From Buffer
const bufferData = Buffer.from('Hello', 'utf8')
const crcFromBuffer = crc32.compute(bufferData)

// Get CRC as Buffer (useful for network protocols)
const crcBuffer = crc32.computeBuffer(bufferData)
}

Constructors

  • Creates an instance of the CRC (Cyclic Redundancy Check) class.

    Parameters

    • name: string

      The name of the CRC algorithm.

    • width: number

      The width of the CRC in bits.

    • polynomial: number

      The polynomial used for the CRC calculation.

    • initial: number

      The initial value for the CRC calculation.

    • finalXor: number

      The value to XOR with the final CRC value.

    • inputReflected: boolean

      Whether the input bytes should be reflected.

    • resultReflected: boolean

      Whether the result should be reflected.

    Returns CRC

Accessors

  • get finalXor(): number
  • Returns number

  • set finalXor(v: number): void
  • Parameters

    • v: number

    Returns void

  • get initial(): number
  • Returns number

  • set initial(v: number): void
  • Parameters

    • v: number

    Returns void

  • get inputReflected(): boolean
  • Returns boolean

  • set inputReflected(v: boolean): void
  • Parameters

    • v: boolean

    Returns void

  • get name(): string
  • Returns string

  • set name(v: string): void
  • Parameters

    • v: string

    Returns void

  • get polynomial(): number
  • Returns number

  • set polynomial(v: number): void
  • Parameters

    • v: number

    Returns void

  • get resultReflected(): boolean
  • Returns boolean

  • set resultReflected(v: boolean): void
  • Parameters

    • v: boolean

    Returns void

  • get table(): number[]
  • Returns number[]

  • get width(): number
  • Returns number

  • set width(v: number): void
  • Parameters

    • v: number

    Returns void

  • get defaults(): CRC[]
  • Returns a list of default CRC configurations.

    The list includes various CRC algorithms with their respective parameters:

    • Name: The name of the CRC algorithm.
    • Width: The width of the CRC (number of bits).
    • Polynomial: The polynomial used for the CRC calculation.
    • Initial Value: The initial value for the CRC calculation.
    • Final XOR Value: The value to XOR with the final CRC value.
    • Reflect Input: Whether to reflect the input bytes.
    • Reflect Output: Whether to reflect the output CRC value.

    Returns CRC[]

    An array of CRC configurations.

Methods

  • Computes the CRC (Cyclic Redundancy Check) value for the given input bytes.

    Parameters

    • bytes: number[] | Buffer

      The input data as an array of numbers or a Buffer.

    Returns number

    • The computed CRC value.
  • Parameters

    • bytes: number[] | Buffer

    Returns Buffer

  • Retrieves a CRC object from the defaults list by its name.

    Parameters

    • name: string

      The name of the CRC object to find.

    Returns undefined | CRC

    The CRC object with the specified name, or undefined if not found.