Code Monkey home page Code Monkey logo

node-crc's Introduction

CRC For Node.js

Introduction

This module can compute different types of CRC checksum by using N-API.

Installation

Run npm i or npm install to install.

npm install node-crc

If you want to save this module to package.json, please add --save option.

npm install node-crc --save

Initialization

Import this module by using require function.

const crc = require('node-crc');

Usage

You can use crc function to compute a CRC value by providing the length of bits, expression, reflection, an initial value and a final xor value. For example, if you want to compute a CRC-24 value.

var result = crc.crc(24, false, 0x00864cfb, 0x00000000, 0x00b704ce, 0x00000000, 0x00000000, 0x00000000, Buffer.from('hello', 'utf8')).toString('hex');
// Arguments: the length of bits, reflection, low bits of expression, high bits of expression, low bits of the initial value, high bits of the initial value, low bits of the final xor value, high bits of the final xor value, the source data buffer

To simplify the usage, there are several common versions of CRC whose computing functions are already built-in.

  • crc8(crc8atm)
  • crc8cdma
  • crc16(crc16ibm)
  • crc16ccitt(crcccitt)
  • crc32(crc32ieee, also called crc32b in mhash)
  • crc32mhash
    • mhash is a common library which has two weird versions of CRC32 called crc32 and crc32b. crc32 and crc32mhash in this module are crc32b and crc32 in mhash respectively.
  • crc32c
  • crc64(crc64ecma)
  • crc64iso
  • crc64jones

Input data and output data are buffers.

For instance,

var result = crc.crc32(Buffer.from('hello', 'utf8')).toString('hex');
var result2 = crc.crc64(Buffer.from('world', 'utf8')).toString('hex');

Tests

To run the test suite, first install the dependencies, then run npm test:

npm install
npm test

Benchmark

To run the benchmark suite, first install the dependencies, then run npm run benchmark:

npm install
npm run benchmark

Here is my result,

CRC-8
  - 76 milliseconds
  - Result HEX: 60
  ✓ another crc module (77ms)
  - 67 milliseconds
  - Result HEX: 60
  ✓ this module (67ms)

CRC-16
  - 84 milliseconds
  - Result HEX: 94bc
  ✓ another crc module (84ms)
  - 70 milliseconds
  - Result HEX: 94bc
  ✓ this module (70ms)

CRC-32
  - 122 milliseconds
  - Result HEX: ab526987
  ✓ another crc module (122ms)
  - 68 milliseconds
  - Result HEX: ab526987
  ✓ this module (68ms)

CRC-64
  - I cannot compute CRC-64!
  ✓ another crc module
  - 74 milliseconds
  - Result HEX: aa9d25ffe7f7c6fd
  ✓ this module (74ms)

License

MIT

node-crc's People

Contributors

magiclen avatar daluf avatar klugerama avatar rstiller avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.