- Notifications
You must be signed in to change notification settings - Fork14
SheetJS/js-adler32
Folders and files
Name | Name | Last commit message | Last commit date | |
---|---|---|---|---|
Repository files navigation
Signed ADLER-32 algorithm implementation in JS (for the browser and nodejs).Emphasis on correctness, performance, and IE6+ support.
Withnpm:
$ npm install adler-32
In the browser:
<scriptsrc="adler32.js"></script>
The browser exposes a variableADLER32
.
When installed globally, npm installs a scriptadler32
that computes thechecksum for a specified file or standard input.
The script will manipulatemodule.exports
if available . This is not alwaysdesirable. To prevent the behavior, defineDO_NOT_EXPORT_ADLER
.
In all cases, the relevant function takes an argument representing data and anoptional second argument representing the starting "seed" (for running hash).
The return value is a signed 32-bit integer.
ADLER32.buf(byte array or buffer[, seed])
assumes the argument is a sequenceof 8-bit unsigned integers (nodejsBuffer
,Uint8Array
or array of bytes).ADLER32.bstr(binary string[, seed])
assumes the argument is a binary stringwhere bytei
is the low byte of the UCS-2 char:str.charCodeAt(i) & 0xFF
ADLER32.str(string)
assumes the argument is a standard JS string andcalculates the hash of the UTF-8 encoding.
For example:
// var ADLER32 = require('adler-32'); // uncomment if in nodeADLER32.str("SheetJS")// 176947863ADLER32.bstr("SheetJS")// 176947863ADLER32.buf([83,104,101,101,116,74,83])// 176947863adler32=ADLER32.buf([83,104])// 17825980 "Sh"adler32=ADLER32.str("eet",adler32)// 95486458 "Sheet"ADLER32.bstr("JS",adler32)// 176947863 "SheetJS"[ADLER32.str("\u2603"),ADLER32.str("\u0003")]// [ 73138686, 262148 ][ADLER32.bstr("\u2603"),ADLER32.bstr("\u0003")]// [ 262148, 262148 ][ADLER32.buf([0x2603]),ADLER32.buf([0x0003])]// [ 262148, 262148 ]
make test
will run the nodejs-based test.
To run the in-browser tests, run a local server and go to thectest
directory.make ctestserv
will start a pythonSimpleHTTPServer
server on port 8000.
To update the browser artifacts, runmake ctest
.
To generate the bits file, use theadler32
function from pythonzlib
:
>>>fromzlibimportadler32>>>x="foo bar baz٪☃🍣">>>adler32(x)1543572022>>>adler32(x+x)-2076896149>>>adler32(x+x+x)2023497376
Theadler32-cli
package includesscripts for processing files or text on standard input:
$echo"this is a test"> t.txt$ adler32-cli t.txt726861088
For comparison, theadler32.py
script in the subdirectory uses pythonzlib
:
$ packages/adler32-cli/bin/adler32.py t.txt726861088
make perf
will run algorithmic performance tests (which should justify certaindecisions in the code).
Bit twiddling is much faster than taking the mod in Safari and Firefox browsers.Instead of taking the literal mod 65521, it is faster to keep it in the integersby bit-shifting:65536 ~ 15 mod 65521
so for nonnegative integera
:
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
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.
Please consult the attached LICENSE file for details. All rights not explicitlygranted by the Apache 2.0 license are reserved by the Original Author.
About
☑️ ADLER-32 checksum
Topics
Resources
License
Uh oh!
There was an error while loading.Please reload this page.
Stars
Watchers
Forks
Packages0
Uh oh!
There was an error while loading.Please reload this page.
Contributors7
Uh oh!
There was an error while loading.Please reload this page.