Code Monkey home page Code Monkey logo

crc-full's Introduction

crc-full

Support Node of LTS npm version Build passing dependencies typescript License mit


Description

The crc-full module is used to calculate any kind of CRC setting parameters such as length, polynomial and others. It's completely written in typescript for node js.

How to use

Use is very simple. Just add the module to project.

Install dependencies:
The only one dependency is typescript compiler

npm install -g typescript

Import in node js

After typescript is installed, simple run:

npm install crc-full

Use in your project

First of all import the module in your project files, then create an instance of the class whith all parameters of the CRC algorithm you need. After the instance is create you can compute the CRC of your data passing an array of byte or simply a buffer object.

For typescript use:

import {CRC} from 'crc-full'
...
let crc =  new CRC("CRC16", 16, 0x8005, 0x0000, 0x0000, false, false);
crc.compute([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21]);
let computed_crc = crc.compute(Buffer.from("Hello world!","ascii"))

For javascript use:

const CRC = require('crc-full').CRC;
var crc = new CRC("CRC16", 16, 0x8005, 0x0000, 0x0000, false, false);
crc.compute([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21]);
var computed_crc = crc.compute(Buffer.from("Hello world!", "ascii"))

Where the parameters in the constructor are:

  • Length: the Length of CRC in bit (8,16,32)
  • Name: friendly name for the configuration
  • Polinomial: the polinomial used to compute CRC
  • InitialValue: initial value of CRC
  • FinalXorValue: final value of CRC
  • InputReflected: boolen that indicate if need to reflect input
  • OutputReflected: boolen that indicate if need to reflect input

Presets

Invoking the static getter defaults is possible to use the internal preconfigurated CRC algorithms. An array of instantiated object is returned back

The algorithms are:

Length Name Polinomial Initial value Final value Input reflected Output reflected
8 CRC8 0x07 0x00 0x00 false false)
8 CRC8_SAE_J1850 0x1D 0xFF 0xFF false false)
8 CRC8_SAE_J1850_ZERO 0x1D 0x00 0x00 false false)
8 CRC8_8H2F 0x2F 0xFF 0xFF false false)
8 CRC8_CDMA2000 0x9B 0xFF 0x00 false false)
8 CRC8_DARC 0x39 0x00 0x00 true true)
8 CRC8_DVB_S2 0xD5 0x00 0x00 false false)
8 CRC8_EBU 0x1D 0xFF 0x00 true true)
8 CRC8_ICODE 0x1D 0xFD 0x00 false false)
8 CRC8_ITU 0x07 0x00 0x55 false false
8 CRC8_MAXIM 0x31 0x00 0x00 true true
8 CRC8_ROHC 0x07 0xFF 0x00 true true
8 CRC8_WCDMA 0x9B 0x00 0x00 true true
16 CRC16_CCIT_ZERO 0x1021 0x0000 0x0000 false false
16 CRC16_ARC 0x8005 0x0000 0x0000 true true
16 CRC16_AUG_CCITT 0x1021 0x1D0F 0x0000 false false
16 CRC16_BUYPASS 0x8005 0x0000 0x0000 false false
16 CRC16_CCITT_FALSE 0x1021 0xFFFF 0x0000 false false
16 CRC16_CDMA2000 0xC867 0xFFFF 0x0000 false false
16 CRC16_DDS_110 0x8005 0x800D 0x0000 false false
16 CRC16_DECT_R 0x0589 0x0000 0x0001 false false
16 CRC16_DECT_X 0x0589 0x0000 0x0000 false false
16 CRC16_DNP 0x3D65 0x0000 0xFFFF true true
16 CRC16_EN_13757 0x3D65 0x0000 0xFFFF false false
16 CRC16_GENIBUS 0x1021 0xFFFF 0xFFFF false false
16 CRC16_MAXIM 0x8005 0x0000 0xFFFF true true
16 CRC16_MCRF4XX 0x1021 0xFFFF 0x0000 true true
16 CRC16_RIELLO 0x1021 0xB2AA 0x0000 true true
16 CRC16_T10_DIF 0x8BB7 0x0000 0x0000 false false
16 CRC16_TELEDISK 0xA097 0x0000 0x0000 false false
16 CRC16_TMS37157 0x1021 0x89EC 0x0000 true true
16 CRC16_USB 0x8005 0xFFFF 0xFFFF true true
16 CRC16_A 0x1021 0xC6C6 0x0000 true true
16 CRC16_KERMIT 0x1021 0x0000 0x0000 true true
16 CRC16_MODBUS 0x8005 0xFFFF 0x0000 true true
16 CRC16_X_25 0x1021 0xFFFF 0xFFFF true true
16 CRC16_XMODEM 0x1021 0x0000 0x0000 false false
32 CRC32 0x04C11DB7 0xFFFFFFFF 0xFFFFFFFF true true
32 CRC32_BZIP2 0x04C11DB7 0xFFFFFFFF 0xFFFFFFFF false false
32 CRC32_C 0x1EDC6F41 0xFFFFFFFF 0xFFFFFFFF true true
32 CRC32_D 0xA833982B 0xFFFFFFFF 0xFFFFFFFF true true
32 CRC32_MPEG2 0x04C11DB7 0xFFFFFFFF 0x00000000 false false
32 CRC32_POSIX 0x04C11DB7 0x00000000 0xFFFFFFFF false false
32 CRC32_Q 0x814141AB 0x00000000 0x00000000 false false
32 CRC32_JAMCRC 0x04C11DB7 0xFFFFFFFF 0x00000000 true true
32 CRC32_XFER 0x000000AF 0x00000000 0x00000000 false false

