Current section
Files
Jump to
Current section
Files
src/nh_sha2.mjs
/*
noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com)
steps to reproduce:
1. git clone git@github.com:paulmillr/noble-hashes.git
2. cd noble-hashes
3. esbuild ./src/sha2.ts --bundle --platform=neutral --minify
*/
function te(e,...r){if(!(e instanceof Uint8Array))throw new Error("Expected Uint8Array");if(r.length>0&&!r.includes(e.length))throw new Error(`Expected Uint8Array of length ${r}, not of length=${e.length}`)}function K(e,r=!0){if(e.destroyed)throw new Error("Hash instance has been destroyed");if(r&&e.finished)throw new Error("Hash#digest() has already been called")}function Y(e,r){te(e);let t=r.outputLen;if(e.length<t)throw new Error(`digestInto() expects output buffer of length at least ${t}`)}var re=e=>e instanceof Uint8Array;var V=e=>new DataView(e.buffer,e.byteOffset,e.byteLength),g=(e,r)=>e<<32-r|e>>>r,ne=new Uint8Array(new Uint32Array([287454020]).buffer)[0]===68;if(!ne)throw new Error("Non little-endian hardware is not supported");function oe(e){if(typeof e!="string")throw new Error(`utf8ToBytes expected string, got ${typeof e}`);return new Uint8Array(new TextEncoder().encode(e))}function R(e){if(typeof e=="string"&&(e=oe(e)),!re(e))throw new Error(`expected Uint8Array, got ${typeof e}`);return e}var k=class{clone(){return this._cloneInto()}},ve={}.toString;function B(e){let r=s=>e().update(R(s)).digest(),t=e();return r.outputLen=t.outputLen,r.blockLen=t.blockLen,r.create=()=>e(),r}function se(e,r,t,s){if(typeof e.setBigUint64=="function")return e.setBigUint64(r,t,s);let n=BigInt(32),o=BigInt(4294967295),u=Number(t>>n&o),b=Number(t&o),i=s?4:0,f=s?0:4;e.setUint32(r+i,u,s),e.setUint32(r+f,b,s)}var S=class extends k{constructor(t,s,n,o){super();this.blockLen=t;this.outputLen=s;this.padOffset=n;this.isLE=o;this.finished=!1;this.length=0;this.pos=0;this.destroyed=!1;this.buffer=new Uint8Array(t),this.view=V(this.buffer)}update(t){K(this);let{view:s,buffer:n,blockLen:o}=this;t=R(t);let u=t.length;for(let b=0;b<u;){let i=Math.min(o-this.pos,u-b);if(i===o){let f=V(t);for(;o<=u-b;b+=o)this.process(f,b);continue}n.set(t.subarray(b,b+i),this.pos),this.pos+=i,b+=i,this.pos===o&&(this.process(s,0),this.pos=0)}return this.length+=t.length,this.roundClean(),this}digestInto(t){K(this),Y(t,this),this.finished=!0;let{buffer:s,view:n,blockLen:o,isLE:u}=this,{pos:b}=this;s[b++]=128,this.buffer.subarray(b).fill(0),this.padOffset>o-b&&(this.process(n,0),b=0);for(let c=b;c<o;c++)s[c]=0;se(n,o-8,BigInt(this.length*8),u),this.process(n,0);let i=V(t),f=this.outputLen;if(f%4)throw new Error("_sha2: outputLen should be aligned to 32bit");let h=f/4,l=this.get();if(h>l.length)throw new Error("_sha2: outputLen bigger than state");for(let c=0;c<h;c++)i.setUint32(4*c,l[c],u)}digest(){let{buffer:t,outputLen:s}=this;this.digestInto(t);let n=t.slice(0,s);return this.destroy(),n}_cloneInto(t){t||=new this.constructor,t.set(...this.get());let{blockLen:s,buffer:n,length:o,finished:u,destroyed:b,pos:i}=this;return t.length=o,t.pos=i,t.finished=u,t.destroyed=b,o%s&&t.buffer.set(n),t}};var ce=(e,r,t)=>e&r^~e&t,ae=(e,r,t)=>e&r^e&t^r&t,be=new Uint32Array([1116352408,1899447441,3049323471,3921009573,961987163,1508970993,2453635748,2870763221,3624381080,310598401,607225278,1426881987,1925078388,2162078206,2614888103,3248222580,3835390401,4022224774,264347078,604807628,770255983,1249150122,1555081692,1996064986,2554220882,2821834349,2952996808,3210313671,3336571891,3584528711,113926993,338241895,666307205,773529912,1294757372,1396182291,1695183700,1986661051,2177026350,2456956037,2730485921,2820302411,3259730800,3345764771,3516065817,3600352804,4094571909,275423344,430227734,506948616,659060556,883997877,958139571,1322822218,1537002063,1747873779,1955562222,2024104815,2227730452,2361852424,2428436474,2756734187,3204031479,3329325298]),T=new Uint32Array([1779033703,3144134277,1013904242,2773480762,1359893119,2600822924,528734635,1541459225]),E=new Uint32Array(64),W=class extends S{constructor(){super(64,32,8,!1);this.A=T[0]|0;this.B=T[1]|0;this.C=T[2]|0;this.D=T[3]|0;this.E=T[4]|0;this.F=T[5]|0;this.G=T[6]|0;this.H=T[7]|0}get(){let{A:t,B:s,C:n,D:o,E:u,F:b,G:i,H:f}=this;return[t,s,n,o,u,b,i,f]}set(t,s,n,o,u,b,i,f){this.A=t|0,this.B=s|0,this.C=n|0,this.D=o|0,this.E=u|0,this.F=b|0,this.G=i|0,this.H=f|0}process(t,s){for(let c=0;c<16;c++,s+=4)E[c]=t.getUint32(s,!1);for(let c=16;c<64;c++){let x=E[c-15],m=E[c-2],y=g(x,7)^g(x,18)^x>>>3,p=g(m,17)^g(m,19)^m>>>10;E[c]=p+E[c-7]+y+E[c-16]|0}let{A:n,B:o,C:u,D:b,E:i,F:f,G:h,H:l}=this;for(let c=0;c<64;c++){let x=g(i,6)^g(i,11)^g(i,25),m=l+x+ce(i,f,h)+be[c]+E[c]|0,p=(g(n,2)^g(n,13)^g(n,22))+ae(n,o,u)|0;l=h,h=f,f=i,i=b+m|0,b=u,u=o,o=n,n=m+p|0}n=n+this.A|0,o=o+this.B|0,u=u+this.C|0,b=b+this.D|0,i=i+this.E|0,f=f+this.F|0,h=h+this.G|0,l=l+this.H|0,this.set(n,o,u,b,i,f,h,l)}roundClean(){E.fill(0)}destroy(){this.set(0,0,0,0,0,0,0,0),this.buffer.fill(0)}},X=class extends W{constructor(){super();this.A=-1056596264;this.B=914150663;this.C=812702999;this.D=-150054599;this.E=-4191439;this.F=1750603025;this.G=1694076839;this.H=-1090891868;this.outputLen=28}},ie=B(()=>new W),ue=B(()=>new X);var j=BigInt(4294967295),J=BigInt(32);function Z(e,r=!1){return r?{h:Number(e&j),l:Number(e>>J&j)}:{h:Number(e>>J&j)|0,l:Number(e&j)|0}}function fe(e,r=!1){let t=new Uint32Array(e.length),s=new Uint32Array(e.length);for(let n=0;n<e.length;n++){let{h:o,l:u}=Z(e[n],r);[t[n],s[n]]=[o,u]}return[t,s]}var de=(e,r)=>BigInt(e>>>0)<<J|BigInt(r>>>0),xe=(e,r,t)=>e>>>t,he=(e,r,t)=>e<<32-t|r>>>t,le=(e,r,t)=>e>>>t|r<<32-t,me=(e,r,t)=>e<<32-t|r>>>t,pe=(e,r,t)=>e<<64-t|r>>>t-32,ye=(e,r,t)=>e>>>t-32|r<<64-t,ge=(e,r)=>r,Ae=(e,r)=>e,we=(e,r,t)=>e<<t|r>>>32-t,He=(e,r,t)=>r<<t|e>>>32-t,Ue=(e,r,t)=>r<<t-32|e>>>64-t,Le=(e,r,t)=>e<<t-32|r>>>64-t;function Be(e,r,t,s){let n=(r>>>0)+(s>>>0);return{h:e+t+(n/2**32|0)|0,l:n|0}}var Te=(e,r,t)=>(e>>>0)+(r>>>0)+(t>>>0),Ee=(e,r,t,s)=>r+t+s+(e/2**32|0)|0,_e=(e,r,t,s)=>(e>>>0)+(r>>>0)+(t>>>0)+(s>>>0),Ce=(e,r,t,s,n)=>r+t+s+n+(e/2**32|0)|0,Se=(e,r,t,s,n)=>(e>>>0)+(r>>>0)+(t>>>0)+(s>>>0)+(n>>>0),Ie=(e,r,t,s,n,o)=>r+t+s+n+o+(e/2**32|0)|0;var De={fromBig:Z,split:fe,toBig:de,shrSH:xe,shrSL:he,rotrSH:le,rotrSL:me,rotrBH:pe,rotrBL:ye,rotr32H:ge,rotr32L:Ae,rotlSH:we,rotlSL:He,rotlBH:Ue,rotlBL:Le,add:Be,add3L:Te,add3H:Ee,add4L:_e,add4H:Ce,add5H:Ie,add5L:Se},a=De;var[Fe,Ge]=(()=>a.split(["0x428a2f98d728ae22","0x7137449123ef65cd","0xb5c0fbcfec4d3b2f","0xe9b5dba58189dbbc","0x3956c25bf348b538","0x59f111f1b605d019","0x923f82a4af194f9b","0xab1c5ed5da6d8118","0xd807aa98a3030242","0x12835b0145706fbe","0x243185be4ee4b28c","0x550c7dc3d5ffb4e2","0x72be5d74f27b896f","0x80deb1fe3b1696b1","0x9bdc06a725c71235","0xc19bf174cf692694","0xe49b69c19ef14ad2","0xefbe4786384f25e3","0x0fc19dc68b8cd5b5","0x240ca1cc77ac9c65","0x2de92c6f592b0275","0x4a7484aa6ea6e483","0x5cb0a9dcbd41fbd4","0x76f988da831153b5","0x983e5152ee66dfab","0xa831c66d2db43210","0xb00327c898fb213f","0xbf597fc7beef0ee4","0xc6e00bf33da88fc2","0xd5a79147930aa725","0x06ca6351e003826f","0x142929670a0e6e70","0x27b70a8546d22ffc","0x2e1b21385c26c926","0x4d2c6dfc5ac42aed","0x53380d139d95b3df","0x650a73548baf63de","0x766a0abb3c77b2a8","0x81c2c92e47edaee6","0x92722c851482353b","0xa2bfe8a14cf10364","0xa81a664bbc423001","0xc24b8b70d0f89791","0xc76c51a30654be30","0xd192e819d6ef5218","0xd69906245565a910","0xf40e35855771202a","0x106aa07032bbd1b8","0x19a4c116b8d2d0c8","0x1e376c085141ab53","0x2748774cdf8eeb99","0x34b0bcb5e19b48a8","0x391c0cb3c5c95a63","0x4ed8aa4ae3418acb","0x5b9cca4f7763e373","0x682e6ff3d6b2b8a3","0x748f82ee5defb2fc","0x78a5636f43172f60","0x84c87814a1f0ab72","0x8cc702081a6439ec","0x90befffa23631e28","0xa4506cebde82bde9","0xbef9a3f7b2c67915","0xc67178f2e372532b","0xca273eceea26619c","0xd186b8c721c0c207","0xeada7dd6cde0eb1e","0xf57d4f7fee6ed178","0x06f067aa72176fba","0x0a637dc5a2c898a6","0x113f9804bef90dae","0x1b710b35131c471b","0x28db77f523047d84","0x32caab7b40c72493","0x3c9ebe0a15c9bebc","0x431d67c49c100d4c","0x4cc5d4becb3e42b6","0x597f299cfc657e2a","0x5fcb6fab3ad6faec","0x6c44198c4a475817"].map(e=>BigInt(e))))(),_=new Uint32Array(80),C=new Uint32Array(80),I=class extends S{constructor(){super(128,64,16,!1);this.Ah=1779033703;this.Al=-205731576;this.Bh=-1150833019;this.Bl=-2067093701;this.Ch=1013904242;this.Cl=-23791573;this.Dh=-1521486534;this.Dl=1595750129;this.Eh=1359893119;this.El=-1377402159;this.Fh=-1694144372;this.Fl=725511199;this.Gh=528734635;this.Gl=-79577749;this.Hh=1541459225;this.Hl=327033209}get(){let{Ah:t,Al:s,Bh:n,Bl:o,Ch:u,Cl:b,Dh:i,Dl:f,Eh:h,El:l,Fh:c,Fl:x,Gh:m,Gl:y,Hh:p,Hl:A}=this;return[t,s,n,o,u,b,i,f,h,l,c,x,m,y,p,A]}set(t,s,n,o,u,b,i,f,h,l,c,x,m,y,p,A){this.Ah=t|0,this.Al=s|0,this.Bh=n|0,this.Bl=o|0,this.Ch=u|0,this.Cl=b|0,this.Dh=i|0,this.Dl=f|0,this.Eh=h|0,this.El=l|0,this.Fh=c|0,this.Fl=x|0,this.Gh=m|0,this.Gl=y|0,this.Hh=p|0,this.Hl=A|0}process(t,s){for(let d=0;d<16;d++,s+=4)_[d]=t.getUint32(s),C[d]=t.getUint32(s+=4);for(let d=16;d<80;d++){let U=_[d-15]|0,L=C[d-15]|0,v=a.rotrSH(U,L,1)^a.rotrSH(U,L,8)^a.shrSH(U,L,7),M=a.rotrSL(U,L,1)^a.rotrSL(U,L,8)^a.shrSL(U,L,7),w=_[d-2]|0,H=C[d-2]|0,G=a.rotrSH(w,H,19)^a.rotrBH(w,H,61)^a.shrSH(w,H,6),N=a.rotrSL(w,H,19)^a.rotrBL(w,H,61)^a.shrSL(w,H,6),O=a.add4L(M,N,C[d-7],C[d-16]),$=a.add4H(O,v,G,_[d-7],_[d-16]);_[d]=$|0,C[d]=O|0}let{Ah:n,Al:o,Bh:u,Bl:b,Ch:i,Cl:f,Dh:h,Dl:l,Eh:c,El:x,Fh:m,Fl:y,Gh:p,Gl:A,Hh:D,Hl:F}=this;for(let d=0;d<80;d++){let U=a.rotrSH(c,x,14)^a.rotrSH(c,x,18)^a.rotrBH(c,x,41),L=a.rotrSL(c,x,14)^a.rotrSL(c,x,18)^a.rotrBL(c,x,41),v=c&m^~c&p,M=x&y^~x&A,w=a.add5L(F,L,M,Ge[d],C[d]),H=a.add5H(w,D,U,v,Fe[d],_[d]),G=w|0,N=a.rotrSH(n,o,28)^a.rotrBH(n,o,34)^a.rotrBH(n,o,39),O=a.rotrSL(n,o,28)^a.rotrBL(n,o,34)^a.rotrBL(n,o,39),$=n&u^n&i^u&i,ee=o&b^o&f^b&f;D=p|0,F=A|0,p=m|0,A=y|0,m=c|0,y=x|0,{h:c,l:x}=a.add(h|0,l|0,H|0,G|0),h=i|0,l=f|0,i=u|0,f=b|0,u=n|0,b=o|0;let Q=a.add3L(G,O,ee);n=a.add3H(Q,H,N,$),o=Q|0}({h:n,l:o}=a.add(this.Ah|0,this.Al|0,n|0,o|0)),{h:u,l:b}=a.add(this.Bh|0,this.Bl|0,u|0,b|0),{h:i,l:f}=a.add(this.Ch|0,this.Cl|0,i|0,f|0),{h,l}=a.add(this.Dh|0,this.Dl|0,h|0,l|0),{h:c,l:x}=a.add(this.Eh|0,this.El|0,c|0,x|0),{h:m,l:y}=a.add(this.Fh|0,this.Fl|0,m|0,y|0),{h:p,l:A}=a.add(this.Gh|0,this.Gl|0,p|0,A|0),{h:D,l:F}=a.add(this.Hh|0,this.Hl|0,D|0,F|0),this.set(n,o,u,b,i,f,h,l,c,x,m,y,p,A,D,F)}roundClean(){_.fill(0),C.fill(0)}destroy(){this.buffer.fill(0),this.set(0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0)}},q=class extends I{constructor(){super();this.Ah=-1942145080;this.Al=424955298;this.Bh=1944164710;this.Bl=-1982016298;this.Ch=502970286;this.Cl=855612546;this.Dh=1738396948;this.Dl=1479516111;this.Eh=258812777;this.El=2077511080;this.Fh=2011393907;this.Fl=79989058;this.Gh=1067287976;this.Gl=1780299464;this.Hh=286451373;this.Hl=-1848208735;this.outputLen=28}},z=class extends I{constructor(){super();this.Ah=573645204;this.Al=-64227540;this.Bh=-1621794909;this.Bl=-934517566;this.Ch=596883563;this.Cl=1867755857;this.Dh=-1774684391;this.Dl=1497426621;this.Eh=-1775747358;this.El=-1467023389;this.Fh=-1101128155;this.Fl=1401305490;this.Gh=721525244;this.Gl=746961066;this.Hh=246885852;this.Hl=-2117784414;this.outputLen=32}},P=class extends I{constructor(){super();this.Ah=-876896931;this.Al=-1056596264;this.Bh=1654270250;this.Bl=914150663;this.Ch=-1856437926;this.Cl=812702999;this.Dh=355462360;this.Dl=-150054599;this.Eh=1731405415;this.El=-4191439;this.Fh=-1900787065;this.Fl=1750603025;this.Gh=-619958771;this.Gl=1694076839;this.Hh=1203062813;this.Hl=-1090891868;this.outputLen=48}},Oe=B(()=>new I),ke=B(()=>new q),Ve=B(()=>new z),We=B(()=>new P);export{ue as sha224,ie as sha256,We as sha384,Oe as sha512,ke as sha512_224,Ve as sha512_256};