🌀 JS standard CRC-32 and CRC32C implementation https://cdn.sheetjs.com/crc-32/
Go to file
Ryan Ghods 3093cbddca
package.json: use file extensions
for esmodules compatibility
2022-01-20 14:50:25 -08:00
bin version bump 1.2.0: infrastructure 2018-01-17 16:27:59 -05:00
bits version bump 1.2.0: infrastructure 2018-01-17 16:27:59 -05:00
ctest version bump 1.2.0: infrastructure 2018-01-17 16:27:59 -05:00
demo version bump 1.2.0: infrastructure 2018-01-17 16:27:59 -05:00
misc flow switch to module.exports 2018-04-21 10:09:47 -04:00
perf version bump 1.0.1: bstr performance 2016-10-12 15:46:18 -04:00
test_files added test artifacts 2019-10-08 02:43:14 -04:00
types version bump 1.2.0: infrastructure 2018-01-17 16:27:59 -05:00
.eslintrc version bump 1.2.0: infrastructure 2018-01-17 16:27:59 -05:00
.flowconfig flow and travis fixes 2017-06-12 16:12:23 -04:00
.gitignore flow switch to module.exports 2018-04-21 10:09:47 -04:00
.jscs.json Initial commit 2014-06-16 17:27:47 -04:00
.jshintrc version bump 0.3.0: cleanup and flow 2015-05-06 14:47:18 -07:00
.spelling version bump 1.2.0: infrastructure 2018-01-17 16:27:59 -05:00
.travis.yml version bump 1.2.0: infrastructure 2018-01-17 16:27:59 -05:00
crc32.flow.js version bump 1.2.0: infrastructure 2018-01-17 16:27:59 -05:00
crc32.js version bump 1.2.0: infrastructure 2018-01-17 16:27:59 -05:00
index.html version bump 1.2.0: infrastructure 2018-01-17 16:27:59 -05:00
large.html version bump 1.0.2: demos and CLI 2017-04-27 17:33:14 -04:00
LICENSE License text 2019-10-08 02:07:56 -04:00
Makefile increased test timeout for node 8.x / 9.x 2018-01-17 19:35:19 -05:00
package.json package.json: use file extensions 2022-01-20 14:50:25 -08:00
README.md version bump 1.2.0: infrastructure 2018-01-17 16:27:59 -05:00
shim.js version bump 1.0.0: rolling checksums 2016-10-08 15:26:03 -04:00
test.js node 4.x Buffer.from fix 2018-07-07 21:02:33 -04:00

crc32

Standard CRC-32 algorithm implementation in JS (for the browser and nodejs). Emphasis on correctness, performance, and IE6+ support.

Installation

With npm:

$ npm install crc-32

In the browser:

<script src="crc32.js"></script>

The browser exposes a variable CRC32.

When installed globally, npm installs a script crc32 that computes the checksum for a specified file or standard input.

The script will manipulate module.exports if available . This is not always desirable. To prevent the behavior, define DO_NOT_EXPORT_CRC.

Usage

In all cases, the relevant function takes an argument representing data and an optional second argument representing the starting "seed" (for rolling CRC).

The return value is a signed 32-bit integer.

  • CRC32.buf(byte array or buffer[, seed]) assumes the argument is a sequence of 8-bit unsigned integers (nodejs Buffer, Uint8Array or array of bytes).

  • CRC32.bstr(binary string[, seed]) assumes the argument is a binary string where byte i is the low byte of the UCS-2 char: str.charCodeAt(i) & 0xFF

  • CRC32.str(string[, seed]) assumes the argument is a standard JS string and calculates the hash of the UTF-8 encoding.

For example:

// var CRC32 = require('crc-32');             // uncomment this line if in node
CRC32.str("SheetJS")                          // -1647298270
CRC32.bstr("SheetJS")                         // -1647298270
CRC32.buf([ 83, 104, 101, 101, 116, 74, 83 ]) // -1647298270

crc32 = CRC32.buf([83, 104])                  // -1826163454  "Sh"
crc32 = CRC32.str("eet", crc32)               //  1191034598  "Sheet"
CRC32.bstr("JS", crc32)                       // -1647298270  "SheetJS"

[CRC32.str("\u2603"),  CRC32.str("\u0003")]   // [ -1743909036,  1259060791 ]
[CRC32.bstr("\u2603"), CRC32.bstr("\u0003")]  // [  1259060791,  1259060791 ]
[CRC32.buf([0x2603]),  CRC32.buf([0x0003])]   // [  1259060791,  1259060791 ]

Testing

make test will run the nodejs-based test.

To run the in-browser tests, run a local server and go to the ctest directory. make ctestserv will start a python SimpleHTTPServer server on port 8000.

To update the browser artifacts, run make ctest.

To generate the bits file, use the crc32 function from python zlib:

>>> from zlib import crc32
>>> x="foo bar baz٪☃🍣"
>>> crc32(x)
1531648243
>>> crc32(x+x)
-218791105
>>> crc32(x+x+x)
1834240887

The included crc32.njs script can process files or standard input:

$ echo "this is a test" > t.txt
$ bin/crc32.njs t.txt
1912935186

For comparison, the included crc32.py script uses python zlib:

$ bin/crc32.py t.txt
1912935186

On OSX the command cksum generates unsigned CRC-32 with Algorithm 3:

$ cksum -o 3 < IE8.Win7.For.Windows.VMware.zip
1891069052 4161613172
$ crc32 --unsigned ~/Downloads/IE8.Win7.For.Windows.VMware.zip
1891069052

Performance

make perf will run algorithmic performance tests (which should justify certain decisions in the code).

The adler-32 project has more performance notes

License

Please consult the attached LICENSE file for details. All rights not explicitly granted by the Apache 2.0 license are reserved by the Original Author.

Badges

Sauce Test Status

Build Status Coverage Status Dependencies Status NPM Downloads ghit.me Analytics