mirror of
https://github.com/emn178/js-sha1.git
synced 2026-11-17 22:28:43 +08:00
- ESM builds and package `exports`. ### Fixed - Incorrect hash for strings with unpaired UTF-16 surrogates; they are now encoded as U+FFFD, the same as `TextEncoder`. - Detect browsers with a `process` polyfill (e.g. BrowserFS) and Electron renderers as non-Node.js environments. ### Changed - Replace Travis CI with GitHub Actions. - Update dev dependencies and fix security issues. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
10 lines
7.1 KiB
JavaScript
10 lines
7.1 KiB
JavaScript
function t(t){return t&&t.__esModule&&Object.prototype.hasOwnProperty.call(t,"default")?t.default:t}var i,r={exports:{}};
|
|
/*
|
|
* [js-sha1]{@link https://github.com/emn178/js-sha1}
|
|
*
|
|
* @version 0.8.0
|
|
* @author Chen, Yi-Cyuan [emn178@gmail.com]
|
|
* @copyright Chen, Yi-Cyuan 2014-2026
|
|
* @license MIT
|
|
*/i=r,function(){var t="input is invalid type",r="object"==typeof window,h=r?window:{};h.JS_SHA1_NO_WINDOW&&(r=!1),!r&&"object"==typeof self&&(h=self);var e=!h.JS_SHA1_NO_COMMON_JS&&i.exports,s=!h.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;!h.JS_SHA1_NO_NODE_JS&&c||(c=function(t){return"[object Array]"===Object.prototype.toString.call(t)});var l=ArrayBuffer.isView;!s||!h.JS_SHA1_NO_ARRAY_BUFFER_IS_VIEW&&l||(l=function(t){return"object"==typeof t&&t.buffer&&t.buffer.constructor===ArrayBuffer});var p=function(i){var r=typeof i;if("string"===r)return[i,!0];if("object"!==r||null===i)throw new Error(t);if(s&&i.constructor===ArrayBuffer)return[new Uint8Array(i),!1];if(!c(i)&&!l(i))throw new Error(t);return[i,!1]},y=function(t){return function(i){return new b(!0).update(i)[t]()}},d=function(t){return function(i,r){return new v(i,!0).update(r)[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,i){var r,h=p(t);if(t=h[0],h[1]){var e,s,n=[],a=t.length,o=0;for(r=0;r<a;++r)(e=t.charCodeAt(r))<128?n[o++]=e:e<2048?(n[o++]=192|e>>>6,n[o++]=128|63&e):e<55296||e>=57344?(n[o++]=224|e>>>12,n[o++]=128|e>>>6&63,n[o++]=128|63&e):e<56320&&(s=t.charCodeAt(r+1))>=56320&&s<57344?(++r,e=65536+((1023&e)<<10|1023&s),n[o++]=240|e>>>18,n[o++]=128|e>>>12&63,n[o++]=128|e>>>6&63,n[o++]=128|63&e):(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(r=0;r<64;++r){var c=t[r]||0;f[r]=92^c,u[r]=54^c}b.call(this,i),this.update(u),this.oKeyPad=f,this.inner=!0,this.sharedMemory=i}b.prototype.update=function(t){if(this.finalized)throw new Error("finalize already called");var i=p(t);t=i[0];for(var r,h,e,s=i[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(e=this.start;n<a&&e<64;++n)(r=t.charCodeAt(n))<128?f[e>>>2]|=r<<o[3&e++]:r<2048?(f[e>>>2]|=(192|r>>>6)<<o[3&e++],f[e>>>2]|=(128|63&r)<<o[3&e++]):r<55296||r>=57344?(f[e>>>2]|=(224|r>>>12)<<o[3&e++],f[e>>>2]|=(128|r>>>6&63)<<o[3&e++],f[e>>>2]|=(128|63&r)<<o[3&e++]):r<56320&&(h=t.charCodeAt(n+1))>=56320&&h<57344?(++n,r=65536+((1023&r)<<10|1023&h),f[e>>>2]|=(240|r>>>18)<<o[3&e++],f[e>>>2]|=(128|r>>>12&63)<<o[3&e++],f[e>>>2]|=(128|r>>>6&63)<<o[3&e++],f[e>>>2]|=(128|63&r)<<o[3&e++]):(f[e>>>2]|=239<<o[3&e++],f[e>>>2]|=191<<o[3&e++],f[e>>>2]|=189<<o[3&e++]);else for(e=this.start;n<a&&e<64;++n)f[e>>>2]|=t[n]<<o[3&e++];this.lastByteIndex=e,this.bytes+=e-this.start,e>=64?(this.block=f[16],this.start=e-64,this.hash(),this.hashed=!0):this.start=e}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,i=this.lastByteIndex;t[16]=this.block,t[i>>>2]|=a[3&i],this.block=t[16],i>=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,i,r=this.h0,h=this.h1,e=this.h2,s=this.h3,n=this.h4,a=this.blocks;for(t=16;t<80;++t)i=a[t-3]^a[t-8]^a[t-14]^a[t-16],a[t]=i<<1|i>>>31;for(t=0;t<20;t+=5)r=(i=(h=(i=(e=(i=(s=(i=(n=(i=r<<5|r>>>27)+(h&e|~h&s)+n+1518500249+a[t]|0)<<5|n>>>27)+(r&(h=h<<30|h>>>2)|~r&e)+s+1518500249+a[t+1]|0)<<5|s>>>27)+(n&(r=r<<30|r>>>2)|~n&h)+e+1518500249+a[t+2]|0)<<5|e>>>27)+(s&(n=n<<30|n>>>2)|~s&r)+h+1518500249+a[t+3]|0)<<5|h>>>27)+(e&(s=s<<30|s>>>2)|~e&n)+r+1518500249+a[t+4]|0,e=e<<30|e>>>2;for(;t<40;t+=5)r=(i=(h=(i=(e=(i=(s=(i=(n=(i=r<<5|r>>>27)+(h^e^s)+n+1859775393+a[t]|0)<<5|n>>>27)+(r^(h=h<<30|h>>>2)^e)+s+1859775393+a[t+1]|0)<<5|s>>>27)+(n^(r=r<<30|r>>>2)^h)+e+1859775393+a[t+2]|0)<<5|e>>>27)+(s^(n=n<<30|n>>>2)^r)+h+1859775393+a[t+3]|0)<<5|h>>>27)+(e^(s=s<<30|s>>>2)^n)+r+1859775393+a[t+4]|0,e=e<<30|e>>>2;for(;t<60;t+=5)r=(i=(h=(i=(e=(i=(s=(i=(n=(i=r<<5|r>>>27)+(h&e|h&s|e&s)+n-1894007588+a[t]|0)<<5|n>>>27)+(r&(h=h<<30|h>>>2)|r&e|h&e)+s-1894007588+a[t+1]|0)<<5|s>>>27)+(n&(r=r<<30|r>>>2)|n&h|r&h)+e-1894007588+a[t+2]|0)<<5|e>>>27)+(s&(n=n<<30|n>>>2)|s&r|n&r)+h-1894007588+a[t+3]|0)<<5|h>>>27)+(e&(s=s<<30|s>>>2)|e&n|s&n)+r-1894007588+a[t+4]|0,e=e<<30|e>>>2;for(;t<80;t+=5)r=(i=(h=(i=(e=(i=(s=(i=(n=(i=r<<5|r>>>27)+(h^e^s)+n-899497514+a[t]|0)<<5|n>>>27)+(r^(h=h<<30|h>>>2)^e)+s-899497514+a[t+1]|0)<<5|s>>>27)+(n^(r=r<<30|r>>>2)^h)+e-899497514+a[t+2]|0)<<5|e>>>27)+(s^(n=n<<30|n>>>2)^r)+h-899497514+a[t+3]|0)<<5|h>>>27)+(e^(s=s<<30|s>>>2)^n)+r-899497514+a[t+4]|0,e=e<<30|e>>>2;this.h0=this.h0+r|0,this.h1=this.h1+h|0,this.h2=this.h2+e|0,this.h3=this.h3+s|0,this.h4=this.h4+n|0},b.prototype.hex=function(){this.finalize();var t=this.h0,i=this.h1,r=this.h2,h=this.h3,e=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[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[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[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]+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]},b.prototype.toString=b.prototype.hex,b.prototype.digest=function(){this.finalize();var t=this.h0,i=this.h1,r=this.h2,h=this.h3,e=this.h4;return[t>>>24&255,t>>>16&255,t>>>8&255,255&t,i>>>24&255,i>>>16&255,i>>>8&255,255&i,r>>>24&255,r>>>16&255,r>>>8&255,255&r,h>>>24&255,h>>>16&255,h>>>8&255,255&h,e>>>24&255,e>>>16&255,e>>>8&255,255&e]},b.prototype.array=b.prototype.digest,b.prototype.arrayBuffer=function(){this.finalize();var t=new ArrayBuffer(20),i=new DataView(t);return i.setUint32(0,this.h0),i.setUint32(4,this.h1),i.setUint32(8,this.h2),i.setUint32(12,this.h3),i.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 _=function(){var t=y("hex");t.create=function(){return new b},t.update=function(i){return t.create().update(i)};for(var i=0;i<f.length;++i){var r=f[i];t[r]=y(r)}return t}();_.sha1=_,_.sha1.hmac=function(){var t=d("hex");t.create=function(t){return new v(t)},t.update=function(i,r){return t.create(i).update(r)};for(var i=0;i<f.length;++i){var r=f[i];t[r]=d(r)}return t}(),e?i.exports=_:h.sha1=_}();var h=t(r.exports);export{h as default,h as sha1};
|