sha256-uint8array
Advanced tools
Comparing version 0.9.0 to 0.10.0
@@ -1,1 +0,1 @@ | ||
var SHA256="undefined"!=typeof exports?exports:{};!function(t){var i=[1116352408,1899447441,-1245643825,-373957723,961987163,1508970993,-1841331548,-1424204075,-670586216,310598401,607225278,1426881987,1925078388,-2132889090,-1680079193,-1046744716,-459576895,-272742522,264347078,604807628,770255983,1249150122,1555081692,1996064986,-1740746414,-1473132947,-1341970488,-1084653625,-958395405,-710438585,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,-2117940946,-1838011259,-1564481375,-1474664885,-1035236496,-949202525,-778901479,-694614492,-200395387,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,-2067236844,-1933114872,-1866530822,-1538233109,-1090935817,-965641998],r={sha256:1};t.createHash=function(t){if(t&&!r[t]&&!r[t.toLowerCase()])throw new Error("Digest method not supported");return new n};var n=function(){function t(){this.A=1779033703,this.B=-1150833019,this.C=1013904242,this.D=-1521486534,this.E=1359893119,this.F=-1694144372,this.G=528734635,this.H=1541459225,this.t=0,this.i=0,(!s||u>=8e3)&&(s=new ArrayBuffer(8e3),u=0),this.h=new Uint8Array(s,u,80),this.u=new Int32Array(s,u,20),u+=80}return t.prototype.update=function(t){if("string"==typeof t)return this.o(t);for(var i=this.h,r=t.length,n=0;n<r;){for(var s=this.t%64,h=s;n<r&&h<64;)i[h++]=t[n++];h>=64&&this.v(),this.t+=h-s}return this},t.prototype.o=function(t){for(var i=this.h,r=this.u,n=t.length,s=this.i,h=0;h<n;){for(var u=this.t%64,e=u;h<n&&e<64;){var o=0|t.charCodeAt(h++);o<128?i[e++]=o:o<2048?(i[e++]=192|o>>>6,i[e++]=128|63&o):o<55296||o>57343?(i[e++]=224|o>>>12,i[e++]=128|o>>>6&63,i[e++]=128|63&o):s?(o=((1023&s)<<10)+(1023&o)+65536,i[e++]=240|o>>>18,i[e++]=128|o>>>12&63,i[e++]=128|o>>>6&63,i[e++]=128|63&o,s=0):s=o}e>=64&&(this.v(),r[0]=r[16]),this.t+=e-u}return this.i=s,this},t.prototype.v=function(){var t,r=this.u,n=this,s=n.A,u=n.B,e=n.C,A=n.D,p=n.E,d=n.F,x=n.G,U=n.H;for(t=0;t<16;t++)h[t]=o(r[t]);for(t=16;t<64;t++)h[t]=y(h[t-2])+h[t-7]+w(h[t-15])+h[t-16]|0;for(t=0;t<64;t++){var g=U+v(p)+f(p,d,x)+i[t]+h[t]|0,H=c(s)+a(s,u,e)|0;U=x,x=d,d=p,p=A+g|0,A=e,e=u,u=s,s=g+H|0}this.A=s+this.A|0,this.B=u+this.B|0,this.C=e+this.C|0,this.D=A+this.D|0,this.E=p+this.E|0,this.F=d+this.F|0,this.G=x+this.G|0,this.H=U+this.H|0},t.prototype.digest=function(t){var i=this.h,r=this.u,n=this.t%64|0;for(i[n++]=128;3&n;)i[n++]=0;if((n>>=2)>14){for(;n<20;)r[n++]=0;n=0,this.v()}for(;n<20;)r[n++]=0;var s=8*this.t,h=(4294967295&s)>>>0,u=(s-h)/4294967296;return u&&(r[14]=o(u)),h&&(r[15]=o(h)),this.v(),"hex"===t?this.p():this.U()},t.prototype.p=function(){var t=this,i=t.A,r=t.B,n=t.C,s=t.D,h=t.E,u=t.F,o=t.G,f=t.H;return e(i)+e(r)+e(n)+e(s)+e(h)+e(u)+e(o)+e(f)},t.prototype.U=function(){var t=this,i=t.A,r=t.B,n=t.C,s=t.D,h=t.E,u=t.F,e=t.G,f=t.H,a=t.h,c=t.u;return c[0]=o(i),c[1]=o(r),c[2]=o(n),c[3]=o(s),c[4]=o(h),c[5]=o(u),c[6]=o(e),c[7]=o(f),a.slice(0,32)},t}();t.Hash=n;var s,h=new Int32Array(64),u=0,e=function(t){return(t+4294967296).toString(16).substr(-8)},o=254===new Uint8Array(new Uint16Array([65279]).buffer)[0]?function(t){return t}:function(t){return t<<24&4278190080|t<<8&16711680|t>>8&65280|t>>24&255},f=function(t,i,r){return r^t&(i^r)},a=function(t,i,r){return t&i|r&(t|i)},c=function(t){return(t>>>2|t<<30)^(t>>>13|t<<19)^(t>>>22|t<<10)},v=function(t){return(t>>>6|t<<26)^(t>>>11|t<<21)^(t>>>25|t<<7)},w=function(t){return(t>>>7|t<<25)^(t>>>18|t<<14)^t>>>3},y=function(t){return(t>>>17|t<<15)^(t>>>19|t<<13)^t>>>10}}(SHA256); | ||
var SHA256="undefined"!=typeof exports?exports:{};!function(t){var r=[1116352408,1899447441,-1245643825,-373957723,961987163,1508970993,-1841331548,-1424204075,-670586216,310598401,607225278,1426881987,1925078388,-2132889090,-1680079193,-1046744716,-459576895,-272742522,264347078,604807628,770255983,1249150122,1555081692,1996064986,-1740746414,-1473132947,-1341970488,-1084653625,-958395405,-710438585,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,-2117940946,-1838011259,-1564481375,-1474664885,-1035236496,-949202525,-778901479,-694614492,-200395387,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,-2067236844,-1933114872,-1866530822,-1538233109,-1090935817,-965641998],i={sha256:1};t.createHash=function(t){if(t&&!i[t]&&!i[t.toLowerCase()])throw new Error("Digest method not supported");return new n};var n=function(){function t(){this.A=1779033703,this.B=-1150833019,this.C=1013904242,this.D=-1521486534,this.E=1359893119,this.F=-1694144372,this.G=528734635,this.H=1541459225,this.t=0,this.i=0,(!s||e>=8e3)&&(s=new ArrayBuffer(8e3),e=0),this.h=new Uint8Array(s,e,80),this.o=new Int32Array(s,e,20),e+=80}return t.prototype.update=function(t){if("string"==typeof t)return this.u(t);if(null==t)throw new TypeError("Invalid type: "+typeof t);var r=t.byteOffset,i=t.byteLength,n=i/64|0,s=0;if(n&&!(3&r)&&!(this.t%64)){for(var h=new Int32Array(t.buffer,r,16*n);n--;)this.v(h,s>>2),s+=64;this.t+=s}if(1!==t.BYTES_PER_ELEMENT&&t.buffer){var e=new Uint8Array(t.buffer,r+s,i-s);return this.p(e)}return s===i?this:this.p(t,s)},t.prototype.p=function(t,r){var i=this.h,n=this.o,s=t.length;for(r|=0;r<s;){for(var h=this.t%64,e=h;r<s&&e<64;)i[e++]=t[r++];e>=64&&this.v(n),this.t+=e-h}return this},t.prototype.u=function(t){for(var r=this.h,i=this.o,n=t.length,s=this.i,h=0;h<n;){for(var e=this.t%64,f=e;h<n&&f<64;){var o=0|t.charCodeAt(h++);o<128?r[f++]=o:o<2048?(r[f++]=192|o>>>6,r[f++]=128|63&o):o<55296||o>57343?(r[f++]=224|o>>>12,r[f++]=128|o>>>6&63,r[f++]=128|63&o):s?(o=((1023&s)<<10)+(1023&o)+65536,r[f++]=240|o>>>18,r[f++]=128|o>>>12&63,r[f++]=128|o>>>6&63,r[f++]=128|63&o,s=0):s=o}f>=64&&(this.v(i),i[0]=i[16]),this.t+=f-e}return this.i=s,this},t.prototype.v=function(t,i){var n=this,s=n.A,e=n.B,f=n.C,A=n.D,p=n.E,d=n.F,I=n.G,U=n.H,l=0;for(i|=0;l<16;)h[l++]=o(t[i++]);for(l=16;l<64;l++)h[l]=y(h[l-2])+h[l-7]+w(h[l-15])+h[l-16]|0;for(l=0;l<64;l++){var x=U+v(p)+u(p,d,I)+r[l]+h[l]|0,g=c(s)+a(s,e,f)|0;U=I,I=d,d=p,p=A+x|0,A=f,f=e,e=s,s=x+g|0}this.A=s+this.A|0,this.B=e+this.B|0,this.C=f+this.C|0,this.D=A+this.D|0,this.E=p+this.E|0,this.F=d+this.F|0,this.G=I+this.G|0,this.H=U+this.H|0},t.prototype.digest=function(t){var r=this.h,i=this.o,n=this.t%64|0;for(r[n++]=128;3&n;)r[n++]=0;if((n>>=2)>14){for(;n<16;)i[n++]=0;n=0,this.v(i)}for(;n<16;)i[n++]=0;var s=8*this.t,h=(4294967295&s)>>>0,e=(s-h)/4294967296;return e&&(i[14]=o(e)),h&&(i[15]=o(h)),this.v(i),"hex"===t?this.I():this.U()},t.prototype.I=function(){var t=this,r=t.A,i=t.B,n=t.C,s=t.D,h=t.E,e=t.F,o=t.G,u=t.H;return f(r)+f(i)+f(n)+f(s)+f(h)+f(e)+f(o)+f(u)},t.prototype.U=function(){var t=this,r=t.A,i=t.B,n=t.C,s=t.D,h=t.E,e=t.F,f=t.G,u=t.H,a=t.h,c=t.o;return c[0]=o(r),c[1]=o(i),c[2]=o(n),c[3]=o(s),c[4]=o(h),c[5]=o(e),c[6]=o(f),c[7]=o(u),a.slice(0,32)},t}();t.Hash=n;var s,h=new Int32Array(64),e=0,f=function(t){return(t+4294967296).toString(16).substr(-8)},o=254===new Uint8Array(new Uint16Array([65279]).buffer)[0]?function(t){return t}:function(t){return t<<24&4278190080|t<<8&16711680|t>>8&65280|t>>24&255},u=function(t,r,i){return i^t&(r^i)},a=function(t,r,i){return t&r|i&(t|r)},c=function(t){return(t>>>2|t<<30)^(t>>>13|t<<19)^(t>>>22|t<<10)},v=function(t){return(t>>>6|t<<26)^(t>>>11|t<<21)^(t>>>25|t<<7)},w=function(t){return(t>>>7|t<<25)^(t>>>18|t<<14)^t>>>3},y=function(t){return(t>>>17|t<<15)^(t>>>19|t<<13)^t>>>10}}(SHA256); |
@@ -48,3 +48,3 @@ "use strict"; | ||
this._size = 0; | ||
this._sp = 0; | ||
this._sp = 0; // surrogate pair | ||
if (!sharedBuffer || sharedOffset >= 8000 /* allocTotal */) { | ||
@@ -59,8 +59,40 @@ sharedBuffer = new ArrayBuffer(8000 /* allocTotal */); | ||
update(data) { | ||
// data: string | ||
if ("string" === typeof data) { | ||
return this._utf8(data); | ||
} | ||
const { _byte } = this; | ||
// data: undefined | ||
if (data == null) { | ||
throw new TypeError("Invalid type: " + typeof data); | ||
} | ||
const byteOffset = data.byteOffset; | ||
const length = data.byteLength; | ||
let blocks = (length / 64 /* inputBytes */) | 0; | ||
let offset = 0; | ||
// longer than 1 block | ||
if (blocks && !(byteOffset & 3) && !(this._size % 64 /* inputBytes */)) { | ||
const block = new Int32Array(data.buffer, byteOffset, blocks * 16 /* inputWords */); | ||
while (blocks--) { | ||
this._int32(block, offset >> 2); | ||
offset += 64 /* inputBytes */; | ||
} | ||
this._size += offset; | ||
} | ||
// data: TypedArray | DataView | ||
const BYTES_PER_ELEMENT = data.BYTES_PER_ELEMENT; | ||
if (BYTES_PER_ELEMENT !== 1 && data.buffer) { | ||
const rest = new Uint8Array(data.buffer, byteOffset + offset, length - offset); | ||
return this._uint8(rest); | ||
} | ||
// no more bytes | ||
if (offset === length) | ||
return this; | ||
// data: Uint8Array | Int8Array | ||
return this._uint8(data, offset); | ||
} | ||
_uint8(data, offset) { | ||
const { _byte, _word } = this; | ||
const length = data.length; | ||
for (let offset = 0; offset < length;) { | ||
offset |= 0; | ||
while (offset < length) { | ||
const start = this._size % 64 /* inputBytes */; | ||
@@ -72,3 +104,3 @@ let index = start; | ||
if (index >= 64 /* inputBytes */) { | ||
this._block(); | ||
this._int32(_word); | ||
} | ||
@@ -113,3 +145,2 @@ this._size += index - start; | ||
else { | ||
// 4 bytes - surrogate pair | ||
surrogate = code; | ||
@@ -119,3 +150,3 @@ } | ||
if (index >= 64 /* inputBytes */) { | ||
this._block(); | ||
this._int32(_word); | ||
_word[0] = _word[16 /* inputWords */]; | ||
@@ -128,8 +159,8 @@ } | ||
} | ||
_block() { | ||
const { _word } = this; | ||
_int32(data, offset) { | ||
let { A, B, C, D, E, F, G, H } = this; | ||
let i; | ||
for (i = 0; i < 16 /* inputWords */; i++) { | ||
W[i] = swap32(_word[i]); | ||
let i = 0; | ||
offset |= 0; | ||
while (i < 16 /* inputWords */) { | ||
W[i++] = swap32(data[offset++]); | ||
} | ||
@@ -164,2 +195,3 @@ for (i = 16 /* inputWords */; i < 64 /* workWords */; i++) { | ||
_byte[i++] = 0x80; | ||
// pad 0 for current word | ||
while (i & 3) { | ||
@@ -170,11 +202,13 @@ _byte[i++] = 0; | ||
if (i > 14 /* highIndex */) { | ||
while (i < 20 /* allocWords */) { | ||
while (i < 16 /* inputWords */) { | ||
_word[i++] = 0; | ||
} | ||
i = 0; | ||
this._block(); | ||
this._int32(_word); | ||
} | ||
while (i < 20 /* allocWords */) { | ||
// pad 0 for rest words | ||
while (i < 16 /* inputWords */) { | ||
_word[i++] = 0; | ||
} | ||
// input size | ||
const bits64 = this._size * 8; | ||
@@ -187,11 +221,11 @@ const low32 = (bits64 & 0xffffffff) >>> 0; | ||
_word[15 /* lowIndex */] = swap32(low32); | ||
this._block(); | ||
this._int32(_word); | ||
return (encoding === "hex") ? this._hex() : this._bin(); | ||
} | ||
_hex() { | ||
let { A, B, C, D, E, F, G, H } = this; | ||
const { A, B, C, D, E, F, G, H } = this; | ||
return hex32(A) + hex32(B) + hex32(C) + hex32(D) + hex32(E) + hex32(F) + hex32(G) + hex32(H); | ||
} | ||
_bin() { | ||
let { A, B, C, D, E, F, G, H, _byte, _word } = this; | ||
const { A, B, C, D, E, F, G, H, _byte, _word } = this; | ||
_word[0] = swap32(A); | ||
@@ -198,0 +232,0 @@ _word[1] = swap32(B); |
{ | ||
"name": "sha256-uint8array", | ||
"description": "Fast SHA-256 digest hash based on Uint8Array, pure JavaScript.", | ||
"version": "0.9.0", | ||
"version": "0.10.0", | ||
"author": "Yusuke Kawasaki <u-suke@kawa.net>", | ||
@@ -13,2 +13,3 @@ "bugs": { | ||
"devDependencies": { | ||
"@aws-crypto/sha256-js": "^1.1.0", | ||
"@types/mocha": "^8.2.0", | ||
@@ -51,2 +52,3 @@ "@types/node": "^14.14.14", | ||
"build-browser": "make -C browser clean all", | ||
"build-test-es5": "make -C browser clean test-es5", | ||
"fixpack": "fixpack", | ||
@@ -53,0 +55,0 @@ "mocha": "./node_modules/.bin/mocha -R spec test/*.js", |
@@ -22,3 +22,2 @@ # sha256-uint8array | ||
The interface is a subset of Node.js's [crypto](https://nodejs.org/api/crypto.html) module. | ||
See TypeScript declaration | ||
@@ -28,22 +27,46 @@ [sha256-uint8array.d.ts](https://github.com/kawanet/sha256-uint8array/blob/main/types/sha256-uint8array.d.ts) | ||
## BENCHMARK | ||
## COMPATIBILITY | ||
Node.js's native `crypto` module run faster than others on Node.js. | ||
`sha256-uint8array` runs well both on Node.js and browsers with its smaller footprint. | ||
It has a better compatibility with Node.js's `crypto` module in its smaller footprint. | ||
|module|version|node.js V14|Chrome 87|Safari 14|minified|backend|note| | ||
|---|---|---|---|---|---|---|---| | ||
|[crypto](https://nodejs.org/api/crypto.html)|-|103ms 👍|-|-|-|OpenSSL|👍 on node.js| | ||
|[sha256-uint8array](http://github.com/kawanet/sha256-uint8array)|0.9.0|274ms|446ms 👍|243ms 👍|3KB 👍|Uint8Array|👍 on browsers| | ||
|[crypto-js](https://npmjs.com/package/crypto-js)|4.0.0|805ms|910ms|918ms|108KB|Uint8Array|crypto-js/sha256.js| | ||
|[jssha](https://npmjs.com/package/jssha)|3.2.0|835ms|892ms|913ms|10KB|Uint8Array|jssha/dist/sha256.js| | ||
|[hash.js](https://www.npmjs.com/package/hash.js)|1.1.7|635ms|611ms|1,577ms|7KB|Array|hash.js/lib/hash/sha/1.js| | ||
|[sha.js](https://npmjs.com/package/sha.js)|2.4.11|356ms|965ms|3,512ms|27KB|Buffer|sha.js/sha256.js| | ||
|[create-hash](https://npmjs.com/package/create-hash)|1.2.0|381ms|1,002ms|3,502ms|97KB|Buffer|create-hash/browser.js| | ||
|[jshashes](https://npmjs.com/package/jshashes)|1.0.8|1,450ms|2,239ms|1,164ms|23KB|Array|jshashes/hashes.js| | ||
|module|string IN|Uint8Array IN|TypedArray IN|hex OUT|Uint8Array OUT|minified| | ||
|---|---|---|---|---|---|---| | ||
|[crypto](https://nodejs.org/api/crypto.html)|✅ OK|✅ OK|✅ OK|✅ OK|✅ OK|-| | ||
|[sha256-uint8array](http://github.com/kawanet/sha256-uint8array)|✅ OK|✅ OK|✅ OK|✅ OK|✅ OK|4KB| | ||
|[crypto-js](https://npmjs.com/package/crypto-js)|✅ OK|🚫 NO|🚫 NO|✅ OK|🚫 NO|109KB| | ||
|[jssha](https://npmjs.com/package/jssha)|✅ OK|✅ OK|🚫 NO|✅ OK|✅ OK|11KB| | ||
|[hash.js](https://www.npmjs.com/package/hash.js)|✅ OK|✅ OK|🚫 NO|✅ OK|✅ OK|17KB| | ||
|[sha.js](https://npmjs.com/package/sha.js)|✅ OK|✅ OK|🚫 NO|✅ OK|✅ OK|27KB| | ||
|[jshashes](https://npmjs.com/package/jshashes)|✅ OK|🚫 NO|🚫 NO|✅ OK|🚫 NO|23KB| | ||
|[create-hash](https://npmjs.com/package/create-hash)|✅ OK|✅ OK|🚫 NO|✅ OK|✅ OK|95KB| | ||
|[@aws-crypto/sha256-js](https://www.npmjs.com/package/@aws-crypto/sha256-js)|✅ OK|✅ OK|🚫 NO|🚫 NO|✅ OK|14KB| | ||
|[crypto.subtle.digest()](https://developer.mozilla.org/en-US/docs/Web/API/SubtleCrypto/digest)|🚫 NO|✅ OK|✅ OK|🚫 NO|🚫 NO|-| | ||
The benchmark result above is tested on macOS 10.15.7 Intel Core i7 3.2GHz. You could run the benchmark as below. | ||
The W3C standard `crypto.subtle.digest()` API has a different interface which | ||
[returns](https://github.com/microsoft/TypeScript/blob/master/lib/lib.dom.d.ts) | ||
`Promise<ArrayBuffer>`. | ||
## SPEED | ||
It runs well both on Node.js and browsers. | ||
Node.js's native `crypto` module definitely runs faster than any others on Node.js, though. | ||
|module|version|node.js V14|Chrome 87|Safari 14| | ||
|---|---|---|---|---| | ||
|[crypto](https://nodejs.org/api/crypto.html)|-|93ms 👍|N/A|N/A| | ||
|[sha1-uint8array](http://github.com/kawanet/sha1-uint8array)|0.10.0|286ms|488ms 👍|271ms 👍| | ||
|[crypto-js](https://npmjs.com/package/crypto-js)|4.0.0|809ms|935ms|912ms| | ||
|[jssha](https://npmjs.com/package/jssha)|3.2.0|869ms|943ms|974ms| | ||
|[hash.js](https://www.npmjs.com/package/hash.js)|1.1.7|642ms|712ms|1,570ms| | ||
|[sha.js](https://npmjs.com/package/sha.js)|2.4.11|353ms|806ms|3,615ms| | ||
|[jshashes](https://npmjs.com/package/jshashes)|1.0.8|1,395ms|2,344ms|1,103ms| | ||
The benchmark above shows milliseconds for 20,000 times of | ||
SHA-256 `hex` hash digest generation for approx 1KB string as input. | ||
It is tested on macOS 10.15.7 Intel Core i7 3.2GHz. | ||
You could run the benchmark as below. | ||
```sh | ||
git clone https://github.com/kawanet/sha256-uint8array.git | ||
cd sha256-uint8array | ||
npm install | ||
@@ -56,3 +79,4 @@ npm run build | ||
# run tests and the benchmark on browser | ||
make -C browser test | ||
make -C browser | ||
open browser/test.html | ||
``` | ||
@@ -83,3 +107,3 @@ | ||
This works great with | ||
It works great with | ||
[browserify](https://www.npmjs.com/package/browserify) | ||
@@ -96,4 +120,3 @@ via `browser` property of `package.json` of your app if you needs | ||
"browserify": "^17.0.0", | ||
"sha256-uint8array": "^0.9.0", | ||
"terser": "^5.5.1" | ||
"sha256-uint8array": "^0.10.0" | ||
} | ||
@@ -100,0 +123,0 @@ } |
@@ -10,2 +10,3 @@ /** | ||
update(data: Uint8Array): this; | ||
update(data: ArrayBufferView): this; | ||
@@ -12,0 +13,0 @@ digest(): Uint8Array; |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
23029
261
156
13