Files
js-sha1/build/sha1.min.mjs
T
Yi-Cyuan ChenandClaude Opus 5.5 4ad1b7a3b2 ### Fixed
- Incorrect hash for `DataView` input, and for `Int8Array` with negative values in browsers.
- Throws `ArrayBuffer is not defined` in browsers without `ArrayBuffer`.

### Changed
- Multi-byte typed arrays such as `Uint16Array`, `Uint32Array` and `Float32Array` are now hashed by their underlying bytes, the same as Node.js `crypto` and Web Crypto. Previously each element was treated as one byte.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-18 13:31:57 +08:00

10 lines
7.2 KiB
JavaScript

function t(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var r,i={exports:{}};
/*
* [js-sha1]{@link https://github.com/emn178/js-sha1}
*
* @version 0.9.0
* @author Chen, Yi-Cyuan [emn178@gmail.com]
* @copyright Chen, Yi-Cyuan 2014-2026
* @license MIT
*/r=i,function(){var t="input is invalid type",i="object"==typeof window,e=i?window:{};e.JS_SHA1_NO_WINDOW&&(i=!1),!i&&"object"==typeof self&&(e=self);var h=!e.JS_SHA1_NO_COMMON_JS&&r.exports,s=!e.JS_SHA1_NO_ARRAY_BUFFER&&"undefined"!=typeof ArrayBuffer,n="0123456789abcdef".split(""),a=[-2147483648,8388608,32768,128],o=[24,16,8,0],f=["hex","array","digest","arrayBuffer"],u=[],c=Array.isArray;!e.JS_SHA1_NO_NODE_JS&&c||(c=function(t){return"[object Array]"===Object.prototype.toString.call(t)});var l=s?ArrayBuffer.isView:null;!s||!e.JS_SHA1_NO_ARRAY_BUFFER_IS_VIEW&&l||(l=function(t){return"object"==typeof t&&t.buffer&&t.buffer.constructor===ArrayBuffer});var y=function(r){var i=typeof r;if("string"===i)return[r,!0];if("object"!==i||null===r)throw new Error(t);if(s&&r.constructor===ArrayBuffer)return[new Uint8Array(r),!1];if(c(r))return[r,!1];if(!s||!l(r))throw new Error(t);return[new Uint8Array(r.buffer,r.byteOffset,r.byteLength),!1]},p=function(t){return function(r){return new b(!0).update(r)[t]()}},d=function(t){return function(r,i){return new v(r,!0).update(i)[t]()}};function b(t){t?(u[0]=u[16]=u[1]=u[2]=u[3]=u[4]=u[5]=u[6]=u[7]=u[8]=u[9]=u[10]=u[11]=u[12]=u[13]=u[14]=u[15]=0,this.blocks=u):this.blocks=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],this.h0=1732584193,this.h1=4023233417,this.h2=2562383102,this.h3=271733878,this.h4=3285377520,this.block=this.start=this.bytes=this.hBytes=0,this.finalized=this.hashed=!1,this.first=!0}function v(t,r){var i,e=y(t);if(t=e[0],e[1]){var h,s,n=[],a=t.length,o=0;for(i=0;i<a;++i)(h=t.charCodeAt(i))<128?n[o++]=h:h<2048?(n[o++]=192|h>>>6,n[o++]=128|63&h):h<55296||h>=57344?(n[o++]=224|h>>>12,n[o++]=128|h>>>6&63,n[o++]=128|63&h):h<56320&&(s=t.charCodeAt(i+1))>=56320&&s<57344?(++i,h=65536+((1023&h)<<10|1023&s),n[o++]=240|h>>>18,n[o++]=128|h>>>12&63,n[o++]=128|h>>>6&63,n[o++]=128|63&h):(n[o++]=239,n[o++]=191,n[o++]=189);t=n}t.length>64&&(t=new b(!0).update(t).array());var f=[],u=[];for(i=0;i<64;++i){var c=t[i]||0;f[i]=92^c,u[i]=54^c}b.call(this,r),this.update(u),this.oKeyPad=f,this.inner=!0,this.sharedMemory=r}b.prototype.update=function(t){if(this.finalized)throw new Error("finalize already called");var r=y(t);t=r[0];for(var i,e,h,s=r[1],n=0,a=t.length||0,f=this.blocks;n<a;){if(this.hashed&&(this.hashed=!1,f[0]=this.block,this.block=f[16]=f[1]=f[2]=f[3]=f[4]=f[5]=f[6]=f[7]=f[8]=f[9]=f[10]=f[11]=f[12]=f[13]=f[14]=f[15]=0),s)for(h=this.start;n<a&&h<64;++n)(i=t.charCodeAt(n))<128?f[h>>>2]|=i<<o[3&h++]:i<2048?(f[h>>>2]|=(192|i>>>6)<<o[3&h++],f[h>>>2]|=(128|63&i)<<o[3&h++]):i<55296||i>=57344?(f[h>>>2]|=(224|i>>>12)<<o[3&h++],f[h>>>2]|=(128|i>>>6&63)<<o[3&h++],f[h>>>2]|=(128|63&i)<<o[3&h++]):i<56320&&(e=t.charCodeAt(n+1))>=56320&&e<57344?(++n,i=65536+((1023&i)<<10|1023&e),f[h>>>2]|=(240|i>>>18)<<o[3&h++],f[h>>>2]|=(128|i>>>12&63)<<o[3&h++],f[h>>>2]|=(128|i>>>6&63)<<o[3&h++],f[h>>>2]|=(128|63&i)<<o[3&h++]):(f[h>>>2]|=239<<o[3&h++],f[h>>>2]|=191<<o[3&h++],f[h>>>2]|=189<<o[3&h++]);else for(h=this.start;n<a&&h<64;++n)f[h>>>2]|=t[n]<<o[3&h++];this.lastByteIndex=h,this.bytes+=h-this.start,h>=64?(this.block=f[16],this.start=h-64,this.hash(),this.hashed=!0):this.start=h}return this.bytes>4294967295&&(this.hBytes+=this.bytes/4294967296|0,this.bytes=this.bytes%4294967296),this},b.prototype.finalize=function(){if(!this.finalized){this.finalized=!0;var t=this.blocks,r=this.lastByteIndex;t[16]=this.block,t[r>>>2]|=a[3&r],this.block=t[16],r>=56&&(this.hashed||this.hash(),t[0]=this.block,t[16]=t[1]=t[2]=t[3]=t[4]=t[5]=t[6]=t[7]=t[8]=t[9]=t[10]=t[11]=t[12]=t[13]=t[14]=t[15]=0),t[14]=this.hBytes<<3|this.bytes>>>29,t[15]=this.bytes<<3,this.hash()}},b.prototype.hash=function(){var t,r,i=this.h0,e=this.h1,h=this.h2,s=this.h3,n=this.h4,a=this.blocks;for(t=16;t<80;++t)r=a[t-3]^a[t-8]^a[t-14]^a[t-16],a[t]=r<<1|r>>>31;for(t=0;t<20;t+=5)i=(r=(e=(r=(h=(r=(s=(r=(n=(r=i<<5|i>>>27)+(e&h|~e&s)+n+1518500249+a[t]|0)<<5|n>>>27)+(i&(e=e<<30|e>>>2)|~i&h)+s+1518500249+a[t+1]|0)<<5|s>>>27)+(n&(i=i<<30|i>>>2)|~n&e)+h+1518500249+a[t+2]|0)<<5|h>>>27)+(s&(n=n<<30|n>>>2)|~s&i)+e+1518500249+a[t+3]|0)<<5|e>>>27)+(h&(s=s<<30|s>>>2)|~h&n)+i+1518500249+a[t+4]|0,h=h<<30|h>>>2;for(;t<40;t+=5)i=(r=(e=(r=(h=(r=(s=(r=(n=(r=i<<5|i>>>27)+(e^h^s)+n+1859775393+a[t]|0)<<5|n>>>27)+(i^(e=e<<30|e>>>2)^h)+s+1859775393+a[t+1]|0)<<5|s>>>27)+(n^(i=i<<30|i>>>2)^e)+h+1859775393+a[t+2]|0)<<5|h>>>27)+(s^(n=n<<30|n>>>2)^i)+e+1859775393+a[t+3]|0)<<5|e>>>27)+(h^(s=s<<30|s>>>2)^n)+i+1859775393+a[t+4]|0,h=h<<30|h>>>2;for(;t<60;t+=5)i=(r=(e=(r=(h=(r=(s=(r=(n=(r=i<<5|i>>>27)+(e&h|e&s|h&s)+n-1894007588+a[t]|0)<<5|n>>>27)+(i&(e=e<<30|e>>>2)|i&h|e&h)+s-1894007588+a[t+1]|0)<<5|s>>>27)+(n&(i=i<<30|i>>>2)|n&e|i&e)+h-1894007588+a[t+2]|0)<<5|h>>>27)+(s&(n=n<<30|n>>>2)|s&i|n&i)+e-1894007588+a[t+3]|0)<<5|e>>>27)+(h&(s=s<<30|s>>>2)|h&n|s&n)+i-1894007588+a[t+4]|0,h=h<<30|h>>>2;for(;t<80;t+=5)i=(r=(e=(r=(h=(r=(s=(r=(n=(r=i<<5|i>>>27)+(e^h^s)+n-899497514+a[t]|0)<<5|n>>>27)+(i^(e=e<<30|e>>>2)^h)+s-899497514+a[t+1]|0)<<5|s>>>27)+(n^(i=i<<30|i>>>2)^e)+h-899497514+a[t+2]|0)<<5|h>>>27)+(s^(n=n<<30|n>>>2)^i)+e-899497514+a[t+3]|0)<<5|e>>>27)+(h^(s=s<<30|s>>>2)^n)+i-899497514+a[t+4]|0,h=h<<30|h>>>2;this.h0=this.h0+i|0,this.h1=this.h1+e|0,this.h2=this.h2+h|0,this.h3=this.h3+s|0,this.h4=this.h4+n|0},b.prototype.hex=function(){this.finalize();var t=this.h0,r=this.h1,i=this.h2,e=this.h3,h=this.h4;return n[t>>>28&15]+n[t>>>24&15]+n[t>>>20&15]+n[t>>>16&15]+n[t>>>12&15]+n[t>>>8&15]+n[t>>>4&15]+n[15&t]+n[r>>>28&15]+n[r>>>24&15]+n[r>>>20&15]+n[r>>>16&15]+n[r>>>12&15]+n[r>>>8&15]+n[r>>>4&15]+n[15&r]+n[i>>>28&15]+n[i>>>24&15]+n[i>>>20&15]+n[i>>>16&15]+n[i>>>12&15]+n[i>>>8&15]+n[i>>>4&15]+n[15&i]+n[e>>>28&15]+n[e>>>24&15]+n[e>>>20&15]+n[e>>>16&15]+n[e>>>12&15]+n[e>>>8&15]+n[e>>>4&15]+n[15&e]+n[h>>>28&15]+n[h>>>24&15]+n[h>>>20&15]+n[h>>>16&15]+n[h>>>12&15]+n[h>>>8&15]+n[h>>>4&15]+n[15&h]},b.prototype.toString=b.prototype.hex,b.prototype.digest=function(){this.finalize();var t=this.h0,r=this.h1,i=this.h2,e=this.h3,h=this.h4;return[t>>>24&255,t>>>16&255,t>>>8&255,255&t,r>>>24&255,r>>>16&255,r>>>8&255,255&r,i>>>24&255,i>>>16&255,i>>>8&255,255&i,e>>>24&255,e>>>16&255,e>>>8&255,255&e,h>>>24&255,h>>>16&255,h>>>8&255,255&h]},b.prototype.array=b.prototype.digest,b.prototype.arrayBuffer=function(){this.finalize();var t=new ArrayBuffer(20),r=new DataView(t);return r.setUint32(0,this.h0),r.setUint32(4,this.h1),r.setUint32(8,this.h2),r.setUint32(12,this.h3),r.setUint32(16,this.h4),t},v.prototype=new b,v.prototype.finalize=function(){if(b.prototype.finalize.call(this),this.inner){this.inner=!1;var t=this.array();b.call(this,this.sharedMemory),this.update(this.oKeyPad),this.update(t),b.prototype.finalize.call(this)}};var w=function(){var t=p("hex");t.create=function(){return new b},t.update=function(r){return t.create().update(r)};for(var r=0;r<f.length;++r){var i=f[r];t[i]=p(i)}return t}();w.sha1=w,w.sha1.hmac=function(){var t=d("hex");t.create=function(t){return new v(t)},t.update=function(r,i){return t.create(r).update(i)};for(var r=0;r<f.length;++r){var i=f[r];t[i]=d(i)}return t}(),h?r.exports=w:e.sha1=w}();var e=t(i.exports);export{e as default,e as sha1};