Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

adler32

Package Overview
Dependencies
Maintainers
1
Versions
14
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

adler32 - npm Package Compare versions

Comparing version 0.1.5 to 0.1.6

lib/algorithm.js

67

index.js
#!/usr/bin/env node
"use strict";
/**
* Largest prime smaller than 2^16 (65536)
*/
var BASE = 65521;
var algorithm = require('./lib/algorithm');
var Hash = require('./lib/Hash');
var register = require('./lib/register');
/**
* Largest value n such that 255n(n+1)/2 + (n+1)(BASE-1) <= 2^32-1
*
* NMAX is just how often modulo needs to be taken of the two checksum word halves to prevent overflowing a 32 bit
* integer. This is an optimization. We "could" take the modulo after each byte, and it must be taken before each
* digest.
*/
var NMAX = 5552;
exports.sum = function(buf, adler)
{
if (adler == null)
adler = 1;
var a = adler & 0xFFFF,
b = (adler >>> 16) & 0xFFFF,
i = 0,
max = buf.length,
n, value;
while (i < max)
{
n = Math.min(NMAX, max - i);
do
{
a += buf[i++]<<0;
b += a;
}
while (--n);
a %= BASE;
b %= BASE;
}
return ((b << 16) | a) >>> 0;
};
exports.roll = function(sum, length, oldByte, newByte)
{
var a = sum & 0xFFFF,
b = (sum >>> 16) & 0xFFFF;
if (newByte != null)
{
a = (a - oldByte + newByte + BASE) % BASE;
b = (b - ((length * oldByte) % BASE) + a - 1 + BASE) % BASE;
}
else
{
a = (a - oldByte + BASE) % BASE;
b = (b - ((length * oldByte) % BASE) - 1 + BASE) % BASE;
}
return ((b << 16) | a) >>> 0;
};
exports.sum = algorithm.sum.bind(algorithm);
exports.roll = algorithm.roll.bind(algorithm);
exports.Hash = Hash;
exports.register = register;
{
"name": "adler32",
"version": "0.1.5",
"version": "0.1.6",
"description": "Adler-32 hashing algorithm",
"main": "index.js",
"devDependencies": {
"mocha": "^1.20.1",
"mhash": "^1.0.0",
"should": "^4.0.4",
"mocha": "^2.2.5",
"mhash": "^2.0.0",
"should": "^7.0.2",
"random-buffer": "^0.1.0"
},
"scripts": {
"test": "mocha -t 10000 -R spec ./test.js",
"test": "mocha -t 30000 -R spec ./test.js",
"bench": "./bench.js"

@@ -15,0 +15,0 @@ },

@@ -51,2 +51,30 @@ # adler32 [![Build Status](https://travis-ci.org/ChrisAckerman/adler32.svg?branch=master)](https://travis-ci.org/ChrisAckerman/adler32)

## `crypto` Module Integration
The adler32 algorithm can be integrated with the
[Node.js crypto module](http://nodejs.org/api/crypto.html#crypto_crypto_createhash_algorithm). Calling the
`adler32.register()` static method will add the 'adler32' key to the crypto module's algorithm list.
Create an adler32 Hash instance using the crypto module:
// Adds the 'adler32' key to the crypto.createHash() method.
adler32.register();
// Get an adler32 hash instance.
var hash = crypto.createHash('adler32');
// Add ascii data to the hash.
hash.update(data, 'ascii');
// Get a digest of all data added to the hash.
var digest = hash.digest('hex');
Create an adler32 Hash instance directly:
var hash = new adler32.Hash();
Like the `crypto.Hash` class, `adler32.Hash` extends the `stream.Transform` class. You can use the stream `.write()` and
`.read()` methods in place of the deprecated `.update()` and `.digest()` methods. See the
[Node.js stream module](http://nodejs.org/api/stream.html) documentation for more information about streaming.
## Caveats

@@ -53,0 +81,0 @@

@@ -6,3 +6,3 @@ #!/usr/bin/env node

var Adler32 = require('./');
var hash = require('mhash').hash;
var mhash = require('mhash');
var rand = require('random-buffer');

@@ -14,3 +14,3 @@

describe('Adler32', function() {
describe('.sum(buf, adler = 1)', function() {
describe('.sum(buf, [adler = 1])', function() {
var sum = Adler32.sum(buf);

@@ -24,3 +24,3 @@

it('should match the sum calculated by mhash', function() {
sum.should.be.exactly(parseInt(hash('adler32', buf), 16));
sum.should.be.exactly(parseInt(mhash('adler32', buf), 16));
});

@@ -40,3 +40,3 @@

describe('.roll(sum, length, oldByte, newByte = null)', function() {
describe('.roll(sum, length, oldByte, [newByte = null])', function() {
it('should result in the same value as sum for each offset chunk of size 64', function() {

@@ -78,2 +78,65 @@ rollTest(64);

});
describe('.Hash', function () {
it('should hash a string', function() {
// Example taken from http://en.wikipedia.org/wiki/Adler-32.
var hash = new Adler32.Hash();
hash.update('Wikipedia');
hash.digest('hex').toLowerCase().should.be.exactly('11e60398');
});
it('should hash a string in parts', function() {
var hash = new Adler32.Hash();
hash.update('Wiki');
hash.update('pedia');
hash.digest('hex').toLowerCase().should.be.exactly('11e60398');
});
it('should work as a Transform stream', function() {
var hash = new Adler32.Hash({encoding: 'hex'});
hash.write('Wiki');
hash.write('pedia');
hash.end();
hash.read().toLowerCase().should.be.exactly('11e60398');
});
it('should throw a TypeError if update() is called after digest()', function() {
var hash = new Adler32.Hash();
hash.update('Wikipedia');
hash.digest('hex');
(function() {
hash.update('Moar!');
}).should.throw(TypeError);
});
it('should throw an Error if digest() is called more than once', function() {
var hash = new Adler32.Hash();
hash.digest('hex');
(function() {
hash.digest('hex');
}).should.throw(Error);
});
});
describe('.register()', function () {
Adler32.register();
var crypto = require('crypto');
it('should make it so crypto.getHashes() contains adler32', function () {
crypto.getHashes().indexOf('adler32').should.not.equal(-1);
});
it('should make it so crypto.createHash(algorithm) works for adler32', function () {
var hash = crypto.createHash('adler32');
should(hash).be.an.instanceOf(Adler32.Hash);
hash.update('Wikipedia');
hash.digest('hex').toLowerCase().should.be.exactly('11e60398');
});
it('should not remove other crypto hash algorithms', function () {
crypto.getHashes().indexOf('sha256').should.not.equal(-1);
should.exist(crypto.createHash('sha256'));
});
});
});

@@ -80,0 +143,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc