├── .covignore ├── .gitignore ├── .travis.yml ├── CHANGELOG.md ├── LICENSE.txt ├── README.md ├── bower.json ├── build ├── crc.min.js └── models.min.js ├── index.d.ts ├── models.d.ts ├── package-lock.json ├── package.json ├── src ├── crc.js └── models.js └── tests ├── index.html ├── models-test.js ├── node-test.js ├── test.js ├── worker-test.js ├── worker.html └── worker.js /.covignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | /tests/ 3 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | /node_modules/ 2 | /covreporter/ 3 | /.nyc_output/ 4 | /coverage/ 5 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | language: node_js 2 | node_js: 3 | - "0.12.15" 4 | - "4.5" 5 | - "6.5.0" 6 | before_install: 7 | - npm install coveralls 8 | - npm install mocha-lcov-reporter 9 | script: npm run-script coveralls 10 | branches: 11 | only: 12 | - master 13 | -------------------------------------------------------------------------------- /CHANGELOG.md: -------------------------------------------------------------------------------- 1 | # Change Log 2 | 3 | ## v0.3.1 / 2024-10-22 4 | ### Fixed 5 | - package.json main path. 6 | 7 | ## v0.3.0 / 2024-10-22 8 | ### Added 9 | - model params support 10 | - Streaming support 11 | - web worker support. 12 | - TypeScript support 13 | - many predefined models. 14 | 15 | ### Changed 16 | - throw error if input type is incorrect. 17 | 18 | ## v0.2.0 / 2017-02-08 19 | ### Added 20 | - AMD support. 21 | 22 | ### Fixed 23 | - `root` is undefined in some special environment. 24 | 25 | ## v0.1.0 / 2015-03-08 26 | ### Added 27 | - create project. 28 | -------------------------------------------------------------------------------- /LICENSE.txt: -------------------------------------------------------------------------------- 1 | Copyright 2015-2024 Chen, Yi-Cyuan 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # js-crc 2 | [![Build Status](https://travis-ci.org/emn178/js-crc.svg?branch=master)](https://travis-ci.org/emn178/js-crc) 3 | [![Coverage Status](https://coveralls.io/repos/emn178/js-crc/badge.svg?branch=master)](https://coveralls.io/r/emn178/js-crc?branch=master) 4 | [![NPM](https://nodei.co/npm/js-crc.png?stars&downloads)](https://nodei.co/npm/js-crc/) 5 | Simple CRC checksum functions for JavaScript. Supports many predefined models such as CRC-8, CRC-16, CRC-24, CRC-32, and CRC-64. It also supports custom CRC models. 6 | 7 | ## Download 8 | ### Core 9 | [Compress](https://raw.github.com/emn178/js-crc/master/build/crc.min.js) 10 | [Uncompress](https://raw.github.com/emn178/js-crc/master/src/crc.js) 11 | 12 | ### Models 13 | [Compress](https://raw.github.com/emn178/js-crc/master/build/models.min.js) 14 | [Uncompress](https://raw.github.com/emn178/js-crc/master/src/model.js) 15 | 16 | ## Installation 17 | You can also install js-crc by using Bower. 18 | 19 | bower install js-crc 20 | 21 | For node.js, you can use this command to install: 22 | 23 | npm install js-crc 24 | 25 | ## Import 26 | There are only 2 default models in `js-crc`. crc32 and crc16. More models are in `js-crc/models`. You can check all models in [this file](https://github.com/emn178/js-crc/blob/master/src/models.js). The `name` and `alias` will convert to lower snake case and export. For example, `CRC-64/ECMA-182` will convert to `crc_64_ecma_182`. 27 | 28 | ### Node.js 29 | If you use node.js, you should require the module first: 30 | ```JavaScript 31 | var crc32 = require('js-crc').crc32; 32 | var crc16 = require('js-crc').crc16; 33 | var crc_64_ecma_182 = require('js-crc/models').crc_64_ecma_182; 34 | ``` 35 | or 36 | ```JavaScript 37 | const { crc32, crc16 } = require('js-crc'); 38 | const { crc_64_ecma_182 } = require('js-crc/models'); 39 | ``` 40 | 41 | ### TypeScript 42 | If you use TypeScript, you can import like this: 43 | ```TypeScript 44 | import { crc32, crc16 } from 'js-crc'; 45 | import { crc_64_ecma_182 } from 'js-crc/models'; 46 | ``` 47 | 48 | ### RequireJS 49 | It supports AMD: 50 | ```JavaScript 51 | require(['your/path/crc.js'], function (crc) { 52 | var crc32 = crc.crc32; 53 | var crc16 = crc.crc16; 54 | // ... 55 | }); 56 | ``` 57 | 58 | ## Usage 59 | ### Basic 60 | You could use like this: 61 | ```JavaScript 62 | crc32('Message to check'); 63 | crc16('Message to check'); 64 | 65 | var crc = crc32.create(); 66 | crc.update('Message to check'); 67 | crc.hex(); 68 | 69 | var crc2 = crc32.update('Message to check'); 70 | crc2.update('Message2 to check'); 71 | crc2.array(); 72 | ``` 73 | 74 | ### Custom Model 75 | You can create custom model: 76 | ```JavaScript 77 | const { createModel } = require('js-crc'); 78 | var myModel = createModel({ 79 | width: 16, 80 | poly: 0x8005, 81 | init: 0x0000, 82 | refin: true, 83 | refout: true, 84 | xorout: 0x0000 85 | }); 86 | myModel('Message to check'); 87 | ``` 88 | 89 | If width more than 32, `poly`, `init` and `xorout` have to split into an array of 32-bit numbers. For example 90 | ```JavaScript 91 | createModel({ 92 | width: 82, 93 | poly: [0x0308c, 0x01110114, 0x01440411], 94 | init: [0, 0, 0], 95 | refin: true, 96 | refout: true, 97 | xorout: [0, 0, 0] 98 | }) 99 | ``` 100 | 101 | ### Example 102 | ```JavaScript 103 | crc32('The quick brown fox jumps over the lazy dog'); // 414fa339 104 | crc32('The quick brown fox jumps over the lazy dog.'); // 519025e9 105 | 106 | // It also supports byte `Array`, `Uint8Array`, `ArrayBuffer` input: 107 | crc32([0]); // d202ef8d 108 | crc32(new Uint8Array([0])); // d202ef8d 109 | ``` 110 | 111 | ## License 112 | The project is released under the [MIT license](http://www.opensource.org/licenses/MIT). 113 | 114 | ## Contact 115 | The project's website is located at https://github.com/emn178/js-crc 116 | Author: Chen, Yi-Cyuan (emn178@gmail.com) 117 | -------------------------------------------------------------------------------- /bower.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "js-crc", 3 | "version": "0.3.1", 4 | "main": ["src/crc.js"], 5 | "ignore": [ 6 | "samples", 7 | "tests" 8 | ] 9 | } 10 | -------------------------------------------------------------------------------- /build/crc.min.js: -------------------------------------------------------------------------------- 1 | /** 2 | * [js-crc]{@link https://github.com/emn178/js-crc} 3 | * 4 | * @namespace crc 5 | * @version 0.3.1 6 | * @author Chen, Yi-Cyuan [emn178@gmail.com] 7 | * @copyright Chen, Yi-Cyuan 2015-2024 8 | * @license MIT 9 | */ 10 | (()=>{var o="input is invalid type",t=(i="object"==typeof window)?window:{},i=!(i=t.JS_CRC_NO_WINDOW?!1:i)&&"object"==typeof self,i=(!t.JS_CRC_NO_NODE_JS&&"object"==typeof process&&process.versions&&process.versions.node?t=global:i&&(t=self),!t.JS_CRC_NO_COMMON_JS&&"object"==typeof module&&module.exports),r="function"==typeof define&&define.amd,f=!t.JS_CRC_NO_ARRAY_BUFFER&&"undefined"!=typeof ArrayBuffer,n="0123456789abcdef".split(""),d=["hex","array"],e=[{width:32,poly:79764919,init:4294967295,refin:!0,refout:!0,xorout:4294967295,name:"crc32"},{width:16,poly:32773,init:0,refin:!0,refout:!0,xorout:0,name:"crc16"}],h=Array.isArray,c=(!t.JS_CRC_NO_NODE_JS&&h||(h=function(t){return"[object Array]"===Object.prototype.toString.call(t)}),ArrayBuffer.isView);function p(i,r){return function(t){return new _(r).update(t)[i]()}}function s(t){var i,r,e=8-(t.width%8||8),s=(Math.ceil(t.width/8)%4||4)<<3,o=1<>>0,s=s-8,f=t.width%32||32,n=32>>32-i;t[r]=t[r]<{var e=[],s=h(t);for(s||(t=[t]),v=0;v<256;++v){for(var o=[v<>>=1;return r}for(var b=[],v=0;v<256;++v)b[v]=m(v,8);function _(t){this.options=t,this.refin=t.refin,this.multiWords=t.multiWords,this.bitOffset=t.bitOffset,this.msbOffset=t.msbOffset,this.maskBits=t.maskBits,this.mask=t.mask,this.table=l(t.tableId,t.poly,this.msbOffset,t.msb),this.multiWords?this.crc=t.crc.slice():this.crc=t.crc}_.prototype.update=function(t){if(this.finalized)throw new Error("finalize already called");var i,r,e=(t=>{var i=typeof t;if("string"==i)return[t,!0];if("object"==i&&null!==t){if(f&&t.constructor===ArrayBuffer)return[new Uint8Array(t),!1];if(h(t)||c(t))return[t,!1]}throw new Error(o)})(t),e=(t=e[0],e[1]),s=t.length;if(e)for(r=0;r>6):(i<55296||57344<=i?this.updateByte(224|i>>12):(i=65536+((1023&i)<<10|1023&t.charCodeAt(++r)),this.updateByte(240|i>>18),this.updateByte(128|i>>12&63)),this.updateByte(128|i>>6&63)),this.updateByte(128|63&i));else for(r=0;r>this.msbOffset&255],y(r,8),a(r,i)):r=(r^=t<>this.msbOffset&255],this.crc=r},_.prototype.finalize=function(){if(!this.finalized)if(this.finalized=!0,this.multiWords){var t=this.crc,i=this.bitOffset;if(i){for(var r=t.length-1;0>>i;t[r]=t[r]>>>i}if(this.crc[0]=this.crc[0]&this.mask,this.options.refout){y(this.crc,32-this.maskBits);for(var e=[],s=0;s>>this.bitOffset&this.mask,this.options.refout&&(this.crc=m(this.crc,this.options.width)),this.crc^=this.options.xorout},_.prototype.toString=_.prototype.hex=function(){this.finalize();var t="",i=this.crc,r=this.options.width;this.multiWords&&(i=i[0],r=r%32||32);for(var e=(Math.ceil(r/4)<<2)-4;0<=e;e-=4)t+=n[i>>e&15];if(this.multiWords)for(var s=1;s>e&15];return t},_.prototype.array=function(){this.finalize();for(var t=new Array(Math.ceil(this.options.width/8)),i=this.crc,r=this.options.width,e=(this.multiWords&&(i=i[0],r=r%32||32),0),s=(Math.ceil(r/8)<<3)-8;0<=s;s-=8)t[e++]=i>>s&255;if(this.multiWords)for(var o=1;o>s&255;return t};for(var w,O={},v=0;v{var r,n=[{width:3,poly:3,init:0,refin:!1,refout:!1,xorout:7,name:"CRC-3/GSM"},{width:3,poly:3,init:7,refin:!0,refout:!0,xorout:0,name:"CRC-3/ROHC"},{width:4,poly:3,init:0,refin:!0,refout:!0,xorout:0,name:"CRC-4/G-704",alias:["CRC-4/ITU"]},{width:4,poly:3,init:15,refin:!1,refout:!1,xorout:15,name:"CRC-4/INTERLAKEN"},{width:5,poly:9,init:9,refin:!1,refout:!1,xorout:0,name:"CRC-5/EPC-C1G2",alias:["CRC-5/EPC"]},{width:5,poly:21,init:0,refin:!0,refout:!0,xorout:0,name:"CRC-5/G-704",alias:["CRC-5/ITU"]},{width:5,poly:5,init:31,refin:!0,refout:!0,xorout:31,name:"CRC-5/USB"},{width:6,poly:39,init:63,refin:!1,refout:!1,xorout:0,name:"CRC-6/CDMA2000-A"},{width:6,poly:7,init:63,refin:!1,refout:!1,xorout:0,name:"CRC-6/CDMA2000-B"},{width:6,poly:25,init:0,refin:!0,refout:!0,xorout:0,name:"CRC-6/DARC"},{width:6,poly:3,init:0,refin:!0,refout:!0,xorout:0,name:"CRC-6/G-704",alias:["CRC-6/ITU"]},{width:6,poly:47,init:0,refin:!1,refout:!1,xorout:63,name:"CRC-6/GSM"},{width:7,poly:9,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-7/MMC",alias:["CRC-7"]},{width:7,poly:79,init:127,refin:!0,refout:!0,xorout:0,name:"CRC-7/ROHC"},{width:7,poly:69,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-7/UMTS"},{width:8,poly:47,init:255,refin:!1,refout:!1,xorout:255,name:"CRC-8/AUTOSAR"},{width:8,poly:167,init:0,refin:!0,refout:!0,xorout:0,name:"CRC-8/BLUETOOTH"},{width:8,poly:155,init:255,refin:!1,refout:!1,xorout:0,name:"CRC-8/CDMA2000"},{width:8,poly:57,init:0,refin:!0,refout:!0,xorout:0,name:"CRC-8/DARC"},{width:8,poly:213,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-8/DVB-S2"},{width:8,poly:29,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-8/GSM-A"},{width:8,poly:73,init:0,refin:!1,refout:!1,xorout:255,name:"CRC-8/GSM-B"},{width:8,poly:29,init:255,refin:!1,refout:!1,xorout:0,name:"CRC-8/HITAG"},{width:8,poly:7,init:0,refin:!1,refout:!1,xorout:85,name:"CRC-8/I-432-1",alias:["CRC-8/ITU"]},{width:8,poly:29,init:253,refin:!1,refout:!1,xorout:0,name:"CRC-8/I-CODE"},{width:8,poly:155,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-8/LTE"},{width:8,poly:49,init:0,refin:!0,refout:!0,xorout:0,name:"CRC-8/MAXIM-DOW",alias:["CRC-8/MAXIM","DOW-CRC"]},{width:8,poly:29,init:199,refin:!1,refout:!1,xorout:0,name:"CRC-8/MIFARE-MAD"},{width:8,poly:49,init:255,refin:!1,refout:!1,xorout:0,name:"CRC-8/NRSC-5"},{width:8,poly:47,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-8/OPENSAFETY"},{width:8,poly:7,init:255,refin:!0,refout:!0,xorout:0,name:"CRC-8/ROHC"},{width:8,poly:29,init:255,refin:!1,refout:!1,xorout:255,name:"CRC-8/SAE-J1850"},{width:8,poly:7,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-8/SMBUS",alias:["CRC-8"]},{width:8,poly:29,init:255,refin:!0,refout:!0,xorout:0,name:"CRC-8/TECH-3250",alias:["CRC-8/AES","CRC-8/EBU"]},{width:8,poly:155,init:0,refin:!0,refout:!0,xorout:0,name:"CRC-8/WCDMA"},{width:10,poly:563,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-10/ATM",alias:["CRC-10","CRC-10/I-610"]},{width:10,poly:985,init:1023,refin:!1,refout:!1,xorout:0,name:"CRC-10/CDMA2000"},{width:10,poly:373,init:0,refin:!1,refout:!1,xorout:1023,name:"CRC-10/GSM"},{width:11,poly:901,init:26,refin:!1,refout:!1,xorout:0,name:"CRC-11/FLEXRAY",alias:["CRC-11"]},{width:11,poly:775,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-11/UMTS"},{width:12,poly:3859,init:4095,refin:!1,refout:!1,xorout:0,name:"CRC-12/CDMA2000"},{width:12,poly:2063,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-12/DECT",alias:["X-CRC-12"]},{width:12,poly:3377,init:0,refin:!1,refout:!1,xorout:4095,name:"CRC-12/GSM"},{width:12,poly:2063,init:0,refin:!1,refout:!0,xorout:0,name:"CRC-12/UMTS",alias:["CRC-12/3GPP"]},{width:13,poly:7413,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-13/BBC"},{width:14,poly:2053,init:0,refin:!0,refout:!0,xorout:0,name:"CRC-14/DARC"},{width:14,poly:8237,init:0,refin:!1,refout:!1,xorout:16383,name:"CRC-14/GSM"},{width:15,poly:17817,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-15/CAN",alias:["CRC-15"]},{width:15,poly:26645,init:0,refin:!1,refout:!1,xorout:1,name:"CRC-15/MPT1327"},{width:16,poly:32773,init:0,refin:!0,refout:!0,xorout:0,name:"CRC-16/ARC",alias:["ARC","CRC-16","CRC-16/LHA","CRC-IBM"]},{width:16,poly:51303,init:65535,refin:!1,refout:!1,xorout:0,name:"CRC-16/CDMA2000"},{width:16,poly:32773,init:65535,refin:!1,refout:!1,xorout:0,name:"CRC-16/CMS"},{width:16,poly:32773,init:32781,refin:!1,refout:!1,xorout:0,name:"CRC-16/DDS-110"},{width:16,poly:1417,init:0,refin:!1,refout:!1,xorout:1,name:"CRC-16/DECT-R",alias:["R-CRC-16"]},{width:16,poly:1417,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-16/DECT-X",alias:["X-CRC-16"]},{width:16,poly:15717,init:0,refin:!0,refout:!0,xorout:65535,name:"CRC-16/DNP"},{width:16,poly:15717,init:0,refin:!1,refout:!1,xorout:65535,name:"CRC-16/EN-13757"},{width:16,poly:4129,init:65535,refin:!1,refout:!1,xorout:65535,name:"CRC-16/GENIBUS",alias:["CRC-16/DARC","CRC-16/EPC","CRC-16/EPC-C1G2","CRC-16/I-CODE"]},{width:16,poly:4129,init:0,refin:!1,refout:!1,xorout:65535,name:"CRC-16/GSM"},{width:16,poly:4129,init:65535,refin:!1,refout:!1,xorout:0,name:"CRC-16/IBM-3740",alias:["CRC-16/AUTOSAR","CRC-16/CCITT-FALSE"]},{width:16,poly:4129,init:65535,refin:!0,refout:!0,xorout:65535,name:"CRC-16/IBM-SDLC",alias:["CRC-16/ISO-HDLC","CRC-16/ISO-IEC-14443-3-B","CRC-16/X-25","CRC-B","X-25"]},{width:16,poly:4129,init:50886,refin:!0,refout:!0,xorout:0,name:"CRC-16/ISO-IEC-14443-3-A",alias:["CRC-A"]},{width:16,poly:4129,init:0,refin:!0,refout:!0,xorout:0,name:"CRC-16/KERMIT",alias:["CRC-16/BLUETOOTH","CRC-16/CCITT","CRC-16/CCITT-TRUE","CRC-16/V-41-LSB","CRC-CCITT","KERMIT"]},{width:16,poly:28515,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-16/LJ1200"},{width:16,poly:22837,init:65535,refin:!1,refout:!1,xorout:0,name:"CRC-16/M17"},{width:16,poly:32773,init:0,refin:!0,refout:!0,xorout:65535,name:"CRC-16/MAXIM-DOW",alias:["CRC-16/MAXIM"]},{width:16,poly:4129,init:65535,refin:!0,refout:!0,xorout:0,name:"CRC-16/MCRF4XX"},{width:16,poly:32773,init:65535,refin:!0,refout:!0,xorout:0,name:"CRC-16/MODBUS",alias:["MODBUS"]},{width:16,poly:2059,init:65535,refin:!0,refout:!0,xorout:0,name:"CRC-16/NRSC-5"},{width:16,poly:22837,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-16/OPENSAFETY-A"},{width:16,poly:30043,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-16/OPENSAFETY-B"},{width:16,poly:7631,init:65535,refin:!1,refout:!1,xorout:65535,name:"CRC-16/PROFIBUS",alias:["CRC-16/IEC-61158-2"]},{width:16,poly:4129,init:45738,refin:!0,refout:!0,xorout:0,name:"CRC-16/RIELLO"},{width:16,poly:4129,init:7439,refin:!1,refout:!1,xorout:0,name:"CRC-16/SPI-FUJITSU",alias:["CRC-16/AUG-CCITT"]},{width:16,poly:35767,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-16/T10-DIF"},{width:16,poly:41111,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-16/TELEDISK"},{width:16,poly:4129,init:35308,refin:!0,refout:!0,xorout:0,name:"CRC-16/TMS37157"},{width:16,poly:32773,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-16/UMTS",alias:["CRC-16/BUYPASS","CRC-16/VERIFONE"]},{width:16,poly:32773,init:65535,refin:!0,refout:!0,xorout:65535,name:"CRC-16/USB"},{width:16,poly:4129,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-16/XMODEM",alias:["CRC-16/ACORN","CRC-16/LTE","CRC-16/V-41-MSB","XMODEM","ZMODEM"]},{width:17,poly:92251,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-17/CAN-FD"},{width:21,poly:1058969,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-21/CAN-FD"},{width:24,poly:1627,init:5592405,refin:!0,refout:!0,xorout:0,name:"CRC-24/BLE"},{width:24,poly:6122955,init:16702650,refin:!1,refout:!1,xorout:0,name:"CRC-24/FLEXRAY-A"},{width:24,poly:6122955,init:11259375,refin:!1,refout:!1,xorout:0,name:"CRC-24/FLEXRAY-B"},{width:24,poly:3312483,init:16777215,refin:!1,refout:!1,xorout:16777215,name:"CRC-24/INTERLAKEN"},{width:24,poly:8801531,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-24/LTE-A"},{width:24,poly:8388707,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-24/LTE-B"},{width:24,poly:8801531,init:11994318,refin:!1,refout:!1,xorout:0,name:"CRC-24/OPENPGP",alias:["CRC-24"]},{width:24,poly:8388707,init:16777215,refin:!1,refout:!1,xorout:16777215,name:"CRC-24/OS-9"},{width:30,poly:540064199,init:1073741823,refin:!1,refout:!1,xorout:1073741823,name:"CRC-30/CDMA"},{width:31,poly:79764919,init:2147483647,refin:!1,refout:!1,xorout:2147483647,name:"CRC-31/PHILIPS"},{width:32,poly:2168537515,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-32/AIXM",alias:["CRC-32Q"]},{width:32,poly:4104977171,init:4294967295,refin:!0,refout:!0,xorout:4294967295,name:"CRC-32/AUTOSAR"},{width:32,poly:2821953579,init:4294967295,refin:!0,refout:!0,xorout:4294967295,name:"CRC-32/BASE91-D",alias:["CRC-32D"]},{width:32,poly:79764919,init:4294967295,refin:!1,refout:!1,xorout:4294967295,name:"CRC-32/BZIP2",alias:["CRC-32/AAL5","CRC-32/DECT-B","B-CRC-32"]},{width:32,poly:2147581979,init:0,refin:!0,refout:!0,xorout:0,name:"CRC-32/CD-ROM-EDC"},{width:32,poly:79764919,init:0,refin:!1,refout:!1,xorout:4294967295,name:"CRC-32/CKSUM",alias:["CKSUM","CRC-32/POSIX"]},{width:32,poly:517762881,init:4294967295,refin:!0,refout:!0,xorout:4294967295,name:"CRC-32/ISCSI",alias:["CRC-32/BASE91-C","CRC-32/CASTAGNOLI","CRC-32/INTERLAKEN","CRC-32C","CRC-32/NVME"]},{width:32,poly:79764919,init:4294967295,refin:!0,refout:!0,xorout:4294967295,name:"CRC-32/ISO-HDLC",alias:["CRC-32","CRC-32/ADCCP","CRC-32/V-42","CRC-32/XZ","PKZIP"]},{width:32,poly:79764919,init:4294967295,refin:!0,refout:!0,xorout:0,name:"CRC-32/JAMCRC",alias:["JAMCRC"]},{width:32,poly:1947962583,init:4294967295,refin:!0,refout:!0,xorout:0,name:"CRC-32/MEF"},{width:32,poly:79764919,init:4294967295,refin:!1,refout:!1,xorout:0,name:"CRC-32/MPEG-2"},{width:32,poly:175,init:0,refin:!1,refout:!1,xorout:0,name:"CRC-32/XFER",alias:["XFER"]},{width:40,poly:[0,75628553],init:[0,0],refin:!1,refout:!1,xorout:[255,4294967295],name:"CRC-40/GSM"},{width:64,poly:[1123082731,2850698899],init:[0,0],refin:!1,refout:!1,xorout:[0,0],name:"CRC-64/ECMA-182",alias:["CRC-64"]},{width:64,poly:[0,27],init:[4294967295,4294967295],refin:!0,refout:!0,xorout:[4294967295,4294967295],name:"CRC-64/GO-ISO"},{width:64,poly:[631014603,2789368649],init:[4294967295,4294967295],refin:!0,refout:!0,xorout:[0,0],name:"CRC-64/MS"},{width:64,poly:[2912145973,2496214617],init:[4294967295,4294967295],refin:!0,refout:!0,xorout:[4294967295,4294967295],name:"CRC-64/NVME"},{width:64,poly:[2912145973,2496214441],init:[0,0],refin:!0,refout:!0,xorout:[0,0],name:"CRC-64/REDIS"},{width:64,poly:[1123082731,2850698899],init:[4294967295,4294967295],refin:!1,refout:!1,xorout:[4294967295,4294967295],name:"CRC-64/WE"},{width:64,poly:[1123082731,2850698899],init:[4294967295,4294967295],refin:!0,refout:!0,xorout:[4294967295,4294967295],name:"CRC-64/XZ",alias:["CRC-64/GO-ECMA"]},{width:82,poly:[12428,17891604,21234705],init:[0,0,0],refin:!0,refout:!0,xorout:[0,0,0],name:"CRC-82/DARC"}],i=(o="object"==typeof window)?window:{},o=!(o=i.JS_CRC_NO_WINDOW?!1:o)&&"object"==typeof self,o=(!i.JS_CRC_NO_NODE_JS&&"object"==typeof process&&process.versions&&process.versions.node?i=global:o&&(i=self),!i.JS_CRC_NO_COMMON_JS&&"object"==typeof module&&module.exports),t="function"==typeof define&&define.amd;function C(i){for(var o=0;o", 45 | "homepage": "https://github.com/emn178/js-crc", 46 | "bugs": { 47 | "url": "https://github.com/emn178/js-crc/issues" 48 | } 49 | } 50 | -------------------------------------------------------------------------------- /src/crc.js: -------------------------------------------------------------------------------- 1 | /** 2 | * [js-crc]{@link https://github.com/emn178/js-crc} 3 | * 4 | * @namespace crc 5 | * @version 0.3.1 6 | * @author Chen, Yi-Cyuan [emn178@gmail.com] 7 | * @copyright Chen, Yi-Cyuan 2015-2024 8 | * @license MIT 9 | */ 10 | /*jslint bitwise: true */ 11 | (function () { 12 | 'use strict'; 13 | 14 | var INPUT_ERROR = 'input is invalid type'; 15 | var FINALIZE_ERROR = 'finalize already called'; 16 | var WINDOW = typeof window === 'object'; 17 | var root = WINDOW ? window : {}; 18 | if (root.JS_CRC_NO_WINDOW) { 19 | WINDOW = false; 20 | } 21 | var WEB_WORKER = !WINDOW && typeof self === 'object'; 22 | var NODE_JS = !root.JS_CRC_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node; 23 | if (NODE_JS) { 24 | root = global; 25 | } else if (WEB_WORKER) { 26 | root = self; 27 | } 28 | var COMMON_JS = !root.JS_CRC_NO_COMMON_JS && typeof module === 'object' && module.exports; 29 | var AMD = typeof define === 'function' && define.amd; 30 | var ARRAY_BUFFER = !root.JS_CRC_NO_ARRAY_BUFFER && typeof ArrayBuffer !== 'undefined'; 31 | var HEX_CHARS = '0123456789abcdef'.split(''); 32 | var OUTPUT_TYPES = ['hex', 'array']; 33 | var MODELS = [ 34 | { 35 | width: 32, 36 | poly: 0x04c11db7, 37 | init: 0xffffffff, 38 | refin: true, 39 | refout: true, 40 | xorout: 0xffffffff, 41 | // CRC-32/ISO-HDLC 42 | name: 'crc32' 43 | }, 44 | { 45 | width: 16, 46 | poly: 0x8005, 47 | init: 0x0000, 48 | refin: true, 49 | refout: true, 50 | xorout: 0x0000, 51 | // CRC-16/ARC 52 | name: 'crc16' 53 | } 54 | ]; 55 | 56 | var isArray = Array.isArray; 57 | if (root.JS_CRC_NO_NODE_JS || !isArray) { 58 | isArray = function (obj) { 59 | return Object.prototype.toString.call(obj) === '[object Array]'; 60 | }; 61 | } 62 | 63 | var isView = ArrayBuffer.isView; 64 | if (ARRAY_BUFFER && (root.JS_CRC_NO_ARRAY_BUFFER_IS_VIEW || !isView)) { 65 | isView = function (obj) { 66 | return typeof obj === 'object' && obj.buffer && obj.buffer.constructor === ArrayBuffer; 67 | }; 68 | } 69 | 70 | // [message: string, isString: bool] 71 | function formatMessage(message) { 72 | var type = typeof message; 73 | if (type === 'string') { 74 | return [message, true]; 75 | } 76 | if (type !== 'object' || message === null) { 77 | throw new Error(INPUT_ERROR); 78 | } 79 | if (ARRAY_BUFFER && message.constructor === ArrayBuffer) { 80 | return [new Uint8Array(message), false]; 81 | } 82 | if (!isArray(message) && !isView(message)) { 83 | throw new Error(INPUT_ERROR); 84 | } 85 | return [message, false]; 86 | } 87 | 88 | function createOutputMethod(outputType, options) { 89 | return function (message) { 90 | return new Crc(options).update(message)[outputType](); 91 | }; 92 | } 93 | 94 | function createMethod(module) { 95 | var bitOffset = 8 - (module.width % 8 || 8); 96 | var firstBlockBytes = Math.ceil(module.width / 8) % 4 || 4; 97 | var firstBlockBits = firstBlockBytes << 3; 98 | var msb = (1 << (firstBlockBits - 1)) >>> 0; 99 | var msbOffset = firstBlockBits - 8; 100 | var maskBits = module.width % 32 || 32; 101 | var crc, poly, tableId; 102 | var multiWords = module.width > 32; 103 | if (multiWords) { 104 | crc = module.init.slice(); 105 | poly = module.poly.slice(); 106 | leftShift(crc, bitOffset); 107 | leftShift(poly, bitOffset); 108 | tableId = [poly.join('-'), msbOffset, msb].join('_'); 109 | } else { 110 | crc = module.init << bitOffset; 111 | poly = module.poly << bitOffset; 112 | tableId = [poly, msbOffset, msb].join('_'); 113 | } 114 | var options = { 115 | refin: module.refin, 116 | refout: module.refout, 117 | xorout: module.xorout, 118 | width: module.width, 119 | multiWords: multiWords, 120 | bitOffset: bitOffset, 121 | msb: msb, 122 | msbOffset: msbOffset, 123 | maskBits: maskBits, 124 | mask: 2**maskBits - 1, 125 | crc: crc, 126 | poly: poly, 127 | tableId: tableId 128 | }; 129 | var method = createOutputMethod('hex', options); 130 | method.create = function () { 131 | return new Crc(options); 132 | }; 133 | method.update = function (message) { 134 | return method.create().update(message); 135 | }; 136 | for (var i = 0; i < OUTPUT_TYPES.length; ++i) { 137 | var type = OUTPUT_TYPES[i]; 138 | method[type] = createOutputMethod(type, options); 139 | } 140 | return method; 141 | } 142 | 143 | function leftShift(words, bits) { 144 | if (!bits) { 145 | return; 146 | } 147 | var i = 0; 148 | for (; i < words.length - 1; ++i) { 149 | words[i] = (words[i] << bits) | (words[i + 1] >>> (32 - bits)); 150 | } 151 | words[i] = (words[i] << bits); 152 | } 153 | 154 | function rightShift(words, bits) { 155 | if (!bits) { 156 | return; 157 | } 158 | var i = words.length - 1; 159 | for (; i > 0; --i) { 160 | words[i] = (words[i - 1] << (32 - bits)) | (words[i] >>> bits); 161 | } 162 | words[i] = words[i] >>> bits; 163 | } 164 | 165 | function xor(a, b) { 166 | for (var i = 0; i < a.length; ++i) { 167 | a[i] ^= b[i]; 168 | } 169 | } 170 | 171 | var TABLES = {}; 172 | function getTable(tableId, poly, msbOffset, msb) { 173 | if (!TABLES[tableId]) { 174 | TABLES[tableId] = createTable(poly, msbOffset, msb); 175 | } 176 | return TABLES[tableId]; 177 | } 178 | 179 | function createTable(poly, msbOffset, msb) { 180 | var table = []; 181 | var multiWords = isArray(poly); 182 | if (!multiWords) { 183 | poly = [poly] 184 | } 185 | for (i = 0; i < 256; ++i) { 186 | var byte = [i << msbOffset]; 187 | for (var j = 1; j < poly.length; ++j) { 188 | byte[j] = 0; 189 | } 190 | for (var j = 0; j < 8; ++j) { 191 | if (byte[0] & msb) { 192 | leftShift(byte, 1); 193 | for (var k = 0; k < poly.length; ++k) { 194 | byte[k] = byte[k] ^ poly[k]; 195 | } 196 | } else { 197 | leftShift(byte, 1); 198 | } 199 | } 200 | table[i] = multiWords ? byte : byte[0]; 201 | } 202 | return table; 203 | } 204 | 205 | function reverse(val, width) { 206 | var result = 0; 207 | for (var i = 0; val; ++i) { 208 | if (val & 1) { 209 | result |= (1 << ((width - 1) - i)); 210 | } 211 | val = val >>> 1; 212 | } 213 | return result; 214 | } 215 | 216 | var REVERSE_BYTE = []; 217 | for (var i = 0; i < 256; ++i) { 218 | REVERSE_BYTE[i] = reverse(i, 8); 219 | } 220 | 221 | function Crc(options) { 222 | this.options = options; 223 | this.refin = options.refin; 224 | this.multiWords = options.multiWords; 225 | this.bitOffset = options.bitOffset; 226 | this.msbOffset = options.msbOffset; 227 | this.maskBits = options.maskBits; 228 | this.mask = options.mask; 229 | this.table = getTable(options.tableId, options.poly, this.msbOffset, options.msb); 230 | if (this.multiWords) { 231 | this.crc = options.crc.slice(); 232 | } else { 233 | this.crc = options.crc; 234 | } 235 | } 236 | 237 | Crc.prototype.update = function (message) { 238 | if (this.finalized) { 239 | throw new Error(FINALIZE_ERROR); 240 | } 241 | var result = formatMessage(message); 242 | message = result[0]; 243 | var isString = result[1]; 244 | var code, i, length = message.length; 245 | if (isString) { 246 | for (i = 0; i < length; ++i) { 247 | code = message.charCodeAt(i); 248 | if (code < 0x80) { 249 | this.updateByte(code); 250 | } else if (code < 0x800) { 251 | this.updateByte((0xc0 | (code >> 6))); 252 | this.updateByte((0x80 | (code & 0x3f))); 253 | } else if (code < 0xd800 || code >= 0xe000) { 254 | this.updateByte((0xe0 | (code >> 12))); 255 | this.updateByte((0x80 | ((code >> 6) & 0x3f))); 256 | this.updateByte((0x80 | (code & 0x3f))); 257 | } else { 258 | code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++i) & 0x3ff)); 259 | this.updateByte((0xf0 | (code >> 18))); 260 | this.updateByte((0x80 | ((code >> 12) & 0x3f))); 261 | this.updateByte((0x80 | ((code >> 6) & 0x3f))); 262 | this.updateByte((0x80 | (code & 0x3f))); 263 | } 264 | } 265 | } else { 266 | for (i = 0; i < length; ++i) { 267 | this.updateByte(message[i]); 268 | } 269 | } 270 | return this; 271 | }; 272 | 273 | Crc.prototype.updateByte = function (byte) { 274 | var crc = this.crc; 275 | if (this.refin) { 276 | byte = REVERSE_BYTE[byte]; 277 | } 278 | if (this.multiWords) { 279 | crc[0] = crc[0] ^ (byte << this.msbOffset); 280 | var cache = this.table[(crc[0] >> this.msbOffset) & 0xFF]; 281 | leftShift(crc, 8); 282 | xor(crc, cache); 283 | } else { 284 | crc = (crc ^ (byte << this.msbOffset)); 285 | crc = (crc << 8) ^ this.table[(crc >> this.msbOffset) & 0xFF]; 286 | } 287 | this.crc = crc; 288 | }; 289 | 290 | Crc.prototype.finalize = function () { 291 | if (this.finalized) { 292 | return; 293 | } 294 | this.finalized = true; 295 | if (this.multiWords) { 296 | rightShift(this.crc, this.bitOffset); 297 | this.crc[0] = this.crc[0] & this.mask; 298 | if (this.options.refout) { 299 | leftShift(this.crc, 32 - this.maskBits); 300 | var crc = []; 301 | for (var i = 0; i < this.crc.length; ++i) { 302 | crc[this.crc.length - i - 1] = reverse(this.crc[i], 32) 303 | } 304 | this.crc = crc; 305 | } 306 | xor(this.crc, this.options.xorout); 307 | } else { 308 | this.crc = (this.crc >>> this.bitOffset) & this.mask; 309 | if (this.options.refout) { 310 | this.crc = reverse(this.crc, this.options.width); 311 | } 312 | this.crc ^= this.options.xorout; 313 | } 314 | }; 315 | 316 | Crc.prototype.hex = function () { 317 | this.finalize(); 318 | var hex = ''; 319 | var crc = this.crc; 320 | var length = this.options.width; 321 | if (this.multiWords) { 322 | crc = crc[0]; 323 | length = length % 32 || 32; 324 | } 325 | for (var i = (Math.ceil(length / 4) << 2) - 4; i >= 0; i -= 4) { 326 | hex += HEX_CHARS[(crc >> i) & 0x0F]; 327 | } 328 | if (this.multiWords) { 329 | for (var j = 1; j < this.crc.length; ++j) { 330 | crc = this.crc[j]; 331 | for (i = 28; i >= 0; i -= 4) { 332 | hex += HEX_CHARS[(crc >> i) & 0x0F]; 333 | } 334 | } 335 | } 336 | return hex; 337 | }; 338 | Crc.prototype.toString = Crc.prototype.hex; 339 | 340 | Crc.prototype.array = function () { 341 | this.finalize(); 342 | var arr = new Array(Math.ceil(this.options.width / 8)); 343 | var crc = this.crc; 344 | var length = this.options.width; 345 | if (this.multiWords) { 346 | crc = crc[0]; 347 | length = length % 32 || 32; 348 | } 349 | var index = 0; 350 | for (var i = (Math.ceil(length / 8) << 3) - 8; i >= 0; i -= 8) { 351 | arr[index++] = (crc >> i) & 0xFF; 352 | } 353 | if (this.multiWords) { 354 | for (var j = 1; j < this.crc.length; ++j) { 355 | crc = this.crc[j]; 356 | for (i = 24; i >= 0; i -= 8) { 357 | arr[index++] = (crc >> i) & 0xFF; 358 | } 359 | } 360 | } 361 | return arr; 362 | }; 363 | 364 | var exports = {}; 365 | for (var i = 0;i < MODELS.length;++i) { 366 | var m = MODELS[i]; 367 | exports[m.name] = createMethod(m); 368 | } 369 | exports.createModel = createMethod; 370 | if (COMMON_JS) { 371 | module.exports = exports; 372 | } else { 373 | for (i = 0;i < MODELS.length;++i) { 374 | var m = MODELS[i]; 375 | root[m.name] = exports[m.name]; 376 | } 377 | root.createModel = createMethod; 378 | if (AMD) { 379 | define(function() { 380 | return exports; 381 | }); 382 | } 383 | } 384 | })(); 385 | -------------------------------------------------------------------------------- /src/models.js: -------------------------------------------------------------------------------- 1 | /** 2 | * [js-crc]{@link https://github.com/emn178/js-crc} 3 | * 4 | * @namespace crc 5 | * @version 0.3.1 6 | * @author Chen, Yi-Cyuan [emn178@gmail.com] 7 | * @copyright Chen, Yi-Cyuan 2015-2024 8 | * @license MIT 9 | */ 10 | // https://reveng.sourceforge.io/crc-catalogue/all.htm 11 | (function () { 12 | var MODELS = [ 13 | { 14 | width: 3, 15 | poly: 0x3, 16 | init: 0x0, 17 | refin: false, 18 | refout: false, 19 | xorout: 0x7, 20 | name: 'CRC-3/GSM' 21 | }, 22 | { 23 | width: 3, 24 | poly: 0x3, 25 | init: 0x7, 26 | refin: true, 27 | refout: true, 28 | xorout: 0x0, 29 | name: 'CRC-3/ROHC' 30 | }, 31 | { 32 | width: 4, 33 | poly: 0x3, 34 | init: 0x0, 35 | refin: true, 36 | refout: true, 37 | xorout: 0x0, 38 | name: 'CRC-4/G-704', 39 | alias: ['CRC-4/ITU'] 40 | }, 41 | { 42 | width: 4, 43 | poly: 0x3, 44 | init: 0xf, 45 | refin: false, 46 | refout: false, 47 | xorout: 0xf, 48 | name: 'CRC-4/INTERLAKEN' 49 | }, 50 | { 51 | width: 5, 52 | poly: 0x09, 53 | init: 0x09, 54 | refin: false, 55 | refout: false, 56 | xorout: 0x00, 57 | name: 'CRC-5/EPC-C1G2', 58 | alias: ['CRC-5/EPC'] 59 | }, 60 | { 61 | width: 5, 62 | poly: 0x15, 63 | init: 0x00, 64 | refin: true, 65 | refout: true, 66 | xorout: 0x00, 67 | name: 'CRC-5/G-704', 68 | alias: ['CRC-5/ITU'] 69 | }, 70 | { 71 | width: 5, 72 | poly: 0x05, 73 | init: 0x1f, 74 | refin: true, 75 | refout: true, 76 | xorout: 0x1f, 77 | name: 'CRC-5/USB' 78 | }, 79 | { 80 | width: 6, 81 | poly: 0x27, 82 | init: 0x3f, 83 | refin: false, 84 | refout: false, 85 | xorout: 0x00, 86 | name: 'CRC-6/CDMA2000-A' 87 | }, 88 | { 89 | width: 6, 90 | poly: 0x07, 91 | init: 0x3f, 92 | refin: false, 93 | refout: false, 94 | xorout: 0x00, 95 | name: 'CRC-6/CDMA2000-B' 96 | }, 97 | { 98 | width: 6, 99 | poly: 0x19, 100 | init: 0x00, 101 | refin: true, 102 | refout: true, 103 | xorout: 0x00, 104 | name: 'CRC-6/DARC' 105 | }, 106 | { 107 | width: 6, 108 | poly: 0x03, 109 | init: 0x00, 110 | refin: true, 111 | refout: true, 112 | xorout: 0x00, 113 | name: 'CRC-6/G-704', 114 | alias: ['CRC-6/ITU'] 115 | }, 116 | { 117 | width: 6, 118 | poly: 0x2f, 119 | init: 0x00, 120 | refin: false, 121 | refout: false, 122 | xorout: 0x3f, 123 | name: 'CRC-6/GSM' 124 | }, 125 | { 126 | width: 7, 127 | poly: 0x09, 128 | init: 0x00, 129 | refin: false, 130 | refout: false, 131 | xorout: 0x00, 132 | name: 'CRC-7/MMC', 133 | alias: ['CRC-7'] 134 | }, 135 | { 136 | width: 7, 137 | poly: 0x4f, 138 | init: 0x7f, 139 | refin: true, 140 | refout: true, 141 | xorout: 0x00, 142 | name: 'CRC-7/ROHC' 143 | }, 144 | { 145 | width: 7, 146 | poly: 0x45, 147 | init: 0x00, 148 | refin: false, 149 | refout: false, 150 | xorout: 0x00, 151 | name: 'CRC-7/UMTS' 152 | }, 153 | { 154 | width: 8, 155 | poly: 0x2f, 156 | init: 0xff, 157 | refin: false, 158 | refout: false, 159 | xorout: 0xff, 160 | name: 'CRC-8/AUTOSAR' 161 | }, 162 | { 163 | width: 8, 164 | poly: 0xa7, 165 | init: 0x00, 166 | refin: true, 167 | refout: true, 168 | xorout: 0x00, 169 | name: 'CRC-8/BLUETOOTH' 170 | }, 171 | { 172 | width: 8, 173 | poly: 0x9b, 174 | init: 0xff, 175 | refin: false, 176 | refout: false, 177 | xorout: 0x00, 178 | name: 'CRC-8/CDMA2000' 179 | }, 180 | { 181 | width: 8, 182 | poly: 0x39, 183 | init: 0x00, 184 | refin: true, 185 | refout: true, 186 | xorout: 0x00, 187 | name: 'CRC-8/DARC' 188 | }, 189 | { 190 | width: 8, 191 | poly: 0xd5, 192 | init: 0x00, 193 | refin: false, 194 | refout: false, 195 | xorout: 0x00, 196 | name: 'CRC-8/DVB-S2' 197 | }, 198 | { 199 | width: 8, 200 | poly: 0x1d, 201 | init: 0x00, 202 | refin: false, 203 | refout: false, 204 | xorout: 0x00, 205 | name: 'CRC-8/GSM-A' 206 | }, 207 | { 208 | width: 8, 209 | poly: 0x49, 210 | init: 0x00, 211 | refin: false, 212 | refout: false, 213 | xorout: 0xff, 214 | name: 'CRC-8/GSM-B' 215 | }, 216 | { 217 | width: 8, 218 | poly: 0x1d, 219 | init: 0xff, 220 | refin: false, 221 | refout: false, 222 | xorout: 0x00, 223 | name: 'CRC-8/HITAG' 224 | }, 225 | { 226 | width: 8, 227 | poly: 0x07, 228 | init: 0x00, 229 | refin: false, 230 | refout: false, 231 | xorout: 0x55, 232 | name: 'CRC-8/I-432-1', 233 | alias: ['CRC-8/ITU'] 234 | }, 235 | { 236 | width: 8, 237 | poly: 0x1d, 238 | init: 0xfd, 239 | refin: false, 240 | refout: false, 241 | xorout: 0x00, 242 | name: 'CRC-8/I-CODE' 243 | }, 244 | { 245 | width: 8, 246 | poly: 0x9b, 247 | init: 0x00, 248 | refin: false, 249 | refout: false, 250 | xorout: 0x00, 251 | name: 'CRC-8/LTE' 252 | }, 253 | { 254 | width: 8, 255 | poly: 0x31, 256 | init: 0x00, 257 | refin: true, 258 | refout: true, 259 | xorout: 0x00, 260 | name: 'CRC-8/MAXIM-DOW', 261 | alias: ['CRC-8/MAXIM', 'DOW-CRC'] 262 | }, 263 | { 264 | width: 8, 265 | poly: 0x1d, 266 | init: 0xc7, 267 | refin: false, 268 | refout: false, 269 | xorout: 0x00, 270 | name: 'CRC-8/MIFARE-MAD' 271 | }, 272 | { 273 | width: 8, 274 | poly: 0x31, 275 | init: 0xff, 276 | refin: false, 277 | refout: false, 278 | xorout: 0x00, 279 | name: 'CRC-8/NRSC-5' 280 | }, 281 | { 282 | width: 8, 283 | poly: 0x2f, 284 | init: 0x00, 285 | refin: false, 286 | refout: false, 287 | xorout: 0x00, 288 | name: 'CRC-8/OPENSAFETY' 289 | }, 290 | { 291 | width: 8, 292 | poly: 0x07, 293 | init: 0xff, 294 | refin: true, 295 | refout: true, 296 | xorout: 0x00, 297 | name: 'CRC-8/ROHC' 298 | }, 299 | { 300 | width: 8, 301 | poly: 0x1d, 302 | init: 0xff, 303 | refin: false, 304 | refout: false, 305 | xorout: 0xff, 306 | name: 'CRC-8/SAE-J1850' 307 | }, 308 | { 309 | width: 8, 310 | poly: 0x07, 311 | init: 0x00, 312 | refin: false, 313 | refout: false, 314 | xorout: 0x00, 315 | name: 'CRC-8/SMBUS', 316 | alias: ['CRC-8'] 317 | }, 318 | { 319 | width: 8, 320 | poly: 0x1d, 321 | init: 0xff, 322 | refin: true, 323 | refout: true, 324 | xorout: 0x00, 325 | name: 'CRC-8/TECH-3250', 326 | alias: ['CRC-8/AES', 'CRC-8/EBU'] 327 | }, 328 | { 329 | width: 8, 330 | poly: 0x9b, 331 | init: 0x00, 332 | refin: true, 333 | refout: true, 334 | xorout: 0x00, 335 | name: 'CRC-8/WCDMA' 336 | }, 337 | { 338 | width: 10, 339 | poly: 0x233, 340 | init: 0x000, 341 | refin: false, 342 | refout: false, 343 | xorout: 0x000, 344 | name: 'CRC-10/ATM', 345 | alias: ['CRC-10', 'CRC-10/I-610'] 346 | }, 347 | { 348 | width: 10, 349 | poly: 0x3d9, 350 | init: 0x3ff, 351 | refin: false, 352 | refout: false, 353 | xorout: 0x000, 354 | name: 'CRC-10/CDMA2000' 355 | }, 356 | { 357 | width: 10, 358 | poly: 0x175, 359 | init: 0x000, 360 | refin: false, 361 | refout: false, 362 | xorout: 0x3ff, 363 | name: 'CRC-10/GSM' 364 | }, 365 | { 366 | width: 11, 367 | poly: 0x385, 368 | init: 0x01a, 369 | refin: false, 370 | refout: false, 371 | xorout: 0x000, 372 | name: 'CRC-11/FLEXRAY', 373 | alias: ['CRC-11'] 374 | }, 375 | { 376 | width: 11, 377 | poly: 0x307, 378 | init: 0x000, 379 | refin: false, 380 | refout: false, 381 | xorout: 0x000, 382 | name: 'CRC-11/UMTS' 383 | }, 384 | { 385 | width: 12, 386 | poly: 0xf13, 387 | init: 0xfff, 388 | refin: false, 389 | refout: false, 390 | xorout: 0x000, 391 | name: 'CRC-12/CDMA2000' 392 | }, 393 | { 394 | width: 12, 395 | poly: 0x80f, 396 | init: 0x000, 397 | refin: false, 398 | refout: false, 399 | xorout: 0x000, 400 | name: 'CRC-12/DECT', 401 | alias: ['X-CRC-12'] 402 | }, 403 | { 404 | width: 12, 405 | poly: 0xd31, 406 | init: 0x000, 407 | refin: false, 408 | refout: false, 409 | xorout: 0xfff, 410 | name: 'CRC-12/GSM' 411 | }, 412 | { 413 | width: 12, 414 | poly: 0x80f, 415 | init: 0x000, 416 | refin: false, 417 | refout: true, 418 | xorout: 0x000, 419 | name: 'CRC-12/UMTS', 420 | alias: ['CRC-12/3GPP'] 421 | }, 422 | { 423 | width: 13, 424 | poly: 0x1cf5, 425 | init: 0x0000, 426 | refin: false, 427 | refout: false, 428 | xorout: 0x0000, 429 | name: 'CRC-13/BBC' 430 | }, 431 | { 432 | width: 14, 433 | poly: 0x0805, 434 | init: 0x0000, 435 | refin: true, 436 | refout: true, 437 | xorout: 0x0000, 438 | name: 'CRC-14/DARC' 439 | }, 440 | { 441 | width: 14, 442 | poly: 0x202d, 443 | init: 0x0000, 444 | refin: false, 445 | refout: false, 446 | xorout: 0x3fff, 447 | name: 'CRC-14/GSM' 448 | }, 449 | { 450 | width: 15, 451 | poly: 0x4599, 452 | init: 0x0000, 453 | refin: false, 454 | refout: false, 455 | xorout: 0x0000, 456 | name: 'CRC-15/CAN', 457 | alias: ['CRC-15'] 458 | }, 459 | { 460 | width: 15, 461 | poly: 0x6815, 462 | init: 0x0000, 463 | refin: false, 464 | refout: false, 465 | xorout: 0x0001, 466 | name: 'CRC-15/MPT1327' 467 | }, 468 | { 469 | width: 16, 470 | poly: 0x8005, 471 | init: 0x0000, 472 | refin: true, 473 | refout: true, 474 | xorout: 0x0000, 475 | name: 'CRC-16/ARC', 476 | alias: ['ARC', 'CRC-16', 'CRC-16/LHA', 'CRC-IBM'] 477 | }, 478 | { 479 | width: 16, 480 | poly: 0xc867, 481 | init: 0xffff, 482 | refin: false, 483 | refout: false, 484 | xorout: 0x0000, 485 | name: 'CRC-16/CDMA2000' 486 | }, 487 | { 488 | width: 16, 489 | poly: 0x8005, 490 | init: 0xffff, 491 | refin: false, 492 | refout: false, 493 | xorout: 0x0000, 494 | name: 'CRC-16/CMS' 495 | }, 496 | { 497 | width: 16, 498 | poly: 0x8005, 499 | init: 0x800d, 500 | refin: false, 501 | refout: false, 502 | xorout: 0x0000, 503 | name: 'CRC-16/DDS-110' 504 | }, 505 | { 506 | width: 16, 507 | poly: 0x0589, 508 | init: 0x0000, 509 | refin: false, 510 | refout: false, 511 | xorout: 0x0001, 512 | name: 'CRC-16/DECT-R', 513 | alias: ['R-CRC-16'] 514 | }, 515 | { 516 | width: 16, 517 | poly: 0x0589, 518 | init: 0x0000, 519 | refin: false, 520 | refout: false, 521 | xorout: 0x0000, 522 | name: 'CRC-16/DECT-X', 523 | alias: ['X-CRC-16'] 524 | }, 525 | { 526 | width: 16, 527 | poly: 0x3d65, 528 | init: 0x0000, 529 | refin: true, 530 | refout: true, 531 | xorout: 0xffff, 532 | name: 'CRC-16/DNP' 533 | }, 534 | { 535 | width: 16, 536 | poly: 0x3d65, 537 | init: 0x0000, 538 | refin: false, 539 | refout: false, 540 | xorout: 0xffff, 541 | name: 'CRC-16/EN-13757' 542 | }, 543 | { 544 | width: 16, 545 | poly: 0x1021, 546 | init: 0xffff, 547 | refin: false, 548 | refout: false, 549 | xorout: 0xffff, 550 | name: 'CRC-16/GENIBUS', 551 | alias: ['CRC-16/DARC', 'CRC-16/EPC', 'CRC-16/EPC-C1G2', 'CRC-16/I-CODE'] 552 | }, 553 | { 554 | width: 16, 555 | poly: 0x1021, 556 | init: 0x0000, 557 | refin: false, 558 | refout: false, 559 | xorout: 0xffff, 560 | name: 'CRC-16/GSM' 561 | }, 562 | { 563 | width: 16, 564 | poly: 0x1021, 565 | init: 0xffff, 566 | refin: false, 567 | refout: false, 568 | xorout: 0x0000, 569 | name: 'CRC-16/IBM-3740', 570 | alias: ['CRC-16/AUTOSAR', 'CRC-16/CCITT-FALSE'] 571 | }, 572 | { 573 | width: 16, 574 | poly: 0x1021, 575 | init: 0xffff, 576 | refin: true, 577 | refout: true, 578 | xorout: 0xffff, 579 | name: 'CRC-16/IBM-SDLC', 580 | alias: ['CRC-16/ISO-HDLC', 'CRC-16/ISO-IEC-14443-3-B', 'CRC-16/X-25', 'CRC-B', 'X-25'] 581 | }, 582 | { 583 | width: 16, 584 | poly: 0x1021, 585 | init: 0xc6c6, 586 | refin: true, 587 | refout: true, 588 | xorout: 0x0000, 589 | name: 'CRC-16/ISO-IEC-14443-3-A', 590 | alias: ['CRC-A'] 591 | }, 592 | { 593 | width: 16, 594 | poly: 0x1021, 595 | init: 0x0000, 596 | refin: true, 597 | refout: true, 598 | xorout: 0x0000, 599 | name: 'CRC-16/KERMIT', 600 | alias: ['CRC-16/BLUETOOTH', 'CRC-16/CCITT', 'CRC-16/CCITT-TRUE', 'CRC-16/V-41-LSB', 'CRC-CCITT', 'KERMIT'] 601 | }, 602 | { 603 | width: 16, 604 | poly: 0x6f63, 605 | init: 0x0000, 606 | refin: false, 607 | refout: false, 608 | xorout: 0x0000, 609 | name: 'CRC-16/LJ1200' 610 | }, 611 | { 612 | width: 16, 613 | poly: 0x5935, 614 | init: 0xffff, 615 | refin: false, 616 | refout: false, 617 | xorout: 0x0000, 618 | name: 'CRC-16/M17' 619 | }, 620 | { 621 | width: 16, 622 | poly: 0x8005, 623 | init: 0x0000, 624 | refin: true, 625 | refout: true, 626 | xorout: 0xffff, 627 | name: 'CRC-16/MAXIM-DOW', 628 | alias: ['CRC-16/MAXIM'] 629 | }, 630 | { 631 | width: 16, 632 | poly: 0x1021, 633 | init: 0xffff, 634 | refin: true, 635 | refout: true, 636 | xorout: 0x0000, 637 | name: 'CRC-16/MCRF4XX' 638 | }, 639 | { 640 | width: 16, 641 | poly: 0x8005, 642 | init: 0xffff, 643 | refin: true, 644 | refout: true, 645 | xorout: 0x0000, 646 | name: 'CRC-16/MODBUS', 647 | alias: ['MODBUS'] 648 | }, 649 | { 650 | width: 16, 651 | poly: 0x080b, 652 | init: 0xffff, 653 | refin: true, 654 | refout: true, 655 | xorout: 0x0000, 656 | name: 'CRC-16/NRSC-5' 657 | }, 658 | { 659 | width: 16, 660 | poly: 0x5935, 661 | init: 0x0000, 662 | refin: false, 663 | refout: false, 664 | xorout: 0x0000, 665 | name: 'CRC-16/OPENSAFETY-A' 666 | }, 667 | { 668 | width: 16, 669 | poly: 0x755b, 670 | init: 0x0000, 671 | refin: false, 672 | refout: false, 673 | xorout: 0x0000, 674 | name: 'CRC-16/OPENSAFETY-B' 675 | }, 676 | { 677 | width: 16, 678 | poly: 0x1dcf, 679 | init: 0xffff, 680 | refin: false, 681 | refout: false, 682 | xorout: 0xffff, 683 | name: 'CRC-16/PROFIBUS', 684 | alias: ['CRC-16/IEC-61158-2'] 685 | }, 686 | { 687 | width: 16, 688 | poly: 0x1021, 689 | init: 0xb2aa, 690 | refin: true, 691 | refout: true, 692 | xorout: 0x0000, 693 | name: 'CRC-16/RIELLO' 694 | }, 695 | { 696 | width: 16, 697 | poly: 0x1021, 698 | init: 0x1d0f, 699 | refin: false, 700 | refout: false, 701 | xorout: 0x0000, 702 | name: 'CRC-16/SPI-FUJITSU', 703 | alias: ['CRC-16/AUG-CCITT'] 704 | }, 705 | { 706 | width: 16, 707 | poly: 0x8bb7, 708 | init: 0x0000, 709 | refin: false, 710 | refout: false, 711 | xorout: 0x0000, 712 | name: 'CRC-16/T10-DIF' 713 | }, 714 | { 715 | width: 16, 716 | poly: 0xa097, 717 | init: 0x0000, 718 | refin: false, 719 | refout: false, 720 | xorout: 0x0000, 721 | name: 'CRC-16/TELEDISK' 722 | }, 723 | { 724 | width: 16, 725 | poly: 0x1021, 726 | init: 0x89ec, 727 | refin: true, 728 | refout: true, 729 | xorout: 0x0000, 730 | name: 'CRC-16/TMS37157' 731 | }, 732 | { 733 | width: 16, 734 | poly: 0x8005, 735 | init: 0x0000, 736 | refin: false, 737 | refout: false, 738 | xorout: 0x0000, 739 | name: 'CRC-16/UMTS', 740 | alias: ['CRC-16/BUYPASS', 'CRC-16/VERIFONE'] 741 | }, 742 | { 743 | width: 16, 744 | poly: 0x8005, 745 | init: 0xffff, 746 | refin: true, 747 | refout: true, 748 | xorout: 0xffff, 749 | name: 'CRC-16/USB' 750 | }, 751 | { 752 | width: 16, 753 | poly: 0x1021, 754 | init: 0x0000, 755 | refin: false, 756 | refout: false, 757 | xorout: 0x0000, 758 | name: 'CRC-16/XMODEM', 759 | alias: ['CRC-16/ACORN', 'CRC-16/LTE', 'CRC-16/V-41-MSB', 'XMODEM', 'ZMODEM'] 760 | }, 761 | { 762 | width: 17, 763 | poly: 0x1685b, 764 | init: 0x00000, 765 | refin: false, 766 | refout: false, 767 | xorout: 0x00000, 768 | name: 'CRC-17/CAN-FD' 769 | }, 770 | { 771 | width: 21, 772 | poly: 0x102899, 773 | init: 0x000000, 774 | refin: false, 775 | refout: false, 776 | xorout: 0x000000, 777 | name: 'CRC-21/CAN-FD' 778 | }, 779 | { 780 | width: 24, 781 | poly: 0x00065b, 782 | init: 0x555555, 783 | refin: true, 784 | refout: true, 785 | xorout: 0x000000, 786 | name: 'CRC-24/BLE' 787 | }, 788 | { 789 | width: 24, 790 | poly: 0x5d6dcb, 791 | init: 0xfedcba, 792 | refin: false, 793 | refout: false, 794 | xorout: 0x000000, 795 | name: 'CRC-24/FLEXRAY-A' 796 | }, 797 | { 798 | width: 24, 799 | poly: 0x5d6dcb, 800 | init: 0xabcdef, 801 | refin: false, 802 | refout: false, 803 | xorout: 0x000000, 804 | name: 'CRC-24/FLEXRAY-B' 805 | }, 806 | { 807 | width: 24, 808 | poly: 0x328b63, 809 | init: 0xffffff, 810 | refin: false, 811 | refout: false, 812 | xorout: 0xffffff, 813 | name: 'CRC-24/INTERLAKEN' 814 | }, 815 | { 816 | width: 24, 817 | poly: 0x864cfb, 818 | init: 0x000000, 819 | refin: false, 820 | refout: false, 821 | xorout: 0x000000, 822 | name: 'CRC-24/LTE-A' 823 | }, 824 | { 825 | width: 24, 826 | poly: 0x800063, 827 | init: 0x000000, 828 | refin: false, 829 | refout: false, 830 | xorout: 0x000000, 831 | name: 'CRC-24/LTE-B' 832 | }, 833 | { 834 | width: 24, 835 | poly: 0x864cfb, 836 | init: 0xb704ce, 837 | refin: false, 838 | refout: false, 839 | xorout: 0x000000, 840 | name: 'CRC-24/OPENPGP', 841 | alias: ['CRC-24'] 842 | }, 843 | { 844 | width: 24, 845 | poly: 0x800063, 846 | init: 0xffffff, 847 | refin: false, 848 | refout: false, 849 | xorout: 0xffffff, 850 | name: 'CRC-24/OS-9' 851 | }, 852 | { 853 | width: 30, 854 | poly: 0x2030b9c7, 855 | init: 0x3fffffff, 856 | refin: false, 857 | refout: false, 858 | xorout: 0x3fffffff, 859 | name: 'CRC-30/CDMA' 860 | }, 861 | { 862 | width: 31, 863 | poly: 0x04c11db7, 864 | init: 0x7fffffff, 865 | refin: false, 866 | refout: false, 867 | xorout: 0x7fffffff, 868 | name: 'CRC-31/PHILIPS' 869 | }, 870 | { 871 | width: 32, 872 | poly: 0x814141ab, 873 | init: 0x00000000, 874 | refin: false, 875 | refout: false, 876 | xorout: 0x00000000, 877 | name: 'CRC-32/AIXM', 878 | alias: ['CRC-32Q'] 879 | }, 880 | { 881 | width: 32, 882 | poly: 0xf4acfb13, 883 | init: 0xffffffff, 884 | refin: true, 885 | refout: true, 886 | xorout: 0xffffffff, 887 | name: 'CRC-32/AUTOSAR' 888 | }, 889 | { 890 | width: 32, 891 | poly: 0xa833982b, 892 | init: 0xffffffff, 893 | refin: true, 894 | refout: true, 895 | xorout: 0xffffffff, 896 | name: 'CRC-32/BASE91-D', 897 | alias: ['CRC-32D'] 898 | }, 899 | { 900 | width: 32, 901 | poly: 0x04c11db7, 902 | init: 0xffffffff, 903 | refin: false, 904 | refout: false, 905 | xorout: 0xffffffff, 906 | name: 'CRC-32/BZIP2', 907 | alias: ['CRC-32/AAL5', 'CRC-32/DECT-B', 'B-CRC-32'] 908 | }, 909 | { 910 | width: 32, 911 | poly: 0x8001801b, 912 | init: 0x00000000, 913 | refin: true, 914 | refout: true, 915 | xorout: 0x00000000, 916 | name: 'CRC-32/CD-ROM-EDC' 917 | }, 918 | { 919 | width: 32, 920 | poly: 0x04c11db7, 921 | init: 0x00000000, 922 | refin: false, 923 | refout: false, 924 | xorout: 0xffffffff, 925 | name: 'CRC-32/CKSUM', 926 | alias: ['CKSUM', 'CRC-32/POSIX'] 927 | }, 928 | { 929 | width: 32, 930 | poly: 0x1edc6f41, 931 | init: 0xffffffff, 932 | refin: true, 933 | refout: true, 934 | xorout: 0xffffffff, 935 | name: 'CRC-32/ISCSI', 936 | alias: ['CRC-32/BASE91-C', 'CRC-32/CASTAGNOLI', 'CRC-32/INTERLAKEN', 'CRC-32C', 'CRC-32/NVME'] 937 | }, 938 | { 939 | width: 32, 940 | poly: 0x04c11db7, 941 | init: 0xffffffff, 942 | refin: true, 943 | refout: true, 944 | xorout: 0xffffffff, 945 | name: 'CRC-32/ISO-HDLC', 946 | alias: ['CRC-32', 'CRC-32/ADCCP', 'CRC-32/V-42', 'CRC-32/XZ', 'PKZIP'] 947 | }, 948 | { 949 | width: 32, 950 | poly: 0x04c11db7, 951 | init: 0xffffffff, 952 | refin: true, 953 | refout: true, 954 | xorout: 0x00000000, 955 | name: 'CRC-32/JAMCRC', 956 | alias: ['JAMCRC'] 957 | }, 958 | { 959 | width: 32, 960 | poly: 0x741b8cd7, 961 | init: 0xffffffff, 962 | refin: true, 963 | refout: true, 964 | xorout: 0x00000000, 965 | name: 'CRC-32/MEF' 966 | }, 967 | { 968 | width: 32, 969 | poly: 0x04c11db7, 970 | init: 0xffffffff, 971 | refin: false, 972 | refout: false, 973 | xorout: 0x00000000, 974 | name: 'CRC-32/MPEG-2' 975 | }, 976 | { 977 | width: 32, 978 | poly: 0x000000af, 979 | init: 0x00000000, 980 | refin: false, 981 | refout: false, 982 | xorout: 0x00000000, 983 | name: 'CRC-32/XFER', 984 | alias: ['XFER'] 985 | }, 986 | { 987 | width: 40, 988 | poly: [0x00, 0x04820009], 989 | init: [0x00, 0x00000000], 990 | refin: false, 991 | refout: false, 992 | xorout: [0xff, 0xffffffff], 993 | name: 'CRC-40/GSM' 994 | }, 995 | { 996 | width: 64, 997 | poly: [0x42f0e1eb, 0xa9ea3693], 998 | init: [0, 0], 999 | refin: false, 1000 | refout: false, 1001 | xorout: [0, 0], 1002 | name: 'CRC-64/ECMA-182', 1003 | alias: ['CRC-64'] 1004 | }, 1005 | { 1006 | width: 64, 1007 | poly: [0x00000000, 0x0000001b], 1008 | init: [0xffffffff, 0xffffffff], 1009 | refin: true, 1010 | refout: true, 1011 | xorout: [0xffffffff, 0xffffffff], 1012 | name: 'CRC-64/GO-ISO' 1013 | }, 1014 | { 1015 | width: 64, 1016 | poly: [0x259c84cb, 0xa6426349], 1017 | init: [0xffffffff, 0xffffffff], 1018 | refin: true, 1019 | refout: true, 1020 | xorout: [0, 0], 1021 | name: 'CRC-64/MS' 1022 | }, 1023 | { 1024 | width: 64, 1025 | poly: [0xad93d235, 0x94c93659], 1026 | init: [0xffffffff, 0xffffffff], 1027 | refin: true, 1028 | refout: true, 1029 | xorout: [0xffffffff, 0xffffffff], 1030 | name: 'CRC-64/NVME' 1031 | }, 1032 | { 1033 | width: 64, 1034 | poly: [0xad93d235, 0x94c935a9], 1035 | init: [0, 0], 1036 | refin: true, 1037 | refout: true, 1038 | xorout: [0, 0], 1039 | name: 'CRC-64/REDIS' 1040 | }, 1041 | { 1042 | width: 64, 1043 | poly: [0x42f0e1eb, 0xa9ea3693], 1044 | init: [0xffffffff, 0xffffffff], 1045 | refin: false, 1046 | refout: false, 1047 | xorout: [0xffffffff, 0xffffffff], 1048 | name: 'CRC-64/WE' 1049 | }, 1050 | { 1051 | width: 64, 1052 | poly: [0x42f0e1eb, 0xa9ea3693], 1053 | init: [0xffffffff, 0xffffffff], 1054 | refin: true, 1055 | refout: true, 1056 | xorout: [0xffffffff, 0xffffffff], 1057 | name: 'CRC-64/XZ', 1058 | alias: ['CRC-64/GO-ECMA'] 1059 | }, 1060 | { 1061 | width: 82, 1062 | poly: [0x0308c, 0x01110114, 0x01440411], 1063 | init: [0, 0, 0], 1064 | refin: true, 1065 | refout: true, 1066 | xorout: [0, 0, 0], 1067 | name: 'CRC-82/DARC' 1068 | } 1069 | ]; 1070 | 1071 | var WINDOW = typeof window === 'object'; 1072 | var root = WINDOW ? window : {}; 1073 | if (root.JS_CRC_NO_WINDOW) { 1074 | WINDOW = false; 1075 | } 1076 | var WEB_WORKER = !WINDOW && typeof self === 'object'; 1077 | var NODE_JS = 1078 | !root.JS_CRC_NO_NODE_JS && 1079 | typeof process === 'object' && 1080 | process.versions && 1081 | process.versions.node; 1082 | if (NODE_JS) { 1083 | root = global; 1084 | } else if (WEB_WORKER) { 1085 | root = self; 1086 | } 1087 | var COMMON_JS = 1088 | !root.JS_CRC_NO_COMMON_JS && typeof module === 'object' && module.exports; 1089 | var AMD = typeof define === 'function' && define.amd; 1090 | 1091 | function createMethods(exports) { 1092 | for (var i = 0; i < MODELS.length; ++i) { 1093 | var model = MODELS[i]; 1094 | var methodName = model.name.replace(/[-/]/g, '_').toLocaleLowerCase(); 1095 | exports[methodName] = createModel(MODELS[i]); 1096 | if (model.alias) { 1097 | for (var j = 0; j < model.alias.length; ++j) { 1098 | var aliasMethodName = model.alias[j].replace(/[-/]/g, '_').toLocaleLowerCase(); 1099 | exports[aliasMethodName] = exports[methodName]; 1100 | } 1101 | } 1102 | } 1103 | } 1104 | 1105 | var createModel; 1106 | var exports = {}; 1107 | if (COMMON_JS) { 1108 | createModel = require('./crc.js').createModel; 1109 | createMethods(exports); 1110 | module.exports = exports; 1111 | } else { 1112 | createModel = root.createModel; 1113 | createMethods(exports); 1114 | for (var key in exports) { 1115 | root[key] = exports[key]; 1116 | } 1117 | if (AMD) { 1118 | define(function () { 1119 | return exports; 1120 | }); 1121 | } 1122 | } 1123 | })(); 1124 | -------------------------------------------------------------------------------- /tests/index.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CRC 6 | 7 | 8 | 9 | 10 | 11 | 12 | 13 |
14 | 17 | 18 | 19 | 23 | 24 | 25 | -------------------------------------------------------------------------------- /tests/models-test.js: -------------------------------------------------------------------------------- 1 | (function () { 2 | var MODELS = [ 3 | { 4 | check: '4', 5 | name: "CRC-3/GSM", 6 | }, 7 | { 8 | check: '6', 9 | name: "CRC-3/ROHC", 10 | }, 11 | { 12 | check: '7', 13 | name: "CRC-4/G-704", 14 | }, 15 | { 16 | check: 'b', 17 | name: "CRC-4/INTERLAKEN", 18 | }, 19 | { 20 | check: '00', 21 | name: "CRC-5/EPC-C1G2", 22 | }, 23 | { 24 | check: '07', 25 | name: "CRC-5/G-704", 26 | }, 27 | { 28 | check: '19', 29 | name: "CRC-5/USB", 30 | }, 31 | { 32 | check: '0d', 33 | name: "CRC-6/CDMA2000-A", 34 | }, 35 | { 36 | check: '3b', 37 | name: "CRC-6/CDMA2000-B", 38 | }, 39 | { 40 | check: '26', 41 | name: "CRC-6/DARC", 42 | }, 43 | { 44 | check: '06', 45 | name: "CRC-6/G-704", 46 | }, 47 | { 48 | check: '13', 49 | name: "CRC-6/GSM", 50 | }, 51 | { 52 | check: '75', 53 | name: "CRC-7/MMC", 54 | }, 55 | { 56 | check: '53', 57 | name: "CRC-7/ROHC", 58 | }, 59 | { 60 | check: '61', 61 | name: "CRC-7/UMTS", 62 | }, 63 | { 64 | check: 'df', 65 | name: "CRC-8/AUTOSAR", 66 | }, 67 | { 68 | check: '26', 69 | name: "CRC-8/BLUETOOTH", 70 | }, 71 | { 72 | check: 'da', 73 | name: "CRC-8/CDMA2000", 74 | }, 75 | { 76 | check: '15', 77 | name: "CRC-8/DARC", 78 | }, 79 | { 80 | check: 'bc', 81 | name: "CRC-8/DVB-S2", 82 | }, 83 | { 84 | check: '37', 85 | name: "CRC-8/GSM-A", 86 | }, 87 | { 88 | check: '94', 89 | name: "CRC-8/GSM-B", 90 | }, 91 | { 92 | check: 'b4', 93 | name: "CRC-8/HITAG", 94 | }, 95 | { 96 | check: 'a1', 97 | name: "CRC-8/I-432-1", 98 | }, 99 | { 100 | check: '7e', 101 | name: "CRC-8/I-CODE", 102 | }, 103 | { 104 | check: 'ea', 105 | name: "CRC-8/LTE", 106 | }, 107 | { 108 | check: 'a1', 109 | name: "CRC-8/MAXIM-DOW", 110 | }, 111 | { 112 | check: '99', 113 | name: "CRC-8/MIFARE-MAD", 114 | }, 115 | { 116 | check: 'f7', 117 | name: "CRC-8/NRSC-5", 118 | }, 119 | { 120 | check: '3e', 121 | name: "CRC-8/OPENSAFETY", 122 | }, 123 | { 124 | check: 'd0', 125 | name: "CRC-8/ROHC", 126 | }, 127 | { 128 | check: '4b', 129 | name: "CRC-8/SAE-J1850", 130 | }, 131 | { 132 | check: 'f4', 133 | name: "CRC-8/SMBUS", 134 | }, 135 | { 136 | check: '97', 137 | name: "CRC-8/TECH-3250", 138 | }, 139 | { 140 | check: '25', 141 | name: "CRC-8/WCDMA", 142 | }, 143 | { 144 | check: '199', 145 | name: "CRC-10/ATM", 146 | }, 147 | { 148 | check: '233', 149 | name: "CRC-10/CDMA2000", 150 | }, 151 | { 152 | check: '12a', 153 | name: "CRC-10/GSM", 154 | }, 155 | { 156 | check: '5a3', 157 | name: "CRC-11/FLEXRAY", 158 | }, 159 | { 160 | check: '061', 161 | name: "CRC-11/UMTS", 162 | }, 163 | { 164 | check: 'd4d', 165 | name: "CRC-12/CDMA2000", 166 | }, 167 | { 168 | check: 'f5b', 169 | name: "CRC-12/DECT", 170 | }, 171 | { 172 | check: 'b34', 173 | name: "CRC-12/GSM", 174 | }, 175 | { 176 | check: 'daf', 177 | name: "CRC-12/UMTS", 178 | }, 179 | { 180 | check: '04fa', 181 | name: "CRC-13/BBC", 182 | }, 183 | { 184 | check: '082d', 185 | name: "CRC-14/DARC", 186 | }, 187 | { 188 | check: '30ae', 189 | name: "CRC-14/GSM", 190 | }, 191 | { 192 | check: '059e', 193 | name: "CRC-15/CAN", 194 | }, 195 | { 196 | check: '2566', 197 | name: "CRC-15/MPT1327", 198 | }, 199 | { 200 | check: 'bb3d', 201 | name: "CRC-16/ARC", 202 | }, 203 | { 204 | check: '4c06', 205 | name: "CRC-16/CDMA2000", 206 | }, 207 | { 208 | check: 'aee7', 209 | name: "CRC-16/CMS", 210 | }, 211 | { 212 | check: '9ecf', 213 | name: "CRC-16/DDS-110", 214 | }, 215 | { 216 | check: '007e', 217 | name: "CRC-16/DECT-R", 218 | }, 219 | { 220 | check: '007f', 221 | name: "CRC-16/DECT-X", 222 | }, 223 | { 224 | check: 'ea82', 225 | name: "CRC-16/DNP", 226 | }, 227 | { 228 | check: 'c2b7', 229 | name: "CRC-16/EN-13757", 230 | }, 231 | { 232 | check: 'd64e', 233 | name: "CRC-16/GENIBUS", 234 | }, 235 | { 236 | check: 'ce3c', 237 | name: "CRC-16/GSM", 238 | }, 239 | { 240 | check: '29b1', 241 | name: "CRC-16/IBM-3740", 242 | }, 243 | { 244 | check: '906e', 245 | name: "CRC-16/IBM-SDLC", 246 | }, 247 | { 248 | check: 'bf05', 249 | name: "CRC-16/ISO-IEC-14443-3-A", 250 | }, 251 | { 252 | check: '2189', 253 | name: "CRC-16/KERMIT", 254 | }, 255 | { 256 | check: 'bdf4', 257 | name: "CRC-16/LJ1200", 258 | }, 259 | { 260 | check: '772b', 261 | name: "CRC-16/M17", 262 | }, 263 | { 264 | check: '44c2', 265 | name: "CRC-16/MAXIM-DOW", 266 | }, 267 | { 268 | check: '6f91', 269 | name: "CRC-16/MCRF4XX", 270 | }, 271 | { 272 | check: '4b37', 273 | name: "CRC-16/MODBUS", 274 | }, 275 | { 276 | check: 'a066', 277 | name: "CRC-16/NRSC-5", 278 | }, 279 | { 280 | check: '5d38', 281 | name: "CRC-16/OPENSAFETY-A", 282 | }, 283 | { 284 | check: '20fe', 285 | name: "CRC-16/OPENSAFETY-B", 286 | }, 287 | { 288 | check: 'a819', 289 | name: "CRC-16/PROFIBUS", 290 | }, 291 | { 292 | check: '63d0', 293 | name: "CRC-16/RIELLO", 294 | }, 295 | { 296 | check: 'e5cc', 297 | name: "CRC-16/SPI-FUJITSU", 298 | }, 299 | { 300 | check: 'd0db', 301 | name: "CRC-16/T10-DIF", 302 | }, 303 | { 304 | check: '0fb3', 305 | name: "CRC-16/TELEDISK", 306 | }, 307 | { 308 | check: '26b1', 309 | name: "CRC-16/TMS37157", 310 | }, 311 | { 312 | check: 'fee8', 313 | name: "CRC-16/UMTS", 314 | }, 315 | { 316 | check: 'b4c8', 317 | name: "CRC-16/USB", 318 | }, 319 | { 320 | check: '31c3', 321 | name: "CRC-16/XMODEM", 322 | }, 323 | { 324 | check: '04f03', 325 | name: "CRC-17/CAN-FD", 326 | }, 327 | { 328 | check: '0ed841', 329 | name: "CRC-21/CAN-FD", 330 | }, 331 | { 332 | check: 'c25a56', 333 | name: "CRC-24/BLE", 334 | }, 335 | { 336 | check: '7979bd', 337 | name: "CRC-24/FLEXRAY-A", 338 | }, 339 | { 340 | check: '1f23b8', 341 | name: "CRC-24/FLEXRAY-B", 342 | }, 343 | { 344 | check: 'b4f3e6', 345 | name: "CRC-24/INTERLAKEN", 346 | }, 347 | { 348 | check: 'cde703', 349 | name: "CRC-24/LTE-A", 350 | }, 351 | { 352 | check: '23ef52', 353 | name: "CRC-24/LTE-B", 354 | }, 355 | { 356 | check: '21cf02', 357 | name: "CRC-24/OPENPGP", 358 | }, 359 | { 360 | check: '200fa5', 361 | name: "CRC-24/OS-9", 362 | }, 363 | { 364 | check: '04c34abf', 365 | name: "CRC-30/CDMA", 366 | }, 367 | { 368 | check: '0ce9e46c', 369 | name: "CRC-31/PHILIPS", 370 | }, 371 | { 372 | check: '3010bf7f', 373 | name: "CRC-32/AIXM", 374 | }, 375 | { 376 | check: '1697d06a', 377 | name: "CRC-32/AUTOSAR", 378 | }, 379 | { 380 | check: '87315576', 381 | name: "CRC-32/BASE91-D", 382 | }, 383 | { 384 | check: 'fc891918', 385 | name: "CRC-32/BZIP2", 386 | }, 387 | { 388 | check: '6ec2edc4', 389 | name: "CRC-32/CD-ROM-EDC", 390 | }, 391 | { 392 | check: '765e7680', 393 | name: "CRC-32/CKSUM", 394 | }, 395 | { 396 | check: 'e3069283', 397 | name: "CRC-32/ISCSI", 398 | }, 399 | { 400 | check: 'cbf43926', 401 | name: "CRC-32/ISO-HDLC", 402 | }, 403 | { 404 | check: '340bc6d9', 405 | name: "CRC-32/JAMCRC", 406 | }, 407 | { 408 | check: 'd2c22f51', 409 | name: "CRC-32/MEF", 410 | }, 411 | { 412 | check: '0376e6e7', 413 | name: "CRC-32/MPEG-2", 414 | }, 415 | { 416 | check: 'bd0be338', 417 | name: "CRC-32/XFER", 418 | }, 419 | { 420 | check: 'd4164fc646', 421 | name: "CRC-40/GSM", 422 | }, 423 | { 424 | check: '6c40df5f0b497347', 425 | name: "CRC-64/ECMA-182", 426 | }, 427 | { 428 | check: 'b90956c775a41001', 429 | name: "CRC-64/GO-ISO", 430 | }, 431 | { 432 | check: '75d4b74f024eceea', 433 | name: "CRC-64/MS", 434 | }, 435 | { 436 | check: 'ae8b14860a799888', 437 | name: "CRC-64/NVME", 438 | }, 439 | { 440 | check: 'e9c6d914c4b8d9ca', 441 | name: "CRC-64/REDIS", 442 | }, 443 | { 444 | check: '62ec59e3f1a4f00a', 445 | name: "CRC-64/WE", 446 | }, 447 | { 448 | check: '995dc9bbdf1939fa', 449 | name: "CRC-64/XZ", 450 | }, 451 | { 452 | check: '09ea83f625023801fd612', 453 | name: "CRC-82/DARC", 454 | } 455 | ]; 456 | 457 | var str = '123456789'; 458 | var WINDOW = typeof window === 'object'; 459 | var root = WINDOW ? window : {}; 460 | if (root.JS_CRC_NO_WINDOW) { 461 | WINDOW = false; 462 | } 463 | var WEB_WORKER = !WINDOW && typeof self === 'object'; 464 | var NODE_JS = !root.JS_CRC_NO_NODE_JS && typeof process === 'object' && process.versions && process.versions.node; 465 | if (NODE_JS) { 466 | root = global; 467 | } else if (WEB_WORKER) { 468 | root = self; 469 | } 470 | 471 | MODELS.forEach(function (model) { 472 | var methodName = model.name.replace(/[-/]/g, '_').toLocaleLowerCase(); 473 | var method = root[methodName]; 474 | describe('#' + model.name, function () { 475 | context('when ' + str, function () { 476 | it('should be equal to ' + model.check, function () { 477 | expect(method(str)).to.be(model.check); 478 | }); 479 | }); 480 | }); 481 | }); 482 | 483 | var crc_82_darc = root.crc_82_darc; 484 | describe('#CRC-82/DARC', function () { 485 | context('when ' + str, function () { 486 | it('should be equal to []', function () { 487 | expect(crc_82_darc.array(str).toHexString()).to.be('009ea83f625023801fd612'); 488 | }); 489 | }); 490 | }); 491 | 492 | var crc_64_ecma_182 = root.crc_64_ecma_182; 493 | describe('#CRC-64/ECMA-182', function () { 494 | context('when ' + str, function () { 495 | it('should be equal to []', function () { 496 | expect(crc_64_ecma_182.array(str).toHexString()).to.be('6c40df5f0b497347'); 497 | }); 498 | }); 499 | }); 500 | })(); 501 | -------------------------------------------------------------------------------- /tests/node-test.js: -------------------------------------------------------------------------------- 1 | expect = require('expect.js'); 2 | Worker = require("tiny-worker"); 3 | 4 | function unset() { 5 | delete require.cache[require.resolve('../src/crc.js')]; 6 | delete require.cache[require.resolve('./test.js')]; 7 | crc16 = null; 8 | crc32 = null; 9 | createModel = null; 10 | Object.keys(require('../src/models.js')).forEach((key) => { 11 | global[key] = null; 12 | }); 13 | delete require.cache[require.resolve('../src/models.js')]; 14 | delete require.cache[require.resolve('./models-test.js')]; 15 | BUFFER = undefined; 16 | JS_CRC_NO_WINDOW = undefined; 17 | JS_CRC_NO_NODE_JS = undefined; 18 | JS_CRC_NO_COMMON_JS = undefined; 19 | JS_CRC_NO_ARRAY_BUFFER = undefined; 20 | JS_CRC_NO_ARRAY_BUFFER_IS_VIEW = undefined; 21 | window = undefined; 22 | } 23 | 24 | function requireToGlobal() { 25 | crc16 = require('../src/crc.js').crc16; 26 | crc32 = require('../src/crc.js').crc32; 27 | createModel = require('../src/crc.js').createModel; 28 | Object.assign(global, require('../src/models.js')); 29 | } 30 | 31 | function runCommonJsTest() { 32 | requireToGlobal(); 33 | require('./test.js'); 34 | require('./models-test.js'); 35 | unset(); 36 | } 37 | 38 | function runWindowTest() { 39 | window = global; 40 | require('../src/crc.js'); 41 | require('../src/models.js'); 42 | require('./test.js'); 43 | require('./models-test.js'); 44 | unset(); 45 | } 46 | 47 | // Node.js env 48 | BUFFER = true; 49 | runCommonJsTest(); 50 | 51 | // Node.js env, no Buffer.from 52 | JS_CRC_NO_BUFFER_FROM = true 53 | runCommonJsTest(); 54 | 55 | // Webpack browser env 56 | JS_CRC_NO_NODE_JS = true; 57 | window = global; 58 | runCommonJsTest(); 59 | 60 | // browser env 61 | JS_CRC_NO_NODE_JS = true; 62 | JS_CRC_NO_COMMON_JS = true; 63 | runWindowTest(); 64 | 65 | // browser env and no array buffer 66 | JS_CRC_NO_NODE_JS = true; 67 | JS_CRC_NO_COMMON_JS = true; 68 | JS_CRC_NO_ARRAY_BUFFER = true; 69 | runWindowTest(); 70 | 71 | // browser env and no isView 72 | JS_CRC_NO_NODE_JS = true; 73 | JS_CRC_NO_COMMON_JS = true; 74 | JS_CRC_NO_ARRAY_BUFFER_IS_VIEW = true; 75 | runWindowTest(); 76 | 77 | // browser AMD 78 | JS_CRC_NO_NODE_JS = true; 79 | JS_CRC_NO_COMMON_JS = true; 80 | JS_CRC_NO_ARRAY_BUFFER_IS_VIEW = false; 81 | window = global; 82 | define = function (func) { 83 | crc16 = func().crc16; 84 | crc32 = func().crc32; 85 | if (crc16) { 86 | require('./test.js'); 87 | } else { 88 | require('./models-test.js'); 89 | } 90 | }; 91 | define.amd = true; 92 | 93 | require('../src/crc.js'); 94 | require('../src/models.js') 95 | unset(); 96 | 97 | // webworker 98 | WORKER = 'tests/worker.js'; 99 | SOURCE = 'src/crc.js'; 100 | 101 | require('./worker-test.js'); 102 | 103 | delete require.cache[require.resolve('./worker-test.js')]; 104 | 105 | // cover webworker 106 | JS_CRC_NO_WINDOW = true; 107 | JS_CRC_NO_NODE_JS = true; 108 | WORKER = './worker.js'; 109 | SOURCE = '../src/crc.js'; 110 | window = global; 111 | self = global; 112 | 113 | Worker = function (file) { 114 | require(file); 115 | currentWorker = this; 116 | 117 | this.postMessage = function (data) { 118 | onmessage({data: data}); 119 | }; 120 | } 121 | 122 | postMessage = function (data) { 123 | currentWorker.onmessage({data: data}); 124 | } 125 | 126 | importScripts = function () {}; 127 | 128 | requireToGlobal(); 129 | require('./worker-test.js'); 130 | -------------------------------------------------------------------------------- /tests/test.js: -------------------------------------------------------------------------------- 1 | (function(crc32, crc16, createModel) { 2 | Array.prototype.toHexString = ArrayBuffer.prototype.toHexString = function () { 3 | var array = new Uint8Array(this); 4 | var hex = ''; 5 | for (var i = 0; i < array.length; ++i) { 6 | var c = array[i].toString('16'); 7 | hex += c.length === 1 ? '0' + c : c; 8 | } 9 | return hex; 10 | }; 11 | 12 | var testCases = { 13 | crc32: { 14 | 'ascii': { 15 | '414fa339': 'The quick brown fox jumps over the lazy dog', 16 | '519025e9': 'The quick brown fox jumps over the lazy dog.' 17 | }, 18 | 'UTF8': { 19 | '5a09ed37': '中文', 20 | 'cf1f6086': 'aécio', 21 | 'a9aeffea': '𠜎' 22 | }, 23 | 'Array': { 24 | 'd202ef8d': [0], 25 | '414fa339': [84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103] 26 | } 27 | }, 28 | crc16: { 29 | 'ascii': { 30 | 'fcdf': 'The quick brown fox jumps over the lazy dog', 31 | '843d': 'The quick brown fox jumps over the lazy dog.' 32 | }, 33 | 'UTF8': { 34 | '6659': '中文', 35 | '9ef3': 'aécio', 36 | '46da': '𠜎' 37 | }, 38 | 'Array': { 39 | '0000': [0], 40 | 'fcdf': [84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103] 41 | } 42 | } 43 | }; 44 | 45 | if (!(typeof JS_CRC_NO_ARRAY_BUFFER === 'boolean' && JS_CRC_NO_ARRAY_BUFFER)) { 46 | testCases.crc32.Uint8Array = { 47 | '414fa339': new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]) 48 | }; 49 | testCases.crc32.Int8Array = { 50 | '414fa339': new Int8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]) 51 | }; 52 | testCases.crc32.ArrayBuffer = { 53 | 'd202ef8d': new ArrayBuffer(1) 54 | }; 55 | testCases.crc16.Uint8Array = { 56 | 'fcdf': new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]) 57 | }; 58 | testCases.crc16.Int8Array = { 59 | 'fcdf': new Int8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]) 60 | }; 61 | testCases.crc16.ArrayBuffer = { 62 | '0000': new ArrayBuffer(1), 63 | }; 64 | } 65 | 66 | if (typeof BUFFER === 'boolean' && BUFFER) { 67 | testCases.crc32.Buffer = { 68 | '414fa339': Buffer.from(new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103])) 69 | }; 70 | testCases.crc16.Buffer = { 71 | 'fcdf': Buffer.from(new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103])) 72 | }; 73 | } 74 | 75 | var errorTestCases = [null, undefined, { length: 0 }, 0, 1, false, true, NaN, Infinity, function () {}]; 76 | 77 | function runTestCases(name, algorithm) { 78 | var methods = [ 79 | { 80 | name: name, 81 | call: algorithm, 82 | }, 83 | { 84 | name: name + '.hex', 85 | call: algorithm.hex 86 | }, 87 | { 88 | name: name + '.array', 89 | call: function (message) { 90 | return algorithm.array(message).toHexString(); 91 | } 92 | } 93 | ]; 94 | 95 | var classMethods = [ 96 | { 97 | name: 'create', 98 | call: function (message) { 99 | return algorithm.create().update(message).toString(); 100 | } 101 | }, 102 | { 103 | name: 'update', 104 | call: function (message) { 105 | return algorithm.update(message).toString(); 106 | } 107 | }, 108 | { 109 | name: 'hex', 110 | call: function (message) { 111 | return algorithm.update(message).hex(); 112 | } 113 | }, 114 | { 115 | name: 'array', 116 | call: function (message) { 117 | return algorithm.update(message).array().toHexString(); 118 | } 119 | }, 120 | { 121 | name: 'finalize', 122 | call: function (message) { 123 | var hash = algorithm.update(message); 124 | hash.hex(); 125 | return hash.hex(); 126 | } 127 | } 128 | ]; 129 | 130 | var subTestCases = testCases[name]; 131 | 132 | describe(name, function () { 133 | methods.forEach(function (method) { 134 | describe('#' + method.name, function () { 135 | for (var testCaseName in subTestCases) { 136 | (function (testCaseName) { 137 | var testCase = subTestCases[testCaseName]; 138 | context('when ' + testCaseName, function () { 139 | for (var hash in testCase) { 140 | (function (message, hash) { 141 | it('should be equal', function () { 142 | expect(method.call(message)).to.be(hash); 143 | }); 144 | })(testCase[hash], hash); 145 | } 146 | }); 147 | })(testCaseName); 148 | } 149 | }); 150 | }); 151 | 152 | classMethods.forEach(function (method) { 153 | describe('#' + method.name, function () { 154 | for (var testCaseName in subTestCases) { 155 | (function (testCaseName) { 156 | var testCase = subTestCases[testCaseName]; 157 | context('when ' + testCaseName, function () { 158 | for (var hash in testCase) { 159 | (function (message, hash) { 160 | it('should be equal', function () { 161 | expect(method.call(message)).to.be(hash); 162 | }); 163 | })(testCase[hash], hash); 164 | } 165 | }); 166 | })(testCaseName); 167 | } 168 | }); 169 | }); 170 | 171 | describe('#' + name, function () { 172 | errorTestCases.forEach(function (testCase) { 173 | context('when ' + testCase, function () { 174 | it('should throw error', function () { 175 | expect(function () { 176 | algorithm(testCase); 177 | }).to.throwError(/input is invalid type/); 178 | }); 179 | }); 180 | }); 181 | 182 | context('when update after finalize', function () { 183 | it('should throw error', function () { 184 | expect(function () { 185 | var hash = algorithm.update('any'); 186 | hash.hex(); 187 | hash.update('any'); 188 | }).to.throwError(/finalize already called/); 189 | }); 190 | }); 191 | }); 192 | }); 193 | } 194 | 195 | describe('crc32', function () { 196 | context('when update multiple', function () { 197 | it('should be equal', function () { 198 | var crc = crc32.update('The quick brown fox jumps over the lazy dog') 199 | crc.update('.') 200 | expect(crc.hex()).to.be('519025e9'); 201 | }); 202 | }); 203 | }); 204 | 205 | runTestCases('crc32', crc32); 206 | runTestCases('crc16', crc16); 207 | 208 | describe('custom', function () { 209 | var str = '123456789'; 210 | 211 | context('when 30 bits', function () { 212 | var model = { 213 | width: 30, 214 | poly: 0x2030b9c7, 215 | init: 0x3fffffff, 216 | xorout: 0x3fffffff 217 | }; 218 | 219 | context('when refin true and refout false', function () { 220 | it('should be equal', function () { 221 | model 222 | var crc = createModel({ 223 | ...model, 224 | refin: true, 225 | refout: false 226 | }); 227 | expect(crc(str)).to.be('24083d85'); 228 | }); 229 | }); 230 | 231 | context('when refin true and refout false', function () { 232 | it('should be equal', function () { 233 | var crc = createModel({ 234 | ...model, 235 | refin: false, 236 | refout: true 237 | }); 238 | expect(crc(str)).to.be('3f54b0c8'); 239 | }); 240 | }); 241 | }); 242 | 243 | context('when 64 bits', function () { 244 | var model = { 245 | width: 64, 246 | poly: [0x259c84cb, 0xa6426349], 247 | init: [0xffffffff, 0xffffffff], 248 | xorout: [0, 0] 249 | }; 250 | 251 | context('when refin true and refout false', function () { 252 | it('should be equal', function () { 253 | var crc = createModel({ 254 | ...model, 255 | refin: true, 256 | refout: false 257 | }); 258 | expect(crc(str)).to.be('57737240f2ed2bae'); 259 | }); 260 | }); 261 | 262 | context('when refin false and refout true', function () { 263 | it('should be equal', function () { 264 | var crc = createModel({ 265 | ...model, 266 | refin: false, 267 | refout: true 268 | }); 269 | expect(crc(str)).to.be('575f8b8ce1d16f98'); 270 | }); 271 | }); 272 | }); 273 | }); 274 | })(crc32, crc16, createModel); 275 | -------------------------------------------------------------------------------- /tests/worker-test.js: -------------------------------------------------------------------------------- 1 | (function (Worker, WORKER, SOURCE) { 2 | var cases = { 3 | '414fa339': 'The quick brown fox jumps over the lazy dog', 4 | '519025e9': 'The quick brown fox jumps over the lazy dog.' 5 | }; 6 | 7 | describe('#crc32', function () { 8 | Object.keys(cases).forEach(function (hash) { 9 | it('should be equal', function (done) { 10 | var worker = new Worker(WORKER); 11 | worker.onmessage = function(event) { 12 | expect(event.data).to.be(hash); 13 | if (worker.terminate) { 14 | worker.terminate(); 15 | } 16 | done(); 17 | }; 18 | worker.postMessage(SOURCE); 19 | worker.postMessage(cases[hash]); 20 | }); 21 | }); 22 | }); 23 | })(Worker, WORKER, SOURCE); 24 | -------------------------------------------------------------------------------- /tests/worker.html: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | CRC 6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 18 | 19 | 23 | 25 | 26 | 27 | -------------------------------------------------------------------------------- /tests/worker.js: -------------------------------------------------------------------------------- 1 | var imported = false; 2 | onmessage = function(e) { 3 | if (imported) { 4 | postMessage(crc32(e.data)); 5 | if (typeof exports !== 'undefined') { 6 | imported = false; 7 | } 8 | } else { 9 | imported = true; 10 | importScripts(e.data); 11 | } 12 | } 13 | --------------------------------------------------------------------------------