☑️ ADLER-32 checksum
Go to file
SheetJS b692b3f7df version bump 0.4.0
- simplified utf8 code
- added browser demo
- added command line tool adler32
- fixed unicode baseline script (node 6 changed default array printing)
- fixed performance tests (benchmark module changed behavior)
- updated travis versions for test
- miscellaneous adjustments to tooling
2016-06-16 12:56:55 -04:00
bin version bump 0.4.0 2016-06-16 12:56:55 -04:00
bits version bump 0.4.0 2016-06-16 12:56:55 -04:00
ctest version bump 0.4.0 2016-06-16 12:56:55 -04:00
demo version bump 0.4.0 2016-06-16 12:56:55 -04:00
misc version bump 0.4.0 2016-06-16 12:56:55 -04:00
perf version bump 0.4.0 2016-06-16 12:56:55 -04:00
test_files version bump 0.4.0 2016-06-16 12:56:55 -04:00
.flowconfig version bump 0.4.0 2016-06-16 12:56:55 -04:00
.gitignore version bump 0.4.0 2016-06-16 12:56:55 -04:00
.jscs.json Initial commit 2014-06-18 13:58:20 -04:00
.jshintrc version bump 0.4.0 2016-06-16 12:56:55 -04:00
.npmignore version bump 0.3.0 2016-01-16 00:36:58 -05:00
.travis.yml version bump 0.4.0 2016-06-16 12:56:55 -04:00
adler32.flow.js version bump 0.4.0 2016-06-16 12:56:55 -04:00
adler32.js version bump 0.4.0 2016-06-16 12:56:55 -04:00
index.html version bump 0.4.0 2016-06-16 12:56:55 -04:00
LICENSE version bump 0.3.0 2016-01-16 00:36:58 -05:00
Makefile version bump 0.4.0 2016-06-16 12:56:55 -04:00
package.json version bump 0.4.0 2016-06-16 12:56:55 -04:00
README.md version bump 0.4.0 2016-06-16 12:56:55 -04:00
test.js version bump 0.4.0 2016-06-16 12:56:55 -04:00

adler32

Signed ADLER-32 algorithm implementation in JS (for the browser and nodejs). Emphasis on correctness and performance.

Installation

With npm:

npm install adler-32

In the browser:

<script lang="javascript" src="adler32.js"></script>

The browser exposes a variable ADLER32

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

Usage

  • ADLER32.buf(byte array or buffer) assumes the argument is a set of 8 bit unsigned integers (e.g. nodejs Buffer or simple array of ints)

  • ADLER32.bstr(binary string) interprets the argument as a binary string where the i-th byte is str.charCodeAt(i)

  • ADLER32.str(string) interprets the argument as a standard JS string

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 adler32 function from python zlib:

>>> from zlib import adler32
>>> x="foo bar baz٪☃🍣"
>>> adler32(x)
1543572022
>>> adler32(x+x)
-2076896149
>>> adler32(x+x+x)
2023497376

The included adler32.njs script can process files or stdin:

$ echo "this is a test" > t.txt
$ bin/adler32.njs t.txt
726861088

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

$ bin/adler32.py t.txt
726861088

Performance

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

js-crc has more performance notes

Bit twiddling is much faster than taking the mod on Safari and older Firefoxes. Instead of taking the literal mod 65521, it is faster to keep it in the integers by bit-shifting: 65536 ~ 15 mod 65521 so for nonnegative integer a:

    a = (a >>> 16) * 65536 + (a & 65535)            [equality]
    a ~ (a >>> 16) * 15    + (a & 65535) mod 65521

The mod is taken at the very end, since the intermediate result may exceed 65521

Magic Number

The magic numbers were chosen so as to not overflow a 31-bit integer:

F[n_] := Reduce[x*(x + 1)*n/2 + (x + 1)*(65521) < (2^31 - 1) && x > 0, x, Integers]
F[255] (* bstr:  x \[Element] Integers && 1 <= x <= 3854 *)
F[127] (* ascii: x \[Element] Integers && 1 <= x <= 5321 *)

Subtract up to 4 elements for the unicode case.

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

Build Status

Coverage Status

Analytics