├── samples ├── ts-sample.ts └── js-sample.js ├── dist ├── crcUtil.d.ts ├── crc.d.ts ├── crcUtil.js ├── crcUtil.js.map ├── crc.js.map └── crc.js ├── tsconfig.json ├── .gitignore ├── src ├── crcUtil.ts └── crc.ts ├── package.json └── readme.md /samples/ts-sample.ts: -------------------------------------------------------------------------------- 1 | import {CRC} from '../src/crc' 2 | 3 | var crc = new CRC("CRC16", 16, 0x8005, 0x0000, 0x0000, false, false); 4 | 5 | var computed_crc = crc.compute(Buffer.from("Hello world!", "ascii")) 6 | 7 | console.log("Hello wolrd!: ", computed_crc); -------------------------------------------------------------------------------- /samples/js-sample.js: -------------------------------------------------------------------------------- 1 | const CRC = require('crc-full').CRC; 2 | 3 | var crc = new CRC("CRC16", 16, 0x8005, 0x0000, 0x0000, false, false); 4 | 5 | var computed_crc = crc.compute(Buffer.from("Hello world!", "ascii")) 6 | 7 | console.log("Hello wolrd!: ", computed_crc); -------------------------------------------------------------------------------- /dist/crcUtil.d.ts: -------------------------------------------------------------------------------- 1 | declare class CrcUtil { 2 | static Reflect8(val: number): number; 3 | static Reflect16(val: number): number; 4 | static Reflect32(val: number): number; 5 | static ReflectGeneric(val: number, width: number): number; 6 | } 7 | export default CrcUtil; 8 | -------------------------------------------------------------------------------- /tsconfig.json: -------------------------------------------------------------------------------- 1 | { 2 | "compilerOptions": { 3 | "target": "es5", 4 | "module": "commonjs", 5 | "declaration": true, 6 | "sourceMap": true, 7 | "removeComments": true, 8 | "noImplicitAny": true, 9 | "outDir": "./dist", 10 | "noImplicitThis": true, 11 | "lib": ["es5", "es6"] 12 | }, 13 | "include": [ 14 | "./src/**/*.ts" 15 | ] 16 | } -------------------------------------------------------------------------------- /dist/crc.d.ts: -------------------------------------------------------------------------------- 1 | /// 2 | export declare class CRC { 3 | private _width; 4 | private _name; 5 | private _polynomial; 6 | private _initialVal; 7 | private _finalXorVal; 8 | private _inputReflected; 9 | private _resultReflected; 10 | private static _list; 11 | private _crcTable; 12 | private _castMask; 13 | private _msbMask; 14 | width: number; 15 | name: string; 16 | polynomial: number; 17 | initial: number; 18 | finalXor: number; 19 | inputReflected: boolean; 20 | resultReflected: boolean; 21 | constructor(name: string, width: number, polynomial: number, initial: number, finalXor: number, inputReflected: boolean, resultReflected: boolean); 22 | static readonly defaults: CRC[]; 23 | makeCrcTable(): void; 24 | makeCrcTableReversed(): void; 25 | compute(bytes: number[] | Buffer): number; 26 | computeBuffer(bytes: number[] | Buffer): Buffer; 27 | readonly table: number[]; 28 | static default(name: string): CRC | undefined; 29 | } 30 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | # Logs 2 | logs 3 | *.log 4 | npm-debug.log* 5 | yarn-debug.log* 6 | yarn-error.log* 7 | 8 | # Runtime data 9 | pids 10 | *.pid 11 | *.seed 12 | *.pid.lock 13 | 14 | # Directory for instrumented libs generated by jscoverage/JSCover 15 | lib-cov 16 | 17 | # Coverage directory used by tools like istanbul 18 | coverage 19 | 20 | # nyc test coverage 21 | .nyc_output 22 | 23 | # Grunt intermediate storage (http://gruntjs.com/creating-plugins#storing-task-files) 24 | .grunt 25 | 26 | # Bower dependency directory (https://bower.io/) 27 | bower_components 28 | 29 | # node-waf configuration 30 | .lock-wscript 31 | 32 | # Compiled binary addons (https://nodejs.org/api/addons.html) 33 | build/Release 34 | 35 | # Dependency directories 36 | node_modules/ 37 | jspm_packages/ 38 | 39 | # Typescript v1 declaration files 40 | typings/ 41 | 42 | # Optional npm cache directory 43 | .npm 44 | 45 | # Optional eslint cache 46 | .eslintcache 47 | 48 | # Optional REPL history 49 | .node_repl_history 50 | 51 | # Output of 'npm pack' 52 | *.tgz 53 | 54 | # Yarn Integrity file 55 | .yarn-integrity 56 | 57 | # dotenv environment variables file 58 | .env -------------------------------------------------------------------------------- /src/crcUtil.ts: -------------------------------------------------------------------------------- 1 | class CrcUtil { 2 | 3 | public static Reflect8(val : number) { 4 | var resByte = 0; 5 | 6 | for (var i = 0; i < 8; i++) { 7 | if ((val & (1 << i)) != 0) { 8 | resByte |= ((1 << (7 - i)) & 0xFF); 9 | } 10 | } 11 | return resByte; 12 | } 13 | 14 | public static Reflect16(val:number) { 15 | var resByte = 0; 16 | 17 | for (var i = 0; i < 16; i++) { 18 | if ((val & (1 << i)) != 0) { 19 | resByte |= ((1 << (15 - i)) & 0xFFFF); 20 | } 21 | } 22 | 23 | return resByte; 24 | } 25 | 26 | public static Reflect32(val : number) { 27 | var resByte = 0; 28 | 29 | for (var i = 0; i < 32; i++) { 30 | if ((val & (1 << i)) != 0) { 31 | resByte |= ((1 << (31 - i)) & 0xFFFFFFFF); 32 | } 33 | } 34 | 35 | return resByte; 36 | } 37 | 38 | public static ReflectGeneric(val : number, width : number) { 39 | var resByte = 0; 40 | for (var i = 0; i < width; i++) { 41 | if ((val & (1 << i)) != 0) { 42 | resByte |= (1 << ((width - 1) - i)); 43 | } 44 | } 45 | return resByte; 46 | } 47 | } 48 | 49 | export default CrcUtil; -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "crc-full", 3 | "version": "1.1.0", 4 | "description": "CRC generator for node js", 5 | "main": "dist/crc.js", 6 | "scripts": { 7 | "prepublish": "tsc", 8 | "test": "echo \"Error: no test specified\" && exit 1" 9 | }, 10 | "keywords": [ 11 | "crc", 12 | "crc8", 13 | "crc16", 14 | "crc32", 15 | "polynomial" 16 | ], 17 | "directories": { 18 | "example": "samples" 19 | }, 20 | "displayName": "crc-full", 21 | "maintainers": [{ 22 | "name": "Giuseppe Riolo", 23 | "email": "riolo.giuseppe@gmail.com" 24 | }], 25 | "author": { 26 | "name": "Giuseppe Riolo", 27 | "email": "riolo.giuseppe@gmail.com", 28 | "url": "https://github.com/RioloGiuseppe" 29 | }, 30 | "license": "MIT", 31 | "typings": "./dist/crc.d.ts", 32 | "repository": { 33 | "type": "git", 34 | "url": "git+https://github.com/RioloGiuseppe/crc-full.git" 35 | }, 36 | "bugs": { 37 | "url": "https://github.com/RioloGiuseppe/crc-full/issues" 38 | }, 39 | "homepage": "https://github.com/RioloGiuseppe/crc-full#readme", 40 | "readme": "https://github.com/RioloGiuseppe/crc-full#readme", 41 | "devDependencies": { 42 | "@types/node": "^8.0.31" 43 | } 44 | } -------------------------------------------------------------------------------- /dist/crcUtil.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | Object.defineProperty(exports, "__esModule", { value: true }); 3 | var CrcUtil = (function () { 4 | function CrcUtil() { 5 | } 6 | CrcUtil.Reflect8 = function (val) { 7 | var resByte = 0; 8 | for (var i = 0; i < 8; i++) { 9 | if ((val & (1 << i)) != 0) { 10 | resByte |= ((1 << (7 - i)) & 0xFF); 11 | } 12 | } 13 | return resByte; 14 | }; 15 | CrcUtil.Reflect16 = function (val) { 16 | var resByte = 0; 17 | for (var i = 0; i < 16; i++) { 18 | if ((val & (1 << i)) != 0) { 19 | resByte |= ((1 << (15 - i)) & 0xFFFF); 20 | } 21 | } 22 | return resByte; 23 | }; 24 | CrcUtil.Reflect32 = function (val) { 25 | var resByte = 0; 26 | for (var i = 0; i < 32; i++) { 27 | if ((val & (1 << i)) != 0) { 28 | resByte |= ((1 << (31 - i)) & 0xFFFFFFFF); 29 | } 30 | } 31 | return resByte; 32 | }; 33 | CrcUtil.ReflectGeneric = function (val, width) { 34 | var resByte = 0; 35 | for (var i = 0; i < width; i++) { 36 | if ((val & (1 << i)) != 0) { 37 | resByte |= (1 << ((width - 1) - i)); 38 | } 39 | } 40 | return resByte; 41 | }; 42 | return CrcUtil; 43 | }()); 44 | exports.default = CrcUtil; 45 | //# sourceMappingURL=crcUtil.js.map -------------------------------------------------------------------------------- /dist/crcUtil.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"crcUtil.js","sourceRoot":"","sources":["../src/crcUtil.ts"],"names":[],"mappings":";;AAAA;IAAA;IA8CA,CAAC;IA5CiB,gBAAQ,GAAtB,UAAuB,GAAY;QAC/B,IAAI,OAAO,GAAG,CAAC,CAAC;QAEhB,KAAK,IAAI,CAAC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EAAE,EAAE;YAC5B,IAAI,CAAC,GAAG,GAAG,CAAC,CAAC,IAAI,CAAC,CAAC,CAAC,IAAI,CAAC,EAAE;gBACvB,OAAO,IAAI,CAAC,CAAC,IAAI,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,CAAC;aACvC;SACJ;QACD,OAAO,OAAO,CAAC;IACnB,CAAC;IACL,cAAC;AAAD,CAAC,AA9CD,IA8CC;AAED,kBAAe,OAAO,CAAC"} -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | # crc-full 2 | 3 | ![Support Node of LTS](https://img.shields.io/badge/node-LTS-brightgreen.svg?style=plastic) ![npm version](https://img.shields.io/badge/npm-3.5.0-brightgreen.svg?style=plastic) ![Build passing](https://img.shields.io/badge/build-passing-brightgreen.svg?style=plastic) ![dependencies typescript](https://img.shields.io/badge/dependencies-typescript-blue.svg?style=plastic) ![License mit](https://img.shields.io/badge/license-MIT-blue.svg?style=plastic) 4 | 5 | --- 6 | 7 | # Description 8 | 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. 9 | 10 | ## How to use 11 | 12 | Use is very simple. Just add the module to project. 13 | 14 | > **Install dependencies:** 15 | > The only one dependency is typescript compiler 16 | > ```sh 17 | > npm install -g typescript 18 | > ``` 19 | 20 | ### Import in node js 21 | After *typescript* is installed, simple run: 22 | ```sh 23 | npm install crc-full 24 | ``` 25 | 26 | ### Use in your project 27 | 28 | 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. 29 | After the instance is create you can compute the CRC of your data passing an array of byte or simply a buffer object. 30 | 31 | For typescript use: 32 | ```ts 33 | import {CRC} from 'crc-full' 34 | ... 35 | let crc = new CRC("CRC16", 16, 0x8005, 0x0000, 0x0000, false, false); 36 | crc.compute([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21]); 37 | let computed_crc = crc.compute(Buffer.from("Hello world!","ascii")) 38 | ``` 39 | For javascript use: 40 | ```js 41 | const CRC = require('crc-full').CRC; 42 | var crc = new CRC("CRC16", 16, 0x8005, 0x0000, 0x0000, false, false); 43 | crc.compute([0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x77, 0x6f, 0x72, 0x6c, 0x64, 0x21]); 44 | var computed_crc = crc.compute(Buffer.from("Hello world!", "ascii")) 45 | ``` 46 | 47 | Where the parameters in the constructor are: 48 | - Length: the Length of CRC in bit (8,16,32) 49 | - Name: friendly name for the configuration 50 | - Polinomial: the polinomial used to compute CRC 51 | - InitialValue: initial value of CRC 52 | - FinalXorValue: final value of CRC 53 | - InputReflected: boolen that indicate if need to reflect input 54 | - OutputReflected: boolen that indicate if need to reflect input 55 | 56 | ### Presets 57 | 58 | Invoking the static getter *defaults* is possible to use the internal preconfigurated CRC algorithms. An array of instantiated object is returned back 59 | 60 | The algorithms are: 61 | 62 | | Length | Name | Polinomial | Initial value | Final value | Input reflected | Output reflected | 63 | |:------:|------|:----------:|:-------------:|:-----------:|:---------------:|:----------------:| 64 | |8|CRC8|0x07|0x00|0x00|false|false)| 65 | |8|CRC8_SAE_J1850|0x1D|0xFF|0xFF|false|false)| 66 | |8|CRC8_SAE_J1850_ZERO|0x1D|0x00|0x00|false|false)| 67 | |8|CRC8_8H2F|0x2F|0xFF|0xFF|false|false)| 68 | |8|CRC8_CDMA2000|0x9B|0xFF|0x00|false|false)| 69 | |8|CRC8_DARC|0x39|0x00|0x00|true|true)| 70 | |8|CRC8_DVB_S2|0xD5|0x00|0x00|false|false)| 71 | |8|CRC8_EBU|0x1D|0xFF|0x00|true|true)| 72 | |8|CRC8_ICODE|0x1D|0xFD|0x00|false|false)| 73 | |8|CRC8_ITU|0x07|0x00|0x55|false|false| 74 | |8|CRC8_MAXIM|0x31|0x00|0x00|true|true| 75 | |8|CRC8_ROHC|0x07|0xFF|0x00|true|true| 76 | |8|CRC8_WCDMA|0x9B|0x00|0x00|true|true| 77 | |16|CRC16_CCIT_ZERO|0x1021|0x0000|0x0000|false|false| 78 | |16|CRC16_ARC|0x8005|0x0000|0x0000|true|true| 79 | |16|CRC16_AUG_CCITT|0x1021|0x1D0F|0x0000|false|false| 80 | |16|CRC16_BUYPASS|0x8005|0x0000|0x0000|false|false| 81 | |16|CRC16_CCITT_FALSE|0x1021|0xFFFF|0x0000|false|false| 82 | |16|CRC16_CDMA2000|0xC867|0xFFFF|0x0000|false|false| 83 | |16|CRC16_DDS_110|0x8005|0x800D|0x0000|false|false| 84 | |16|CRC16_DECT_R|0x0589|0x0000|0x0001|false|false| 85 | |16|CRC16_DECT_X|0x0589|0x0000|0x0000|false|false| 86 | |16|CRC16_DNP|0x3D65|0x0000|0xFFFF|true|true| 87 | |16|CRC16_EN_13757|0x3D65|0x0000|0xFFFF|false|false| 88 | |16|CRC16_GENIBUS|0x1021|0xFFFF|0xFFFF|false|false| 89 | |16|CRC16_MAXIM|0x8005|0x0000|0xFFFF|true|true| 90 | |16|CRC16_MCRF4XX|0x1021|0xFFFF|0x0000|true|true| 91 | |16|CRC16_RIELLO|0x1021|0xB2AA|0x0000|true|true| 92 | |16|CRC16_T10_DIF|0x8BB7|0x0000|0x0000|false|false| 93 | |16|CRC16_TELEDISK|0xA097|0x0000|0x0000|false|false| 94 | |16|CRC16_TMS37157|0x1021|0x89EC|0x0000|true|true| 95 | |16|CRC16_USB|0x8005|0xFFFF|0xFFFF|true|true| 96 | |16|CRC16_A|0x1021|0xC6C6|0x0000|true|true| 97 | |16|CRC16_KERMIT|0x1021|0x0000|0x0000|true|true| 98 | |16|CRC16_MODBUS|0x8005|0xFFFF|0x0000|true|true| 99 | |16|CRC16_X_25|0x1021|0xFFFF|0xFFFF|true|true| 100 | |16|CRC16_XMODEM|0x1021|0x0000|0x0000|false|false| 101 | |32|CRC32|0x04C11DB7|0xFFFFFFFF|0xFFFFFFFF|true|true| 102 | |32|CRC32_BZIP2|0x04C11DB7|0xFFFFFFFF|0xFFFFFFFF|false|false| 103 | |32|CRC32_C|0x1EDC6F41|0xFFFFFFFF|0xFFFFFFFF|true|true| 104 | |32|CRC32_D|0xA833982B|0xFFFFFFFF|0xFFFFFFFF|true|true| 105 | |32|CRC32_MPEG2|0x04C11DB7|0xFFFFFFFF|0x00000000|false|false| 106 | |32|CRC32_POSIX|0x04C11DB7|0x00000000|0xFFFFFFFF|false|false| 107 | |32|CRC32_Q|0x814141AB|0x00000000|0x00000000|false|false| 108 | |32|CRC32_JAMCRC|0x04C11DB7|0xFFFFFFFF|0x00000000|true|true| 109 | |32|CRC32_XFER|0x000000AF|0x00000000|0x00000000|false|false| 110 | 111 | Is possible to use one of above CRC preset using the method default(name) where parameter _name_ is the name of chosen preset. 112 | 113 | Typescript users can use: 114 | 115 | ```ts 116 | import {CRC} from 'crc-full' 117 | let crc = CRC.default("CRC16_CCIT_ZERO"); 118 | let computed_crc = crc.compute(Buffer.from("Hello world!","ascii")) 119 | ``` 120 | Typescript users can use: 121 | 122 | ```ts 123 | const CRC = require('crc-full').CRC; 124 | var crc = CRC.default("CRC16_CCIT_ZERO"); 125 | var computed_crc = crc.compute(Buffer.from("Hello world!","ascii")) 126 | ``` 127 | 128 | ### Advanced 129 | 130 | The module also support advanced methods to calculate CRC tables and export them as byte arrays: 131 | 132 | ```js 133 | ... 134 | crc.makeCrcTable(); 135 | crc.makeCrcTableReversed(); 136 | var table = crc.table; 137 | ... 138 | ``` 139 | 140 | ### Notes 141 | 142 | The method `compute` take a byte array (or Buffer) as input and returns a number. 143 | If you need to convert a string you have to pass it as a byte array. 144 | If you need to read the CRC result in hex format string use `.toString(16)` -------------------------------------------------------------------------------- /dist/crc.js.map: -------------------------------------------------------------------------------- 1 | {"version":3,"file":"crc.js","sourceRoot":"","sources":["../src/crc.ts"],"names":[],"mappings":";;AAAA,qCAAgC;AAEhC;IAgEI,aAAY,IAAY,EAAE,KAAa,EAAE,UAAkB,EAAE,OAAe,EAAE,QAAgB,EAAE,cAAuB,EAAE,eA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QAC9B,IAAI,IAAI,CAAC,KAAK,KAAK,CAAC,EAAE;YAClB,OAAO,MAAM,CAAC,IAAI,CAAC,CAAC,GAAG,CAAC,CAAC,CAAA;SAC5B;aAAM,IAAI,IAAI,CAAC,KAAK,KAAK,EAAE,EAAE;YAC1B,IAAI,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YACxB,CAAC,CAAC,aAAa,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;YACxB,OAAO,CAAC,CAAC;SACZ;aAAM,IAAI,IAAI,CAAC,KAAK,KAAK,EAAE,EAAE;YAC1B,IAAI,CAAC,GAAG,MAAM,CAAC,KAAK,CAAC,CAAC,CAAC,CAAC;YACxB,CAAC,CAAC,aAAa,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC;YACxB,OAAO,CAAC,CAAC;SACZ;aAAM;YACH,MAAM,IAAI,KAAK,CAAC,oBAAoB,CAAC,CAAC;SACzC;IACL,CAAC;IAED,sBAAW,sBAAK;aAAhB;YACI,OAAO,IAAI,CAAC,SAAS,CAAC;QAC1B,CAAC;;;OAAA;IAEa,WAAO,GAArB,UAAsB,IAAY;QAC9B,OAAO,GAAG;aACL,QAAQ;aACR,IAAI,CAAC,UAAC,CAAM,IAAc,OAAA,CAAC,CAAC,IAAI,KAAK,IAAI,EAAf,CAAe,CAAC,CAAC;IACpD,CAAC;IACL,UAAC;AAAD,CAAC,AA9ND,IA8NC;AA9NY,kBAAG"} -------------------------------------------------------------------------------- /src/crc.ts: -------------------------------------------------------------------------------- 1 | import CrcUtil from './crcUtil'; 2 | 3 | export class CRC { 4 | 5 | private _width: number; 6 | private _name: string; 7 | private _polynomial: number; 8 | private _initialVal: number; 9 | private _finalXorVal: number; 10 | private _inputReflected: boolean; 11 | private _resultReflected: boolean; 12 | private static _list: CRC[]; 13 | 14 | private _crcTable: number[]; 15 | private _castMask: number; 16 | private _msbMask: number 17 | 18 | public get width(): number { return this._width; } 19 | public set width(v: number) { 20 | this._width = v; 21 | switch (v) { 22 | case 8: 23 | this._castMask = 0xFF; 24 | break; 25 | case 16: 26 | this._castMask = 0xFFFF; 27 | break; 28 | case 32: 29 | this._castMask = 0xFFFFFFFF; 30 | break; 31 | default: 32 | throw "Invalid CRC width"; 33 | } 34 | this._msbMask = 0x01 << (v - 1) 35 | } 36 | 37 | public get name(): string { return this._name; } 38 | public set name(v: string) { 39 | this._name = v; 40 | } 41 | 42 | public get polynomial(): number { return this._polynomial; } 43 | public set polynomial(v: number) { 44 | this._polynomial = v; 45 | } 46 | 47 | public get initial(): number { return this._initialVal; } 48 | public set initial(v: number) { 49 | this._initialVal = v; 50 | } 51 | 52 | public get finalXor(): number { return this._finalXorVal; } 53 | public set finalXor(v: number) { 54 | this._finalXorVal = v; 55 | } 56 | 57 | public get inputReflected(): boolean { return this._inputReflected; } 58 | public set inputReflected(v: boolean) { 59 | this._inputReflected = v; 60 | } 61 | 62 | public get resultReflected(): boolean { return this._resultReflected; } 63 | public set resultReflected(v: boolean) { 64 | this._resultReflected = v; 65 | } 66 | 67 | constructor(name: string, width: number, polynomial: number, initial: number, finalXor: number, inputReflected: boolean, resultReflected: boolean) { 68 | this.width = width; 69 | this.name = name; 70 | this.polynomial = polynomial; 71 | this.initial = initial; 72 | this.finalXor = finalXor; 73 | this.inputReflected = inputReflected; 74 | this.resultReflected = resultReflected; 75 | } 76 | 77 | public static get defaults(): CRC[] { 78 | if (!this._list) { 79 | this._list = [ 80 | new CRC("CRC8", 8, 0x07, 0x00, 0x00, false, false), 81 | new CRC("CRC8_SAE_J1850", 8, 0x1D, 0xFF, 0xFF, false, false), 82 | new CRC("CRC8_SAE_J1850_ZERO", 8, 0x1D, 0x00, 0x00, false, false), 83 | new CRC("CRC8_8H2F", 8, 0x2F, 0xFF, 0xFF, false, false), 84 | new CRC("CRC8_CDMA2000", 8, 0x9B, 0xFF, 0x00, false, false), 85 | new CRC("CRC8_DARC", 8, 0x39, 0x00, 0x00, true, true), 86 | new CRC("CRC8_DVB_S2", 8, 0xD5, 0x00, 0x00, false, false), 87 | new CRC("CRC8_EBU", 8, 0x1D, 0xFF, 0x00, true, true), 88 | new CRC("CRC8_ICODE", 8, 0x1D, 0xFD, 0x00, false, false), 89 | new CRC("CRC8_ITU", 8, 0x07, 0x00, 0x55, false, false), 90 | new CRC("CRC8_MAXIM", 8, 0x31, 0x00, 0x00, true, true), 91 | new CRC("CRC8_ROHC", 8, 0x07, 0xFF, 0x00, true, true), 92 | new CRC("CRC8_WCDMA", 8, 0x9B, 0x00, 0x00, true, true), 93 | new CRC("CRC16_CCIT_ZERO", 16, 0x1021, 0x0000, 0x0000, false, false), 94 | new CRC("CRC16_ARC", 16, 0x8005, 0x0000, 0x0000, true, true), 95 | new CRC("CRC16_AUG_CCITT", 16, 0x1021, 0x1D0F, 0x0000, false, false), 96 | new CRC("CRC16_BUYPASS", 16, 0x8005, 0x0000, 0x0000, false, false), 97 | new CRC("CRC16_CCITT_FALSE", 16, 0x1021, 0xFFFF, 0x0000, false, false), 98 | new CRC("CRC16_CDMA2000", 16, 0xC867, 0xFFFF, 0x0000, false, false), 99 | new CRC("CRC16_DDS_110", 16, 0x8005, 0x800D, 0x0000, false, false), 100 | new CRC("CRC16_DECT_R", 16, 0x0589, 0x0000, 0x0001, false, false), 101 | new CRC("CRC16_DECT_X", 16, 0x0589, 0x0000, 0x0000, false, false), 102 | new CRC("CRC16_DNP", 16, 0x3D65, 0x0000, 0xFFFF, true, true), 103 | new CRC("CRC16_EN_13757", 16, 0x3D65, 0x0000, 0xFFFF, false, false), 104 | new CRC("CRC16_GENIBUS", 16, 0x1021, 0xFFFF, 0xFFFF, false, false), 105 | new CRC("CRC16_MAXIM", 16, 0x8005, 0x0000, 0xFFFF, true, true), 106 | new CRC("CRC16_MCRF4XX", 16, 0x1021, 0xFFFF, 0x0000, true, true), 107 | new CRC("CRC16_RIELLO", 16, 0x1021, 0xB2AA, 0x0000, true, true), 108 | new CRC("CRC16_T10_DIF", 16, 0x8BB7, 0x0000, 0x0000, false, false), 109 | new CRC("CRC16_TELEDISK", 16, 0xA097, 0x0000, 0x0000, false, false), 110 | new CRC("CRC16_TMS37157", 16, 0x1021, 0x89EC, 0x0000, true, true), 111 | new CRC("CRC16_USB", 16, 0x8005, 0xFFFF, 0xFFFF, true, true), 112 | new CRC("CRC16_A", 16, 0x1021, 0xC6C6, 0x0000, true, true), 113 | new CRC("CRC16_KERMIT", 16, 0x1021, 0x0000, 0x0000, true, true), 114 | new CRC("CRC16_MODBUS", 16, 0x8005, 0xFFFF, 0x0000, true, true), 115 | new CRC("CRC16_X_25", 16, 0x1021, 0xFFFF, 0xFFFF, true, true), 116 | new CRC("CRC16_XMODEM", 16, 0x1021, 0x0000, 0x0000, false, false), 117 | new CRC("CRC32", 32, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, true), 118 | new CRC("CRC32_BZIP2", 32, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, false, false), 119 | new CRC("CRC32_C", 32, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true), 120 | new CRC("CRC32_D", 32, 0xA833982B, 0xFFFFFFFF, 0xFFFFFFFF, true, true), 121 | new CRC("CRC32_MPEG2", 32, 0x04C11DB7, 0xFFFFFFFF, 0x00000000, false, false), 122 | new CRC("CRC32_POSIX", 32, 0x04C11DB7, 0x00000000, 0xFFFFFFFF, false, false), 123 | new CRC("CRC32_Q", 32, 0x814141AB, 0x00000000, 0x00000000, false, false), 124 | new CRC("CRC32_JAMCRC", 32, 0x04C11DB7, 0xFFFFFFFF, 0x00000000, true, true), 125 | new CRC("CRC32_XFER", 32, 0x000000AF, 0x00000000, 0x00000000, false, false) 126 | ] 127 | } 128 | return this._list; 129 | } 130 | 131 | public makeCrcTable() { 132 | this._crcTable = new Array(256); 133 | 134 | for (var divident = 0; divident < 256; divident++) { 135 | var currByte = (divident << (this._width - 8)) & this._castMask; 136 | for (var bit = 0; bit < 8; bit++) { 137 | if ((currByte & this._msbMask) != 0) { 138 | currByte <<= 1; 139 | currByte ^= this._polynomial; 140 | } else { 141 | currByte <<= 1; 142 | } 143 | } 144 | this._crcTable[divident] = (currByte & this._castMask); 145 | } 146 | } 147 | 148 | public makeCrcTableReversed() { 149 | this._crcTable = new Array(256); 150 | 151 | for (var divident = 0; divident < 256; divident++) { 152 | var reflectedDivident = CrcUtil.Reflect8(divident); 153 | 154 | var currByte = (reflectedDivident << (this._width - 8)) & this._castMask; 155 | 156 | for (var bit = 0; bit < 8; bit++) { 157 | if ((currByte & this._msbMask) != 0) { 158 | currByte <<= 1; 159 | currByte ^= this._polynomial; 160 | } else { 161 | currByte <<= 1; 162 | } 163 | } 164 | 165 | currByte = CrcUtil.ReflectGeneric(currByte, this.width); 166 | 167 | this._crcTable[divident] = (currByte & this._castMask); 168 | } 169 | } 170 | 171 | public compute(bytes: number[] | Buffer) { 172 | if (!this._crcTable) 173 | this.makeCrcTable(); 174 | var crc = this._initialVal; 175 | for (var i = 0; i < bytes.length; i++) { 176 | 177 | var curByte = bytes[i] & 0xFF; 178 | 179 | if (this.inputReflected) { 180 | curByte = CrcUtil.Reflect8(curByte); 181 | } 182 | 183 | /* update the MSB of crc value with next input byte */ 184 | crc = (crc ^ (curByte << (this._width - 8))) & this._castMask; 185 | /* this MSB byte value is the index into the lookup table */ 186 | var pos = (crc >> (this.width - 8)) & 0xFF; 187 | /* shift out this index */ 188 | crc = (crc << 8) & this._castMask; 189 | /* XOR-in remainder from lookup table using the calculated index */ 190 | crc = (crc ^ this._crcTable[pos]) & this._castMask; 191 | } 192 | 193 | if (this.resultReflected) { 194 | crc = CrcUtil.ReflectGeneric(crc, this.width); 195 | } 196 | return ((crc ^ this._finalXorVal) & this._castMask); 197 | } 198 | 199 | public computeBuffer(bytes: number[] | Buffer) { 200 | let val = this.compute(bytes); 201 | if (this.width === 8) { 202 | return Buffer.from([val]) 203 | } else if (this.width === 16) { 204 | let b = Buffer.alloc(2); 205 | b.writeUInt16BE(val, 0); 206 | return b; 207 | } else if (this.width === 32) { 208 | let b = Buffer.alloc(4); 209 | b.writeUInt32BE(val, 0); 210 | return b; 211 | } else { 212 | throw new Error("Unsupported length"); 213 | } 214 | } 215 | 216 | public get table() { 217 | return this._crcTable; 218 | } 219 | 220 | public static default(name: string): CRC | undefined { 221 | return CRC 222 | .defaults 223 | .find((o: CRC): boolean => o.name === name); 224 | } 225 | } -------------------------------------------------------------------------------- /dist/crc.js: -------------------------------------------------------------------------------- 1 | "use strict"; 2 | Object.defineProperty(exports, "__esModule", { value: true }); 3 | var crcUtil_1 = require("./crcUtil"); 4 | var CRC = (function () { 5 | function CRC(name, width, polynomial, initial, finalXor, inputReflected, resultReflected) { 6 | this.width = width; 7 | this.name = name; 8 | this.polynomial = polynomial; 9 | this.initial = initial; 10 | this.finalXor = finalXor; 11 | this.inputReflected = inputReflected; 12 | this.resultReflected = resultReflected; 13 | } 14 | Object.defineProperty(CRC.prototype, "width", { 15 | get: function () { return this._width; }, 16 | set: function (v) { 17 | this._width = v; 18 | switch (v) { 19 | case 8: 20 | this._castMask = 0xFF; 21 | break; 22 | case 16: 23 | this._castMask = 0xFFFF; 24 | break; 25 | case 32: 26 | this._castMask = 0xFFFFFFFF; 27 | break; 28 | default: 29 | throw "Invalid CRC width"; 30 | } 31 | this._msbMask = 0x01 << (v - 1); 32 | }, 33 | enumerable: true, 34 | configurable: true 35 | }); 36 | Object.defineProperty(CRC.prototype, "name", { 37 | get: function () { return this._name; }, 38 | set: function (v) { 39 | this._name = v; 40 | }, 41 | enumerable: true, 42 | configurable: true 43 | }); 44 | Object.defineProperty(CRC.prototype, "polynomial", { 45 | get: function () { return this._polynomial; }, 46 | set: function (v) { 47 | this._polynomial = v; 48 | }, 49 | enumerable: true, 50 | configurable: true 51 | }); 52 | Object.defineProperty(CRC.prototype, "initial", { 53 | get: function () { return this._initialVal; }, 54 | set: function (v) { 55 | this._initialVal = v; 56 | }, 57 | enumerable: true, 58 | configurable: true 59 | }); 60 | Object.defineProperty(CRC.prototype, "finalXor", { 61 | get: function () { return this._finalXorVal; }, 62 | set: function (v) { 63 | this._finalXorVal = v; 64 | }, 65 | enumerable: true, 66 | configurable: true 67 | }); 68 | Object.defineProperty(CRC.prototype, "inputReflected", { 69 | get: function () { return this._inputReflected; }, 70 | set: function (v) { 71 | this._inputReflected = v; 72 | }, 73 | enumerable: true, 74 | configurable: true 75 | }); 76 | Object.defineProperty(CRC.prototype, "resultReflected", { 77 | get: function () { return this._resultReflected; }, 78 | set: function (v) { 79 | this._resultReflected = v; 80 | }, 81 | enumerable: true, 82 | configurable: true 83 | }); 84 | Object.defineProperty(CRC, "defaults", { 85 | get: function () { 86 | if (!this._list) { 87 | this._list = [ 88 | new CRC("CRC8", 8, 0x07, 0x00, 0x00, false, false), 89 | new CRC("CRC8_SAE_J1850", 8, 0x1D, 0xFF, 0xFF, false, false), 90 | new CRC("CRC8_SAE_J1850_ZERO", 8, 0x1D, 0x00, 0x00, false, false), 91 | new CRC("CRC8_8H2F", 8, 0x2F, 0xFF, 0xFF, false, false), 92 | new CRC("CRC8_CDMA2000", 8, 0x9B, 0xFF, 0x00, false, false), 93 | new CRC("CRC8_DARC", 8, 0x39, 0x00, 0x00, true, true), 94 | new CRC("CRC8_DVB_S2", 8, 0xD5, 0x00, 0x00, false, false), 95 | new CRC("CRC8_EBU", 8, 0x1D, 0xFF, 0x00, true, true), 96 | new CRC("CRC8_ICODE", 8, 0x1D, 0xFD, 0x00, false, false), 97 | new CRC("CRC8_ITU", 8, 0x07, 0x00, 0x55, false, false), 98 | new CRC("CRC8_MAXIM", 8, 0x31, 0x00, 0x00, true, true), 99 | new CRC("CRC8_ROHC", 8, 0x07, 0xFF, 0x00, true, true), 100 | new CRC("CRC8_WCDMA", 8, 0x9B, 0x00, 0x00, true, true), 101 | new CRC("CRC16_CCIT_ZERO", 16, 0x1021, 0x0000, 0x0000, false, false), 102 | new CRC("CRC16_ARC", 16, 0x8005, 0x0000, 0x0000, true, true), 103 | new CRC("CRC16_AUG_CCITT", 16, 0x1021, 0x1D0F, 0x0000, false, false), 104 | new CRC("CRC16_BUYPASS", 16, 0x8005, 0x0000, 0x0000, false, false), 105 | new CRC("CRC16_CCITT_FALSE", 16, 0x1021, 0xFFFF, 0x0000, false, false), 106 | new CRC("CRC16_CDMA2000", 16, 0xC867, 0xFFFF, 0x0000, false, false), 107 | new CRC("CRC16_DDS_110", 16, 0x8005, 0x800D, 0x0000, false, false), 108 | new CRC("CRC16_DECT_R", 16, 0x0589, 0x0000, 0x0001, false, false), 109 | new CRC("CRC16_DECT_X", 16, 0x0589, 0x0000, 0x0000, false, false), 110 | new CRC("CRC16_DNP", 16, 0x3D65, 0x0000, 0xFFFF, true, true), 111 | new CRC("CRC16_EN_13757", 16, 0x3D65, 0x0000, 0xFFFF, false, false), 112 | new CRC("CRC16_GENIBUS", 16, 0x1021, 0xFFFF, 0xFFFF, false, false), 113 | new CRC("CRC16_MAXIM", 16, 0x8005, 0x0000, 0xFFFF, true, true), 114 | new CRC("CRC16_MCRF4XX", 16, 0x1021, 0xFFFF, 0x0000, true, true), 115 | new CRC("CRC16_RIELLO", 16, 0x1021, 0xB2AA, 0x0000, true, true), 116 | new CRC("CRC16_T10_DIF", 16, 0x8BB7, 0x0000, 0x0000, false, false), 117 | new CRC("CRC16_TELEDISK", 16, 0xA097, 0x0000, 0x0000, false, false), 118 | new CRC("CRC16_TMS37157", 16, 0x1021, 0x89EC, 0x0000, true, true), 119 | new CRC("CRC16_USB", 16, 0x8005, 0xFFFF, 0xFFFF, true, true), 120 | new CRC("CRC16_A", 16, 0x1021, 0xC6C6, 0x0000, true, true), 121 | new CRC("CRC16_KERMIT", 16, 0x1021, 0x0000, 0x0000, true, true), 122 | new CRC("CRC16_MODBUS", 16, 0x8005, 0xFFFF, 0x0000, true, true), 123 | new CRC("CRC16_X_25", 16, 0x1021, 0xFFFF, 0xFFFF, true, true), 124 | new CRC("CRC16_XMODEM", 16, 0x1021, 0x0000, 0x0000, false, false), 125 | new CRC("CRC32", 32, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, true, true), 126 | new CRC("CRC32_BZIP2", 32, 0x04C11DB7, 0xFFFFFFFF, 0xFFFFFFFF, false, false), 127 | new CRC("CRC32_C", 32, 0x1EDC6F41, 0xFFFFFFFF, 0xFFFFFFFF, true, true), 128 | new CRC("CRC32_D", 32, 0xA833982B, 0xFFFFFFFF, 0xFFFFFFFF, true, true), 129 | new CRC("CRC32_MPEG2", 32, 0x04C11DB7, 0xFFFFFFFF, 0x00000000, false, false), 130 | new CRC("CRC32_POSIX", 32, 0x04C11DB7, 0x00000000, 0xFFFFFFFF, false, false), 131 | new CRC("CRC32_Q", 32, 0x814141AB, 0x00000000, 0x00000000, false, false), 132 | new CRC("CRC32_JAMCRC", 32, 0x04C11DB7, 0xFFFFFFFF, 0x00000000, true, true), 133 | new CRC("CRC32_XFER", 32, 0x000000AF, 0x00000000, 0x00000000, false, false) 134 | ]; 135 | } 136 | return this._list; 137 | }, 138 | enumerable: true, 139 | configurable: true 140 | }); 141 | CRC.prototype.makeCrcTable = function () { 142 | this._crcTable = new Array(256); 143 | for (var divident = 0; divident < 256; divident++) { 144 | var currByte = (divident << (this._width - 8)) & this._castMask; 145 | for (var bit = 0; bit < 8; bit++) { 146 | if ((currByte & this._msbMask) != 0) { 147 | currByte <<= 1; 148 | currByte ^= this._polynomial; 149 | } 150 | else { 151 | currByte <<= 1; 152 | } 153 | } 154 | this._crcTable[divident] = (currByte & this._castMask); 155 | } 156 | }; 157 | CRC.prototype.makeCrcTableReversed = function () { 158 | this._crcTable = new Array(256); 159 | for (var divident = 0; divident < 256; divident++) { 160 | var reflectedDivident = crcUtil_1.default.Reflect8(divident); 161 | var currByte = (reflectedDivident << (this._width - 8)) & this._castMask; 162 | for (var bit = 0; bit < 8; bit++) { 163 | if ((currByte & this._msbMask) != 0) { 164 | currByte <<= 1; 165 | currByte ^= this._polynomial; 166 | } 167 | else { 168 | currByte <<= 1; 169 | } 170 | } 171 | currByte = crcUtil_1.default.ReflectGeneric(currByte, this.width); 172 | this._crcTable[divident] = (currByte & this._castMask); 173 | } 174 | }; 175 | CRC.prototype.compute = function (bytes) { 176 | if (!this._crcTable) 177 | this.makeCrcTable(); 178 | var crc = this._initialVal; 179 | for (var i = 0; i < bytes.length; i++) { 180 | var curByte = bytes[i] & 0xFF; 181 | if (this.inputReflected) { 182 | curByte = crcUtil_1.default.Reflect8(curByte); 183 | } 184 | crc = (crc ^ (curByte << (this._width - 8))) & this._castMask; 185 | var pos = (crc >> (this.width - 8)) & 0xFF; 186 | crc = (crc << 8) & this._castMask; 187 | crc = (crc ^ this._crcTable[pos]) & this._castMask; 188 | } 189 | if (this.resultReflected) { 190 | crc = crcUtil_1.default.ReflectGeneric(crc, this.width); 191 | } 192 | return ((crc ^ this._finalXorVal) & this._castMask); 193 | }; 194 | CRC.prototype.computeBuffer = function (bytes) { 195 | var val = this.compute(bytes); 196 | if (this.width === 8) { 197 | return Buffer.from([val]); 198 | } 199 | else if (this.width === 16) { 200 | var b = Buffer.alloc(2); 201 | b.writeUInt16BE(val, 0); 202 | return b; 203 | } 204 | else if (this.width === 32) { 205 | var b = Buffer.alloc(4); 206 | b.writeUInt32BE(val, 0); 207 | return b; 208 | } 209 | else { 210 | throw new Error("Unsupported length"); 211 | } 212 | }; 213 | Object.defineProperty(CRC.prototype, "table", { 214 | get: function () { 215 | return this._crcTable; 216 | }, 217 | enumerable: true, 218 | configurable: true 219 | }); 220 | CRC.default = function (name) { 221 | return CRC 222 | .defaults 223 | .find(function (o) { return o.name === name; }); 224 | }; 225 | return CRC; 226 | }()); 227 | exports.CRC = CRC; 228 | //# sourceMappingURL=crc.js.map --------------------------------------------------------------------------------