diff --git a/CHANGELOG.md b/CHANGELOG.md index 32a9bb0..c345243 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,9 @@ +# v0.2.1 / 2015-02-08 + +* Remove ascii parameter. +* Improve performance. +* Add test cases. + # v0.2.0 / 2015-02-02 * Add test cases. diff --git a/README.md b/README.md index 16c7af8..f189ed2 100644 --- a/README.md +++ b/README.md @@ -42,56 +42,6 @@ sha384 = require('js-sha512').sha384; sha512_256 = require('js-sha512').sha512_256; sha512_224 = require('js-sha512').sha512_224; ``` -### Methods - -#### sha512(str, asciiOnly) - -Hash string to sha512, set asciiOnly to true for better performace if you ensure input is ascii. - -##### *str: `String`* - -String to hash. - -##### *asciiOnly: `Boolean` (default: `false`)* - -Specify the string encoding is ASCII. - -#### sha384(str, asciiOnly) - -Hash string to sha384, set asciiOnly to true for better performace if you ensure input is ascii. - -##### *str: `String`* - -String to hash. - -##### *asciiOnly: `Boolean` (default: `false`)* - -Specify the string encoding is ASCII. - -#### sha512_256(str, asciiOnly) - -Hash string to sha512/256, set asciiOnly to true for better performace if you ensure input is ascii. - -##### *str: `String`* - -String to hash. - -##### *asciiOnly: `Boolean` (default: `false`)* - -Specify the string encoding is ASCII. - -#### sha512_224(str, asciiOnly) - -Hash string to sha512/224, set asciiOnly to true for better performace if you ensure input is ascii. - -##### *str: `String`* - -String to hash. - -##### *asciiOnly: `Boolean` (default: `false`)* - -Specify the string encoding is ASCII. - ## Example Code ```JavaScript diff --git a/bower.json b/bower.json index 2606b9e..220ee47 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "js-sha512", - "version": "0.2.0", + "version": "0.2.1", "main": ["build/sha512.min.js"], "ignore": [ "samples", diff --git a/build/sha512.min.js b/build/sha512.min.js index 2191080..0c1236b 100644 --- a/build/sha512.min.js +++ b/build/sha512.min.js @@ -1,14 +1,16 @@ -(function(q,ha){var ca="undefined"!=typeof module;ca&&(q=global);var p="0123456789abcdef".split(""),ba=[24,16,8,0],da=[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],r=function(f,l){return Q(f,512,l)},ea=function(f,l){return Q(f,384,l)},fa=function(f,l){return Q(f,256,l)},ga=function(f,l){return Q(f,224,l)},Q=function(f,p,m){var n,A,B,C,D,E,F,G, -H,I,J,K,L,M,N,O;if(!m&&/[^\x00-\x7F]/.test(f)){m=[];for(var t=0,d=0;da?m[t++]=a:(2048>a?m[t++]=192|a>>6:(55296>a||57344<=a?m[t++]=224|a>>12:(a=65536+((a&1023)<<10|f.charCodeAt(++d)&1023),m[t++]=240|a>>18,m[t++]=128|a>>12&63),m[t++]=128|a>>6&63),m[t++]=128|a&63)}f=m.length;t=(f+16>>7)+1<<5;d=[];for(a=0;a>2]|=m[a]<>2]|=128<>7)+1<<5;d=[];for(a=0;a>2]|=f.charCodeAt(a)<>2]|=128<h;++h)d[h]=f[m+h];for(h=32;160>h;h+=2)g=d[h-30],k=d[h-29],P=(g>>>1|k<<31)^(g>>>8|k<<24)^g>>>7,b=(k>>>1|g<<31)^(k>>>8|g<<24)^(k>>>7|g<<25),g=d[h-4],k=d[h-3],y=(g>>>19|k<<13)^(k>>>29|g<<3)^g>>>6,z=(k>>>19|g<<13)^(g>>>29|k<<3)^(k>>>6|g<<26),g=d[h-32],k=d[h-31],a=(k&65535)+(b&65535)+(z&65535),b=(k>>>16)+(b>>>16)+(z>>>16),c=(g&65535)+(P&65535)+(y&65535),e=(g>>>16)+(P>>>16)+(y>>>16),g=d[h-14],k=d[h-13],a+=k&65535,b+=(k>>>16)+(a>>>16),c+=(g&65535)+(b>>>16),e+=(g>>>16)+(c>>>16),d[h]= -e<<16|c&65535,d[h+1]=b<<16|a&65535;u=n;v=A;q=B;r=C;R=D;S=E;T=F;U=G;w=H;x=I;V=J;W=K;X=L;Y=M;Z=N;aa=O;for(h=0;160>h;h+=2)P=(u>>>28|v<<4)^(v>>>2|u<<30)^(v>>>7|u<<25),b=(v>>>28|u<<4)^(u>>>2|v<<30)^(u>>>7|v<<25),y=(w>>>14|x<<18)^(w>>>18|x<<14)^(x>>>9|w<<23),z=(x>>>14|w<<18)^(x>>>18|w<<14)^(w>>>9|x<<23),g=u&q^u&R^q&R,c=v&r^v&S^r&S,a=(c&65535)+(b&65535),b=(c>>>16)+(b>>>16)+(a>>>16),c=(g&65535)+(P&65535)+(b>>>16),e=(g>>>16)+(P>>>16)+(c>>>16),P=e<<16|c&65535,Q=b<<16|a&65535,e=w&V^~w&X,b=x&W^~x&Y,g=d[h],k= -d[h+1],a=(k&65535)+(b&65535)+(z&65535)+(aa&65535),b=(k>>>16)+(b>>>16)+(z>>>16)+(aa>>>16),c=(g&65535)+(e&65535)+(y&65535)+(Z&65535),e=(g>>>16)+(e>>>16)+(y>>>16)+(Z>>>16),g=da[h],k=da[h+1],a+=k&65535,b+=(k>>>16)+(a>>>16),c+=(g&65535)+(b>>>16),e+=(g>>>16)+(c>>>16),y=e<<16|c&65535,z=b<<16|a&65535,Z=X,aa=Y,X=V,Y=W,V=w,W=x,a=(U&65535)+(z&65535),b=(U>>>16)+(z>>>16)+(a>>>16),c=(T&65535)+(y&65535)+(b>>>16),e=(T>>>16)+(y>>>16)+(c>>>16),w=e<<16|c&65535,x=b<<16|a&65535,T=R,U=S,R=q,S=r,q=u,r=v,a=(Q&65535)+(z& -65535),b=(Q>>>16)+(z>>>16)+(a>>>16),c=(P&65535)+(y&65535)+(b>>>16),e=(P>>>16)+(y>>>16)+(c>>>16),u=e<<16|c&65535,v=b<<16|a&65535;a=(A&65535)+(v&65535);b=(A>>>16)+(v>>>16)+(a>>>16);c=(n&65535)+(u&65535)+(b>>>16);e=(n>>>16)+(u>>>16)+(c>>>16);n=e<<16|c&65535;A=b<<16|a&65535;a=(C&65535)+(r&65535);b=(C>>>16)+(r>>>16)+(a>>>16);c=(B&65535)+(q&65535)+(b>>>16);e=(B>>>16)+(q>>>16)+(c>>>16);B=e<<16|c&65535;C=b<<16|a&65535;a=(E&65535)+(S&65535);b=(E>>>16)+(S>>>16)+(a>>>16);c=(D&65535)+(R&65535)+(b>>>16);e=(D>>> -16)+(R>>>16)+(c>>>16);D=e<<16|c&65535;E=b<<16|a&65535;a=(G&65535)+(U&65535);b=(G>>>16)+(U>>>16)+(a>>>16);c=(F&65535)+(T&65535)+(b>>>16);e=(F>>>16)+(T>>>16)+(c>>>16);F=e<<16|c&65535;G=b<<16|a&65535;a=(I&65535)+(x&65535);b=(I>>>16)+(x>>>16)+(a>>>16);c=(H&65535)+(w&65535)+(b>>>16);e=(H>>>16)+(w>>>16)+(c>>>16);H=e<<16|c&65535;I=b<<16|a&65535;a=(K&65535)+(W&65535);b=(K>>>16)+(W>>>16)+(a>>>16);c=(J&65535)+(V&65535)+(b>>>16);e=(J>>>16)+(V>>>16)+(c>>>16);J=e<<16|c&65535;K=b<<16|a&65535;a=(M&65535)+(Y&65535); -b=(M>>>16)+(Y>>>16)+(a>>>16);c=(L&65535)+(X&65535)+(b>>>16);e=(L>>>16)+(X>>>16)+(c>>>16);L=e<<16|c&65535;M=b<<16|a&65535;a=(O&65535)+(aa&65535);b=(O>>>16)+(aa>>>16)+(a>>>16);c=(N&65535)+(Z&65535)+(b>>>16);e=(N>>>16)+(Z>>>16)+(c>>>16);N=e<<16|c&65535;O=b<<16|a&65535}n=l(n)+l(A)+l(B)+l(C)+l(D)+l(E)+l(F)+l(G);if(224==p)return n.substr(0,n.length-8);384<=p&&(n+=l(H)+l(I)+l(J)+l(K));512==p&&(n+=l(L)+l(M)+l(N)+l(O));return n},l=function(f){return p[f>>28&15]+p[f>>24&15]+p[f>>20&15]+p[f>>16&15]+p[f>>12& -15]+p[f>>8&15]+p[f>>4&15]+p[f&15]};!q.JS_SHA512_TEST&&ca?(r.sha512=r,r.sha384=ea,r.sha512_256=fa,r.sha512_224=ga,module.exports=r):q&&(q.sha512=r,q.sha384=ea,q.sha512_256=fa,q.sha512_224=ga)})(this); +(function(E,pa){var ja="undefined"!=typeof module;ja&&(E=global);var a="0123456789abcdef".split(""),oa=[-2147483648,8388608,32768,128],O=[24,16,8,0],ka=[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],b=[],K=function(a,b){return N(a,512,b)},la=function(a,b){return N(a,384,b)},ma=function(a,b){return N(a,256,b)},na=function(a,b){return N(a,224,b)}, +N=function(J,E,K){var m,n,p,q,r,t,u,v,w,x,y,z,A,B,C,D,l;K=!1;var c,P=0,N=0,ea=0,fa=J.length,Q,R,g,d,f,e,ga,ha,h,k,L,M,ia;512==E?(m=1779033703,n=4089235720,p=3144134277,q=2227873595,r=1013904242,t=4271175723,u=2773480762,v=1595750129,w=1359893119,x=2917565137,y=2600822924,z=725511199,A=528734635,B=4215389547,C=1541459225,D=327033209):384==E?(m=3418070365,n=3238371032,p=1654270250,q=914150663,r=2438529370,t=812702999,u=355462360,v=4144912697,w=1731405415,x=4290775857,y=2394180231,z=1750603025,A=3675008525, +B=1694076839,C=1203062813,D=3204075428):256==E?(m=573645204,n=4230739756,p=2673172387,q=3360449730,r=596883563,t=1867755857,u=2520282905,v=1497426621,w=2519219938,x=2827943907,y=3193839141,z=1401305490,A=721525244,B=746961066,C=246885852,D=2177182882):224==E&&(m=2352822216,n=424955298,p=1944164710,q=2312950998,r=502970286,t=855612546,u=1738396948,v=1479516111,w=258812777,x=2077511080,y=2011393907,z=79989058,A=1067287976,B=1780299464,C=286451373,D=2446758561);l=0;do{b[0]=l;b[1]=b[2]=b[3]=b[4]=b[5]= +b[6]=b[7]=b[8]=b[9]=b[10]=b[11]=b[12]=b[13]=b[14]=b[15]=b[16]=b[17]=b[18]=b[19]=b[20]=b[21]=b[22]=b[23]=b[24]=b[25]=b[26]=b[27]=b[28]=b[29]=b[30]=b[31]=b[32]=0;for(c=N;Pc;++P)l=J.charCodeAt(P),128>l?b[c>>2]|=l<l?b[c>>2]|=(192|l>>6)<l||57344<=l?b[c>>2]|=(224|l>>12)<>2]|=(240|l>>18)<>2]|=(128|l>>12&63)<>2]|=(128|l>>6&63)<>2]|=(128|l&63)<>2]|=oa[c&3],++P);l=b[32];P>fa&&112>c&&(b[31]=ea<<3,K=!0);for(c=32;160>c;c+=2)h=b[c-30],k=b[c-29],Q=(h>>>1|k<<31)^(h>>>8|k<<24)^h>>>7,R=(k>>>1|h<<31)^(k>>>8|h<<24)^(k>>>7|h<<25),h=b[c-4],k=b[c-3],g=(h>>>19|k<<13)^(k>>>29|h<<3)^h>>>6,d=(k>>>19|h<<13)^(h>>>29|k<<3)^(k>>>6|h<<26),h=b[c-32],k=b[c-31],L=b[c-14],M=b[c-13],f=(M&65535)+(k&65535)+(R&65535)+(d&65535),d=(M>>>16)+(k>>>16)+(R>>>16)+(d>>>16)+(f>>>16),e=(L&65535)+(h&65535)+(Q&65535)+(g&65535)+(d>>>16),g=(L>>>16)+(h>>>16)+(Q>>> +16)+(g>>>16)+(e>>>16),b[c]=g<<16|e&65535,b[c+1]=d<<16|f&65535;var F=m,G=n,S=p,T=q,U=r,V=t,W=u,X=v,H=w,I=x,Y=y,Z=z,aa=A,ba=B,ca=C,da=D;for(c=0;160>c;c+=2)Q=(F>>>28|G<<4)^(G>>>2|F<<30)^(G>>>7|F<<25),R=(G>>>28|F<<4)^(F>>>2|G<<30)^(F>>>7|G<<25),g=(H>>>14|I<<18)^(H>>>18|I<<14)^(I>>>9|H<<23),d=(I>>>14|H<<18)^(I>>>18|H<<14)^(H>>>9|I<<23),ga=F&S^F&U^S&U,ha=G&T^G&V^T&V,ia=H&Y^~H&aa,e=I&Z^~I&ba,h=b[c],k=b[c+1],L=ka[c],M=ka[c+1],f=(M&65535)+(k&65535)+(e&65535)+(d&65535)+(da&65535),d=(M>>>16)+(k>>>16)+(e>>>16)+ +(d>>>16)+(da>>>16)+(f>>>16),e=(L&65535)+(h&65535)+(ia&65535)+(g&65535)+(ca&65535)+(d>>>16),g=(L>>>16)+(h>>>16)+(ia>>>16)+(g>>>16)+(ca>>>16)+(e>>>16),h=g<<16|e&65535,k=d<<16|f&65535,f=(ha&65535)+(R&65535),d=(ha>>>16)+(R>>>16)+(f>>>16),e=(ga&65535)+(Q&65535)+(d>>>16),g=(ga>>>16)+(Q>>>16)+(e>>>16),L=g<<16|e&65535,M=d<<16|f&65535,ca=aa,da=ba,aa=Y,ba=Z,Y=H,Z=I,f=(X&65535)+(k&65535),d=(X>>>16)+(k>>>16)+(f>>>16),e=(W&65535)+(h&65535)+(d>>>16),g=(W>>>16)+(h>>>16)+(e>>>16),H=g<<16|e&65535,I=d<<16|f&65535, +W=U,X=V,U=S,V=T,S=F,T=G,f=(M&65535)+(k&65535),d=(M>>>16)+(k>>>16)+(f>>>16),e=(L&65535)+(h&65535)+(d>>>16),g=(L>>>16)+(h>>>16)+(e>>>16),F=g<<16|e&65535,G=d<<16|f&65535;f=(n&65535)+(G&65535);d=(n>>>16)+(G>>>16)+(f>>>16);e=(m&65535)+(F&65535)+(d>>>16);g=(m>>>16)+(F>>>16)+(e>>>16);m=g<<16|e&65535;n=d<<16|f&65535;f=(q&65535)+(T&65535);d=(q>>>16)+(T>>>16)+(f>>>16);e=(p&65535)+(S&65535)+(d>>>16);g=(p>>>16)+(S>>>16)+(e>>>16);p=g<<16|e&65535;q=d<<16|f&65535;f=(t&65535)+(V&65535);d=(t>>>16)+(V>>>16)+(f>>>16); +e=(r&65535)+(U&65535)+(d>>>16);g=(r>>>16)+(U>>>16)+(e>>>16);r=g<<16|e&65535;t=d<<16|f&65535;f=(v&65535)+(X&65535);d=(v>>>16)+(X>>>16)+(f>>>16);e=(u&65535)+(W&65535)+(d>>>16);g=(u>>>16)+(W>>>16)+(e>>>16);u=g<<16|e&65535;v=d<<16|f&65535;f=(x&65535)+(I&65535);d=(x>>>16)+(I>>>16)+(f>>>16);e=(w&65535)+(H&65535)+(d>>>16);g=(w>>>16)+(H>>>16)+(e>>>16);w=g<<16|e&65535;x=d<<16|f&65535;f=(z&65535)+(Z&65535);d=(z>>>16)+(Z>>>16)+(f>>>16);e=(y&65535)+(Y&65535)+(d>>>16);g=(y>>>16)+(Y>>>16)+(e>>>16);y=g<<16|e&65535; +z=d<<16|f&65535;f=(B&65535)+(ba&65535);d=(B>>>16)+(ba>>>16)+(f>>>16);e=(A&65535)+(aa&65535)+(d>>>16);g=(A>>>16)+(aa>>>16)+(e>>>16);A=g<<16|e&65535;B=d<<16|f&65535;f=(D&65535)+(da&65535);d=(D>>>16)+(da>>>16)+(f>>>16);e=(C&65535)+(ca&65535)+(d>>>16);g=(C>>>16)+(ca>>>16)+(e>>>16);C=g<<16|e&65535;D=d<<16|f&65535}while(!K);J=a[m>>28&15]+a[m>>24&15]+a[m>>20&15]+a[m>>16&15]+a[m>>12&15]+a[m>>8&15]+a[m>>4&15]+a[m&15]+a[n>>28&15]+a[n>>24&15]+a[n>>20&15]+a[n>>16&15]+a[n>>12&15]+a[n>>8&15]+a[n>>4&15]+a[n&15]+ +a[p>>28&15]+a[p>>24&15]+a[p>>20&15]+a[p>>16&15]+a[p>>12&15]+a[p>>8&15]+a[p>>4&15]+a[p&15]+a[q>>28&15]+a[q>>24&15]+a[q>>20&15]+a[q>>16&15]+a[q>>12&15]+a[q>>8&15]+a[q>>4&15]+a[q&15]+a[r>>28&15]+a[r>>24&15]+a[r>>20&15]+a[r>>16&15]+a[r>>12&15]+a[r>>8&15]+a[r>>4&15]+a[r&15]+a[t>>28&15]+a[t>>24&15]+a[t>>20&15]+a[t>>16&15]+a[t>>12&15]+a[t>>8&15]+a[t>>4&15]+a[t&15]+a[u>>28&15]+a[u>>24&15]+a[u>>20&15]+a[u>>16&15]+a[u>>12&15]+a[u>>8&15]+a[u>>4&15]+a[u&15]+a[v>>28&15]+a[v>>24&15]+a[v>>20&15]+a[v>>16&15]+a[v>> +12&15]+a[v>>8&15]+a[v>>4&15]+a[v&15];if(224==E)return J.substr(0,J.length-8);384<=E&&(J+=a[w>>28&15]+a[w>>24&15]+a[w>>20&15]+a[w>>16&15]+a[w>>12&15]+a[w>>8&15]+a[w>>4&15]+a[w&15]+a[x>>28&15]+a[x>>24&15]+a[x>>20&15]+a[x>>16&15]+a[x>>12&15]+a[x>>8&15]+a[x>>4&15]+a[x&15]+a[y>>28&15]+a[y>>24&15]+a[y>>20&15]+a[y>>16&15]+a[y>>12&15]+a[y>>8&15]+a[y>>4&15]+a[y&15]+a[z>>28&15]+a[z>>24&15]+a[z>>20&15]+a[z>>16&15]+a[z>>12&15]+a[z>>8&15]+a[z>>4&15]+a[z&15]);512==E&&(J+=a[A>>28&15]+a[A>>24&15]+a[A>>20&15]+a[A>> +16&15]+a[A>>12&15]+a[A>>8&15]+a[A>>4&15]+a[A&15]+a[B>>28&15]+a[B>>24&15]+a[B>>20&15]+a[B>>16&15]+a[B>>12&15]+a[B>>8&15]+a[B>>4&15]+a[B&15]+a[C>>28&15]+a[C>>24&15]+a[C>>20&15]+a[C>>16&15]+a[C>>12&15]+a[C>>8&15]+a[C>>4&15]+a[C&15]+a[D>>28&15]+a[D>>24&15]+a[D>>20&15]+a[D>>16&15]+a[D>>12&15]+a[D>>8&15]+a[D>>4&15]+a[D&15]);return J};!E.JS_SHA512_TEST&&ja?(K.sha512=K,K.sha384=la,K.sha512_256=ma,K.sha512_224=na,module.exports=K):E&&(E.sha512=K,E.sha384=la,E.sha512_256=ma,E.sha512_224=na)})(this); diff --git a/package.json b/package.json index da2e289..5ec1791 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "js-sha512", - "version": "0.2.0", + "version": "0.2.1", "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 3f95f98..8fca639 100644 --- a/src/sha512.js +++ b/src/sha512.js @@ -1,5 +1,5 @@ /* - * js-sha512 v0.2.0 + * js-sha512 v0.2.1 * https://github.com/emn178/js-sha512 * * Copyright 2014-2015, emn178@gmail.com @@ -15,8 +15,8 @@ root = global; } var HEX_CHARS = '0123456789abcdef'.split(''); + var EXTRA = [-2147483648, 8388608, 32768, 128]; var SHIFT = [24, 16, 8, 0]; - var K =[0x428A2F98, 0xD728AE22, 0x71374491, 0x23EF65CD, 0xB5C0FBCF, 0xEC4D3B2F, 0xE9B5DBA5, 0x8189DBBC, 0x3956C25B, 0xF348B538, 0x59F111F1, 0xB605D019, @@ -58,6 +58,8 @@ 0x4CC5D4BE, 0xCB3E42B6, 0x597F299C, 0xFC657E2A, 0x5FCB6FAB, 0x3AD6FAEC, 0x6C44198C, 0x4A475817]; + var blocks = []; + var sha512 = function(message, asciiOnly) { return sha2(message, 512, asciiOnly); }; @@ -75,13 +77,11 @@ }; var sha2 = function(message, tbit, asciiOnly) { - var blocks, h0h, h0l, h1h, h1l, h2h, h2l, h3h, h3l, - h4h, h4l, h5h, h5l, h6h, h6l, h7h, h7l; - if(!asciiOnly && /[^\x00-\x7F]/.test(message)) { - blocks = getBlocksFromUtf8(message); - } else { - blocks = getBlocksFromAscii(message); - } + 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, + majh, majl, t1h, t1l, t2h, t2l, chh, chl; if(tbit == 512) { h0h = 0x6A09E667; @@ -152,55 +152,74 @@ h7h = 0x1112E6AD; h7l = 0x91D692A1; } - - for(var i = 0, length = blocks.length;i < length;i += 32) { - var w = [], s0h, s0l, s1h, s1l, c1, c2, c3, c4, j, - ah, al, bh, bl, ch, cl, dh, dl, eh, el, fh, fl, gh, gl, hh, hl, - majh, majl, t1h, t1l, t2h, t2l, chh, chl; - for(j = 0;j < 32;++j) { - w[j] = blocks[i + j]; + block = 0; + do { + blocks[0] = 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; + 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; + } + for(j = 32;j < 160;j += 2) { - t1h = w[j - 30]; - t1l = w[j - 29]; + 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 = w[j - 4]; - t1l = w[j - 3]; + 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 = w[j - 32]; - t1l = w[j - 31]; - t2h = w[j - 14]; - t2l = w[j - 13]; + 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); - w[j] = (c4 << 16) | (c3 & 0xFFFF); - w[j + 1] = (c2 << 16) | (c1 & 0xFFFF); + blocks[j] = (c4 << 16) | (c3 & 0xFFFF); + blocks[j + 1] = (c2 << 16) | (c1 & 0xFFFF); } - 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; + 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; for(j = 0;j < 160;j += 2) { 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)); @@ -214,8 +233,8 @@ chh = (eh & fh) ^ (~eh & gh); chl = (el & fl) ^ (~el & gl); - t1h = w[j]; - t1l = w[j + 1]; + t1h = blocks[j]; + t1l = blocks[j + 1]; t2h = K[j]; t2l = K[j + 1]; @@ -329,7 +348,7 @@ h7h = (c4 << 16) | (c3 & 0xFFFF); h7l = (c2 << 16) | (c1 & 0xFFFF); - } + } while(!end); var hex = HEX_CHARS[(h0h >> 28) & 0x0F] + HEX_CHARS[(h0h >> 24) & 0x0F] + HEX_CHARS[(h0h >> 20) & 0x0F] + HEX_CHARS[(h0h >> 16) & 0x0F] + @@ -405,64 +424,6 @@ return hex; }; - var getBytesFromUtf8 = function(str) { - var bytes = [], index = 0; - for (var i = 0;i < str.length; i++) { - var c = str.charCodeAt(i); - if (c < 0x80) { - bytes[index++] = c; - } else if (c < 0x800) { - bytes[index++] = 0xc0 | (c >> 6); - bytes[index++] = 0x80 | (c & 0x3f); - } else if (c < 0xd800 || c >= 0xe000) { - bytes[index++] = 0xe0 | (c >> 12); - bytes[index++] = 0x80 | ((c >> 6) & 0x3f); - bytes[index++] = 0x80 | (c & 0x3f); - } else { - c = 0x10000 + (((c & 0x3ff) << 10) | (str.charCodeAt(++i) & 0x3ff)); - bytes[index++] = 0xf0 | (c >> 18); - bytes[index++] = 0x80 | ((c >> 12) & 0x3f); - bytes[index++] = 0x80 | ((c >> 6) & 0x3f); - bytes[index++] = 0x80 | (c & 0x3f); - } - } - return bytes; - }; - - var getBlocksFromAscii = function(message) { - // a block is 32 bits(4 bytes), a chunk is 1024 bits(128 bytes) - var length = message.length; - var chunkCount = ((length + 16) >> 7) + 1; - var blockCount = chunkCount << 5; // chunkCount * 32 - var blocks = [], i; - for(i = 0;i < blockCount;++i) { - blocks[i] = 0; - } - for(i = 0;i < length;++i) { - blocks[i >> 2] |= message.charCodeAt(i) << SHIFT[i & 3]; - } - blocks[i >> 2] |= 0x80 << SHIFT[i & 3]; - blocks[blockCount - 1] = length << 3; // length * 8 - return blocks; - }; - - var getBlocksFromUtf8 = function(message) { - var bytes = getBytesFromUtf8(message); - var length = bytes.length; - var chunkCount = ((length + 16) >> 7) + 1; - var blockCount = chunkCount << 5; // chunkCount * 32 - var blocks = [], i; - for(i = 0;i < blockCount;++i) { - blocks[i] = 0; - } - for(i = 0;i < length;++i) { - blocks[i >> 2] |= bytes[i] << SHIFT[i & 3]; - } - blocks[i >> 2] |= 0x80 << SHIFT[i & 3]; - blocks[blockCount - 1] = length << 3; // length * 8 - return blocks; - }; - if(!root.JS_SHA512_TEST && NODE_JS) { sha512.sha512 = sha512; sha512.sha384 = sha384; diff --git a/tests/test.js b/tests/test.js index 9c21662..72850e0 100644 --- a/tests/test.js +++ b/tests/test.js @@ -1,155 +1,177 @@ -describe('sha512', function() { - describe('ascii', function() { - describe('less than 64 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.', true)).to.be('91ea1245f20d46ae9a037a989f54f1f790f0a47607eeb8a14d12890cea77a1bbc6c7ed9cf205e67b7f2b8fd4c7dfd3a7a8617e45f3c463d481c7e586c39ac1ed'); - expect(sha512('01234567801234567801234567801234567801234567801234567801234')).to.be('20caf4b6d9df9eb55dedd917bbb22ca81f1629c3877501fc33fd6d58729d7fe917dae36901fa45ea682c7660f68a4593c7594f538828ded46a1f6cf33ff97c25'); - expect(sha512('012345678012345678012345678012345678012345678012345678012345')).to.be('78a3041f566c0fa6573251dd584cf8532406cbe643e7198f093ce7f07eca1bc8852e8d25c2d50cfacb781c488a1517fba4b34c05e5c21ac3699c48328dd12c22'); +(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.', true)).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'); + }); + }); + + 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'); + }); }); }); - describe('more than 64 bytes', function() { + describe('special length', 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'); + expect(sha512('012345678012345678012345678012345678012345678012345678012345012345678012345678012345678012345678012345678012345')).to.be('a58b6132677883b0ab565dfb4284a4441a35a0954738d90ad8c17800de5ee44f6482efd4c13e05a6e907fb830fc9d9039bd96f004e5e3e8d00802fb9448dec03'); + expect(sha512('0123456780123456780123456780123456780123456780123456780123450123456780123456780123456780123456780123456780123456')).to.be('34a93615cbf314a799857dc9c1804ed04be60b481229ea3a53e00316e62ac7924ea7e65435cbd9fc7d8e10e0e52e56bd99ac571c1c470cbf9252bdd70b3fd8a3'); expect(sha512('123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567')).to.be('4555ce42c2e19f9a09a34b3e40f85178c22cde939ac42de4d33ca0e08e3c6a2e531a3abd9265a72cc22a19d085eb2c468c20d6cfb022d8914b53d6a2b485b7c9'); expect(sha512('1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678')).to.be('8bfa769385fe4b7d4aabf304498522d5aefa5356e42e9ae5ac9fa6b00a483685586cc0fa44da3df335719cbb524b12433afda18361d70e4c584ea81a927c670c'); }); }); }); - describe('UTF8', function() { - describe('less than 64 bytes', function() { - it('should be successful', function() { - expect(sha512('中文')).to.be('8b88efc2ebbcbdad5ac2d65af05bec57bda25e71fd5fb25bbd892057a2755fbd05d8d8491cb2946febd5b0f124ffdfbaecf7e34946353c4f1b5ab29545895468'); - expect(sha512('aécio')).to.be('e1c6925243db76985abacaf9fa85e22697f549e67f65a36c88e4046a2260990ff9eefc3402396ea8dcbe8c592d8d5671bea612156eda38d3708d394bbd17d493'); - expect(sha512('𠜎')).to.be('f3e7ee9cdf7dbb52f7edd59ce3d49868c64f2b3aceceab060b8eaaebdf9de0dae5866d660e3319c5aad426a2176cb1703efc73eb24d1a90458ceda1b7f4e3940'); + 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.', true)).to.be('ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7'); + }); }); - }); - describe('more than 64 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'); + 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'); + }); }); }); - }); -}); -describe('sha384', function() { - describe('ascii', function() { - describe('less than 64 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.', true)).to.be('ed892481d8272ca6df370bf706e4d7bc1b5739fa2177aae6c50e946678718fc67a7af2819a021c2fc34e91bdb63409d7'); - expect(sha384('01234567801234567801234567801234567801234567801234567801234')).to.be('1a5b170da97d4042ae4759f0b8966a3007d8d1132e1881785bd8ac1b07b5f6dc427fc6bdf14104305abb8cafb071625f'); - expect(sha384('012345678012345678012345678012345678012345678012345678012345')).to.be('c285661ebb1b808a8e6dc3f4c5252bb3cd2b2d74cffa91ad0b7009402a088b9032476320791398ab438bfd66ec3e90c8'); + 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('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'); + }); }); }); - describe('more than 64 bytes', function() { + describe('special length', 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'); + 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('UTF8', function() { - describe('less than 64 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('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.', true)).to.be('1546741840f8a492b959d9b8b2344b9b0eb51b004bba35c0aebaac86d45264c3'); + }); }); - }); - describe('more than 64 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'); + 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('sha512/256', function() { - describe('ascii', function() { - describe('less than 64 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.', true)).to.be('1546741840f8a492b959d9b8b2344b9b0eb51b004bba35c0aebaac86d45264c3'); - expect(sha512_256('01234567801234567801234567801234567801234567801234567801234')).to.be('e137e353ba2d34a94341dbb8d69ed1f2eefb1f045ff48f5b61272be51dc4bb61'); - expect(sha512_256('012345678012345678012345678012345678012345678012345678012345')).to.be('a5dcc9632aba17ce3c38de2c9674b75389931bfb6dc4836f8c31643013e0cd7d'); + 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('more than 64 bytes', function() { + describe('special length', 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'); + 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('UTF8', function() { - describe('less than 64 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('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.', true)).to.be('6d6a9279495ec4061769752e7ff9c68b6b0b3c5a281b7917ce0572de'); + }); }); - }); - describe('more than 64 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('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('sha512/224', function() { - describe('ascii', function() { - describe('less than 64 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.', true)).to.be('6d6a9279495ec4061769752e7ff9c68b6b0b3c5a281b7917ce0572de'); - expect(sha512_224('01234567801234567801234567801234567801234567801234567801234')).to.be('1dfaaf5806ec0ceba60da809c2b4151e5dc5081c6a346e504ea556ea'); - expect(sha512_224('012345678012345678012345678012345678012345678012345678012345')).to.be('10dced83a53416883dbd7ace74955bb3d42de0b65b2087b902537c91'); + 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 64 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'); - expect(sha512_224('123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567')).to.be('1d60954ce7bee0bf5d235fe6bfc928c0f32c0f1c4992451dbc0f4dbf'); - expect(sha512_224('1234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678123456781234567812345678')).to.be('672aef84d579799cedf2fec090ce4ea0d72e0edd2788deaa8200fd67'); + 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('UTF8', function() { - describe('less than 64 bytes', function() { + describe('special length', 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 64 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'); + 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'); }); }); - }); -}); + }); +})(sha512, sha384, sha512_256, sha512_224);