Git Product home page Git Product logo

js-adler32's Introduction

adler32

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

Installation

With npm:

$ npm install adler-32

In the browser:

<script 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.

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

Usage

In all cases, the relevant function takes an argument representing data and an optional 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 sequence of 8-bit unsigned integers (nodejs Buffer, Uint8Array or array of bytes).

  • ADLER32.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

  • ADLER32.str(string) assumes the argument is a standard JS string and calculates the hash of the UTF-8 encoding.

For example:

// var ADLER32 = require('adler-32');           // uncomment if in node
ADLER32.str("SheetJS")                          // 176947863
ADLER32.bstr("SheetJS")                         // 176947863
ADLER32.buf([ 83, 104, 101, 101, 116, 74, 83 ]) // 176947863

adler32 = 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 ]

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 adler32-cli package includes scripts for processing files or text on standard input:

$ echo "this is a test" > t.txt
$ adler32-cli t.txt
726861088

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

$ packages/adler32-cli/bin/adler32.py t.txt
726861088

Performance

make perf will run algorithmic performance tests (which should justify certain decisions 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 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

Sauce Test Status

Build Status

Coverage Status

Analytics

js-adler32's People

Contributors

101arrowz avatar ashnur avatar garrettluu avatar ntnyq avatar sheetjsdev avatar stof avatar thenickdude avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar

js-adler32's Issues

print4j version and bundle size

Issue

We're trying to dedupe our dependencies, and adler-32 appears to depend on printj: ~1.2.2. We have another lib that depends on a slightly different version.

If we changed it to printj: ^1.2.2, we'd be able to dedupe.

Question

What's the motivation for the more conservative version? Is there any flexibility, here?

Giving -ve CheckSum for formData.

Hi I am using this library in my current project. It gives perfect checksum for the Test Strings, but in my real scenario I need to calculate the checksum of my formdata.
A typical formdata will be like;
var ADLER32 = require('adler-32'); let checkSum = ADLER32.str("formdata={\"data\":\"value\", \"key\":\"value1\", \"key2\":\"value2\"}123456");

I get a negative checksum for this input. -1415900241

for let checkSum = ADLER32.str("SheetJS");
I get proper checksum Value. 176947863

Hoping to get a solution !!!

Does this work without two's complement?

Hi,

Taking the value thumbprint.utilities.jobilla.com this should convert to the hex: D8700CD0 which should convert into the decimal: 3631221968 however it is converting to: -663745328 from signed 2's complement.

Decrypt checksum?

Is it possible to decode the checksum, so I get the original decodes string?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.