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

rusha

Package Overview
Dependencies
Maintainers
1
Versions
25
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

rusha - npm Package Compare versions

Comparing version 0.7.7 to 0.7.8

benchmark/benchmark.js

35

package.json
{
"name": "rusha",
"version": "0.7.7",
"version": "0.7.8",
"description": "A high-performance pure-javascript SHA1 implementation suitable for large binary data.",

@@ -13,3 +13,5 @@ "main": "rusha.js",

"scripts": {
"test": "mocha --harmony --reporter spec"
"test": "./node_modules/.bin/grunt test",
"test-saucelabs": "./node_modules/.bin/grunt test-saucelabs",
"build": "./node_modules/.bin/grunt build"
},

@@ -21,9 +23,30 @@ "repository": {

"devDependencies": {
"mocha": "~1.15.0",
"jsmin": "~1.0",
"sweet.js": "~0.7.1"
"sweet.js": "~0.7.1",
"grunt": "~0.4.5",
"grunt-cli": "~0.1.13",
"grunt-contrib-uglify": "~0.5.1",
"grunt-sweet.js": "~0.1.5",
"grunt-mocha-test": "~0.11.0",
"blanket": "~1.1.6",
"benchmark": "^1.0.0",
"microtime": "^1.0.1",
"grunt-saucelabs": "^8.3.2"
},
"testling": {
"files": "test/test.js",
"harness": "mocha-tdd",
"browsers": [
"ie/6..latest",
"chrome/22..latest",
"firefox/16..latest",
"safari/latest",
"opera/11.0..latest"
]
},
"author": "Sam Rijs",
"license": "MIT",
"readmeFilename": "README.md"
"readmeFilename": "README.md",
"dependencies": {
"grunt-contrib-connect": "^0.8.0"
}
}

8

README.md
# Rusha [![Build Status](https://travis-ci.org/srijs/rusha.png?branch=master)](https://travis-ci.org/srijs/rusha)
*A high-performance pure-javascript SHA1 implementation suitable for large binary data.*
[![NPM](https://nodei.co/npm/rusha.png?downloads=true&downloadRank=true)](https://nodei.co/npm/rusha/)
## Prologue: The Sad State of Javascript SHA1 implementations

@@ -50,5 +52,5 @@

* Download npm dependencies with `npm install .`
* Source file to be edited is `rusha.pp.js`
* Build with `make rusha.min.js`
* Download npm dependencies with `npm install`
* Source file to be edited is `rusha.sweet.js`
* Build with `npm run build`
* Run tests with `npm test`

@@ -55,0 +57,0 @@

@@ -46,20 +46,39 @@ /*

var hash, data = event.data.data;
if (data instanceof Blob) {
try {
data = reader.readAsBinaryString(data);
} catch (e) {
self.postMessage({
id: event.data.id,
error: e.name
});
return;
try {
hash = hasher.digest(data);
self.postMessage({
id: event.data.id,
hash: hash
});
} catch (e) {
self.postMessage({
id: event.data.id,
error: e.name
});
}
};
}
var util = {
getDataType: function (data) {
if (typeof data === 'string') {
return 'string';
}
if (data instanceof Array) {
return 'array';
}
if (typeof global !== 'undefined' && global.Buffer && global.Buffer.isBuffer(data)) {
return 'buffer';
}
if (data instanceof ArrayBuffer) {
return 'arraybuffer';
}
if (data.buffer instanceof ArrayBuffer) {
return 'view';
}
if (data instanceof Blob) {
return 'blob';
}
throw new Error('Unsupported data type.');
}
hash = hasher.digest(data);
self.postMessage({
id: event.data.id,
hash: hash
});
};
}
// The Rusha object is a wrapper around the low-level RushaCore.

@@ -86,55 +105,115 @@ // It provides means of converting different inputs to the

};
// Convert a binary string to a big-endian Int32Array using
// four characters per slot and pad it per the sha1 spec.
// Convert a binary string and write it to the heap.
// A binary string is expected to only contain char codes < 256.
var convStr = function (str, bin, start, len) {
var i, m = len % 4, j = len - m;
for (i = 0; i < j; i = i + 4 | 0) {
bin[i >> 2] = str.charCodeAt(start + i) << 24 | str.charCodeAt(start + i + 1) << 16 | str.charCodeAt(start + i + 2) << 8 | str.charCodeAt(start + i + 3);
var convStr = function (H8, H32, start, len, off) {
var str = this, i, om = off % 4, lm = len % 4, j = len - lm;
if (j > 0) {
switch (om) {
case 0:
H8[off + 3 | 0] = str.charCodeAt(start);
case 1:
H8[off + 2 | 0] = str.charCodeAt(start + 1);
case 2:
H8[off + 1 | 0] = str.charCodeAt(start + 2);
case 3:
H8[off | 0] = str.charCodeAt(start + 3);
}
}
switch (m) {
case 0:
bin[j >> 2] |= str.charCodeAt(start + j + 3);
for (i = om; i < j; i = i + 4 | 0) {
H32[off + i >> 2] = str.charCodeAt(start + i) << 24 | str.charCodeAt(start + i + 1) << 16 | str.charCodeAt(start + i + 2) << 8 | str.charCodeAt(start + i + 3);
}
switch (lm) {
case 3:
bin[j >> 2] |= str.charCodeAt(start + j + 2) << 8;
H8[off + j + 1 | 0] = str.charCodeAt(start + j + 2);
case 2:
bin[j >> 2] |= str.charCodeAt(start + j + 1) << 16;
H8[off + j + 2 | 0] = str.charCodeAt(start + j + 1);
case 1:
bin[j >> 2] |= str.charCodeAt(start + j) << 24;
H8[off + j + 3 | 0] = str.charCodeAt(start + j);
}
};
// Convert a buffer or array to a big-endian Int32Array using
// four elements per slot and pad it per the sha1 spec.
// Convert a buffer or array and write it to the heap.
// The buffer or array is expected to only contain elements < 256.
var convBuf = function (buf, bin, start, len) {
var i, m = len % 4, j = len - m;
for (i = 0; i < j; i = i + 4 | 0) {
bin[i >> 2] = buf[start + i] << 24 | buf[start + i + 1] << 16 | buf[start + i + 2] << 8 | buf[start + i + 3];
var convBuf = function (H8, H32, start, len, off) {
var buf = this, i, om = off % 4, lm = len % 4, j = len - lm;
if (j > 0) {
switch (om) {
case 0:
H8[off + 3 | 0] = buf[start];
case 1:
H8[off + 2 | 0] = buf[start + 1];
case 2:
H8[off + 1 | 0] = buf[start + 2];
case 3:
H8[off | 0] = buf[start + 3];
}
}
switch (m) {
case 0:
bin[j >> 2] |= buf[start + j + 3];
for (i = 4 - om; i < j; i = i += 4 | 0) {
H32[off + i >> 2] = buf[start + i] << 24 | buf[start + i + 1] << 16 | buf[start + i + 2] << 8 | buf[start + i + 3];
}
switch (lm) {
case 3:
bin[j >> 2] |= buf[start + j + 2] << 8;
H8[off + j + 1 | 0] = buf[start + j + 2];
case 2:
bin[j >> 2] |= buf[start + j + 1] << 16;
H8[off + j + 2 | 0] = buf[start + j + 1];
case 1:
bin[j >> 2] |= buf[start + j] << 24;
H8[off + j + 3 | 0] = buf[start + j];
}
};
// Convert general data to a big-endian Int32Array written on the
// heap.
var conv = function (data, bin, start, len) {
if (typeof data === 'string') {
return convStr(data, bin, start, len);
} else if (data instanceof Array || typeof global !== 'undefined' && typeof global.Buffer !== 'undefined' && global.Buffer.isBuffer(data)) {
return convBuf(data, bin, start, len);
} else if (data instanceof ArrayBuffer) {
return convBuf(new Uint8Array(data), bin, start, len);
} else if (data.buffer instanceof ArrayBuffer) {
return convBuf(new Uint8Array(data.buffer), bin, start, len);
} else {
throw new Error('Unsupported data type.');
var convBlob = function (H8, H32, start, len, off) {
var blob = this, i, om = off % 4, lm = len % 4, j = len - lm;
var buf = new Uint8Array(reader.readAsArrayBuffer(blob.slice(start, start + len)));
if (j > 0) {
switch (om) {
case 0:
H8[off + 3 | 0] = buf[0];
case 1:
H8[off + 2 | 0] = buf[1];
case 2:
H8[off + 1 | 0] = buf[2];
case 3:
H8[off | 0] = buf[3];
}
}
for (i = 4 - om; i < j; i = i += 4 | 0) {
H32[off + i >> 2] = buf[i] << 24 | buf[i + 1] << 16 | buf[i + 2] << 8 | buf[i + 3];
}
switch (lm) {
case 3:
H8[off + j + 1 | 0] = buf[j + 2];
case 2:
H8[off + j + 2 | 0] = buf[j + 1];
case 1:
H8[off + j + 3 | 0] = buf[j];
}
};
var convFn = function (data) {
switch (util.getDataType(data)) {
case 'string':
return convStr.bind(data);
case 'array':
return convBuf.bind(data);
case 'buffer':
return convBuf.bind(data);
case 'arraybuffer':
return convBuf.bind(new Uint8Array(data));
case 'view':
return convBuf.bind(new Uint8Array(data.buffer, data.byteOffset, data.byteLength));
case 'blob':
return convBlob.bind(data);
}
};
var slice = function (data, offset) {
switch (util.getDataType(data)) {
case 'string':
return data.slice(offset);
case 'array':
return data.slice(offset);
case 'buffer':
return data.slice(offset);
case 'arraybuffer':
return data.slice(offset);
case 'view':
return data.buffer.slice(offset);
}
};
// Convert an ArrayBuffer into its hexadecimal string representation.

@@ -168,4 +247,4 @@ var hex = function (arrayBuffer) {

};
// Resize the internal data structures to a new capacity.
var resize = function (size) {
// Initialize the internal data structures to a new capacity.
var init = function (size) {
if (size % 64 > 0) {

@@ -181,2 +260,4 @@ throw new Error('Chunk size must be a multiple of 128 bit');

self$2.heap = new ArrayBuffer(ceilHeapSize(self$2.padMaxChunkLen + 320 + 20));
self$2.h32 = new Int32Array(self$2.heap);
self$2.h8 = new Int8Array(self$2.heap);
self$2.core = RushaCore({

@@ -188,5 +269,5 @@ Int32Array: Int32Array,

};
// On initialize, resize the datastructures according
// to an optional size hint.
resize(chunkSize || 4 * 1024 * 1024);
// Iinitializethe datastructures according
// to a chunk siyze.
init(chunkSize || 64 * 1024);
var initState = function (heap, padMsgLen) {

@@ -200,22 +281,21 @@ var io = new Int32Array(heap, padMsgLen + 320, 5);

};
var initChunk = function (chunkLen, msgLen, finalize) {
var padChunkLen = chunkLen;
if (finalize) {
padChunkLen = padlen(chunkLen);
}
var padChunk = function (chunkLen, msgLen) {
var padChunkLen = padlen(chunkLen);
var view = new Int32Array(self$2.heap, 0, padChunkLen >> 2);
if (finalize) {
padZeroes(view, chunkLen);
}
if (finalize) {
padData(view, chunkLen, msgLen);
}
padZeroes(view, chunkLen);
padData(view, chunkLen, msgLen);
return padChunkLen;
};
// Write data to the heap.
var write = function (data, chunkOffset, chunkLen) {
convFn(data)(self$2.h8, self$2.h32, chunkOffset, chunkLen, 0);
};
// Initialize and call the RushaCore,
// assuming an input buffer of length len * 4.
var coreCall = function (data, chunkStart, chunkLen, msgLen, finalize) {
var padChunkLen = initChunk(chunkLen, msgLen, finalize);
var view = new Int32Array(self$2.heap, 0, padChunkLen >> 2);
conv(data, view, chunkStart, chunkLen);
var coreCall = function (data, chunkOffset, chunkLen, msgLen, finalize) {
var padChunkLen = chunkLen;
if (finalize) {
padChunkLen = padChunk(chunkLen, msgLen);
}
write(data, chunkOffset, chunkLen);
self$2.core.hash(padChunkLen, self$2.padMaxChunkLen);

@@ -236,3 +316,3 @@ };

var rawDigest = this.rawDigest = function (str) {
var msgLen = str.byteLength || str.length;
var msgLen = str.byteLength || str.length || str.size;
initState(self$2.heap, self$2.padMaxChunkLen);

@@ -248,4 +328,4 @@ var chunkOffset = 0, chunkLen = self$2.maxChunkLen, last;

// as a hex string.
this.digest = this.digestFromString = this.digestFromBuffer = this.digestFromArrayBuffer = function (str, start) {
return hex(rawDigest(str, start).buffer);
this.digest = this.digestFromString = this.digestFromBuffer = this.digestFromArrayBuffer = function (str) {
return hex(rawDigest(str).buffer);
};

@@ -252,0 +332,0 @@ }

@@ -1,24 +0,2 @@

(function(){if(typeof module!=='undefined'){module.exports=Rusha;}
if(typeof window!=='undefined'){window.Rusha=Rusha;}
if(typeof FileReaderSync!=='undefined'){var reader=new FileReaderSync(),hasher=new Rusha(4*1024*1024);self.onmessage=function onMessage(event){var hash,data=event.data.data;if(data instanceof Blob){try{data=reader.readAsBinaryString(data);}catch(e){self.postMessage({id:event.data.id,error:e.name});return;}}
hash=hasher.digest(data);self.postMessage({id:event.data.id,hash:hash});};}
function Rusha(chunkSize){'use strict';var self$2={fill:0};var padlen=function(len){for(len+=9;len%64>0;len+=1);return len;};var padZeroes=function(bin,len){for(var i=len>>2;i<bin.length;i++)
bin[i]=0;};var padData=function(bin,chunkLen,msgLen){bin[chunkLen>>2]|=128<<24-(chunkLen%4<<3);bin[((chunkLen>>2)+2&~15)+15]=msgLen<<3;};var convStr=function(str,bin,start,len){var i,m=len%4,j=len-m;for(i=0;i<j;i=i+4|0){bin[i>>2]=str.charCodeAt(start+i)<<24|str.charCodeAt(start+i+1)<<16|str.charCodeAt(start+i+2)<<8|str.charCodeAt(start+i+3);}
switch(m){case 0:bin[j>>2]|=str.charCodeAt(start+j+3);case 3:bin[j>>2]|=str.charCodeAt(start+j+2)<<8;case 2:bin[j>>2]|=str.charCodeAt(start+j+1)<<16;case 1:bin[j>>2]|=str.charCodeAt(start+j)<<24;}};var convBuf=function(buf,bin,start,len){var i,m=len%4,j=len-m;for(i=0;i<j;i=i+4|0){bin[i>>2]=buf[start+i]<<24|buf[start+i+1]<<16|buf[start+i+2]<<8|buf[start+i+3];}
switch(m){case 0:bin[j>>2]|=buf[start+j+3];case 3:bin[j>>2]|=buf[start+j+2]<<8;case 2:bin[j>>2]|=buf[start+j+1]<<16;case 1:bin[j>>2]|=buf[start+j]<<24;}};var conv=function(data,bin,start,len){if(typeof data==='string'){return convStr(data,bin,start,len);}else if(data instanceof Array||typeof global!=='undefined'&&typeof global.Buffer!=='undefined'&&global.Buffer.isBuffer(data)){return convBuf(data,bin,start,len);}else if(data instanceof ArrayBuffer){return convBuf(new Uint8Array(data),bin,start,len);}else if(data.buffer instanceof ArrayBuffer){return convBuf(new Uint8Array(data.buffer),bin,start,len);}else{throw new Error('Unsupported data type.');}};var hex=function(arrayBuffer){var i,x,hex_tab='0123456789abcdef',res=[],binarray=new Uint8Array(arrayBuffer);for(i=0;i<binarray.length;i++){x=binarray[i];res[i]=hex_tab.charAt(x>>4&15)+hex_tab.charAt(x>>0&15);}
return res.join('');};var ceilHeapSize=function(v){var p;if(v<=65536)
return 65536;if(v<16777216){for(p=1;p<v;p=p<<1);}else{for(p=16777216;p<v;p+=16777216);}
return p;};var resize=function(size){if(size%64>0){throw new Error('Chunk size must be a multiple of 128 bit');}
self$2.maxChunkLen=size;self$2.padMaxChunkLen=padlen(size);self$2.heap=new ArrayBuffer(ceilHeapSize(self$2.padMaxChunkLen+320+20));self$2.core=RushaCore({Int32Array:Int32Array,DataView:DataView},{},self$2.heap);self$2.buffer=null;};resize(chunkSize||4*1024*1024);var initState=function(heap,padMsgLen){var io=new Int32Array(heap,padMsgLen+320,5);io[0]=1732584193;io[1]=-271733879;io[2]=-1732584194;io[3]=271733878;io[4]=-1009589776;};var initChunk=function(chunkLen,msgLen,finalize){var padChunkLen=chunkLen;if(finalize){padChunkLen=padlen(chunkLen);}
var view=new Int32Array(self$2.heap,0,padChunkLen>>2);if(finalize){padZeroes(view,chunkLen);}
if(finalize){padData(view,chunkLen,msgLen);}
return padChunkLen;};var coreCall=function(data,chunkStart,chunkLen,msgLen,finalize){var padChunkLen=initChunk(chunkLen,msgLen,finalize);var view=new Int32Array(self$2.heap,0,padChunkLen>>2);conv(data,view,chunkStart,chunkLen);self$2.core.hash(padChunkLen,self$2.padMaxChunkLen);};var getRawDigest=function(heap,padMaxChunkLen){var io=new Int32Array(heap,padMaxChunkLen+320,5);var out=new Int32Array(5);var arr=new DataView(out.buffer);arr.setInt32(0,io[0],false);arr.setInt32(4,io[1],false);arr.setInt32(8,io[2],false);arr.setInt32(12,io[3],false);arr.setInt32(16,io[4],false);return out;};var rawDigest=this.rawDigest=function(str){var msgLen=str.byteLength||str.length;initState(self$2.heap,self$2.padMaxChunkLen);var chunkOffset=0,chunkLen=self$2.maxChunkLen,last;for(chunkOffset=0;msgLen>chunkOffset+chunkLen;chunkOffset+=chunkLen){coreCall(str,chunkOffset,chunkLen,msgLen,false);}
coreCall(str,chunkOffset,msgLen-chunkOffset,msgLen,true);return getRawDigest(self$2.heap,self$2.padMaxChunkLen);};this.digest=this.digestFromString=this.digestFromBuffer=this.digestFromArrayBuffer=function(str,start){return hex(rawDigest(str,start).buffer);};};function RushaCore(stdlib,foreign,heap){'use asm';var H=new stdlib.Int32Array(heap);function hash(k,x){k=k|0;x=x|0;var i=0,j=0,y0=0,z0=0,y1=0,z1=0,y2=0,z2=0,y3=0,z3=0,y4=0,z4=0,t0=0,t1=0;y0=H[x+320>>2]|0;y1=H[x+324>>2]|0;y2=H[x+328>>2]|0;y3=H[x+332>>2]|0;y4=H[x+336>>2]|0;for(i=0;(i|0)<(k|0);i=i+64|0){z0=y0;z1=y1;z2=y2;z3=y3;z4=y4;for(j=0;(j|0)<64;j=j+4|0){t1=H[i+j>>2]|0;t0=((y0<<5|y0>>>27)+(y1&y2|~y1&y3)|0)+((t1+y4|0)+1518500249|0)|0;y4=y3;y3=y2;y2=y1<<30|y1>>>2;y1=y0;y0=t0;;H[k+j>>2]=t1;}
for(j=k+64|0;(j|0)<(k+80|0);j=j+4|0){t1=(H[j-12>>2]^H[j-32>>2]^H[j-56>>2]^H[j-64>>2])<<1|(H[j-12>>2]^H[j-32>>2]^H[j-56>>2]^H[j-64>>2])>>>31;t0=((y0<<5|y0>>>27)+(y1&y2|~y1&y3)|0)+((t1+y4|0)+1518500249|0)|0;y4=y3;y3=y2;y2=y1<<30|y1>>>2;y1=y0;y0=t0;;H[j>>2]=t1;}
for(j=k+80|0;(j|0)<(k+160|0);j=j+4|0){t1=(H[j-12>>2]^H[j-32>>2]^H[j-56>>2]^H[j-64>>2])<<1|(H[j-12>>2]^H[j-32>>2]^H[j-56>>2]^H[j-64>>2])>>>31;t0=((y0<<5|y0>>>27)+(y1^y2^y3)|0)+((t1+y4|0)+1859775393|0)|0;y4=y3;y3=y2;y2=y1<<30|y1>>>2;y1=y0;y0=t0;;H[j>>2]=t1;}
for(j=k+160|0;(j|0)<(k+240|0);j=j+4|0){t1=(H[j-12>>2]^H[j-32>>2]^H[j-56>>2]^H[j-64>>2])<<1|(H[j-12>>2]^H[j-32>>2]^H[j-56>>2]^H[j-64>>2])>>>31;t0=((y0<<5|y0>>>27)+(y1&y2|y1&y3|y2&y3)|0)+((t1+y4|0)-1894007588|0)|0;y4=y3;y3=y2;y2=y1<<30|y1>>>2;y1=y0;y0=t0;;H[j>>2]=t1;}
for(j=k+240|0;(j|0)<(k+320|0);j=j+4|0){t1=(H[j-12>>2]^H[j-32>>2]^H[j-56>>2]^H[j-64>>2])<<1|(H[j-12>>2]^H[j-32>>2]^H[j-56>>2]^H[j-64>>2])>>>31;t0=((y0<<5|y0>>>27)+(y1^y2^y3)|0)+((t1+y4|0)-899497514|0)|0;y4=y3;y3=y2;y2=y1<<30|y1>>>2;y1=y0;y0=t0;;H[j>>2]=t1;}
y0=y0+z0|0;y1=y1+z1|0;y2=y2+z2|0;y3=y3+z3|0;y4=y4+z4|0;}
H[x+320>>2]=y0;H[x+324>>2]=y1;H[x+328>>2]=y2;H[x+332>>2]=y3;H[x+336>>2]=y4;}
return{hash:hash};}}());
/*! rusha 2015-01-08 */
!function(){function a(a){"use strict";var d={fill:0},f=function(a){for(a+=9;a%64>0;a+=1);return a},g=function(a,b){for(var c=b>>2;c<a.length;c++)a[c]=0},h=function(a,b,c){a[b>>2]|=128<<24-(b%4<<3),a[((b>>2)+2&-16)+15]=c<<3},i=function(a,b,c,d,e){var f,g=this,h=e%4,i=d%4,j=d-i;if(j>0)switch(h){case 0:a[e+3|0]=g.charCodeAt(c);case 1:a[e+2|0]=g.charCodeAt(c+1);case 2:a[e+1|0]=g.charCodeAt(c+2);case 3:a[0|e]=g.charCodeAt(c+3)}for(f=h;j>f;f=f+4|0)b[e+f>>2]=g.charCodeAt(c+f)<<24|g.charCodeAt(c+f+1)<<16|g.charCodeAt(c+f+2)<<8|g.charCodeAt(c+f+3);switch(i){case 3:a[e+j+1|0]=g.charCodeAt(c+j+2);case 2:a[e+j+2|0]=g.charCodeAt(c+j+1);case 1:a[e+j+3|0]=g.charCodeAt(c+j)}},j=function(a,b,c,d,e){var f,g=this,h=e%4,i=d%4,j=d-i;if(j>0)switch(h){case 0:a[e+3|0]=g[c];case 1:a[e+2|0]=g[c+1];case 2:a[e+1|0]=g[c+2];case 3:a[0|e]=g[c+3]}for(f=4-h;j>f;f=f+=4)b[e+f>>2]=g[c+f]<<24|g[c+f+1]<<16|g[c+f+2]<<8|g[c+f+3];switch(i){case 3:a[e+j+1|0]=g[c+j+2];case 2:a[e+j+2|0]=g[c+j+1];case 1:a[e+j+3|0]=g[c+j]}},k=function(a,b,d,e,f){var g,h=this,i=f%4,j=e%4,k=e-j,l=new Uint8Array(c.readAsArrayBuffer(h.slice(d,d+e)));if(k>0)switch(i){case 0:a[f+3|0]=l[0];case 1:a[f+2|0]=l[1];case 2:a[f+1|0]=l[2];case 3:a[0|f]=l[3]}for(g=4-i;k>g;g=g+=4)b[f+g>>2]=l[g]<<24|l[g+1]<<16|l[g+2]<<8|l[g+3];switch(j){case 3:a[f+k+1|0]=l[k+2];case 2:a[f+k+2|0]=l[k+1];case 1:a[f+k+3|0]=l[k]}},l=function(a){switch(e.getDataType(a)){case"string":return i.bind(a);case"array":return j.bind(a);case"buffer":return j.bind(a);case"arraybuffer":return j.bind(new Uint8Array(a));case"view":return j.bind(new Uint8Array(a.buffer,a.byteOffset,a.byteLength));case"blob":return k.bind(a)}},m=function(a){var b,c,d="0123456789abcdef",e=[],f=new Uint8Array(a);for(b=0;b<f.length;b++)c=f[b],e[b]=d.charAt(c>>4&15)+d.charAt(c>>0&15);return e.join("")},n=function(a){var b;if(65536>=a)return 65536;if(16777216>a)for(b=1;a>b;b<<=1);else for(b=16777216;a>b;b+=16777216);return b},o=function(a){if(a%64>0)throw new Error("Chunk size must be a multiple of 128 bit");d.maxChunkLen=a,d.padMaxChunkLen=f(a),d.heap=new ArrayBuffer(n(d.padMaxChunkLen+320+20)),d.h32=new Int32Array(d.heap),d.h8=new Int8Array(d.heap),d.core=b({Int32Array:Int32Array,DataView:DataView},{},d.heap),d.buffer=null};o(a||65536);var p=function(a,b){var c=new Int32Array(a,b+320,5);c[0]=1732584193,c[1]=-271733879,c[2]=-1732584194,c[3]=271733878,c[4]=-1009589776},q=function(a,b){var c=f(a),e=new Int32Array(d.heap,0,c>>2);return g(e,a),h(e,a,b),c},r=function(a,b,c){l(a)(d.h8,d.h32,b,c,0)},s=function(a,b,c,e,f){var g=c;f&&(g=q(c,e)),r(a,b,c),d.core.hash(g,d.padMaxChunkLen)},t=function(a,b){var c=new Int32Array(a,b+320,5),d=new Int32Array(5),e=new DataView(d.buffer);return e.setInt32(0,c[0],!1),e.setInt32(4,c[1],!1),e.setInt32(8,c[2],!1),e.setInt32(12,c[3],!1),e.setInt32(16,c[4],!1),d},u=this.rawDigest=function(a){var b=a.byteLength||a.length||a.size;p(d.heap,d.padMaxChunkLen);var c=0,e=d.maxChunkLen;for(c=0;b>c+e;c+=e)s(a,c,e,b,!1);return s(a,c,b-c,b,!0),t(d.heap,d.padMaxChunkLen)};this.digest=this.digestFromString=this.digestFromBuffer=this.digestFromArrayBuffer=function(a){return m(u(a).buffer)}}function b(a,b,c){"use asm";function d(a,b){a|=0,b|=0;var c=0,d=0,f=0,g=0,h=0,i=0,j=0,k=0,l=0,m=0,n=0,o=0,p=0,q=0;for(f=e[b+320>>2]|0,h=e[b+324>>2]|0,j=e[b+328>>2]|0,l=e[b+332>>2]|0,n=e[b+336>>2]|0,c=0;(c|0)<(a|0);c=c+64|0){for(g=f,i=h,k=j,m=l,o=n,d=0;(d|0)<64;d=d+4|0)q=e[c+d>>2]|0,p=((f<<5|f>>>27)+(h&j|~h&l)|0)+((q+n|0)+1518500249|0)|0,n=l,l=j,j=h<<30|h>>>2,h=f,f=p,e[a+d>>2]=q;for(d=a+64|0;(d|0)<(a+80|0);d=d+4|0)q=(e[d-12>>2]^e[d-32>>2]^e[d-56>>2]^e[d-64>>2])<<1|(e[d-12>>2]^e[d-32>>2]^e[d-56>>2]^e[d-64>>2])>>>31,p=((f<<5|f>>>27)+(h&j|~h&l)|0)+((q+n|0)+1518500249|0)|0,n=l,l=j,j=h<<30|h>>>2,h=f,f=p,e[d>>2]=q;for(d=a+80|0;(d|0)<(a+160|0);d=d+4|0)q=(e[d-12>>2]^e[d-32>>2]^e[d-56>>2]^e[d-64>>2])<<1|(e[d-12>>2]^e[d-32>>2]^e[d-56>>2]^e[d-64>>2])>>>31,p=((f<<5|f>>>27)+(h^j^l)|0)+((q+n|0)+1859775393|0)|0,n=l,l=j,j=h<<30|h>>>2,h=f,f=p,e[d>>2]=q;for(d=a+160|0;(d|0)<(a+240|0);d=d+4|0)q=(e[d-12>>2]^e[d-32>>2]^e[d-56>>2]^e[d-64>>2])<<1|(e[d-12>>2]^e[d-32>>2]^e[d-56>>2]^e[d-64>>2])>>>31,p=((f<<5|f>>>27)+(h&j|h&l|j&l)|0)+((q+n|0)-1894007588|0)|0,n=l,l=j,j=h<<30|h>>>2,h=f,f=p,e[d>>2]=q;for(d=a+240|0;(d|0)<(a+320|0);d=d+4|0)q=(e[d-12>>2]^e[d-32>>2]^e[d-56>>2]^e[d-64>>2])<<1|(e[d-12>>2]^e[d-32>>2]^e[d-56>>2]^e[d-64>>2])>>>31,p=((f<<5|f>>>27)+(h^j^l)|0)+((q+n|0)-899497514|0)|0,n=l,l=j,j=h<<30|h>>>2,h=f,f=p,e[d>>2]=q;f=f+g|0,h=h+i|0,j=j+k|0,l=l+m|0,n=n+o|0}e[b+320>>2]=f,e[b+324>>2]=h,e[b+328>>2]=j,e[b+332>>2]=l,e[b+336>>2]=n}var e=new a.Int32Array(c);return{hash:d}}if("undefined"!=typeof module&&(module.exports=a),"undefined"!=typeof window&&(window.Rusha=a),"undefined"!=typeof FileReaderSync){var c=new FileReaderSync,d=new a(4194304);self.onmessage=function(a){var b,c=a.data.data;try{b=d.digest(c),self.postMessage({id:a.data.id,hash:b})}catch(e){self.postMessage({id:a.data.id,error:e.name})}}}var e={getDataType:function(a){if("string"==typeof a)return"string";if(a instanceof Array)return"array";if("undefined"!=typeof global&&global.Buffer&&global.Buffer.isBuffer(a))return"buffer";if(a instanceof ArrayBuffer)return"arraybuffer";if(a.buffer instanceof ArrayBuffer)return"view";if(a instanceof Blob)return"blob";throw new Error("Unsupported data type.")}}}();

@@ -51,15 +51,35 @@ /*

var hash, data = event.data.data;
if (data instanceof Blob) {
try {
data = reader.readAsBinaryString(data);
} catch (e) {
self.postMessage({id: event.data.id, error: e.name});
return;
}
try {
hash = hasher.digest(data);
self.postMessage({id: event.data.id, hash: hash});
} catch (e) {
self.postMessage({id: event.data.id, error: e.name});
}
hash = hasher.digest(data);
self.postMessage({id: event.data.id, hash: hash});
};
}
var util = {
getDataType: function (data) {
if (typeof data === 'string') {
return 'string';
}
if (data instanceof Array) {
return 'array';
}
if (typeof global !== 'undefined' && global.Buffer && global.Buffer.isBuffer(data)) {
return 'buffer';
}
if (data instanceof ArrayBuffer) {
return 'arraybuffer';
}
if (data.buffer instanceof ArrayBuffer) {
return 'view';
}
if (data instanceof Blob) {
return 'blob';
}
throw new Error('Unsupported data type.');
}
};
// The Rusha object is a wrapper around the low-level RushaCore.

@@ -90,58 +110,99 @@ // It provides means of converting different inputs to the

// Convert a binary string to a big-endian Int32Array using
// four characters per slot and pad it per the sha1 spec.
// Convert a binary string and write it to the heap.
// A binary string is expected to only contain char codes < 256.
var convStr = function (str, bin, start, len) {
var i, m = len % 4, j = len - m;
for (i = 0; i < j; i = i + 4 |0) {
bin[i>>2] = str.charCodeAt(start+i) << 24 |
str.charCodeAt(start+i+1) << 16 |
str.charCodeAt(start+i+2) << 8 |
str.charCodeAt(start+i+3);
var convStr = function (H8, H32, start, len, off) {
var str = this, i, om = off % 4, lm = len % 4, j = len - lm;
if (j > 0) {
switch (om) {
case 0: H8[off+3|0] = str.charCodeAt(start);
case 1: H8[off+2|0] = str.charCodeAt(start+1);
case 2: H8[off+1|0] = str.charCodeAt(start+2);
case 3: H8[off|0] = str.charCodeAt(start+3);
}
}
switch (m) {
case 0: bin[j>>2] |= str.charCodeAt(start+j+3);
case 3: bin[j>>2] |= str.charCodeAt(start+j+2) << 8;
case 2: bin[j>>2] |= str.charCodeAt(start+j+1) << 16;
case 1: bin[j>>2] |= str.charCodeAt(start+j) << 24;
for (i = om; i < j; i = i + 4 |0) {
H32[off+i>>2] = str.charCodeAt(start+i) << 24 |
str.charCodeAt(start+i+1) << 16 |
str.charCodeAt(start+i+2) << 8 |
str.charCodeAt(start+i+3);
}
switch (lm) {
case 3: H8[off+j+1|0] = str.charCodeAt(start+j+2);
case 2: H8[off+j+2|0] = str.charCodeAt(start+j+1);
case 1: H8[off+j+3|0] = str.charCodeAt(start+j);
}
};
// Convert a buffer or array to a big-endian Int32Array using
// four elements per slot and pad it per the sha1 spec.
// Convert a buffer or array and write it to the heap.
// The buffer or array is expected to only contain elements < 256.
var convBuf = function (buf, bin, start, len) {
var i, m = len % 4, j = len - m;
for (i = 0; i < j; i = i + 4 |0) {
bin[i>>2] = buf[start+i] << 24 |
buf[start+i+1] << 16 |
buf[start+i+2] << 8 |
buf[start+i+3];
var convBuf = function (H8, H32, start, len, off) {
var buf = this, i, om = off % 4, lm = len % 4, j = len - lm;
if (j > 0) {
switch (om) {
case 0: H8[off+3|0] = buf[start];
case 1: H8[off+2|0] = buf[start+1];
case 2: H8[off+1|0] = buf[start+2];
case 3: H8[off|0] = buf[start+3];
}
}
switch (m) {
case 0: bin[j>>2] |= buf[start+j+3];
case 3: bin[j>>2] |= buf[start+j+2] << 8;
case 2: bin[j>>2] |= buf[start+j+1] << 16;
case 1: bin[j>>2] |= buf[start+j] << 24;
for (i = 4 - om; i < j; i = i += 4 |0) {
H32[off+i>>2] = buf[start+i] << 24 |
buf[start+i+1] << 16 |
buf[start+i+2] << 8 |
buf[start+i+3];
}
switch (lm) {
case 3: H8[off+j+1|0] = buf[start+j+2];
case 2: H8[off+j+2|0] = buf[start+j+1];
case 1: H8[off+j+3|0] = buf[start+j];
}
};
// Convert general data to a big-endian Int32Array written on the
// heap.
var conv = function (data, bin, start, len) {
if (typeof data === 'string') {
return convStr(data, bin, start, len);
} else if (data instanceof Array || (typeof global !== 'undefined' &&
typeof global.Buffer !== 'undefined' &&
global.Buffer.isBuffer(data))) {
return convBuf(data, bin, start, len);
} else if (data instanceof ArrayBuffer) {
return convBuf(new Uint8Array(data), bin, start, len);
} else if (data.buffer instanceof ArrayBuffer) {
return convBuf(new Uint8Array(data.buffer), bin, start, len);
} else {
throw new Error('Unsupported data type.');
var convBlob = function (H8, H32, start, len, off) {
var blob = this, i, om = off % 4, lm = len % 4, j = len - lm;
var buf = new Uint8Array(
reader.readAsArrayBuffer(blob.slice(start, start+len))
);
if (j > 0) {
switch (om) {
case 0: H8[off + 3 | 0] = buf[0];
case 1: H8[off + 2 | 0] = buf[1];
case 2: H8[off + 1 | 0] = buf[2];
case 3: H8[off | 0] = buf[3];
}
}
for (i = 4 - om; i < j; i = i += 4 | 0) {
H32[off + i >> 2] = buf[i] << 24 |
buf[i + 1] << 16 |
buf[i + 2] << 8 |
buf[i + 3];
}
switch (lm) {
case 3: H8[off + j + 1 | 0] = buf[j + 2];
case 2: H8[off + j + 2 | 0] = buf[j + 1];
case 1: H8[off + j + 3 | 0] = buf[j];
}
};
var convFn = function (data) {
switch (util.getDataType(data)) {
case 'string': return convStr.bind(data);
case 'array': return convBuf.bind(data);
case 'buffer': return convBuf.bind(data);
case 'arraybuffer': return convBuf.bind(new Uint8Array(data));
case 'view': return convBuf.bind(new Uint8Array(data.buffer, data.byteOffset, data.byteLength));
case 'blob': return convBlob.bind(data);
}
};
var slice = function (data, offset) {
switch (util.getDataType(data)) {
case 'string': return data.slice(offset);
case 'array': return data.slice(offset);
case 'buffer': return data.slice(offset);
case 'arraybuffer': return data.slice(offset);
case 'view': return data.buffer.slice(offset);
}
};
// Convert an ArrayBuffer into its hexadecimal string representation.

@@ -177,4 +238,4 @@ var hex = function (arrayBuffer) {

// Resize the internal data structures to a new capacity.
var resize = function (size) {
// Initialize the internal data structures to a new capacity.
var init = function (size) {
if (size % 64 > 0) {

@@ -190,2 +251,4 @@ throw new Error('Chunk size must be a multiple of 128 bit');

self.heap = new ArrayBuffer(ceilHeapSize(self.padMaxChunkLen + 320 + 20));
self.h32 = new Int32Array(self.heap);
self.h8 = new Int8Array(self.heap);
self.core = RushaCore({Int32Array: Int32Array, DataView: DataView}, {}, self.heap);

@@ -195,5 +258,5 @@ self.buffer = null;

// On initialize, resize the datastructures according
// to an optional size hint.
resize(chunkSize || 4 * 1024 * 1024);
// Iinitializethe datastructures according
// to a chunk siyze.
init(chunkSize || 64 * 1024);

@@ -209,23 +272,23 @@ var initState = function (heap, padMsgLen) {

var initChunk = function (chunkLen, msgLen, finalize) {
var padChunkLen = chunkLen;
if (finalize) {
padChunkLen = padlen(chunkLen);
}
var padChunk = function (chunkLen, msgLen) {
var padChunkLen = padlen(chunkLen);
var view = new Int32Array(self.heap, 0, padChunkLen >> 2);
if (finalize) {
padZeroes(view, chunkLen);
}
if (finalize) {
padData(view, chunkLen, msgLen);
}
padZeroes(view, chunkLen);
padData(view, chunkLen, msgLen);
return padChunkLen;
};
// Write data to the heap.
var write = function (data, chunkOffset, chunkLen) {
convFn(data)(self.h8, self.h32, chunkOffset, chunkLen, 0);
};
// Initialize and call the RushaCore,
// assuming an input buffer of length len * 4.
var coreCall = function (data, chunkStart, chunkLen, msgLen, finalize) {
var padChunkLen = initChunk(chunkLen, msgLen, finalize);
var view = new Int32Array(self.heap, 0, padChunkLen >> 2);
conv(data, view, chunkStart, chunkLen);
var coreCall = function (data, chunkOffset, chunkLen, msgLen, finalize) {
var padChunkLen = chunkLen;
if (finalize) {
padChunkLen = padChunk(chunkLen, msgLen);
}
write(data, chunkOffset, chunkLen);
self.core.hash(padChunkLen, self.padMaxChunkLen);

@@ -248,3 +311,3 @@ };

var rawDigest = this.rawDigest = function (str) {
var msgLen = (str.byteLength || str.length);
var msgLen = (str.byteLength || str.length) || str.size;
initState(self.heap, self.padMaxChunkLen);

@@ -263,4 +326,4 @@ var chunkOffset = 0, chunkLen = self.maxChunkLen, last;

this.digestFromBuffer = this.digestFromArrayBuffer =
function (str, start) {
return hex(rawDigest(str, start).buffer);
function (str) {
return hex(rawDigest(str).buffer);
};

@@ -267,0 +330,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