Is possible to use one of above CRC preset using the method default(name) where parameter name is the name of chosen preset.

Typescript users can use:

import {CRC} from 'crc-full'
let crc = CRC.default("CRC16_CCIT_ZERO");
let computed_crc = crc.compute(Buffer.from("Hello world!","ascii"))

Typescript users can use:

const CRC = require('crc-full').CRC;
var crc = CRC.default("CRC16_CCIT_ZERO");
var computed_crc = crc.compute(Buffer.from("Hello world!","ascii"))

Advanced

The module also support advanced methods to calculate CRC tables and export them as byte arrays:

...
crc.makeCrcTable();
crc.makeCrcTableReversed();
var table = crc.table;
...

Notes

The method compute take a byte array (or Buffer) as input and returns a number.
If you need to convert a string you have to pass it as a byte array.
If you need to read the CRC result in hex format string use .toString(16)

crc-full's People

Contributors

riologiuseppe avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

crc-full's Issues

Match CRC from C code to crc-full

Hello,

I have to send a message from typescript to our device and at the device side, it validates the message using a CRC16.

Here is how the device validates it:

#define CRC_MASK 0x1021 /* x^16 + x^12 + x^5 + x^0 */

UINT16 CRC_Calc (unsigned char *pbData, int iLength)
{
    UINT16 wData, wCRC = 0;
    int i;

    for ( ;iLength > 0; iLength--, pbData++) {
        wData = (UINT16) (((UINT16) *pbData) << 8);
        for (i = 0; i < 8; i++, wData <<= 1) {
            if ((wCRC ^ wData) & 0x8000)
                wCRC = (UINT16) ((wCRC << 1) ^ CRC_MASK);
            else
                wCRC <<= 1;
        }
    }
    return wCRC;
}

The input data would be "OPN\x17" (where \x17 is an ETB control character). The output must be A8A9.

Given that input data, the C sample code, can you provide me a sample code using your package to achieve the same result?

Thank you! I appreciate any help.

Best regards,
Gutemberg

[CRC32_C] Compute result is negative value

For example:

// for (0-100 hex)
let data = '0102030405060708090A0B0C0D0E0F101112131415161718191A1B1C1D1E1F202122232425262728292A2B2C2D2E2F303132333435363738393A3B3C3D3E3F404142434445464748494A4B4C4D4E4F505152535455565758595A5B5C5D5E5F6061626364';
let buf = Buffer.from(data, 'hex'); 
let crc = CRC.default("CRC32_C");
let computed_crc = crc.compute(source);
// output is: -5DA0519C

But for correct value should be 0xA25FAE64, you can test in website 'http://crccalc.com/' with the same data and 'hex' option selected.

Pls help to check it.

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.