diff --git a/CHANGELOG.md b/CHANGELOG.md index 670c5f0..c4d4672 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,8 +1,15 @@ # Change Log +## v0.4.0 / 2017-07-08 +### Added +- Streaming support(update). +- support for ArrayBuffer input/output. +- support for byte array input/output. + ## v0.3.0 / 2017-01-23 ### Added - AMD support. + ### Fixed - ArrayBuffer dosen't work in Webpack. @@ -13,20 +20,24 @@ ## v0.2.1 / 2015-02-08 ### Added - test cases. + ### Removed - ascii parameter. + ### Improved - performance. ## v0.2.0 / 2015-02-02 ### Added - test cases. + ### Improved - performance. ## v0.1.3 / 2015-01-26 ### Added - test cases. + ### Improved - performance. @@ -35,6 +46,7 @@ - bower package. - travis. - coveralls. + ### Fixed - JSHint warnings. diff --git a/README.md b/README.md index b43f8d1..9f674fb 100644 --- a/README.md +++ b/README.md @@ -31,6 +31,14 @@ sha512('Message to hash'); sha384('Message to hash'); sha512_256('Message to hash'); sha512_224('Message to hash'); + +var hash = sha512.create(); +hash.update('Message to hash'); +hash.hex(); + +var hash2 = sha512.update('Message to hash'); +hash2.update('Message2 to hash'); +hash2.array(); ``` If you use node.js, you should require the module first: ```JavaScript @@ -69,6 +77,13 @@ sha512('中文'); // 8b88efc2ebbcbdad5ac2d65af05bec57bda25e71fd5fb25bbd892057a27 sha384('中文'); // 93422ceb8291a69b22f02dc1114c39a287493ad525dcebc77e4019a44eaee2633a85d0f29cd298ee6799048c33a4be0c sha512_256('中文'); // b6dab29c16ec35ab34a5d92ff135b58de96741dda78b1009a2181cf8b45d2f72 sha512_224('中文'); // 0f46a0ae7f226517dd66ece0ce1efa29ffb7ced05ac4566fdcaed188 + +// Different output +sha512(''); // cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e +sha512.hex(''); // cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e +sha512.array(''); // [207, 131, 225, 53, 126, 239, 184, 189, 241, 84, 40, 80, 214, 109, 128, 7, 214, 32, 228, 5, 11, 87, 21, 220, 131, 244, 169, 33, 211, 108, 233, 206, 71, 208, 209, 60, 93, 133, 242, 176, 255, 131, 24, 210, 135, 126, 236, 47, 99, 185, 49, 189, 71, 65, 122, 129, 165, 56, 50, 122, 249, 39, 218, 62] +sha512.digest(''); // [207, 131, 225, 53, 126, 239, 184, 189, 241, 84, 40, 80, 214, 109, 128, 7, 214, 32, 228, 5, 11, 87, 21, 220, 131, 244, 169, 33, 211, 108, 233, 206, 71, 208, 209, 60, 93, 133, 242, 176, 255, 131, 24, 210, 135, 126, 236, 47, 99, 185, 49, 189, 71, 65, 122, 129, 165, 56, 50, 122, 249, 39, 218, 62] +sha512.arrayBuffer(''); // ArrayBuffer ``` ## License diff --git a/bower.json b/bower.json index 72f3798..71c9f7b 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "js-sha512", - "version": "0.3.0", + "version": "0.4.0", "main": ["src/sha512.js"], "ignore": [ "samples", diff --git a/build/sha512.min.js b/build/sha512.min.js index d965832..0e842c1 100644 --- a/build/sha512.min.js +++ b/build/sha512.min.js @@ -1,9 +1,9 @@ /* * [js-sha512]{@link https://github.com/emn178/js-sha512} * - * @version 0.3.0 + * @version 0.4.0 * @author Chen, Yi-Cyuan [emn178@gmail.com] * @copyright Chen, Yi-Cyuan 2014-2017 * @license MIT */ -!function(){"use strict";var e="object"==typeof window?window:{},o=!e.JS_SHA512_NO_NODE_JS&&"object"==typeof process&&process.versions&&process.versions.node;o&&(e=global);var n=!e.JS_SHA512_NO_COMMON_JS&&"object"==typeof module&&module.exports,r="function"==typeof define&&define.amd,t="0123456789abcdef".split(""),s=[-2147483648,8388608,32768,128],a=[24,16,8,0],f=[1116352408,3609767458,1899447441,602891725,3049323471,3964484399,3921009573,2173295548,961987163,4081628472,1508970993,3053834265,2453635748,2937671579,2870763221,3664609560,3624381080,2734883394,310598401,1164996542,607225278,1323610764,1426881987,3590304994,1925078388,4068182383,2162078206,991336113,2614888103,633803317,3248222580,3479774868,3835390401,2666613458,4022224774,944711139,264347078,2341262773,604807628,2007800933,770255983,1495990901,1249150122,1856431235,1555081692,3175218132,1996064986,2198950837,2554220882,3999719339,2821834349,766784016,2952996808,2566594879,3210313671,3203337956,3336571891,1034457026,3584528711,2466948901,113926993,3758326383,338241895,168717936,666307205,1188179964,773529912,1546045734,1294757372,1522805485,1396182291,2643833823,1695183700,2343527390,1986661051,1014477480,2177026350,1206759142,2456956037,344077627,2730485921,1290863460,2820302411,3158454273,3259730800,3505952657,3345764771,106217008,3516065817,3606008344,3600352804,1432725776,4094571909,1467031594,275423344,851169720,430227734,3100823752,506948616,1363258195,659060556,3750685593,883997877,3785050280,958139571,3318307427,1322822218,3812723403,1537002063,2003034995,1747873779,3602036899,1955562222,1575990012,2024104815,1125592928,2227730452,2716904306,2361852424,442776044,2428436474,593698344,2756734187,3733110249,3204031479,2999351573,3329325298,3815920427,3391569614,3928383900,3515267271,566280711,3940187606,3454069534,4118630271,4000239992,116418474,1914138554,174292421,2731055270,289380356,3203993006,460393269,320620315,685471733,587496836,852142971,1086792851,1017036298,365543100,1126000580,2618297676,1288033470,3409855158,1501505948,4234509866,1607167915,987167468,1816402316,1246189591],c=[],i=function(e){return h(e,384)},u=function(e){return h(e,256)},d=function(e){return h(e,224)},h=function(e,o){var n,r,i,u,d,h,_,p,l,v,S,b,w,O,m,y,A,J,N,j,C,g,x,H,M,D,E,k,q,z,B,F,G,I,K,L,P,Q,R,T,U,V,W,X,Y=!1,Z=0,$=0,ee=0,oe=e.length;384==o?(n=3418070365,r=3238371032,i=1654270250,u=914150663,d=2438529370,h=812702999,_=355462360,p=4144912697,l=1731405415,v=4290775857,S=2394180231,b=1750603025,w=3675008525,O=1694076839,m=1203062813,y=3204075428):256==o?(n=573645204,r=4230739756,i=2673172387,u=3360449730,d=596883563,h=1867755857,_=2520282905,p=1497426621,l=2519219938,v=2827943907,S=3193839141,b=1401305490,w=721525244,O=746961066,m=246885852,y=2177182882):224==o?(n=2352822216,r=424955298,i=1944164710,u=2312950998,d=502970286,h=855612546,_=1738396948,p=1479516111,l=258812777,v=2077511080,S=2011393907,b=79989058,w=1067287976,O=1780299464,m=286451373,y=2446758561):(n=1779033703,r=4089235720,i=3144134277,u=2227873595,d=1013904242,h=4271175723,_=2773480762,p=1595750129,l=1359893119,v=2917565137,S=2600822924,b=725511199,w=528734635,O=4215389547,m=1541459225,y=327033209,o=512),A=0;do{for(c[0]=A,c[1]=c[2]=c[3]=c[4]=c[5]=c[6]=c[7]=c[8]=c[9]=c[10]=c[11]=c[12]=c[13]=c[14]=c[15]=c[16]=c[17]=c[18]=c[19]=c[20]=c[21]=c[22]=c[23]=c[24]=c[25]=c[26]=c[27]=c[28]=c[29]=c[30]=c[31]=c[32]=0,N=$;oe>Z&&128>N;++Z)J=e.charCodeAt(Z),128>J?c[N>>2]|=J<J?(c[N>>2]|=(192|J>>6)<>2]|=(128|63&J)<J||J>=57344?(c[N>>2]|=(224|J>>12)<>2]|=(128|J>>6&63)<>2]|=(128|63&J)<>2]|=(240|J>>18)<>2]|=(128|J>>12&63)<>2]|=(128|J>>6&63)<>2]|=(128|63&J)<>2]|=s[3&N],++Z),A=c[32],Z>oe&&112>N&&(c[31]=ee<<3,Y=!0),j=32;160>j;j+=2)R=c[j-30],T=c[j-29],C=(R>>>1|T<<31)^(R>>>8|T<<24)^R>>>7,g=(T>>>1|R<<31)^(T>>>8|R<<24)^(T>>>7|R<<25),R=c[j-4],T=c[j-3],x=(R>>>19|T<<13)^(T>>>29|R<<3)^R>>>6,H=(T>>>19|R<<13)^(R>>>29|T<<3)^(T>>>6|R<<26),R=c[j-32],T=c[j-31],U=c[j-14],V=c[j-13],M=(65535&V)+(65535&T)+(65535&g)+(65535&H),D=(V>>>16)+(T>>>16)+(g>>>16)+(H>>>16)+(M>>>16),E=(65535&U)+(65535&R)+(65535&C)+(65535&x)+(D>>>16),k=(U>>>16)+(R>>>16)+(C>>>16)+(x>>>16)+(E>>>16),c[j]=k<<16|65535&E,c[j+1]=D<<16|65535&M;var ne=n,re=r,te=i,se=u,ae=d,fe=h,ce=_,ie=p,ue=l,de=v,he=S,_e=b,pe=w,le=O,ve=m,Se=y;for(K=te&ae,L=se&fe,j=0;160>j;j+=8)C=(ne>>>28|re<<4)^(re>>>2|ne<<30)^(re>>>7|ne<<25),g=(re>>>28|ne<<4)^(ne>>>2|re<<30)^(ne>>>7|re<<25),x=(ue>>>14|de<<18)^(ue>>>18|de<<14)^(de>>>9|ue<<23),H=(de>>>14|ue<<18)^(de>>>18|ue<<14)^(ue>>>9|de<<23),q=ne&te,z=re&se,P=q^ne&ae^K,Q=z^re&fe^L,W=ue&he^~ue&pe,X=de&_e^~de&le,R=c[j],T=c[j+1],U=f[j],V=f[j+1],M=(65535&V)+(65535&T)+(65535&X)+(65535&H)+(65535&Se),D=(V>>>16)+(T>>>16)+(X>>>16)+(H>>>16)+(Se>>>16)+(M>>>16),E=(65535&U)+(65535&R)+(65535&W)+(65535&x)+(65535&ve)+(D>>>16),k=(U>>>16)+(R>>>16)+(W>>>16)+(x>>>16)+(ve>>>16)+(E>>>16),R=k<<16|65535&E,T=D<<16|65535&M,M=(65535&Q)+(65535&g),D=(Q>>>16)+(g>>>16)+(M>>>16),E=(65535&P)+(65535&C)+(D>>>16),k=(P>>>16)+(C>>>16)+(E>>>16),U=k<<16|65535&E,V=D<<16|65535&M,M=(65535&ie)+(65535&T),D=(ie>>>16)+(T>>>16)+(M>>>16),E=(65535&ce)+(65535&R)+(D>>>16),k=(ce>>>16)+(R>>>16)+(E>>>16),ve=k<<16|65535&E,Se=D<<16|65535&M,M=(65535&V)+(65535&T),D=(V>>>16)+(T>>>16)+(M>>>16),E=(65535&U)+(65535&R)+(D>>>16),k=(U>>>16)+(R>>>16)+(E>>>16),ce=k<<16|65535&E,ie=D<<16|65535&M,C=(ce>>>28|ie<<4)^(ie>>>2|ce<<30)^(ie>>>7|ce<<25),g=(ie>>>28|ce<<4)^(ce>>>2|ie<<30)^(ce>>>7|ie<<25),x=(ve>>>14|Se<<18)^(ve>>>18|Se<<14)^(Se>>>9|ve<<23),H=(Se>>>14|ve<<18)^(Se>>>18|ve<<14)^(ve>>>9|Se<<23),B=ce&ne,F=ie&re,P=B^ce&te^q,Q=F^ie&se^z,W=ve&ue^~ve&he,X=Se&de^~Se&_e,R=c[j+2],T=c[j+3],U=f[j+2],V=f[j+3],M=(65535&V)+(65535&T)+(65535&X)+(65535&H)+(65535&le),D=(V>>>16)+(T>>>16)+(X>>>16)+(H>>>16)+(le>>>16)+(M>>>16),E=(65535&U)+(65535&R)+(65535&W)+(65535&x)+(65535&pe)+(D>>>16),k=(U>>>16)+(R>>>16)+(W>>>16)+(x>>>16)+(pe>>>16)+(E>>>16),R=k<<16|65535&E,T=D<<16|65535&M,M=(65535&Q)+(65535&g),D=(Q>>>16)+(g>>>16)+(M>>>16),E=(65535&P)+(65535&C)+(D>>>16),k=(P>>>16)+(C>>>16)+(E>>>16),U=k<<16|65535&E,V=D<<16|65535&M,M=(65535&fe)+(65535&T),D=(fe>>>16)+(T>>>16)+(M>>>16),E=(65535&ae)+(65535&R)+(D>>>16),k=(ae>>>16)+(R>>>16)+(E>>>16),pe=k<<16|65535&E,le=D<<16|65535&M,M=(65535&V)+(65535&T),D=(V>>>16)+(T>>>16)+(M>>>16),E=(65535&U)+(65535&R)+(D>>>16),k=(U>>>16)+(R>>>16)+(E>>>16),ae=k<<16|65535&E,fe=D<<16|65535&M,C=(ae>>>28|fe<<4)^(fe>>>2|ae<<30)^(fe>>>7|ae<<25),g=(fe>>>28|ae<<4)^(ae>>>2|fe<<30)^(ae>>>7|fe<<25),x=(pe>>>14|le<<18)^(pe>>>18|le<<14)^(le>>>9|pe<<23),H=(le>>>14|pe<<18)^(le>>>18|pe<<14)^(pe>>>9|le<<23),G=ae&ce,I=fe&ie,P=G^ae&ne^B,Q=I^fe&re^F,W=pe&ve^~pe&ue,X=le&Se^~le&de,R=c[j+4],T=c[j+5],U=f[j+4],V=f[j+5],M=(65535&V)+(65535&T)+(65535&X)+(65535&H)+(65535&_e),D=(V>>>16)+(T>>>16)+(X>>>16)+(H>>>16)+(_e>>>16)+(M>>>16),E=(65535&U)+(65535&R)+(65535&W)+(65535&x)+(65535&he)+(D>>>16),k=(U>>>16)+(R>>>16)+(W>>>16)+(x>>>16)+(he>>>16)+(E>>>16),R=k<<16|65535&E,T=D<<16|65535&M,M=(65535&Q)+(65535&g),D=(Q>>>16)+(g>>>16)+(M>>>16),E=(65535&P)+(65535&C)+(D>>>16),k=(P>>>16)+(C>>>16)+(E>>>16),U=k<<16|65535&E,V=D<<16|65535&M,M=(65535&se)+(65535&T),D=(se>>>16)+(T>>>16)+(M>>>16),E=(65535&te)+(65535&R)+(D>>>16),k=(te>>>16)+(R>>>16)+(E>>>16),he=k<<16|65535&E,_e=D<<16|65535&M,M=(65535&V)+(65535&T),D=(V>>>16)+(T>>>16)+(M>>>16),E=(65535&U)+(65535&R)+(D>>>16),k=(U>>>16)+(R>>>16)+(E>>>16),te=k<<16|65535&E,se=D<<16|65535&M,C=(te>>>28|se<<4)^(se>>>2|te<<30)^(se>>>7|te<<25),g=(se>>>28|te<<4)^(te>>>2|se<<30)^(te>>>7|se<<25),x=(he>>>14|_e<<18)^(he>>>18|_e<<14)^(_e>>>9|he<<23),H=(_e>>>14|he<<18)^(_e>>>18|he<<14)^(he>>>9|_e<<23),K=te&ae,L=se&fe,P=K^te&ce^G,Q=L^se&ie^I,W=he&pe^~he&ve,X=_e&le^~_e&Se,R=c[j+6],T=c[j+7],U=f[j+6],V=f[j+7],M=(65535&V)+(65535&T)+(65535&X)+(65535&H)+(65535&de),D=(V>>>16)+(T>>>16)+(X>>>16)+(H>>>16)+(de>>>16)+(M>>>16),E=(65535&U)+(65535&R)+(65535&W)+(65535&x)+(65535&ue)+(D>>>16),k=(U>>>16)+(R>>>16)+(W>>>16)+(x>>>16)+(ue>>>16)+(E>>>16),R=k<<16|65535&E,T=D<<16|65535&M,M=(65535&Q)+(65535&g),D=(Q>>>16)+(g>>>16)+(M>>>16),E=(65535&P)+(65535&C)+(D>>>16),k=(P>>>16)+(C>>>16)+(E>>>16),U=k<<16|65535&E,V=D<<16|65535&M,M=(65535&re)+(65535&T),D=(re>>>16)+(T>>>16)+(M>>>16),E=(65535&ne)+(65535&R)+(D>>>16),k=(ne>>>16)+(R>>>16)+(E>>>16),ue=k<<16|65535&E,de=D<<16|65535&M,M=(65535&V)+(65535&T),D=(V>>>16)+(T>>>16)+(M>>>16),E=(65535&U)+(65535&R)+(D>>>16),k=(U>>>16)+(R>>>16)+(E>>>16),ne=k<<16|65535&E,re=D<<16|65535&M;M=(65535&r)+(65535&re),D=(r>>>16)+(re>>>16)+(M>>>16),E=(65535&n)+(65535&ne)+(D>>>16),k=(n>>>16)+(ne>>>16)+(E>>>16),n=k<<16|65535&E,r=D<<16|65535&M,M=(65535&u)+(65535&se),D=(u>>>16)+(se>>>16)+(M>>>16),E=(65535&i)+(65535&te)+(D>>>16),k=(i>>>16)+(te>>>16)+(E>>>16),i=k<<16|65535&E,u=D<<16|65535&M,M=(65535&h)+(65535&fe),D=(h>>>16)+(fe>>>16)+(M>>>16),E=(65535&d)+(65535&ae)+(D>>>16),k=(d>>>16)+(ae>>>16)+(E>>>16),d=k<<16|65535&E,h=D<<16|65535&M,M=(65535&p)+(65535&ie),D=(p>>>16)+(ie>>>16)+(M>>>16),E=(65535&_)+(65535&ce)+(D>>>16),k=(_>>>16)+(ce>>>16)+(E>>>16),_=k<<16|65535&E,p=D<<16|65535&M,M=(65535&v)+(65535&de),D=(v>>>16)+(de>>>16)+(M>>>16),E=(65535&l)+(65535&ue)+(D>>>16),k=(l>>>16)+(ue>>>16)+(E>>>16),l=k<<16|65535&E,v=D<<16|65535&M,M=(65535&b)+(65535&_e),D=(b>>>16)+(_e>>>16)+(M>>>16),E=(65535&S)+(65535&he)+(D>>>16),k=(S>>>16)+(he>>>16)+(E>>>16),S=k<<16|65535&E,b=D<<16|65535&M,M=(65535&O)+(65535&le),D=(O>>>16)+(le>>>16)+(M>>>16),E=(65535&w)+(65535&pe)+(D>>>16),k=(w>>>16)+(pe>>>16)+(E>>>16),w=k<<16|65535&E,O=D<<16|65535&M,M=(65535&y)+(65535&Se),D=(y>>>16)+(Se>>>16)+(M>>>16),E=(65535&m)+(65535&ve)+(D>>>16),k=(m>>>16)+(ve>>>16)+(E>>>16),m=k<<16|65535&E,y=D<<16|65535&M}while(!Y);var be=t[n>>28&15]+t[n>>24&15]+t[n>>20&15]+t[n>>16&15]+t[n>>12&15]+t[n>>8&15]+t[n>>4&15]+t[15&n]+t[r>>28&15]+t[r>>24&15]+t[r>>20&15]+t[r>>16&15]+t[r>>12&15]+t[r>>8&15]+t[r>>4&15]+t[15&r]+t[i>>28&15]+t[i>>24&15]+t[i>>20&15]+t[i>>16&15]+t[i>>12&15]+t[i>>8&15]+t[i>>4&15]+t[15&i]+t[u>>28&15]+t[u>>24&15]+t[u>>20&15]+t[u>>16&15]+t[u>>12&15]+t[u>>8&15]+t[u>>4&15]+t[15&u]+t[d>>28&15]+t[d>>24&15]+t[d>>20&15]+t[d>>16&15]+t[d>>12&15]+t[d>>8&15]+t[d>>4&15]+t[15&d]+t[h>>28&15]+t[h>>24&15]+t[h>>20&15]+t[h>>16&15]+t[h>>12&15]+t[h>>8&15]+t[h>>4&15]+t[15&h]+t[_>>28&15]+t[_>>24&15]+t[_>>20&15]+t[_>>16&15]+t[_>>12&15]+t[_>>8&15]+t[_>>4&15]+t[15&_];return o>=256&&(be+=t[p>>28&15]+t[p>>24&15]+t[p>>20&15]+t[p>>16&15]+t[p>>12&15]+t[p>>8&15]+t[p>>4&15]+t[15&p]),o>=384&&(be+=t[l>>28&15]+t[l>>24&15]+t[l>>20&15]+t[l>>16&15]+t[l>>12&15]+t[l>>8&15]+t[l>>4&15]+t[15&l]+t[v>>28&15]+t[v>>24&15]+t[v>>20&15]+t[v>>16&15]+t[v>>12&15]+t[v>>8&15]+t[v>>4&15]+t[15&v]+t[S>>28&15]+t[S>>24&15]+t[S>>20&15]+t[S>>16&15]+t[S>>12&15]+t[S>>8&15]+t[S>>4&15]+t[15&S]+t[b>>28&15]+t[b>>24&15]+t[b>>20&15]+t[b>>16&15]+t[b>>12&15]+t[b>>8&15]+t[b>>4&15]+t[15&b]),512==o&&(be+=t[w>>28&15]+t[w>>24&15]+t[w>>20&15]+t[w>>16&15]+t[w>>12&15]+t[w>>8&15]+t[w>>4&15]+t[15&w]+t[O>>28&15]+t[O>>24&15]+t[O>>20&15]+t[O>>16&15]+t[O>>12&15]+t[O>>8&15]+t[O>>4&15]+t[15&O]+t[m>>28&15]+t[m>>24&15]+t[m>>20&15]+t[m>>16&15]+t[m>>12&15]+t[m>>8&15]+t[m>>4&15]+t[15&m]+t[y>>28&15]+t[y>>24&15]+t[y>>20&15]+t[y>>16&15]+t[y>>12&15]+t[y>>8&15]+t[y>>4&15]+t[15&y]),be},_=h;_.sha512=h,_.sha384=i,_.sha512_256=u,_.sha512_224=d,n?module.exports=_:(e.sha512=h,e.sha384=i,e.sha512_256=u,e.sha512_224=d,r&&define(function(){return _}))}(); \ No newline at end of file +!function(){"use strict";function h(h,t){t?(u[0]=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]=u[16]=u[17]=u[18]=u[19]=u[20]=u[21]=u[22]=u[23]=u[24]=u[25]=u[26]=u[27]=u[28]=u[29]=u[30]=u[31]=u[32]=0,this.blocks=u):this.blocks=[0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0],384==h?(this.h0h=3418070365,this.h0l=3238371032,this.h1h=1654270250,this.h1l=914150663,this.h2h=2438529370,this.h2l=812702999,this.h3h=355462360,this.h3l=4144912697,this.h4h=1731405415,this.h4l=4290775857,this.h5h=2394180231,this.h5l=1750603025,this.h6h=3675008525,this.h6l=1694076839,this.h7h=1203062813,this.h7l=3204075428):256==h?(this.h0h=573645204,this.h0l=4230739756,this.h1h=2673172387,this.h1l=3360449730,this.h2h=596883563,this.h2l=1867755857,this.h3h=2520282905,this.h3l=1497426621,this.h4h=2519219938,this.h4l=2827943907,this.h5h=3193839141,this.h5l=1401305490,this.h6h=721525244,this.h6l=746961066,this.h7h=246885852,this.h7l=2177182882):224==h?(this.h0h=2352822216,this.h0l=424955298,this.h1h=1944164710,this.h1l=2312950998,this.h2h=502970286,this.h2l=855612546,this.h3h=1738396948,this.h3l=1479516111,this.h4h=258812777,this.h4l=2077511080,this.h5h=2011393907,this.h5l=79989058,this.h6h=1067287976,this.h6l=1780299464,this.h7h=286451373,this.h7l=2446758561):(this.h0h=1779033703,this.h0l=4089235720,this.h1h=3144134277,this.h1l=2227873595,this.h2h=1013904242,this.h2l=4271175723,this.h3h=2773480762,this.h3l=1595750129,this.h4h=1359893119,this.h4l=2917565137,this.h5h=2600822924,this.h5l=725511199,this.h6h=528734635,this.h6l=4215389547,this.h7h=1541459225,this.h7l=327033209),this.bits=h,this.block=this.start=this.bytes=0,this.finalized=this.hashed=!1}var t="object"==typeof window?window:{},s=!t.JS_SHA512_NO_NODE_JS&&"object"==typeof process&&process.versions&&process.versions.node;s&&(t=global);var i=!t.JS_SHA512_NO_COMMON_JS&&"object"==typeof module&&module.exports,e="function"==typeof define&&define.amd,l="undefined"!=typeof ArrayBuffer,r="0123456789abcdef".split(""),n=[-2147483648,8388608,32768,128],o=[24,16,8,0],a=[1116352408,3609767458,1899447441,602891725,3049323471,3964484399,3921009573,2173295548,961987163,4081628472,1508970993,3053834265,2453635748,2937671579,2870763221,3664609560,3624381080,2734883394,310598401,1164996542,607225278,1323610764,1426881987,3590304994,1925078388,4068182383,2162078206,991336113,2614888103,633803317,3248222580,3479774868,3835390401,2666613458,4022224774,944711139,264347078,2341262773,604807628,2007800933,770255983,1495990901,1249150122,1856431235,1555081692,3175218132,1996064986,2198950837,2554220882,3999719339,2821834349,766784016,2952996808,2566594879,3210313671,3203337956,3336571891,1034457026,3584528711,2466948901,113926993,3758326383,338241895,168717936,666307205,1188179964,773529912,1546045734,1294757372,1522805485,1396182291,2643833823,1695183700,2343527390,1986661051,1014477480,2177026350,1206759142,2456956037,344077627,2730485921,1290863460,2820302411,3158454273,3259730800,3505952657,3345764771,106217008,3516065817,3606008344,3600352804,1432725776,4094571909,1467031594,275423344,851169720,430227734,3100823752,506948616,1363258195,659060556,3750685593,883997877,3785050280,958139571,3318307427,1322822218,3812723403,1537002063,2003034995,1747873779,3602036899,1955562222,1575990012,2024104815,1125592928,2227730452,2716904306,2361852424,442776044,2428436474,593698344,2756734187,3733110249,3204031479,2999351573,3329325298,3815920427,3391569614,3928383900,3515267271,566280711,3940187606,3454069534,4118630271,4000239992,116418474,1914138554,174292421,2731055270,289380356,3203993006,460393269,320620315,685471733,587496836,852142971,1086792851,1017036298,365543100,1126000580,2618297676,1288033470,3409855158,1501505948,4234509866,1607167915,987167468,1816402316,1246189591],f=["hex","array","digest","arrayBuffer"],u=[],c=function(t,s){return function(i){return new h(s,!0).update(i)[t]()}},p=function(t){var s=c("hex",t);s.create=function(){return new h(t)},s.update=function(h){return s.create().update(h)};for(var i=0;ir;){if(this.hashed&&(this.hashed=!1,a[0]=this.block,a[1]=a[2]=a[3]=a[4]=a[5]=a[6]=a[7]=a[8]=a[9]=a[10]=a[11]=a[12]=a[13]=a[14]=a[15]=a[16]=a[17]=a[18]=a[19]=a[20]=a[21]=a[22]=a[23]=a[24]=a[25]=a[26]=a[27]=a[28]=a[29]=a[30]=a[31]=a[32]=0),s)for(e=this.start;n>r&&128>e;++r)a[e>>2]|=h[r]<r&&128>e;++r)i=h.charCodeAt(r),128>i?a[e>>2]|=i<i?(a[e>>2]|=(192|i>>6)<>2]|=(128|63&i)<i||i>=57344?(a[e>>2]|=(224|i>>12)<>2]|=(128|i>>6&63)<>2]|=(128|63&i)<>2]|=(240|i>>18)<>2]|=(128|i>>12&63)<>2]|=(128|i>>6&63)<>2]|=(128|63&i)<=128?(this.block=a[32],this.start=e-128,this.hash(),this.hashed=!0):this.start=e}return this}},h.prototype.finalize=function(){if(!this.finalized){this.finalized=!0;var h=this.blocks,t=this.lastByteIndex;h[32]=this.block,h[t>>2]|=n[3&t],this.block=h[32],t>=112&&(this.hashed||this.hash(),h[0]=this.block,h[1]=h[2]=h[3]=h[4]=h[5]=h[6]=h[7]=h[8]=h[9]=h[10]=h[11]=h[12]=h[13]=h[14]=h[15]=h[16]=h[17]=h[18]=h[19]=h[20]=h[21]=h[22]=h[23]=h[24]=h[25]=h[26]=h[27]=h[28]=h[29]=h[30]=h[31]=h[32]=0),h[31]=this.bytes<<3,this.hash()}},h.prototype.hash=function(){var h,t,s,i,e,l,r,n,o,f,u,c,p,d,y,b,U,v,_,k,w,g,x,z,A,B=this.h0h,S=this.h0l,O=this.h1h,m=this.h1l,J=this.h2h,N=this.h2l,j=this.h3h,C=this.h3l,D=this.h4h,H=this.h4l,I=this.h5h,M=this.h5l,E=this.h6h,V=this.h6l,q=this.h7h,F=this.h7l,G=this.blocks;for(h=32;160>h;h+=2)k=G[h-30],w=G[h-29],t=(k>>>1|w<<31)^(k>>>8|w<<24)^k>>>7,s=(w>>>1|k<<31)^(w>>>8|k<<24)^(w>>>7|k<<25),k=G[h-4],w=G[h-3],i=(k>>>19|w<<13)^(w>>>29|k<<3)^k>>>6,e=(w>>>19|k<<13)^(k>>>29|w<<3)^(w>>>6|k<<26),k=G[h-32],w=G[h-31],g=G[h-14],x=G[h-13],l=(65535&x)+(65535&w)+(65535&s)+(65535&e),r=(x>>>16)+(w>>>16)+(s>>>16)+(e>>>16)+(l>>>16),n=(65535&g)+(65535&k)+(65535&t)+(65535&i)+(r>>>16),o=(g>>>16)+(k>>>16)+(t>>>16)+(i>>>16)+(n>>>16),G[h]=o<<16|65535&n,G[h+1]=r<<16|65535&l;var K=B,L=S,P=O,Q=m,R=J,T=N,W=j,X=C,Y=D,Z=H,$=I,hh=M,th=E,sh=V,ih=q,eh=F;for(b=P&R,U=Q&T,h=0;160>h;h+=8)t=(K>>>28|L<<4)^(L>>>2|K<<30)^(L>>>7|K<<25),s=(L>>>28|K<<4)^(K>>>2|L<<30)^(K>>>7|L<<25),i=(Y>>>14|Z<<18)^(Y>>>18|Z<<14)^(Z>>>9|Y<<23),e=(Z>>>14|Y<<18)^(Z>>>18|Y<<14)^(Y>>>9|Z<<23),f=K&P,u=L&Q,v=f^K&R^b,_=u^L&T^U,z=Y&$^~Y&th,A=Z&hh^~Z&sh,k=G[h],w=G[h+1],g=a[h],x=a[h+1],l=(65535&x)+(65535&w)+(65535&A)+(65535&e)+(65535&eh),r=(x>>>16)+(w>>>16)+(A>>>16)+(e>>>16)+(eh>>>16)+(l>>>16),n=(65535&g)+(65535&k)+(65535&z)+(65535&i)+(65535&ih)+(r>>>16),o=(g>>>16)+(k>>>16)+(z>>>16)+(i>>>16)+(ih>>>16)+(n>>>16),k=o<<16|65535&n,w=r<<16|65535&l,l=(65535&_)+(65535&s),r=(_>>>16)+(s>>>16)+(l>>>16),n=(65535&v)+(65535&t)+(r>>>16),o=(v>>>16)+(t>>>16)+(n>>>16),g=o<<16|65535&n,x=r<<16|65535&l,l=(65535&X)+(65535&w),r=(X>>>16)+(w>>>16)+(l>>>16),n=(65535&W)+(65535&k)+(r>>>16),o=(W>>>16)+(k>>>16)+(n>>>16),ih=o<<16|65535&n,eh=r<<16|65535&l,l=(65535&x)+(65535&w),r=(x>>>16)+(w>>>16)+(l>>>16),n=(65535&g)+(65535&k)+(r>>>16),o=(g>>>16)+(k>>>16)+(n>>>16),W=o<<16|65535&n,X=r<<16|65535&l,t=(W>>>28|X<<4)^(X>>>2|W<<30)^(X>>>7|W<<25),s=(X>>>28|W<<4)^(W>>>2|X<<30)^(W>>>7|X<<25),i=(ih>>>14|eh<<18)^(ih>>>18|eh<<14)^(eh>>>9|ih<<23),e=(eh>>>14|ih<<18)^(eh>>>18|ih<<14)^(ih>>>9|eh<<23),c=W&K,p=X&L,v=c^W&P^f,_=p^X&Q^u,z=ih&Y^~ih&$,A=eh&Z^~eh&hh,k=G[h+2],w=G[h+3],g=a[h+2],x=a[h+3],l=(65535&x)+(65535&w)+(65535&A)+(65535&e)+(65535&sh),r=(x>>>16)+(w>>>16)+(A>>>16)+(e>>>16)+(sh>>>16)+(l>>>16),n=(65535&g)+(65535&k)+(65535&z)+(65535&i)+(65535&th)+(r>>>16),o=(g>>>16)+(k>>>16)+(z>>>16)+(i>>>16)+(th>>>16)+(n>>>16),k=o<<16|65535&n,w=r<<16|65535&l,l=(65535&_)+(65535&s),r=(_>>>16)+(s>>>16)+(l>>>16),n=(65535&v)+(65535&t)+(r>>>16),o=(v>>>16)+(t>>>16)+(n>>>16),g=o<<16|65535&n,x=r<<16|65535&l,l=(65535&T)+(65535&w),r=(T>>>16)+(w>>>16)+(l>>>16),n=(65535&R)+(65535&k)+(r>>>16),o=(R>>>16)+(k>>>16)+(n>>>16),th=o<<16|65535&n,sh=r<<16|65535&l,l=(65535&x)+(65535&w),r=(x>>>16)+(w>>>16)+(l>>>16),n=(65535&g)+(65535&k)+(r>>>16),o=(g>>>16)+(k>>>16)+(n>>>16),R=o<<16|65535&n,T=r<<16|65535&l,t=(R>>>28|T<<4)^(T>>>2|R<<30)^(T>>>7|R<<25),s=(T>>>28|R<<4)^(R>>>2|T<<30)^(R>>>7|T<<25),i=(th>>>14|sh<<18)^(th>>>18|sh<<14)^(sh>>>9|th<<23),e=(sh>>>14|th<<18)^(sh>>>18|th<<14)^(th>>>9|sh<<23),d=R&W,y=T&X,v=d^R&K^c,_=y^T&L^p,z=th&ih^~th&Y,A=sh&eh^~sh&Z,k=G[h+4],w=G[h+5],g=a[h+4],x=a[h+5],l=(65535&x)+(65535&w)+(65535&A)+(65535&e)+(65535&hh),r=(x>>>16)+(w>>>16)+(A>>>16)+(e>>>16)+(hh>>>16)+(l>>>16),n=(65535&g)+(65535&k)+(65535&z)+(65535&i)+(65535&$)+(r>>>16),o=(g>>>16)+(k>>>16)+(z>>>16)+(i>>>16)+($>>>16)+(n>>>16),k=o<<16|65535&n,w=r<<16|65535&l,l=(65535&_)+(65535&s),r=(_>>>16)+(s>>>16)+(l>>>16),n=(65535&v)+(65535&t)+(r>>>16),o=(v>>>16)+(t>>>16)+(n>>>16),g=o<<16|65535&n,x=r<<16|65535&l,l=(65535&Q)+(65535&w),r=(Q>>>16)+(w>>>16)+(l>>>16),n=(65535&P)+(65535&k)+(r>>>16),o=(P>>>16)+(k>>>16)+(n>>>16),$=o<<16|65535&n,hh=r<<16|65535&l,l=(65535&x)+(65535&w),r=(x>>>16)+(w>>>16)+(l>>>16),n=(65535&g)+(65535&k)+(r>>>16),o=(g>>>16)+(k>>>16)+(n>>>16),P=o<<16|65535&n,Q=r<<16|65535&l,t=(P>>>28|Q<<4)^(Q>>>2|P<<30)^(Q>>>7|P<<25),s=(Q>>>28|P<<4)^(P>>>2|Q<<30)^(P>>>7|Q<<25),i=($>>>14|hh<<18)^($>>>18|hh<<14)^(hh>>>9|$<<23),e=(hh>>>14|$<<18)^(hh>>>18|$<<14)^($>>>9|hh<<23),b=P&R,U=Q&T,v=b^P&W^d,_=U^Q&X^y,z=$&th^~$&ih,A=hh&sh^~hh&eh,k=G[h+6],w=G[h+7],g=a[h+6],x=a[h+7],l=(65535&x)+(65535&w)+(65535&A)+(65535&e)+(65535&Z),r=(x>>>16)+(w>>>16)+(A>>>16)+(e>>>16)+(Z>>>16)+(l>>>16),n=(65535&g)+(65535&k)+(65535&z)+(65535&i)+(65535&Y)+(r>>>16),o=(g>>>16)+(k>>>16)+(z>>>16)+(i>>>16)+(Y>>>16)+(n>>>16),k=o<<16|65535&n,w=r<<16|65535&l,l=(65535&_)+(65535&s),r=(_>>>16)+(s>>>16)+(l>>>16),n=(65535&v)+(65535&t)+(r>>>16),o=(v>>>16)+(t>>>16)+(n>>>16),g=o<<16|65535&n,x=r<<16|65535&l,l=(65535&L)+(65535&w),r=(L>>>16)+(w>>>16)+(l>>>16),n=(65535&K)+(65535&k)+(r>>>16),o=(K>>>16)+(k>>>16)+(n>>>16),Y=o<<16|65535&n,Z=r<<16|65535&l,l=(65535&x)+(65535&w),r=(x>>>16)+(w>>>16)+(l>>>16),n=(65535&g)+(65535&k)+(r>>>16),o=(g>>>16)+(k>>>16)+(n>>>16),K=o<<16|65535&n,L=r<<16|65535&l;l=(65535&S)+(65535&L),r=(S>>>16)+(L>>>16)+(l>>>16),n=(65535&B)+(65535&K)+(r>>>16),o=(B>>>16)+(K>>>16)+(n>>>16),this.h0h=o<<16|65535&n,this.h0l=r<<16|65535&l,l=(65535&m)+(65535&Q),r=(m>>>16)+(Q>>>16)+(l>>>16),n=(65535&O)+(65535&P)+(r>>>16),o=(O>>>16)+(P>>>16)+(n>>>16),this.h1h=o<<16|65535&n,this.h1l=r<<16|65535&l,l=(65535&N)+(65535&T),r=(N>>>16)+(T>>>16)+(l>>>16),n=(65535&J)+(65535&R)+(r>>>16),o=(J>>>16)+(R>>>16)+(n>>>16),this.h2h=o<<16|65535&n,this.h2l=r<<16|65535&l,l=(65535&C)+(65535&X),r=(C>>>16)+(X>>>16)+(l>>>16),n=(65535&j)+(65535&W)+(r>>>16),o=(j>>>16)+(W>>>16)+(n>>>16),this.h3h=o<<16|65535&n,this.h3l=r<<16|65535&l,l=(65535&H)+(65535&Z),r=(H>>>16)+(Z>>>16)+(l>>>16),n=(65535&D)+(65535&Y)+(r>>>16),o=(D>>>16)+(Y>>>16)+(n>>>16),this.h4h=o<<16|65535&n,this.h4l=r<<16|65535&l,l=(65535&M)+(65535&hh),r=(M>>>16)+(hh>>>16)+(l>>>16),n=(65535&I)+(65535&$)+(r>>>16),o=(I>>>16)+($>>>16)+(n>>>16),this.h5h=o<<16|65535&n,this.h5l=r<<16|65535&l,l=(65535&V)+(65535&sh),r=(V>>>16)+(sh>>>16)+(l>>>16),n=(65535&E)+(65535&th)+(r>>>16),o=(E>>>16)+(th>>>16)+(n>>>16),this.h6h=o<<16|65535&n,this.h6l=r<<16|65535&l,l=(65535&F)+(65535&eh),r=(F>>>16)+(eh>>>16)+(l>>>16),n=(65535&q)+(65535&ih)+(r>>>16),o=(q>>>16)+(ih>>>16)+(n>>>16),this.h7h=o<<16|65535&n,this.h7l=r<<16|65535&l},h.prototype.hex=function(){this.finalize();var h=this.h0h,t=this.h0l,s=this.h1h,i=this.h1l,e=this.h2h,l=this.h2l,n=this.h3h,o=this.h3l,a=this.h4h,f=this.h4l,u=this.h5h,c=this.h5l,p=this.h6h,d=this.h6l,y=this.h7h,b=this.h7l,U=this.bits,v=r[h>>28&15]+r[h>>24&15]+r[h>>20&15]+r[h>>16&15]+r[h>>12&15]+r[h>>8&15]+r[h>>4&15]+r[15&h]+r[t>>28&15]+r[t>>24&15]+r[t>>20&15]+r[t>>16&15]+r[t>>12&15]+r[t>>8&15]+r[t>>4&15]+r[15&t]+r[s>>28&15]+r[s>>24&15]+r[s>>20&15]+r[s>>16&15]+r[s>>12&15]+r[s>>8&15]+r[s>>4&15]+r[15&s]+r[i>>28&15]+r[i>>24&15]+r[i>>20&15]+r[i>>16&15]+r[i>>12&15]+r[i>>8&15]+r[i>>4&15]+r[15&i]+r[e>>28&15]+r[e>>24&15]+r[e>>20&15]+r[e>>16&15]+r[e>>12&15]+r[e>>8&15]+r[e>>4&15]+r[15&e]+r[l>>28&15]+r[l>>24&15]+r[l>>20&15]+r[l>>16&15]+r[l>>12&15]+r[l>>8&15]+r[l>>4&15]+r[15&l]+r[n>>28&15]+r[n>>24&15]+r[n>>20&15]+r[n>>16&15]+r[n>>12&15]+r[n>>8&15]+r[n>>4&15]+r[15&n];return U>=256&&(v+=r[o>>28&15]+r[o>>24&15]+r[o>>20&15]+r[o>>16&15]+r[o>>12&15]+r[o>>8&15]+r[o>>4&15]+r[15&o]),U>=384&&(v+=r[a>>28&15]+r[a>>24&15]+r[a>>20&15]+r[a>>16&15]+r[a>>12&15]+r[a>>8&15]+r[a>>4&15]+r[15&a]+r[f>>28&15]+r[f>>24&15]+r[f>>20&15]+r[f>>16&15]+r[f>>12&15]+r[f>>8&15]+r[f>>4&15]+r[15&f]+r[u>>28&15]+r[u>>24&15]+r[u>>20&15]+r[u>>16&15]+r[u>>12&15]+r[u>>8&15]+r[u>>4&15]+r[15&u]+r[c>>28&15]+r[c>>24&15]+r[c>>20&15]+r[c>>16&15]+r[c>>12&15]+r[c>>8&15]+r[c>>4&15]+r[15&c]),512==U&&(v+=r[p>>28&15]+r[p>>24&15]+r[p>>20&15]+r[p>>16&15]+r[p>>12&15]+r[p>>8&15]+r[p>>4&15]+r[15&p]+r[d>>28&15]+r[d>>24&15]+r[d>>20&15]+r[d>>16&15]+r[d>>12&15]+r[d>>8&15]+r[d>>4&15]+r[15&d]+r[y>>28&15]+r[y>>24&15]+r[y>>20&15]+r[y>>16&15]+r[y>>12&15]+r[y>>8&15]+r[y>>4&15]+r[15&y]+r[b>>28&15]+r[b>>24&15]+r[b>>20&15]+r[b>>16&15]+r[b>>12&15]+r[b>>8&15]+r[b>>4&15]+r[15&b]),v},h.prototype.toString=h.prototype.hex,h.prototype.digest=function(){this.finalize();var h=this.h0h,t=this.h0l,s=this.h1h,i=this.h1l,e=this.h2h,l=this.h2l,r=this.h3h,n=this.h3l,o=this.h4h,a=this.h4l,f=this.h5h,u=this.h5l,c=this.h6h,p=this.h6l,d=this.h7h,y=this.h7l,b=this.bits,U=[h>>24&255,h>>16&255,h>>8&255,255&h,t>>24&255,t>>16&255,t>>8&255,255&t,s>>24&255,s>>16&255,s>>8&255,255&s,i>>24&255,i>>16&255,i>>8&255,255&i,e>>24&255,e>>16&255,e>>8&255,255&e,l>>24&255,l>>16&255,l>>8&255,255&l,r>>24&255,r>>16&255,r>>8&255,255&r];return b>=256&&U.push(n>>24&255,n>>16&255,n>>8&255,255&n),b>=384&&U.push(o>>24&255,o>>16&255,o>>8&255,255&o,a>>24&255,a>>16&255,a>>8&255,255&a,f>>24&255,f>>16&255,f>>8&255,255&f,u>>24&255,u>>16&255,u>>8&255,255&u),512==b&&U.push(c>>24&255,c>>16&255,c>>8&255,255&c,p>>24&255,p>>16&255,p>>8&255,255&p,d>>24&255,d>>16&255,d>>8&255,255&d,y>>24&255,y>>16&255,y>>8&255,255&y),U},h.prototype.array=h.prototype.digest,h.prototype.arrayBuffer=function(){this.finalize();var h=this.bits,t=new ArrayBuffer(h/8),s=new DataView(t);return s.setUint32(0,this.h0h),s.setUint32(4,this.h0l),s.setUint32(8,this.h1h),s.setUint32(12,this.h1l),s.setUint32(16,this.h2h),s.setUint32(20,this.h2l),s.setUint32(24,this.h3h),h>=256&&s.setUint32(28,this.h3l),h>=384&&(s.setUint32(32,this.h4h),s.setUint32(36,this.h4l),s.setUint32(40,this.h5h),s.setUint32(44,this.h5l)),512==h&&(s.setUint32(48,this.h6h),s.setUint32(52,this.h6l),s.setUint32(56,this.h7h),s.setUint32(60,this.h7l)),t};var d=p(512);d.sha512=d,d.sha384=p(384),d.sha512_256=p(256),d.sha512_224=p(224),i?module.exports=d:(t.sha512=d.sha512,t.sha384=d.sha384,t.sha512_256=d.sha512_256,t.sha512_224=d.sha512_224,e&&define(function(){return d}))}(); \ No newline at end of file diff --git a/package.json b/package.json index b75d473..7583a0d 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "js-sha512", - "version": "0.3.0", + "version": "0.4.0", "description": "This is a simple SHA-512, SHA-384, SHA-512/224, SHA-512/256 hash functions for JavaScript supports UTF-8 encoding.", "main": "src/sha512.js", "devDependencies": { diff --git a/src/sha512.js b/src/sha512.js index 3e32d96..a179a3a 100644 --- a/src/sha512.js +++ b/src/sha512.js @@ -1,7 +1,7 @@ /* * [js-sha512]{@link https://github.com/emn178/js-sha512} * - * @version 0.3.0 + * @version 0.4.0 * @author Chen, Yi-Cyuan [emn178@gmail.com] * @copyright Chen, Yi-Cyuan 2014-2017 * @license MIT @@ -17,10 +17,11 @@ } var COMMON_JS = !root.JS_SHA512_NO_COMMON_JS && typeof module === 'object' && module.exports; var AMD = typeof define === 'function' && define.amd; + var ARRAY_BUFFER = typeof ArrayBuffer !== 'undefined'; var HEX_CHARS = '0123456789abcdef'.split(''); var EXTRA = [-2147483648, 8388608, 32768, 128]; var SHIFT = [24, 16, 8, 0]; - var K =[ + var K = [ 0x428A2F98, 0xD728AE22, 0x71374491, 0x23EF65CD, 0xB5C0FBCF, 0xEC4D3B2F, 0xE9B5DBA5, 0x8189DBBC, 0x3956C25B, 0xF348B538, 0x59F111F1, 0xB605D019, @@ -63,101 +64,199 @@ 0x5FCB6FAB, 0x3AD6FAEC, 0x6C44198C, 0x4A475817 ]; - var blocks = []; + var OUTPUT_TYPES = ['hex', 'array', 'digest', 'arrayBuffer']; - var sha384 = function (message) { - return sha512(message, 384); - }; + var blocks = []; - var sha512_256 = function (message) { - return sha512(message, 256); + var createOutputMethod = function (outputType, bits) { + return function (message) { + return new Sha512(bits, true).update(message)[outputType](); + }; }; - var sha512_224 = function (message) { - return sha512(message, 224); + var createMethod = function (bits) { + var method = createOutputMethod('hex', bits); + method.create = function () { + return new Sha512(bits); + }; + method.update = function (message) { + return method.create().update(message); + }; + for (var i = 0; i < OUTPUT_TYPES.length; ++i) { + var type = OUTPUT_TYPES[i]; + method[type] = createOutputMethod(type, bits); + } + return method; }; - var sha512 = function (message, bits) { - var h0h, h0l, h1h, h1l, h2h, h2l, h3h, h3l, - h4h, h4l, h5h, h5l, h6h, h6l, h7h, h7l, block, code, end = false, - i, j, index = 0, start = 0, bytes = 0, length = message.length, - s0h, s0l, s1h, s1l, c1, c2, c3, c4, - abh, abl, dah, dal, cdh, cdl, bch, bcl, - majh, majl, t1h, t1l, t2h, t2l, chh, chl; + function Sha512(bits, sharedMemory) { + if (sharedMemory) { + blocks[0] = blocks[1] = blocks[2] = blocks[3] = blocks[4] = + blocks[5] = blocks[6] = blocks[7] = blocks[8] = + blocks[9] = blocks[10] = blocks[11] = blocks[12] = + blocks[13] = blocks[14] = blocks[15] = blocks[16] = + blocks[17] = blocks[18] = blocks[19] = blocks[20] = + blocks[21] = blocks[22] = blocks[23] = blocks[24] = + blocks[25] = blocks[26] = blocks[27] = blocks[28] = + blocks[29] = blocks[30] = blocks[31] = blocks[32] = 0; + this.blocks = blocks; + } else { + this.blocks = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0]; + } if (bits == 384) { - h0h = 0xCBBB9D5D; - h0l = 0xC1059ED8; - h1h = 0x629A292A; - h1l = 0x367CD507; - h2h = 0x9159015A; - h2l = 0x3070DD17; - h3h = 0x152FECD8; - h3l = 0xF70E5939; - h4h = 0x67332667; - h4l = 0xFFC00B31; - h5h = 0x8EB44A87; - h5l = 0x68581511; - h6h = 0xDB0C2E0D; - h6l = 0x64F98FA7; - h7h = 0x47B5481D; - h7l = 0xBEFA4FA4; + this.h0h = 0xCBBB9D5D; + this.h0l = 0xC1059ED8; + this.h1h = 0x629A292A; + this.h1l = 0x367CD507; + this.h2h = 0x9159015A; + this.h2l = 0x3070DD17; + this.h3h = 0x152FECD8; + this.h3l = 0xF70E5939; + this.h4h = 0x67332667; + this.h4l = 0xFFC00B31; + this.h5h = 0x8EB44A87; + this.h5l = 0x68581511; + this.h6h = 0xDB0C2E0D; + this.h6l = 0x64F98FA7; + this.h7h = 0x47B5481D; + this.h7l = 0xBEFA4FA4; } else if (bits == 256) { - h0h = 0x22312194; - h0l = 0xFC2BF72C; - h1h = 0x9F555FA3; - h1l = 0xC84C64C2; - h2h = 0x2393B86B; - h2l = 0x6F53B151; - h3h = 0x96387719; - h3l = 0x5940EABD; - h4h = 0x96283EE2; - h4l = 0xA88EFFE3; - h5h = 0xBE5E1E25; - h5l = 0x53863992; - h6h = 0x2B0199FC; - h6l = 0x2C85B8AA; - h7h = 0x0EB72DDC; - h7l = 0x81C52CA2; + this.h0h = 0x22312194; + this.h0l = 0xFC2BF72C; + this.h1h = 0x9F555FA3; + this.h1l = 0xC84C64C2; + this.h2h = 0x2393B86B; + this.h2l = 0x6F53B151; + this.h3h = 0x96387719; + this.h3l = 0x5940EABD; + this.h4h = 0x96283EE2; + this.h4l = 0xA88EFFE3; + this.h5h = 0xBE5E1E25; + this.h5l = 0x53863992; + this.h6h = 0x2B0199FC; + this.h6l = 0x2C85B8AA; + this.h7h = 0x0EB72DDC; + this.h7l = 0x81C52CA2; } else if (bits == 224) { - h0h = 0x8C3D37C8; - h0l = 0x19544DA2; - h1h = 0x73E19966; - h1l = 0x89DCD4D6; - h2h = 0x1DFAB7AE; - h2l = 0x32FF9C82; - h3h = 0x679DD514; - h3l = 0x582F9FCF; - h4h = 0x0F6D2B69; - h4l = 0x7BD44DA8; - h5h = 0x77E36F73; - h5l = 0x04C48942; - h6h = 0x3F9D85A8; - h6l = 0x6A1D36C8; - h7h = 0x1112E6AD; - h7l = 0x91D692A1; + this.h0h = 0x8C3D37C8; + this.h0l = 0x19544DA2; + this.h1h = 0x73E19966; + this.h1l = 0x89DCD4D6; + this.h2h = 0x1DFAB7AE; + this.h2l = 0x32FF9C82; + this.h3h = 0x679DD514; + this.h3l = 0x582F9FCF; + this.h4h = 0x0F6D2B69; + this.h4l = 0x7BD44DA8; + this.h5h = 0x77E36F73; + this.h5l = 0x04C48942; + this.h6h = 0x3F9D85A8; + this.h6l = 0x6A1D36C8; + this.h7h = 0x1112E6AD; + this.h7l = 0x91D692A1; } else { // 512 - h0h = 0x6A09E667; - h0l = 0xF3BCC908; - h1h = 0xBB67AE85; - h1l = 0x84CAA73B; - h2h = 0x3C6EF372; - h2l = 0xFE94F82B; - h3h = 0xA54FF53A; - h3l = 0x5F1D36F1; - h4h = 0x510E527F; - h4l = 0xADE682D1; - h5h = 0x9B05688C; - h5l = 0x2B3E6C1F; - h6h = 0x1F83D9AB; - h6l = 0xFB41BD6B; - h7h = 0x5BE0CD19; - h7l = 0x137E2179; - bits = 512; + this.h0h = 0x6A09E667; + this.h0l = 0xF3BCC908; + this.h1h = 0xBB67AE85; + this.h1l = 0x84CAA73B; + this.h2h = 0x3C6EF372; + this.h2l = 0xFE94F82B; + this.h3h = 0xA54FF53A; + this.h3l = 0x5F1D36F1; + this.h4h = 0x510E527F; + this.h4l = 0xADE682D1; + this.h5h = 0x9B05688C; + this.h5l = 0x2B3E6C1F; + this.h6h = 0x1F83D9AB; + this.h6l = 0xFB41BD6B; + this.h7h = 0x5BE0CD19; + this.h7l = 0x137E2179; + } + this.bits = bits; + + this.block = this.start = this.bytes = 0; + this.finalized = this.hashed = false; + } + + Sha512.prototype.update = function (message) { + if (this.finalized) { + return; + } + var notString = typeof(message) !== 'string'; + if (notString && ARRAY_BUFFER && message instanceof root.ArrayBuffer) { + message = new Uint8Array(message); } - block = 0; - do { - blocks[0] = block; + var code, index = 0, i, length = message.length || 0, blocks = this.blocks; + + while (index < length) { + if (this.hashed) { + this.hashed = false; + blocks[0] = this.block; + blocks[1] = blocks[2] = blocks[3] = blocks[4] = + blocks[5] = blocks[6] = blocks[7] = blocks[8] = + blocks[9] = blocks[10] = blocks[11] = blocks[12] = + blocks[13] = blocks[14] = blocks[15] = blocks[16] = + blocks[17] = blocks[18] = blocks[19] = blocks[20] = + blocks[21] = blocks[22] = blocks[23] = blocks[24] = + blocks[25] = blocks[26] = blocks[27] = blocks[28] = + blocks[29] = blocks[30] = blocks[31] = blocks[32] = 0; + } + + if(notString) { + for (i = this.start; index < length && i < 128; ++index) { + blocks[i >> 2] |= message[index] << SHIFT[i++ & 3]; + } + } else { + for (i = this.start; index < length && i < 128; ++index) { + code = message.charCodeAt(index); + if (code < 0x80) { + blocks[i >> 2] |= code << SHIFT[i++ & 3]; + } else if (code < 0x800) { + blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } else if (code < 0xd800 || code >= 0xe000) { + blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } else { + code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff)); + blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; + blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; + } + } + } + + this.lastByteIndex = i; + this.bytes += i - this.start; + if (i >= 128) { + this.block = blocks[32]; + this.start = i - 128; + this.hash(); + this.hashed = true; + } else { + this.start = i; + } + } + return this; + }; + + Sha512.prototype.finalize = function () { + if (this.finalized) { + return; + } + this.finalized = true; + var blocks = this.blocks, i = this.lastByteIndex; + blocks[32] = this.block; + blocks[i >> 2] |= EXTRA[i & 3]; + this.block = blocks[32]; + if (i >= 112) { + if (!this.hashed) { + this.hash(); + } + blocks[0] = this.block; blocks[1] = blocks[2] = blocks[3] = blocks[4] = blocks[5] = blocks[6] = blocks[7] = blocks[8] = blocks[9] = blocks[10] = blocks[11] = blocks[12] = @@ -166,335 +265,327 @@ blocks[21] = blocks[22] = blocks[23] = blocks[24] = blocks[25] = blocks[26] = blocks[27] = blocks[28] = blocks[29] = blocks[30] = blocks[31] = blocks[32] = 0; - for (i = start; index < length && i < 128; ++index) { - code = message.charCodeAt(index); - if (code < 0x80) { - blocks[i >> 2] |= code << SHIFT[i++ & 3]; - } else if (code < 0x800) { - blocks[i >> 2] |= (0xc0 | (code >> 6)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; - } else if (code < 0xd800 || code >= 0xe000) { - blocks[i >> 2] |= (0xe0 | (code >> 12)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; - } else { - code = 0x10000 + (((code & 0x3ff) << 10) | (message.charCodeAt(++index) & 0x3ff)); - blocks[i >> 2] |= (0xf0 | (code >> 18)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | ((code >> 12) & 0x3f)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | ((code >> 6) & 0x3f)) << SHIFT[i++ & 3]; - blocks[i >> 2] |= (0x80 | (code & 0x3f)) << SHIFT[i++ & 3]; - } - } - bytes += i - start; - start = i - 128; - if (index == length) { - blocks[i >> 2] |= EXTRA[i & 3]; - ++index; - } - block = blocks[32]; - if (index > length && i < 112) { - blocks[31] = bytes << 3; - end = true; - } + } + blocks[31] = this.bytes << 3; + this.hash(); + }; - for (j = 32; j < 160; j += 2) { - t1h = blocks[j - 30]; - t1l = blocks[j - 29]; - s0h = ((t1h >>> 1) | (t1l << 31)) ^ ((t1h >>> 8) | (t1l << 24)) ^ (t1h >>> 7); - s0l = ((t1l >>> 1) | (t1h << 31)) ^ ((t1l >>> 8) | (t1h << 24)) ^ ((t1l >>> 7) | t1h << 25); - - t1h = blocks[j - 4]; - t1l = blocks[j - 3]; - s1h = ((t1h >>> 19) | (t1l << 13)) ^ ((t1l >>> 29) | (t1h << 3)) ^ (t1h >>> 6); - s1l = ((t1l >>> 19) | (t1h << 13)) ^ ((t1h >>> 29) | (t1l << 3)) ^ ((t1l >>> 6) | t1h << 26); - - t1h = blocks[j - 32]; - t1l = blocks[j - 31]; - t2h = blocks[j - 14]; - t2l = blocks[j - 13]; - - c1 = (t2l & 0xFFFF) + (t1l & 0xFFFF) + (s0l & 0xFFFF) + (s1l & 0xFFFF); - c2 = (t2l >>> 16) + (t1l >>> 16) + (s0l >>> 16) + (s1l >>> 16) + (c1 >>> 16); - c3 = (t2h & 0xFFFF) + (t1h & 0xFFFF) + (s0h & 0xFFFF) + (s1h & 0xFFFF) + (c2 >>> 16); - c4 = (t2h >>> 16) + (t1h >>> 16) + (s0h >>> 16) + (s1h >>> 16) + (c3 >>> 16); - - blocks[j] = (c4 << 16) | (c3 & 0xFFFF); - blocks[j + 1] = (c2 << 16) | (c1 & 0xFFFF); - } + Sha512.prototype.hash = function () { + var h0h = this.h0h, h0l = this.h0l, h1h = this.h1h, h1l = this.h1l, + h2h = this.h2h, h2l = this.h2l, h3h = this.h3h, h3l = this.h3l, + h4h = this.h4h, h4l = this.h4l, h5h = this.h5h, h5l = this.h5l, + h6h = this.h6h, h6l = this.h6l, h7h = this.h7h, h7l = this.h7l, + blocks = this.blocks, j, s0h, s0l, s1h, s1l, c1, c2, c3, c4, + abh, abl, dah, dal, cdh, cdl, bch, bcl, + majh, majl, t1h, t1l, t2h, t2l, chh, chl; - var ah = h0h, al = h0l, bh = h1h, bl = h1l, ch = h2h, cl = h2l, dh = h3h, dl = h3l, eh = h4h, el = h4l, fh = h5h, fl = h5l, gh = h6h, gl = h6l, hh = h7h, hl = h7l; - bch = bh & ch; - bcl = bl & cl; - for (j = 0; j < 160; j += 8) { - s0h = ((ah >>> 28) | (al << 4)) ^ ((al >>> 2) | (ah << 30)) ^ ((al >>> 7) | (ah << 25)); - s0l = ((al >>> 28) | (ah << 4)) ^ ((ah >>> 2) | (al << 30)) ^ ((ah >>> 7) | (al << 25)); + for (j = 32; j < 160; j += 2) { + t1h = blocks[j - 30]; + t1l = blocks[j - 29]; + s0h = ((t1h >>> 1) | (t1l << 31)) ^ ((t1h >>> 8) | (t1l << 24)) ^ (t1h >>> 7); + s0l = ((t1l >>> 1) | (t1h << 31)) ^ ((t1l >>> 8) | (t1h << 24)) ^ ((t1l >>> 7) | t1h << 25); + + t1h = blocks[j - 4]; + t1l = blocks[j - 3]; + s1h = ((t1h >>> 19) | (t1l << 13)) ^ ((t1l >>> 29) | (t1h << 3)) ^ (t1h >>> 6); + s1l = ((t1l >>> 19) | (t1h << 13)) ^ ((t1h >>> 29) | (t1l << 3)) ^ ((t1l >>> 6) | t1h << 26); + + t1h = blocks[j - 32]; + t1l = blocks[j - 31]; + t2h = blocks[j - 14]; + t2l = blocks[j - 13]; + + c1 = (t2l & 0xFFFF) + (t1l & 0xFFFF) + (s0l & 0xFFFF) + (s1l & 0xFFFF); + c2 = (t2l >>> 16) + (t1l >>> 16) + (s0l >>> 16) + (s1l >>> 16) + (c1 >>> 16); + c3 = (t2h & 0xFFFF) + (t1h & 0xFFFF) + (s0h & 0xFFFF) + (s1h & 0xFFFF) + (c2 >>> 16); + c4 = (t2h >>> 16) + (t1h >>> 16) + (s0h >>> 16) + (s1h >>> 16) + (c3 >>> 16); + + blocks[j] = (c4 << 16) | (c3 & 0xFFFF); + blocks[j + 1] = (c2 << 16) | (c1 & 0xFFFF); + } - s1h = ((eh >>> 14) | (el << 18)) ^ ((eh >>> 18) | (el << 14)) ^ ((el >>> 9) | (eh << 23)); - s1l = ((el >>> 14) | (eh << 18)) ^ ((el >>> 18) | (eh << 14)) ^ ((eh >>> 9) | (el << 23)); + var ah = h0h, al = h0l, bh = h1h, bl = h1l, ch = h2h, cl = h2l, dh = h3h, dl = h3l, eh = h4h, el = h4l, fh = h5h, fl = h5l, gh = h6h, gl = h6l, hh = h7h, hl = h7l; + bch = bh & ch; + bcl = bl & cl; + for (j = 0; j < 160; j += 8) { + s0h = ((ah >>> 28) | (al << 4)) ^ ((al >>> 2) | (ah << 30)) ^ ((al >>> 7) | (ah << 25)); + s0l = ((al >>> 28) | (ah << 4)) ^ ((ah >>> 2) | (al << 30)) ^ ((ah >>> 7) | (al << 25)); - abh = ah & bh; - abl = al & bl; - majh = abh ^ (ah & ch) ^ bch; - majl = abl ^ (al & cl) ^ bcl; + s1h = ((eh >>> 14) | (el << 18)) ^ ((eh >>> 18) | (el << 14)) ^ ((el >>> 9) | (eh << 23)); + s1l = ((el >>> 14) | (eh << 18)) ^ ((el >>> 18) | (eh << 14)) ^ ((eh >>> 9) | (el << 23)); - chh = (eh & fh) ^ (~eh & gh); - chl = (el & fl) ^ (~el & gl); + abh = ah & bh; + abl = al & bl; + majh = abh ^ (ah & ch) ^ bch; + majl = abl ^ (al & cl) ^ bcl; - t1h = blocks[j]; - t1l = blocks[j + 1]; - t2h = K[j]; - t2l = K[j + 1]; + chh = (eh & fh) ^ (~eh & gh); + chl = (el & fl) ^ (~el & gl); - c1 = (t2l & 0xFFFF) + (t1l & 0xFFFF) + (chl & 0xFFFF) + (s1l & 0xFFFF) + (hl & 0xFFFF); - c2 = (t2l >>> 16) + (t1l >>> 16) + (chl >>> 16) + (s1l >>> 16) + (hl >>> 16) + (c1 >>> 16); - c3 = (t2h & 0xFFFF) + (t1h & 0xFFFF) + (chh & 0xFFFF) + (s1h & 0xFFFF) + (hh & 0xFFFF) + (c2 >>> 16); - c4 = (t2h >>> 16) + (t1h >>> 16) + (chh >>> 16) + (s1h >>> 16) + (hh >>> 16) + (c3 >>> 16); + t1h = blocks[j]; + t1l = blocks[j + 1]; + t2h = K[j]; + t2l = K[j + 1]; - t1h = (c4 << 16) | (c3 & 0xFFFF); - t1l = (c2 << 16) | (c1 & 0xFFFF); + c1 = (t2l & 0xFFFF) + (t1l & 0xFFFF) + (chl & 0xFFFF) + (s1l & 0xFFFF) + (hl & 0xFFFF); + c2 = (t2l >>> 16) + (t1l >>> 16) + (chl >>> 16) + (s1l >>> 16) + (hl >>> 16) + (c1 >>> 16); + c3 = (t2h & 0xFFFF) + (t1h & 0xFFFF) + (chh & 0xFFFF) + (s1h & 0xFFFF) + (hh & 0xFFFF) + (c2 >>> 16); + c4 = (t2h >>> 16) + (t1h >>> 16) + (chh >>> 16) + (s1h >>> 16) + (hh >>> 16) + (c3 >>> 16); - c1 = (majl & 0xFFFF) + (s0l & 0xFFFF); - c2 = (majl >>> 16) + (s0l >>> 16) + (c1 >>> 16); - c3 = (majh & 0xFFFF) + (s0h & 0xFFFF) + (c2 >>> 16); - c4 = (majh >>> 16) + (s0h >>> 16) + (c3 >>> 16); + t1h = (c4 << 16) | (c3 & 0xFFFF); + t1l = (c2 << 16) | (c1 & 0xFFFF); - t2h = (c4 << 16) | (c3 & 0xFFFF); - t2l = (c2 << 16) | (c1 & 0xFFFF); + c1 = (majl & 0xFFFF) + (s0l & 0xFFFF); + c2 = (majl >>> 16) + (s0l >>> 16) + (c1 >>> 16); + c3 = (majh & 0xFFFF) + (s0h & 0xFFFF) + (c2 >>> 16); + c4 = (majh >>> 16) + (s0h >>> 16) + (c3 >>> 16); - c1 = (dl & 0xFFFF) + (t1l & 0xFFFF); - c2 = (dl >>> 16) + (t1l >>> 16) + (c1 >>> 16); - c3 = (dh & 0xFFFF) + (t1h & 0xFFFF) + (c2 >>> 16); - c4 = (dh >>> 16) + (t1h >>> 16) + (c3 >>> 16); + t2h = (c4 << 16) | (c3 & 0xFFFF); + t2l = (c2 << 16) | (c1 & 0xFFFF); - hh = (c4 << 16) | (c3 & 0xFFFF); - hl = (c2 << 16) | (c1 & 0xFFFF); + c1 = (dl & 0xFFFF) + (t1l & 0xFFFF); + c2 = (dl >>> 16) + (t1l >>> 16) + (c1 >>> 16); + c3 = (dh & 0xFFFF) + (t1h & 0xFFFF) + (c2 >>> 16); + c4 = (dh >>> 16) + (t1h >>> 16) + (c3 >>> 16); - c1 = (t2l & 0xFFFF) + (t1l & 0xFFFF); - c2 = (t2l >>> 16) + (t1l >>> 16) + (c1 >>> 16); - c3 = (t2h & 0xFFFF) + (t1h & 0xFFFF) + (c2 >>> 16); - c4 = (t2h >>> 16) + (t1h >>> 16) + (c3 >>> 16); + hh = (c4 << 16) | (c3 & 0xFFFF); + hl = (c2 << 16) | (c1 & 0xFFFF); - dh = (c4 << 16) | (c3 & 0xFFFF); - dl = (c2 << 16) | (c1 & 0xFFFF); + c1 = (t2l & 0xFFFF) + (t1l & 0xFFFF); + c2 = (t2l >>> 16) + (t1l >>> 16) + (c1 >>> 16); + c3 = (t2h & 0xFFFF) + (t1h & 0xFFFF) + (c2 >>> 16); + c4 = (t2h >>> 16) + (t1h >>> 16) + (c3 >>> 16); - s0h = ((dh >>> 28) | (dl << 4)) ^ ((dl >>> 2) | (dh << 30)) ^ ((dl >>> 7) | (dh << 25)); - s0l = ((dl >>> 28) | (dh << 4)) ^ ((dh >>> 2) | (dl << 30)) ^ ((dh >>> 7) | (dl << 25)); + dh = (c4 << 16) | (c3 & 0xFFFF); + dl = (c2 << 16) | (c1 & 0xFFFF); - s1h = ((hh >>> 14) | (hl << 18)) ^ ((hh >>> 18) | (hl << 14)) ^ ((hl >>> 9) | (hh << 23)); - s1l = ((hl >>> 14) | (hh << 18)) ^ ((hl >>> 18) | (hh << 14)) ^ ((hh >>> 9) | (hl << 23)); + s0h = ((dh >>> 28) | (dl << 4)) ^ ((dl >>> 2) | (dh << 30)) ^ ((dl >>> 7) | (dh << 25)); + s0l = ((dl >>> 28) | (dh << 4)) ^ ((dh >>> 2) | (dl << 30)) ^ ((dh >>> 7) | (dl << 25)); - dah = dh & ah; - dal = dl & al; - majh = dah ^ (dh & bh) ^ abh; - majl = dal ^ (dl & bl) ^ abl; + s1h = ((hh >>> 14) | (hl << 18)) ^ ((hh >>> 18) | (hl << 14)) ^ ((hl >>> 9) | (hh << 23)); + s1l = ((hl >>> 14) | (hh << 18)) ^ ((hl >>> 18) | (hh << 14)) ^ ((hh >>> 9) | (hl << 23)); - chh = (hh & eh) ^ (~hh & fh); - chl = (hl & el) ^ (~hl & fl); + dah = dh & ah; + dal = dl & al; + majh = dah ^ (dh & bh) ^ abh; + majl = dal ^ (dl & bl) ^ abl; - t1h = blocks[j + 2]; - t1l = blocks[j + 3]; - t2h = K[j + 2]; - t2l = K[j + 3]; + chh = (hh & eh) ^ (~hh & fh); + chl = (hl & el) ^ (~hl & fl); - c1 = (t2l & 0xFFFF) + (t1l & 0xFFFF) + (chl & 0xFFFF) + (s1l & 0xFFFF) + (gl & 0xFFFF); - c2 = (t2l >>> 16) + (t1l >>> 16) + (chl >>> 16) + (s1l >>> 16) + (gl >>> 16) + (c1 >>> 16); - c3 = (t2h & 0xFFFF) + (t1h & 0xFFFF) + (chh & 0xFFFF) + (s1h & 0xFFFF) + (gh & 0xFFFF) + (c2 >>> 16); - c4 = (t2h >>> 16) + (t1h >>> 16) + (chh >>> 16) + (s1h >>> 16) + (gh >>> 16) + (c3 >>> 16); + t1h = blocks[j + 2]; + t1l = blocks[j + 3]; + t2h = K[j + 2]; + t2l = K[j + 3]; - t1h = (c4 << 16) | (c3 & 0xFFFF); - t1l = (c2 << 16) | (c1 & 0xFFFF); + c1 = (t2l & 0xFFFF) + (t1l & 0xFFFF) + (chl & 0xFFFF) + (s1l & 0xFFFF) + (gl & 0xFFFF); + c2 = (t2l >>> 16) + (t1l >>> 16) + (chl >>> 16) + (s1l >>> 16) + (gl >>> 16) + (c1 >>> 16); + c3 = (t2h & 0xFFFF) + (t1h & 0xFFFF) + (chh & 0xFFFF) + (s1h & 0xFFFF) + (gh & 0xFFFF) + (c2 >>> 16); + c4 = (t2h >>> 16) + (t1h >>> 16) + (chh >>> 16) + (s1h >>> 16) + (gh >>> 16) + (c3 >>> 16); - c1 = (majl & 0xFFFF) + (s0l & 0xFFFF); - c2 = (majl >>> 16) + (s0l >>> 16) + (c1 >>> 16); - c3 = (majh & 0xFFFF) + (s0h & 0xFFFF) + (c2 >>> 16); - c4 = (majh >>> 16) + (s0h >>> 16) + (c3 >>> 16); + t1h = (c4 << 16) | (c3 & 0xFFFF); + t1l = (c2 << 16) | (c1 & 0xFFFF); - t2h = (c4 << 16) | (c3 & 0xFFFF); - t2l = (c2 << 16) | (c1 & 0xFFFF); + c1 = (majl & 0xFFFF) + (s0l & 0xFFFF); + c2 = (majl >>> 16) + (s0l >>> 16) + (c1 >>> 16); + c3 = (majh & 0xFFFF) + (s0h & 0xFFFF) + (c2 >>> 16); + c4 = (majh >>> 16) + (s0h >>> 16) + (c3 >>> 16); - c1 = (cl & 0xFFFF) + (t1l & 0xFFFF); - c2 = (cl >>> 16) + (t1l >>> 16) + (c1 >>> 16); - c3 = (ch & 0xFFFF) + (t1h & 0xFFFF) + (c2 >>> 16); - c4 = (ch >>> 16) + (t1h >>> 16) + (c3 >>> 16); + t2h = (c4 << 16) | (c3 & 0xFFFF); + t2l = (c2 << 16) | (c1 & 0xFFFF); - gh = (c4 << 16) | (c3 & 0xFFFF); - gl = (c2 << 16) | (c1 & 0xFFFF); + c1 = (cl & 0xFFFF) + (t1l & 0xFFFF); + c2 = (cl >>> 16) + (t1l >>> 16) + (c1 >>> 16); + c3 = (ch & 0xFFFF) + (t1h & 0xFFFF) + (c2 >>> 16); + c4 = (ch >>> 16) + (t1h >>> 16) + (c3 >>> 16); - c1 = (t2l & 0xFFFF) + (t1l & 0xFFFF); - c2 = (t2l >>> 16) + (t1l >>> 16) + (c1 >>> 16); - c3 = (t2h & 0xFFFF) + (t1h & 0xFFFF) + (c2 >>> 16); - c4 = (t2h >>> 16) + (t1h >>> 16) + (c3 >>> 16); + gh = (c4 << 16) | (c3 & 0xFFFF); + gl = (c2 << 16) | (c1 & 0xFFFF); - ch = (c4 << 16) | (c3 & 0xFFFF); - cl = (c2 << 16) | (c1 & 0xFFFF); + c1 = (t2l & 0xFFFF) + (t1l & 0xFFFF); + c2 = (t2l >>> 16) + (t1l >>> 16) + (c1 >>> 16); + c3 = (t2h & 0xFFFF) + (t1h & 0xFFFF) + (c2 >>> 16); + c4 = (t2h >>> 16) + (t1h >>> 16) + (c3 >>> 16); - s0h = ((ch >>> 28) | (cl << 4)) ^ ((cl >>> 2) | (ch << 30)) ^ ((cl >>> 7) | (ch << 25)); - s0l = ((cl >>> 28) | (ch << 4)) ^ ((ch >>> 2) | (cl << 30)) ^ ((ch >>> 7) | (cl << 25)); + ch = (c4 << 16) | (c3 & 0xFFFF); + cl = (c2 << 16) | (c1 & 0xFFFF); - s1h = ((gh >>> 14) | (gl << 18)) ^ ((gh >>> 18) | (gl << 14)) ^ ((gl >>> 9) | (gh << 23)); - s1l = ((gl >>> 14) | (gh << 18)) ^ ((gl >>> 18) | (gh << 14)) ^ ((gh >>> 9) | (gl << 23)); + s0h = ((ch >>> 28) | (cl << 4)) ^ ((cl >>> 2) | (ch << 30)) ^ ((cl >>> 7) | (ch << 25)); + s0l = ((cl >>> 28) | (ch << 4)) ^ ((ch >>> 2) | (cl << 30)) ^ ((ch >>> 7) | (cl << 25)); - cdh = ch & dh; - cdl = cl & dl; - majh = cdh ^ (ch & ah) ^ dah; - majl = cdl ^ (cl & al) ^ dal; + s1h = ((gh >>> 14) | (gl << 18)) ^ ((gh >>> 18) | (gl << 14)) ^ ((gl >>> 9) | (gh << 23)); + s1l = ((gl >>> 14) | (gh << 18)) ^ ((gl >>> 18) | (gh << 14)) ^ ((gh >>> 9) | (gl << 23)); - chh = (gh & hh) ^ (~gh & eh); - chl = (gl & hl) ^ (~gl & el); + cdh = ch & dh; + cdl = cl & dl; + majh = cdh ^ (ch & ah) ^ dah; + majl = cdl ^ (cl & al) ^ dal; - t1h = blocks[j + 4]; - t1l = blocks[j + 5]; - t2h = K[j + 4]; - t2l = K[j + 5]; + chh = (gh & hh) ^ (~gh & eh); + chl = (gl & hl) ^ (~gl & el); - c1 = (t2l & 0xFFFF) + (t1l & 0xFFFF) + (chl & 0xFFFF) + (s1l & 0xFFFF) + (fl & 0xFFFF); - c2 = (t2l >>> 16) + (t1l >>> 16) + (chl >>> 16) + (s1l >>> 16) + (fl >>> 16) + (c1 >>> 16); - c3 = (t2h & 0xFFFF) + (t1h & 0xFFFF) + (chh & 0xFFFF) + (s1h & 0xFFFF) + (fh & 0xFFFF) + (c2 >>> 16); - c4 = (t2h >>> 16) + (t1h >>> 16) + (chh >>> 16) + (s1h >>> 16) + (fh >>> 16) + (c3 >>> 16); + t1h = blocks[j + 4]; + t1l = blocks[j + 5]; + t2h = K[j + 4]; + t2l = K[j + 5]; - t1h = (c4 << 16) | (c3 & 0xFFFF); - t1l = (c2 << 16) | (c1 & 0xFFFF); + c1 = (t2l & 0xFFFF) + (t1l & 0xFFFF) + (chl & 0xFFFF) + (s1l & 0xFFFF) + (fl & 0xFFFF); + c2 = (t2l >>> 16) + (t1l >>> 16) + (chl >>> 16) + (s1l >>> 16) + (fl >>> 16) + (c1 >>> 16); + c3 = (t2h & 0xFFFF) + (t1h & 0xFFFF) + (chh & 0xFFFF) + (s1h & 0xFFFF) + (fh & 0xFFFF) + (c2 >>> 16); + c4 = (t2h >>> 16) + (t1h >>> 16) + (chh >>> 16) + (s1h >>> 16) + (fh >>> 16) + (c3 >>> 16); - c1 = (majl & 0xFFFF) + (s0l & 0xFFFF); - c2 = (majl >>> 16) + (s0l >>> 16) + (c1 >>> 16); - c3 = (majh & 0xFFFF) + (s0h & 0xFFFF) + (c2 >>> 16); - c4 = (majh >>> 16) + (s0h >>> 16) + (c3 >>> 16); + t1h = (c4 << 16) | (c3 & 0xFFFF); + t1l = (c2 << 16) | (c1 & 0xFFFF); - t2h = (c4 << 16) | (c3 & 0xFFFF); - t2l = (c2 << 16) | (c1 & 0xFFFF); + c1 = (majl & 0xFFFF) + (s0l & 0xFFFF); + c2 = (majl >>> 16) + (s0l >>> 16) + (c1 >>> 16); + c3 = (majh & 0xFFFF) + (s0h & 0xFFFF) + (c2 >>> 16); + c4 = (majh >>> 16) + (s0h >>> 16) + (c3 >>> 16); - c1 = (bl & 0xFFFF) + (t1l & 0xFFFF); - c2 = (bl >>> 16) + (t1l >>> 16) + (c1 >>> 16); - c3 = (bh & 0xFFFF) + (t1h & 0xFFFF) + (c2 >>> 16); - c4 = (bh >>> 16) + (t1h >>> 16) + (c3 >>> 16); + t2h = (c4 << 16) | (c3 & 0xFFFF); + t2l = (c2 << 16) | (c1 & 0xFFFF); - fh = (c4 << 16) | (c3 & 0xFFFF); - fl = (c2 << 16) | (c1 & 0xFFFF); + c1 = (bl & 0xFFFF) + (t1l & 0xFFFF); + c2 = (bl >>> 16) + (t1l >>> 16) + (c1 >>> 16); + c3 = (bh & 0xFFFF) + (t1h & 0xFFFF) + (c2 >>> 16); + c4 = (bh >>> 16) + (t1h >>> 16) + (c3 >>> 16); - c1 = (t2l & 0xFFFF) + (t1l & 0xFFFF); - c2 = (t2l >>> 16) + (t1l >>> 16) + (c1 >>> 16); - c3 = (t2h & 0xFFFF) + (t1h & 0xFFFF) + (c2 >>> 16); - c4 = (t2h >>> 16) + (t1h >>> 16) + (c3 >>> 16); + fh = (c4 << 16) | (c3 & 0xFFFF); + fl = (c2 << 16) | (c1 & 0xFFFF); - bh = (c4 << 16) | (c3 & 0xFFFF); - bl = (c2 << 16) | (c1 & 0xFFFF); + c1 = (t2l & 0xFFFF) + (t1l & 0xFFFF); + c2 = (t2l >>> 16) + (t1l >>> 16) + (c1 >>> 16); + c3 = (t2h & 0xFFFF) + (t1h & 0xFFFF) + (c2 >>> 16); + c4 = (t2h >>> 16) + (t1h >>> 16) + (c3 >>> 16); - s0h = ((bh >>> 28) | (bl << 4)) ^ ((bl >>> 2) | (bh << 30)) ^ ((bl >>> 7) | (bh << 25)); - s0l = ((bl >>> 28) | (bh << 4)) ^ ((bh >>> 2) | (bl << 30)) ^ ((bh >>> 7) | (bl << 25)); + bh = (c4 << 16) | (c3 & 0xFFFF); + bl = (c2 << 16) | (c1 & 0xFFFF); - s1h = ((fh >>> 14) | (fl << 18)) ^ ((fh >>> 18) | (fl << 14)) ^ ((fl >>> 9) | (fh << 23)); - s1l = ((fl >>> 14) | (fh << 18)) ^ ((fl >>> 18) | (fh << 14)) ^ ((fh >>> 9) | (fl << 23)); + s0h = ((bh >>> 28) | (bl << 4)) ^ ((bl >>> 2) | (bh << 30)) ^ ((bl >>> 7) | (bh << 25)); + s0l = ((bl >>> 28) | (bh << 4)) ^ ((bh >>> 2) | (bl << 30)) ^ ((bh >>> 7) | (bl << 25)); - bch = bh & ch; - bcl = bl & cl; - majh = bch ^ (bh & dh) ^ cdh; - majl = bcl ^ (bl & dl) ^ cdl; + s1h = ((fh >>> 14) | (fl << 18)) ^ ((fh >>> 18) | (fl << 14)) ^ ((fl >>> 9) | (fh << 23)); + s1l = ((fl >>> 14) | (fh << 18)) ^ ((fl >>> 18) | (fh << 14)) ^ ((fh >>> 9) | (fl << 23)); - chh = (fh & gh) ^ (~fh & hh); - chl = (fl & gl) ^ (~fl & hl); + bch = bh & ch; + bcl = bl & cl; + majh = bch ^ (bh & dh) ^ cdh; + majl = bcl ^ (bl & dl) ^ cdl; - t1h = blocks[j + 6]; - t1l = blocks[j + 7]; - t2h = K[j + 6]; - t2l = K[j + 7]; + chh = (fh & gh) ^ (~fh & hh); + chl = (fl & gl) ^ (~fl & hl); - c1 = (t2l & 0xFFFF) + (t1l & 0xFFFF) + (chl & 0xFFFF) + (s1l & 0xFFFF) + (el & 0xFFFF); - c2 = (t2l >>> 16) + (t1l >>> 16) + (chl >>> 16) + (s1l >>> 16) + (el >>> 16) + (c1 >>> 16); - c3 = (t2h & 0xFFFF) + (t1h & 0xFFFF) + (chh & 0xFFFF) + (s1h & 0xFFFF) + (eh & 0xFFFF) + (c2 >>> 16); - c4 = (t2h >>> 16) + (t1h >>> 16) + (chh >>> 16) + (s1h >>> 16) + (eh >>> 16) + (c3 >>> 16); + t1h = blocks[j + 6]; + t1l = blocks[j + 7]; + t2h = K[j + 6]; + t2l = K[j + 7]; - t1h = (c4 << 16) | (c3 & 0xFFFF); - t1l = (c2 << 16) | (c1 & 0xFFFF); + c1 = (t2l & 0xFFFF) + (t1l & 0xFFFF) + (chl & 0xFFFF) + (s1l & 0xFFFF) + (el & 0xFFFF); + c2 = (t2l >>> 16) + (t1l >>> 16) + (chl >>> 16) + (s1l >>> 16) + (el >>> 16) + (c1 >>> 16); + c3 = (t2h & 0xFFFF) + (t1h & 0xFFFF) + (chh & 0xFFFF) + (s1h & 0xFFFF) + (eh & 0xFFFF) + (c2 >>> 16); + c4 = (t2h >>> 16) + (t1h >>> 16) + (chh >>> 16) + (s1h >>> 16) + (eh >>> 16) + (c3 >>> 16); - c1 = (majl & 0xFFFF) + (s0l & 0xFFFF); - c2 = (majl >>> 16) + (s0l >>> 16) + (c1 >>> 16); - c3 = (majh & 0xFFFF) + (s0h & 0xFFFF) + (c2 >>> 16); - c4 = (majh >>> 16) + (s0h >>> 16) + (c3 >>> 16); + t1h = (c4 << 16) | (c3 & 0xFFFF); + t1l = (c2 << 16) | (c1 & 0xFFFF); - t2h = (c4 << 16) | (c3 & 0xFFFF); - t2l = (c2 << 16) | (c1 & 0xFFFF); + c1 = (majl & 0xFFFF) + (s0l & 0xFFFF); + c2 = (majl >>> 16) + (s0l >>> 16) + (c1 >>> 16); + c3 = (majh & 0xFFFF) + (s0h & 0xFFFF) + (c2 >>> 16); + c4 = (majh >>> 16) + (s0h >>> 16) + (c3 >>> 16); - c1 = (al & 0xFFFF) + (t1l & 0xFFFF); - c2 = (al >>> 16) + (t1l >>> 16) + (c1 >>> 16); - c3 = (ah & 0xFFFF) + (t1h & 0xFFFF) + (c2 >>> 16); - c4 = (ah >>> 16) + (t1h >>> 16) + (c3 >>> 16); + t2h = (c4 << 16) | (c3 & 0xFFFF); + t2l = (c2 << 16) | (c1 & 0xFFFF); - eh = (c4 << 16) | (c3 & 0xFFFF); - el = (c2 << 16) | (c1 & 0xFFFF); + c1 = (al & 0xFFFF) + (t1l & 0xFFFF); + c2 = (al >>> 16) + (t1l >>> 16) + (c1 >>> 16); + c3 = (ah & 0xFFFF) + (t1h & 0xFFFF) + (c2 >>> 16); + c4 = (ah >>> 16) + (t1h >>> 16) + (c3 >>> 16); - c1 = (t2l & 0xFFFF) + (t1l & 0xFFFF); - c2 = (t2l >>> 16) + (t1l >>> 16) + (c1 >>> 16); - c3 = (t2h & 0xFFFF) + (t1h & 0xFFFF) + (c2 >>> 16); - c4 = (t2h >>> 16) + (t1h >>> 16) + (c3 >>> 16); + eh = (c4 << 16) | (c3 & 0xFFFF); + el = (c2 << 16) | (c1 & 0xFFFF); - ah = (c4 << 16) | (c3 & 0xFFFF); - al = (c2 << 16) | (c1 & 0xFFFF); - } + c1 = (t2l & 0xFFFF) + (t1l & 0xFFFF); + c2 = (t2l >>> 16) + (t1l >>> 16) + (c1 >>> 16); + c3 = (t2h & 0xFFFF) + (t1h & 0xFFFF) + (c2 >>> 16); + c4 = (t2h >>> 16) + (t1h >>> 16) + (c3 >>> 16); - c1 = (h0l & 0xFFFF) + (al & 0xFFFF); - c2 = (h0l >>> 16) + (al >>> 16) + (c1 >>> 16); - c3 = (h0h & 0xFFFF) + (ah & 0xFFFF) + (c2 >>> 16); - c4 = (h0h >>> 16) + (ah >>> 16) + (c3 >>> 16); + ah = (c4 << 16) | (c3 & 0xFFFF); + al = (c2 << 16) | (c1 & 0xFFFF); + } - h0h = (c4 << 16) | (c3 & 0xFFFF); - h0l = (c2 << 16) | (c1 & 0xFFFF); + c1 = (h0l & 0xFFFF) + (al & 0xFFFF); + c2 = (h0l >>> 16) + (al >>> 16) + (c1 >>> 16); + c3 = (h0h & 0xFFFF) + (ah & 0xFFFF) + (c2 >>> 16); + c4 = (h0h >>> 16) + (ah >>> 16) + (c3 >>> 16); - c1 = (h1l & 0xFFFF) + (bl & 0xFFFF); - c2 = (h1l >>> 16) + (bl >>> 16) + (c1 >>> 16); - c3 = (h1h & 0xFFFF) + (bh & 0xFFFF) + (c2 >>> 16); - c4 = (h1h >>> 16) + (bh >>> 16) + (c3 >>> 16); + this.h0h = (c4 << 16) | (c3 & 0xFFFF); + this.h0l = (c2 << 16) | (c1 & 0xFFFF); - h1h = (c4 << 16) | (c3 & 0xFFFF); - h1l = (c2 << 16) | (c1 & 0xFFFF); + c1 = (h1l & 0xFFFF) + (bl & 0xFFFF); + c2 = (h1l >>> 16) + (bl >>> 16) + (c1 >>> 16); + c3 = (h1h & 0xFFFF) + (bh & 0xFFFF) + (c2 >>> 16); + c4 = (h1h >>> 16) + (bh >>> 16) + (c3 >>> 16); - c1 = (h2l & 0xFFFF) + (cl & 0xFFFF); - c2 = (h2l >>> 16) + (cl >>> 16) + (c1 >>> 16); - c3 = (h2h & 0xFFFF) + (ch & 0xFFFF) + (c2 >>> 16); - c4 = (h2h >>> 16) + (ch >>> 16) + (c3 >>> 16); + this.h1h = (c4 << 16) | (c3 & 0xFFFF); + this.h1l = (c2 << 16) | (c1 & 0xFFFF); - h2h = (c4 << 16) | (c3 & 0xFFFF); - h2l = (c2 << 16) | (c1 & 0xFFFF); + c1 = (h2l & 0xFFFF) + (cl & 0xFFFF); + c2 = (h2l >>> 16) + (cl >>> 16) + (c1 >>> 16); + c3 = (h2h & 0xFFFF) + (ch & 0xFFFF) + (c2 >>> 16); + c4 = (h2h >>> 16) + (ch >>> 16) + (c3 >>> 16); - c1 = (h3l & 0xFFFF) + (dl & 0xFFFF); - c2 = (h3l >>> 16) + (dl >>> 16) + (c1 >>> 16); - c3 = (h3h & 0xFFFF) + (dh & 0xFFFF) + (c2 >>> 16); - c4 = (h3h >>> 16) + (dh >>> 16) + (c3 >>> 16); + this.h2h = (c4 << 16) | (c3 & 0xFFFF); + this.h2l = (c2 << 16) | (c1 & 0xFFFF); - h3h = (c4 << 16) | (c3 & 0xFFFF); - h3l = (c2 << 16) | (c1 & 0xFFFF); + c1 = (h3l & 0xFFFF) + (dl & 0xFFFF); + c2 = (h3l >>> 16) + (dl >>> 16) + (c1 >>> 16); + c3 = (h3h & 0xFFFF) + (dh & 0xFFFF) + (c2 >>> 16); + c4 = (h3h >>> 16) + (dh >>> 16) + (c3 >>> 16); - c1 = (h4l & 0xFFFF) + (el & 0xFFFF); - c2 = (h4l >>> 16) + (el >>> 16) + (c1 >>> 16); - c3 = (h4h & 0xFFFF) + (eh & 0xFFFF) + (c2 >>> 16); - c4 = (h4h >>> 16) + (eh >>> 16) + (c3 >>> 16); + this.h3h = (c4 << 16) | (c3 & 0xFFFF); + this.h3l = (c2 << 16) | (c1 & 0xFFFF); - h4h = (c4 << 16) | (c3 & 0xFFFF); - h4l = (c2 << 16) | (c1 & 0xFFFF); + c1 = (h4l & 0xFFFF) + (el & 0xFFFF); + c2 = (h4l >>> 16) + (el >>> 16) + (c1 >>> 16); + c3 = (h4h & 0xFFFF) + (eh & 0xFFFF) + (c2 >>> 16); + c4 = (h4h >>> 16) + (eh >>> 16) + (c3 >>> 16); - c1 = (h5l & 0xFFFF) + (fl & 0xFFFF); - c2 = (h5l >>> 16) + (fl >>> 16) + (c1 >>> 16); - c3 = (h5h & 0xFFFF) + (fh & 0xFFFF) + (c2 >>> 16); - c4 = (h5h >>> 16) + (fh >>> 16) + (c3 >>> 16); + this.h4h = (c4 << 16) | (c3 & 0xFFFF); + this.h4l = (c2 << 16) | (c1 & 0xFFFF); - h5h = (c4 << 16) | (c3 & 0xFFFF); - h5l = (c2 << 16) | (c1 & 0xFFFF); + c1 = (h5l & 0xFFFF) + (fl & 0xFFFF); + c2 = (h5l >>> 16) + (fl >>> 16) + (c1 >>> 16); + c3 = (h5h & 0xFFFF) + (fh & 0xFFFF) + (c2 >>> 16); + c4 = (h5h >>> 16) + (fh >>> 16) + (c3 >>> 16); - c1 = (h6l & 0xFFFF) + (gl & 0xFFFF); - c2 = (h6l >>> 16) + (gl >>> 16) + (c1 >>> 16); - c3 = (h6h & 0xFFFF) + (gh & 0xFFFF) + (c2 >>> 16); - c4 = (h6h >>> 16) + (gh >>> 16) + (c3 >>> 16); + this.h5h = (c4 << 16) | (c3 & 0xFFFF); + this.h5l = (c2 << 16) | (c1 & 0xFFFF); - h6h = (c4 << 16) | (c3 & 0xFFFF); - h6l = (c2 << 16) | (c1 & 0xFFFF); + c1 = (h6l & 0xFFFF) + (gl & 0xFFFF); + c2 = (h6l >>> 16) + (gl >>> 16) + (c1 >>> 16); + c3 = (h6h & 0xFFFF) + (gh & 0xFFFF) + (c2 >>> 16); + c4 = (h6h >>> 16) + (gh >>> 16) + (c3 >>> 16); - c1 = (h7l & 0xFFFF) + (hl & 0xFFFF); - c2 = (h7l >>> 16) + (hl >>> 16) + (c1 >>> 16); - c3 = (h7h & 0xFFFF) + (hh & 0xFFFF) + (c2 >>> 16); - c4 = (h7h >>> 16) + (hh >>> 16) + (c3 >>> 16); + this.h6h = (c4 << 16) | (c3 & 0xFFFF); + this.h6l = (c2 << 16) | (c1 & 0xFFFF); - h7h = (c4 << 16) | (c3 & 0xFFFF); - h7l = (c2 << 16) | (c1 & 0xFFFF); - } while (!end); + c1 = (h7l & 0xFFFF) + (hl & 0xFFFF); + c2 = (h7l >>> 16) + (hl >>> 16) + (c1 >>> 16); + c3 = (h7h & 0xFFFF) + (hh & 0xFFFF) + (c2 >>> 16); + c4 = (h7h >>> 16) + (hh >>> 16) + (c3 >>> 16); + + this.h7h = (c4 << 16) | (c3 & 0xFFFF); + this.h7l = (c2 << 16) | (c1 & 0xFFFF); + }; + + Sha512.prototype.hex = function () { + this.finalize(); + + var h0h = this.h0h, h0l = this.h0l, h1h = this.h1h, h1l = this.h1l, + h2h = this.h2h, h2l = this.h2l, h3h = this.h3h, h3l = this.h3l, + h4h = this.h4h, h4l = this.h4l, h5h = this.h5h, h5l = this.h5l, + h6h = this.h6h, h6l = this.h6l, h7h = this.h7h, h7l = this.h7l, + bits = this.bits; var hex = HEX_CHARS[(h0h >> 28) & 0x0F] + HEX_CHARS[(h0h >> 24) & 0x0F] + HEX_CHARS[(h0h >> 20) & 0x0F] + HEX_CHARS[(h0h >> 16) & 0x0F] + @@ -569,19 +660,96 @@ return hex; }; - var exports = sha512; - exports.sha512 = sha512; - exports.sha384 = sha384; - exports.sha512_256 = sha512_256; - exports.sha512_224 = sha512_224; + Sha512.prototype.toString = Sha512.prototype.hex; + + Sha512.prototype.digest = function () { + this.finalize(); + + var h0h = this.h0h, h0l = this.h0l, h1h = this.h1h, h1l = this.h1l, + h2h = this.h2h, h2l = this.h2l, h3h = this.h3h, h3l = this.h3l, + h4h = this.h4h, h4l = this.h4l, h5h = this.h5h, h5l = this.h5l, + h6h = this.h6h, h6l = this.h6l, h7h = this.h7h, h7l = this.h7l, + bits = this.bits; + + var arr = [ + (h0h >> 24) & 0xFF, (h0h >> 16) & 0xFF, (h0h >> 8) & 0xFF, h0h & 0xFF, + (h0l >> 24) & 0xFF, (h0l >> 16) & 0xFF, (h0l >> 8) & 0xFF, h0l & 0xFF, + (h1h >> 24) & 0xFF, (h1h >> 16) & 0xFF, (h1h >> 8) & 0xFF, h1h & 0xFF, + (h1l >> 24) & 0xFF, (h1l >> 16) & 0xFF, (h1l >> 8) & 0xFF, h1l & 0xFF, + (h2h >> 24) & 0xFF, (h2h >> 16) & 0xFF, (h2h >> 8) & 0xFF, h2h & 0xFF, + (h2l >> 24) & 0xFF, (h2l >> 16) & 0xFF, (h2l >> 8) & 0xFF, h2l & 0xFF, + (h3h >> 24) & 0xFF, (h3h >> 16) & 0xFF, (h3h >> 8) & 0xFF, h3h & 0xFF + ]; + + if (bits >= 256) { + arr.push((h3l >> 24) & 0xFF, (h3l >> 16) & 0xFF, (h3l >> 8) & 0xFF, h3l & 0xFF); + } + if (bits >= 384) { + arr.push( + (h4h >> 24) & 0xFF, (h4h >> 16) & 0xFF, (h4h >> 8) & 0xFF, h4h & 0xFF, + (h4l >> 24) & 0xFF, (h4l >> 16) & 0xFF, (h4l >> 8) & 0xFF, h4l & 0xFF, + (h5h >> 24) & 0xFF, (h5h >> 16) & 0xFF, (h5h >> 8) & 0xFF, h5h & 0xFF, + (h5l >> 24) & 0xFF, (h5l >> 16) & 0xFF, (h5l >> 8) & 0xFF, h5l & 0xFF + ); + } + if (bits == 512) { + arr.push( + (h6h >> 24) & 0xFF, (h6h >> 16) & 0xFF, (h6h >> 8) & 0xFF, h6h & 0xFF, + (h6l >> 24) & 0xFF, (h6l >> 16) & 0xFF, (h6l >> 8) & 0xFF, h6l & 0xFF, + (h7h >> 24) & 0xFF, (h7h >> 16) & 0xFF, (h7h >> 8) & 0xFF, h7h & 0xFF, + (h7l >> 24) & 0xFF, (h7l >> 16) & 0xFF, (h7l >> 8) & 0xFF, h7l & 0xFF + ); + } + return arr; + }; + + Sha512.prototype.array = Sha512.prototype.digest; + + Sha512.prototype.arrayBuffer = function () { + this.finalize(); + + var bits = this.bits; + var buffer = new ArrayBuffer(bits / 8); + var dataView = new DataView(buffer); + dataView.setUint32(0, this.h0h); + dataView.setUint32(4, this.h0l); + dataView.setUint32(8, this.h1h); + dataView.setUint32(12, this.h1l); + dataView.setUint32(16, this.h2h); + dataView.setUint32(20, this.h2l); + dataView.setUint32(24, this.h3h); + + if (bits >= 256) { + dataView.setUint32(28, this.h3l); + } + if (bits >= 384) { + dataView.setUint32(32, this.h4h); + dataView.setUint32(36, this.h4l); + dataView.setUint32(40, this.h5h); + dataView.setUint32(44, this.h5l); + } + if (bits == 512) { + dataView.setUint32(48, this.h6h); + dataView.setUint32(52, this.h6l); + dataView.setUint32(56, this.h7h); + dataView.setUint32(60, this.h7l); + } + return buffer; + }; + + var exports = createMethod(512); + exports.sha512 = exports; + exports.sha384 = createMethod(384); + exports.sha512_256 = createMethod(256); + exports.sha512_224 = createMethod(224); if (COMMON_JS) { module.exports = exports; } else { - root.sha512 = sha512; - root.sha384 = sha384; - root.sha512_256 = sha512_256; - root.sha512_224 = sha512_224; + root.sha512 = exports.sha512; + root.sha384 = exports.sha384; + root.sha512_256 = exports.sha512_256; + root.sha512_224 = exports.sha512_224; if (AMD) { define(function () { return exports; diff --git a/tests/test.js b/tests/test.js index 138d1d6..a09c493 100644 --- a/tests/test.js +++ b/tests/test.js @@ -1,177 +1,308 @@ (function (sha512, sha384, sha512_256, sha512_224) { - describe('sha512', function () { - describe('ascii', function () { - describe('less than 128 bytes', function () { - it('should be successful', function () { - expect(sha512('')).to.be('cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e'); - expect(sha512('The quick brown fox jumps over the lazy dog')).to.be('07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6'); - expect(sha512('The quick brown fox jumps over the lazy dog.')).to.be('91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed'); - }); - }); - - describe('more than 128 bytes', function () { - it('should be successful', function () { - expect(sha512('The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.')).to.be('a8dedff31e3be9df6413ef5b4ecb93d62d3fbcb04297552eab5370e04afd45927854a4373037e81a50186e678d818c9ba824f4c850f3d0f02764af0252076979'); - }); - }); - }); - - describe('UTF8', function () { - describe('less than 128 bytes', function () { - it('should be successful', function () { - expect(sha512('中文')).to.be('8b88efc2ebbcbdad5ac2d65af05bec57bda25e71fd5fb25bbd892057a2755fbd05d8d8491cb2946febd5b0f124ffdfbaecf7e34946353c4f1b5ab29545895468'); - expect(sha512('aécio')).to.be('e1c6925243db76985abacaf9fa85e22697f549e67f65a36c88e4046a2260990ff9eefc3402396ea8dcbe8c592d8d5671bea612156eda38d3708d394bbd17d493'); - expect(sha512('𠜎')).to.be('f3e7ee9cdf7dbb52f7edd59ce3d49868c64f2b3aceceab060b8eaaebdf9de0dae5866d660e3319c5aad426a2176cb1703efc73eb24d1a90458ceda1b7f4e3940'); - }); - }); + Array.prototype.toHexString = ArrayBuffer.prototype.toHexString = function () { + var array = new Uint8Array(this); + var hex = ''; + for (var i = 0; i < array.length; ++i) { + var c = array[i].toString('16'); + hex += c.length === 1 ? '0' + c : c; + } + return hex; + }; - describe('more than 128 bytes', function () { - it('should be successful', function () { - expect(sha512('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一')).to.be('6cb7f6d3381a187edadb43c7cdcfbbed4d2c213a7dce8ea08fe42b9882b64e643202b4974a6db94f94650ab9173d97c58bd59f6d19d27e01aab76d8d08855c65'); - expect(sha512('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('d24af1901aaf1458f089a6eddf784ce61c3012aee0df98bdb67ad2dc6b41a3b4051d40caac524373930ae396a2dde99a9204871b40892eea3e5f3c8d46da0c3c'); - }); - }); - }); + var testCases = { + sha512: { + 'ascii': { + 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e': '', + '07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6': 'The quick brown fox jumps over the lazy dog', + '91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed': 'The quick brown fox jumps over the lazy dog.' + }, + 'ascii more than 64 bytes': { + 'a8dedff31e3be9df6413ef5b4ecb93d62d3fbcb04297552eab5370e04afd45927854a4373037e81a50186e678d818c9ba824f4c850f3d0f02764af0252076979': 'The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.' + }, + 'UTF8': { + '8b88efc2ebbcbdad5ac2d65af05bec57bda25e71fd5fb25bbd892057a2755fbd05d8d8491cb2946febd5b0f124ffdfbaecf7e34946353c4f1b5ab29545895468': '中文', + 'e1c6925243db76985abacaf9fa85e22697f549e67f65a36c88e4046a2260990ff9eefc3402396ea8dcbe8c592d8d5671bea612156eda38d3708d394bbd17d493': 'aécio', + 'f3e7ee9cdf7dbb52f7edd59ce3d49868c64f2b3aceceab060b8eaaebdf9de0dae5866d660e3319c5aad426a2176cb1703efc73eb24d1a90458ceda1b7f4e3940': '𠜎' + }, + 'UTF8 more than 64 bytes': { + '6cb7f6d3381a187edadb43c7cdcfbbed4d2c213a7dce8ea08fe42b9882b64e643202b4974a6db94f94650ab9173d97c58bd59f6d19d27e01aab76d8d08855c65': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一', + 'd24af1901aaf1458f089a6eddf784ce61c3012aee0df98bdb67ad2dc6b41a3b4051d40caac524373930ae396a2dde99a9204871b40892eea3e5f3c8d46da0c3c': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。' + }, + 'special length': { + 'a58b6132677883b0ab565dfb4284a4441a35a0954738d90ad8c17800de5ee44f6482efd4c13e05a6e907fb830fc9d9039bd96f004e5e3e8d00802fb9448dec03': '012345678012345678012345678012345678012345678012345678012345012345678012345678012345678012345678012345678012345', + '34a93615cbf314a799857dc9c1804ed04be60b481229ea3a53e00316e62ac7924ea7e65435cbd9fc7d8e10e0e52e56bd99ac571c1c470cbf9252bdd70b3fd8a3': '0123456780123456780123456780123456780123456780123456780123450123456780123456780123456780123456780123456780123456', + '4555ce42c2e19f9a09a34b3e40f85178c22cde939ac42de4d33ca0e08e3c6a2e531a3abd9265a72cc22a19d085eb2c468c20d6cfb022d8914b53d6a2b485b7c9': '123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567', + '8bfa769385fe4b7d4aabf304498522d5aefa5356e42e9ae5ac9fa6b00a483685586cc0fa44da3df335719cbb524b12433afda18361d70e4c584ea81a927c670c': '1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678' + }, + 'Array': { + 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e': [], + '07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6': [84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103] + }, + 'Uint8Array': { + '07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6': new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]) + }, + 'Int8Array': { + '07e547d9586f6a73f73fbac0435ed76951218fb7d0c8d788a309d785436bbb642e93a252a954f23912547d1e8a3b5ed6e1bfd7097821233fa0538f3db854fee6': new Int8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]) + }, + 'ArrayBuffer': { + 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e': new ArrayBuffer(0), + 'b8244d028981d693af7b456af8efa4cad63d282e19ff14942c246e50d9351d22704a802a71c3580b6370de4ceb293c324a8423342557d4e5c38438f0e36910ee': new ArrayBuffer(1), + }, + 'Object': { + 'cf83e1357eefb8bdf1542850d66d8007d620e4050b5715dc83f4a921d36ce9ce47d0d13c5d85f2b0ff8318d2877eec2f63b931bd47417a81a538327af927da3e': {what: 'ever'} + } + }, + sha384: { + 'ascii': { + '38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b': '', + 'ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1': 'The quick brown fox jumps over the lazy dog', + 'ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7': 'The quick brown fox jumps over the lazy dog.' + }, + 'ascii more than 64 bytes': { + '3ab853dee5785c19346e8578d9cdb3b7258a8212c15fdf25d3055b4fbbc892a529e552788992fdae68dfa34f13f0af4e': 'The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.' + }, + 'UTF8': { + '93422ceb8291a69b22f02dc1114c39a287493ad525dcebc77e4019a44eaee2633a85d0f29cd298ee6799048c33a4be0c': '中文', + 'a2146805faafc45b0055b49386768811c803ef9fa8a85b648e114276c1bf49ef1092ec1bc2d3f7e036238a97eace2087': 'aécio', + 'e929475cf9b4d73e2a2366146381ac779b17499c92ddf910e028eb9f30280878a2843643ed8a539af5dffed64143b93d': '𠜎' + }, + 'UTF8 more than 64 bytes': { + '143f4bb1a89e576ea05bd8433a3903352c199bf6ef35bfa30afa1505d6fcef77476d1b1824105da160a5164b7c792328': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一', + '432a8d25efb1a8d463acc7bbbed57365cddc72de3cc19d42e1d260b1479770ecea9dd403fead2949d4fabb36939f263a': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。' + }, + 'special length': { + 'a0ecc55799e1db4e7b30c2e78d445ada5ea2939d47e8aab51d6ae86cdcf93d5048b2666aa25fbaede7ec59ee59f46e88': '012345678012345678012345678012345678012345678012345678012345012345678012345678012345678012345678012345678012345', + '484b51b9f03f453a08c823afea1a32434b6545fb682b0b91d94e1f4bf2812abc8a31387050a2e74909f862436b88c798': '0123456780123456780123456780123456780123456780123456780123450123456780123456780123456780123456780123456780123456', + 'e4c83d6e5e18b4391a0b2e5fab84234ce9fc1071d87a9dcf38ca050ca3635781ef1d65ad4f9a835df26d186384105eac': '123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567', + 'd7f6874057808f9e2eaa4eddace032c7bd548fbe3989be6591910277a25dee5972e1297deced3ad7b4e3040de88855f6': '1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678' + }, + 'Array': { + '38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b': [], + 'ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1': [84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103] + }, + 'Uint8Array': { + 'ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1': new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]) + }, + 'Int8Array': { + 'ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1': new Int8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]) + }, + 'ArrayBuffer': { + '38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b': new ArrayBuffer(0), + 'bec021b4f368e3069134e012c2b4307083d3a9bdd206e24e5f0d86e13d6636655933ec2b413465966817a9c208a11717': new ArrayBuffer(1), + }, + 'Object': { + '38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b': {what: 'ever'} + } + }, + sha512_256: { + 'ascii': { + 'c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967a': '', + 'dd9d67b371519c339ed8dbd25af90e976a1eeefd4ad3d889005e532fc5bef04d': 'The quick brown fox jumps over the lazy dog', + '1546741840f8a492b959d9b8b2344b9b0eb51b004bba35c0aebaac86d45264c3': 'The quick brown fox jumps over the lazy dog.' + }, + 'ascii more than 64 bytes': { + '21e2e940930b23f1de6377086d07e22033c6bbf3fd9fbf4b62ec66e6c08c25be': 'The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.' + }, + 'UTF8': { + 'b6dab29c16ec35ab34a5d92ff135b58de96741dda78b1009a2181cf8b45d2f72': '中文', + '122802ca08e39c2ef46f6a81379dc5683bd8aa074dfb54259f0add4d8b5504bc': 'aécio', + '1032308151c0f4f5f8d4e0d96956352eb8ff87da98df8878d8795a858a7e7c08': '𠜎' + }, + 'UTF8 more than 64 bytes': { + 'd32a41d9858e45b68402f77cf9f3c3f992c36a4bffd230f78d666c87f97eaf7e': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一', + 'bd1abad59e6b8ad69bc17b6e05aa13f0cb725467fbeb45b83d3e4094332d1367': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。' + }, + 'special length': { + 'b8bd1d6cae9992f8619904683d54cbed560ec3ef2a33d8bc225175ec27dc09a4': '012345678012345678012345678012345678012345678012345678012345012345678012345678012345678012345678012345678012345', + '4edb8bb93b959f482ce2e434585f3e164f53b00c42c4c46797445b39d225cfe3': '0123456780123456780123456780123456780123456780123456780123450123456780123456780123456780123456780123456780123456', + '44da7ca023996ec4b4ded88410e4acc209ca26c34dc0f26550d28503974fe2d1': '123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567', + '465859c0edc706a2a9c39d3175a715fdf6b764ccc43d45b5668c56f6d8aa9b9e': '1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678' + }, + 'Array': { + 'c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967a': [], + 'dd9d67b371519c339ed8dbd25af90e976a1eeefd4ad3d889005e532fc5bef04d': [84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103] + }, + 'Uint8Array': { + 'dd9d67b371519c339ed8dbd25af90e976a1eeefd4ad3d889005e532fc5bef04d': new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]) + }, + 'Int8Array': { + 'dd9d67b371519c339ed8dbd25af90e976a1eeefd4ad3d889005e532fc5bef04d': new Int8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]) + }, + 'ArrayBuffer': { + 'c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967a': new ArrayBuffer(0), + '10baad1713566ac2333467bddb0597dec9066120dd72ac2dcb8394221dcbe43d': new ArrayBuffer(1), + }, + 'Object': { + 'c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967a': {what: 'ever'} + } + }, + sha512_224: { + 'ascii': { + '6ed0dd02806fa89e25de060c19d3ac86cabb87d6a0ddd05c333b84f4': '', + '944cd2847fb54558d4775db0485a50003111c8e5daa63fe722c6aa37': 'The quick brown fox jumps over the lazy dog', + '6d6a9279495ec4061769752e7ff9c68b6b0b3c5a281b7917ce0572de': 'The quick brown fox jumps over the lazy dog.' + }, + 'ascii more than 64 bytes': { + '2e962464977b198ee758d615bbc92251ad2e3c0960068e279fd21d2f': 'The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.' + }, + 'UTF8': { + '0f46a0ae7f226517dd66ece0ce1efa29ffb7ced05ac4566fdcaed188': '中文', + '562f2e4ee7f7451d20dcc6a0ac1a1e1c4a75f09baaf1cf19af3e15f4': 'aécio', + '0533318c52b3d4ad355c2a6c7e727ae3d2efa749db480ac33560b059': '𠜎' + }, + 'UTF8 more than 64 bytes': { + 'f67e191a5d4ee67a272ccaf6cf597f0c4d6a0c46bd631be7cadb0944': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一', + '009c3d1e3172d6df71344982eada855421592aea28acbf660ada7569': '訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。' + }, + 'special length': { + '36d6398ca22d885432e2fe3af53b9f83b2dc7a8e9284d65b81f7a879': '012345678012345678012345678012345678012345678012345678012345012345678012345678012345678012345678012345678012345', + 'd930407a742d10f70747f05271181ad6c1c92f87482ce6a494cb566a': '0123456780123456780123456780123456780123456780123456780123450123456780123456780123456780123456780123456780123456', + '1d60954ce7bee0bf5d235fe6bfc928c0f32c0f1c4992451dbc0f4dbf': '123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567', + '672aef84d579799cedf2fec090ce4ea0d72e0edd2788deaa8200fd67': '1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678' + }, + 'Array': { + '6ed0dd02806fa89e25de060c19d3ac86cabb87d6a0ddd05c333b84f4': [], + '944cd2847fb54558d4775db0485a50003111c8e5daa63fe722c6aa37': [84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103] + }, + 'Uint8Array': { + '944cd2847fb54558d4775db0485a50003111c8e5daa63fe722c6aa37': new Uint8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]) + }, + 'Int8Array': { + '944cd2847fb54558d4775db0485a50003111c8e5daa63fe722c6aa37': new Int8Array([84, 104, 101, 32, 113, 117, 105, 99, 107, 32, 98, 114, 111, 119, 110, 32, 102, 111, 120, 32, 106, 117, 109, 112, 115, 32, 111, 118, 101, 114, 32, 116, 104, 101, 32, 108, 97, 122, 121, 32, 100, 111, 103]) + }, + 'ArrayBuffer': { + '6ed0dd02806fa89e25de060c19d3ac86cabb87d6a0ddd05c333b84f4': new ArrayBuffer(0), + '283bb59af7081ed08197227d8f65b9591ffe1155be43e9550e57f941': new ArrayBuffer(1), + }, + 'Object': { + '6ed0dd02806fa89e25de060c19d3ac86cabb87d6a0ddd05c333b84f4': {what: 'ever'} + } + } + }; - describe('special length', function () { - it('should be successful', function () { - expect(sha512('012345678012345678012345678012345678012345678012345678012345012345678012345678012345678012345678012345678012345')).to.be('a58b6132677883b0ab565dfb4284a4441a35a0954738d90ad8c17800de5ee44f6482efd4c13e05a6e907fb830fc9d9039bd96f004e5e3e8d00802fb9448dec03'); - expect(sha512('0123456780123456780123456780123456780123456780123456780123450123456780123456780123456780123456780123456780123456')).to.be('34a93615cbf314a799857dc9c1804ed04be60b481229ea3a53e00316e62ac7924ea7e65435cbd9fc7d8e10e0e52e56bd99ac571c1c470cbf9252bdd70b3fd8a3'); - expect(sha512('123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567')).to.be('4555ce42c2e19f9a09a34b3e40f85178c22cde939ac42de4d33ca0e08e3c6a2e531a3abd9265a72cc22a19d085eb2c468c20d6cfb022d8914b53d6a2b485b7c9'); - expect(sha512('1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678')).to.be('8bfa769385fe4b7d4aabf304498522d5aefa5356e42e9ae5ac9fa6b00a483685586cc0fa44da3df335719cbb524b12433afda18361d70e4c584ea81a927c670c'); - }); - }); - }); + function runTestCases(name, algorithm) { + var methods = [ + { + name: name, + call: algorithm, + }, + { + name: name + '.hex', + call: algorithm.hex + }, + { + name: name + '.array', + call: function (message) { + return algorithm.array(message).toHexString(); + } + }, + { + name: name + '.digest', + call: function (message) { + return algorithm.digest(message).toHexString(); + } + }, + { + name: name + '.arrayBuffer', + call: function (message) { + return algorithm.arrayBuffer(message).toHexString(); + } + } + ]; - describe('sha384', function () { - describe('ascii', function () { - describe('less than 128 bytes', function () { - it('should be successful', function () { - expect(sha384('')).to.be('38b060a751ac96384cd9327eb1b1e36a21fdb71114be07434c0cc7bf63f6e1da274edebfe76f65fbd51ad2f14898b95b'); - expect(sha384('The quick brown fox jumps over the lazy dog')).to.be('ca737f1014a48f4c0b6dd43cb177b0afd9e5169367544c494011e3317dbf9a509cb1e5dc1e85a941bbee3d7f2afbc9b1'); - expect(sha384('The quick brown fox jumps over the lazy dog.')).to.be('ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7'); - }); - }); + var classMethods = [ + { + name: 'create', + call: function (message) { + return algorithm.create().update(message).toString(); + } + }, + { + name: 'update', + call: function (message) { + return algorithm.update(message).toString(); + } + }, + { + name: 'hex', + call: function (message) { + return algorithm.update(message).hex(); + } + }, + { + name: 'array', + call: function (message) { + return algorithm.update(message).array().toHexString(); + } + }, + { + name: 'digest', + call: function (message) { + return algorithm.update(message).digest().toHexString(); + } + }, + { + name: 'arrayBuffer', + call: function (message) { + return algorithm.update(message).arrayBuffer().toHexString(); + } + }, + { + name: 'finalize', + call: function (message) { + var hash = algorithm.update(message); + hash.hex(); + hash.update(message); + return hash.hex(); + } + } + ]; - describe('more than 128 bytes', function () { - it('should be successful', function () { - expect(sha384('The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.')).to.be('3ab853dee5785c19346e8578d9cdb3b7258a8212c15fdf25d3055b4fbbc892a529e552788992fdae68dfa34f13f0af4e'); - }); - }); - }); + var subTestCases = testCases[name]; - describe('UTF8', function () { - describe('less than 128 bytes', function () { - it('should be successful', function () { - expect(sha384('中文')).to.be('93422ceb8291a69b22f02dc1114c39a287493ad525dcebc77e4019a44eaee2633a85d0f29cd298ee6799048c33a4be0c'); - expect(sha384('aécio')).to.be('a2146805faafc45b0055b49386768811c803ef9fa8a85b648e114276c1bf49ef1092ec1bc2d3f7e036238a97eace2087'); - expect(sha384('𠜎')).to.be('e929475cf9b4d73e2a2366146381ac779b17499c92ddf910e028eb9f30280878a2843643ed8a539af5dffed64143b93d'); + describe(name, function () { + methods.forEach(function (method) { + describe('#' + method.name, function () { + for (var testCaseName in subTestCases) { + (function (testCaseName) { + var testCase = subTestCases[testCaseName]; + context('when ' + testCaseName, function () { + for (var hash in testCase) { + (function (message, hash) { + it('should be equal', function () { + expect(method.call(message)).to.be(hash); + }); + })(testCase[hash], hash); + } + }); + })(testCaseName); + } }); }); - describe('more than 128 bytes', function () { - it('should be successful', function () { - expect(sha384('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一')).to.be('143f4bb1a89e576ea05bd8433a3903352c199bf6ef35bfa30afa1505d6fcef77476d1b1824105da160a5164b7c792328'); - expect(sha384('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('432a8d25efb1a8d463acc7bbbed57365cddc72de3cc19d42e1d260b1479770ecea9dd403fead2949d4fabb36939f263a'); + classMethods.forEach(function (method) { + describe('#' + method.name, function () { + for (var testCaseName in subTestCases) { + (function (testCaseName) { + var testCase = subTestCases[testCaseName]; + context('when ' + testCaseName, function () { + for (var hash in testCase) { + (function (message, hash) { + it('should be equal', function () { + expect(method.call(message)).to.be(hash); + }); + })(testCase[hash], hash); + } + }); + })(testCaseName); + } }); }); }); + } - describe('special length', function () { - it('should be successful', function () { - expect(sha384('012345678012345678012345678012345678012345678012345678012345012345678012345678012345678012345678012345678012345')).to.be('a0ecc55799e1db4e7b30c2e78d445ada5ea2939d47e8aab51d6ae86cdcf93d5048b2666aa25fbaede7ec59ee59f46e88'); - expect(sha384('0123456780123456780123456780123456780123456780123456780123450123456780123456780123456780123456780123456780123456')).to.be('484b51b9f03f453a08c823afea1a32434b6545fb682b0b91d94e1f4bf2812abc8a31387050a2e74909f862436b88c798'); - expect(sha384('123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567')).to.be('e4c83d6e5e18b4391a0b2e5fab84234ce9fc1071d87a9dcf38ca050ca3635781ef1d65ad4f9a835df26d186384105eac'); - expect(sha384('1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678')).to.be('d7f6874057808f9e2eaa4eddace032c7bd548fbe3989be6591910277a25dee5972e1297deced3ad7b4e3040de88855f6'); - }); - }); - }); - - describe('sha512/256', function () { - describe('ascii', function () { - describe('less than 128 bytes', function () { - it('should be successful', function () { - expect(sha512_256('')).to.be('c672b8d1ef56ed28ab87c3622c5114069bdd3ad7b8f9737498d0c01ecef0967a'); - expect(sha512_256('The quick brown fox jumps over the lazy dog')).to.be('dd9d67b371519c339ed8dbd25af90e976a1eeefd4ad3d889005e532fc5bef04d'); - expect(sha512_256('The quick brown fox jumps over the lazy dog.')).to.be('1546741840f8a492b959d9b8b2344b9b0eb51b004bba35c0aebaac86d45264c3'); - }); - }); - - describe('more than 128 bytes', function () { - it('should be successful', function () { - expect(sha512_256('The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.')).to.be('21e2e940930b23f1de6377086d07e22033c6bbf3fd9fbf4b62ec66e6c08c25be'); - }); - }); - }); - - describe('UTF8', function () { - describe('less than 128 bytes', function () { - it('should be successful', function () { - expect(sha512_256('中文')).to.be('b6dab29c16ec35ab34a5d92ff135b58de96741dda78b1009a2181cf8b45d2f72'); - expect(sha512_256('aécio')).to.be('122802ca08e39c2ef46f6a81379dc5683bd8aa074dfb54259f0add4d8b5504bc'); - expect(sha512_256('𠜎')).to.be('1032308151c0f4f5f8d4e0d96956352eb8ff87da98df8878d8795a858a7e7c08'); - }); - }); - - describe('more than 128 bytes', function () { - it('should be successful', function () { - expect(sha512_256('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一')).to.be('d32a41d9858e45b68402f77cf9f3c3f992c36a4bffd230f78d666c87f97eaf7e'); - expect(sha512_256('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('bd1abad59e6b8ad69bc17b6e05aa13f0cb725467fbeb45b83d3e4094332d1367'); - }); - }); - }); - - describe('special length', function () { - it('should be successful', function () { - expect(sha512_256('012345678012345678012345678012345678012345678012345678012345012345678012345678012345678012345678012345678012345')).to.be('b8bd1d6cae9992f8619904683d54cbed560ec3ef2a33d8bc225175ec27dc09a4'); - expect(sha512_256('0123456780123456780123456780123456780123456780123456780123450123456780123456780123456780123456780123456780123456')).to.be('4edb8bb93b959f482ce2e434585f3e164f53b00c42c4c46797445b39d225cfe3'); - expect(sha512_256('123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567')).to.be('44da7ca023996ec4b4ded88410e4acc209ca26c34dc0f26550d28503974fe2d1'); - expect(sha512_256('1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678')).to.be('465859c0edc706a2a9c39d3175a715fdf6b764ccc43d45b5668c56f6d8aa9b9e'); - }); - }); - }); - - describe('sha512/224', function () { - describe('ascii', function () { - describe('less than 128 bytes', function () { - it('should be successful', function () { - expect(sha512_224('')).to.be('6ed0dd02806fa89e25de060c19d3ac86cabb87d6a0ddd05c333b84f4'); - expect(sha512_224('The quick brown fox jumps over the lazy dog')).to.be('944cd2847fb54558d4775db0485a50003111c8e5daa63fe722c6aa37'); - expect(sha512_224('The quick brown fox jumps over the lazy dog.')).to.be('6d6a9279495ec4061769752e7ff9c68b6b0b3c5a281b7917ce0572de'); - }); - }); - - describe('more than 128 bytes', function () { - it('should be successful', function () { - expect(sha512_224('The MD5 message-digest algorithm is a widely used cryptographic hash function producing a 128-bit (16-byte) hash value, typically expressed in text format as a 32 digit hexadecimal number. MD5 has been utilized in a wide variety of cryptographic applications, and is also commonly used to verify data integrity.')).to.be('2e962464977b198ee758d615bbc92251ad2e3c0960068e279fd21d2f'); - }); - }); - }); - - describe('UTF8', function () { - describe('less than 128 bytes', function () { - it('should be successful', function () { - expect(sha512_224('中文')).to.be('0f46a0ae7f226517dd66ece0ce1efa29ffb7ced05ac4566fdcaed188'); - expect(sha512_224('aécio')).to.be('562f2e4ee7f7451d20dcc6a0ac1a1e1c4a75f09baaf1cf19af3e15f4'); - expect(sha512_224('𠜎')).to.be('0533318c52b3d4ad355c2a6c7e727ae3d2efa749db480ac33560b059'); - }); - }); - - describe('more than 128 bytes', function () { - it('should be successful', function () { - expect(sha512_224('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一')).to.be('f67e191a5d4ee67a272ccaf6cf597f0c4d6a0c46bd631be7cadb0944'); - expect(sha512_224('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('009c3d1e3172d6df71344982eada855421592aea28acbf660ada7569'); - }); - }); - }); - - describe('special length', function () { - it('should be successful', function () { - expect(sha512_224('012345678012345678012345678012345678012345678012345678012345012345678012345678012345678012345678012345678012345')).to.be('36d6398ca22d885432e2fe3af53b9f83b2dc7a8e9284d65b81f7a879'); - expect(sha512_224('0123456780123456780123456780123456780123456780123456780123450123456780123456780123456780123456780123456780123456')).to.be('d930407a742d10f70747f05271181ad6c1c92f87482ce6a494cb566a'); - expect(sha512_224('123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567')).to.be('1d60954ce7bee0bf5d235fe6bfc928c0f32c0f1c4992451dbc0f4dbf'); - expect(sha512_224('1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678')).to.be('672aef84d579799cedf2fec090ce4ea0d72e0edd2788deaa8200fd67'); - }); - }); - }); + runTestCases('sha512', sha512); + runTestCases('sha384', sha384); + runTestCases('sha512_256', sha512_256); + runTestCases('sha512_224', sha512_224); })(sha512, sha384, sha512_256, sha512_224);