From 7851f7ad68fdfaad30ec8e1c6717851ae724db23 Mon Sep 17 00:00:00 2001 From: Chen Yi-Cyuan Date: Thu, 21 May 2015 19:28:19 +0800 Subject: [PATCH] Support byte array and ArrayBuffer input. --- CHANGELOG.md | 4 ++ LICENSE.txt | 4 +- README.md | 14 ++++++ bower.json | 2 +- build/sha3.min.js | 28 +++++------ package.json | 2 +- src/sha3.js | 47 +++++++++++------- tests/test-keccak.js | 114 +++++++++++++++++++++++++------------------ tests/test.js | 114 +++++++++++++++++++++++++------------------ 9 files changed, 200 insertions(+), 129 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7e57b06..0afb118 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# v0.3.0 / 2015-05-21 + +* Support byte array and ArrayBuffer input. + # v0.2.0 / 2015-04-04 * Implement NIST's May 2014 SHA-3 version. diff --git a/LICENSE.txt b/LICENSE.txt index df6ab18..aeb45cf 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,4 +1,6 @@ -Copyright 2015 emn178@gmail.com +Copyright (c) 2015 Chen Yi-Cyuan + +MIT License Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the diff --git a/README.md b/README.md index 8798d4c..7f68c08 100644 --- a/README.md +++ b/README.md @@ -157,6 +157,20 @@ keccak_256('中文'); keccak_224('中文'); // f71837502ba8e10837bdd8d365adb85591895602fc552b48b7390abd ``` + +It also supports byte `Array`, `Uint8Array`, `ArrayBuffer` input: + +Code +```JavaScript +sha3_512([]); +// a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26 + +sha3_512(new Uint8Array([])); +// a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26 + +// ... +``` + ## Benchmark [UTF8](http://jsperf.com/sha3/5) [ASCII](http://jsperf.com/sha3/4) diff --git a/bower.json b/bower.json index ca06b90..e6c8929 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "js-sha3", - "version": "0.2.0", + "version": "0.3.0", "main": ["src/sha3.js"], "ignore": [ "samples", diff --git a/build/sha3.min.js b/build/sha3.min.js index 211641f..de57dc2 100644 --- a/build/sha3.min.js +++ b/build/sha3.min.js @@ -1,5 +1,5 @@ /* - * js-sha3 v0.2.0 + * js-sha3 v0.3.0 * https://github.com/emn178/js-sha3 * * Copyright 2015, emn178@gmail.com @@ -7,16 +7,16 @@ * Licensed under the MIT license: * http://www.opensource.org/licenses/MIT */ -(function(e,za){var E="undefined"!=typeof module;E&&(e=global,e.JS_SHA3_TEST&&(e.navigator={userAgent:"Chrome"}));var Aa=(e.JS_SHA3_TEST||!E)&&-1!=navigator.userAgent.indexOf("Chrome"),b="0123456789abcdef".split(""),H=[1,256,65536,16777216],B=[6,1536,393216,100663296],C=[0,8,16,24],sa=[1,0,32898,0,32906,2147483648,2147516416,2147483648,32907,0,2147483649,0,2147516545,2147483648,32777,2147483648,138,0,136,0,2147516425,0,2147483658,0,2147516555,0,139,2147483648,32905,2147483648,32771,2147483648,32770, -2147483648,128,2147483648,32778,0,2147483658,2147483648,2147516545,2147483648,32896,2147483648,2147483649,0,2147516424,2147483648],f=[],a=[],G=function(a){return A(a,224,H)},ta=function(a){return A(a,256,H)},ua=function(a){return A(a,384,H)},va=function(a){return A(a,224,B)},wa=function(a){return A(a,256,B)},xa=function(a){return A(a,384,B)},ya=function(a){return A(a,512,B)},A=function(v,e,A){e===za&&(e=512,A=H);var B,g,E=!1,D=0,G=0,qa=v.length,c,d,h,k,l,m,n,p,q,r,t,u,w,x,y,z,I,J,K,L,M,N,O,P,Q,R, -S,T,U,V,W,X,Y,Z,aa,ba,ca,da,ea,fa,ga,ha,ia,ja,ka,la,ma,na,oa,pa,F=(1600-2*e)/32,ra=4*F;for(c=0;50>c;++c)a[c]=0;B=0;do{f[0]=B;for(c=1;cg?f[c>>2]|=g<g?f[c>>2]|=(192|g>>6)<g||57344<=g?f[c>>2]|=(224|g>>12)<>2]|=(240|g>>18)<>2]|=(128|g>>12&63)<>2]|=(128|g>>6&63)<>2]|=(128|g&63)<> -2]|=A[c&3],++D);B=f[F];D>qa&&cg;g+=2)h=a[0]^a[10]^a[20]^a[30]^a[40],k=a[1]^a[11]^a[21]^a[31]^a[41],l=a[2]^a[12]^a[22]^a[32]^a[42],m=a[3]^a[13]^a[23]^a[33]^a[43],n=a[4]^a[14]^a[24]^a[34]^a[44],p=a[5]^a[15]^a[25]^a[35]^a[45],q=a[6]^a[16]^a[26]^a[36]^a[46],r=a[7]^a[17]^a[27]^a[37]^a[47],t=a[8]^a[18]^a[28]^a[38]^a[48],u=a[9]^a[19]^a[29]^a[39]^a[49],d=t^(l<<1|m>>>31),c=u^(m<<1|l>>>31),a[0]^=d,a[1]^=c,a[10]^=d,a[11]^=c,a[20]^=d,a[21]^= -c,a[30]^=d,a[31]^=c,a[40]^=d,a[41]^=c,d=h^(n<<1|p>>>31),c=k^(p<<1|n>>>31),a[2]^=d,a[3]^=c,a[12]^=d,a[13]^=c,a[22]^=d,a[23]^=c,a[32]^=d,a[33]^=c,a[42]^=d,a[43]^=c,d=l^(q<<1|r>>>31),c=m^(r<<1|q>>>31),a[4]^=d,a[5]^=c,a[14]^=d,a[15]^=c,a[24]^=d,a[25]^=c,a[34]^=d,a[35]^=c,a[44]^=d,a[45]^=c,d=n^(t<<1|u>>>31),c=p^(u<<1|t>>>31),a[6]^=d,a[7]^=c,a[16]^=d,a[17]^=c,a[26]^=d,a[27]^=c,a[36]^=d,a[37]^=c,a[46]^=d,a[47]^=c,d=q^(h<<1|k>>>31),c=r^(k<<1|h>>>31),a[8]^=d,a[9]^=c,a[18]^=d,a[19]^=c,a[28]^=d,a[29]^=c,a[38]^= -d,a[39]^=c,a[48]^=d,a[49]^=c,d=a[0],c=a[1],Y=a[11]<<4|a[10]>>>28,Z=a[10]<<4|a[11]>>>28,y=a[20]<<3|a[21]>>>29,z=a[21]<<3|a[20]>>>29,ma=a[31]<<9|a[30]>>>23,na=a[30]<<9|a[31]>>>23,U=a[40]<<18|a[41]>>>14,V=a[41]<<18|a[40]>>>14,M=a[2]<<1|a[3]>>>31,N=a[3]<<1|a[2]>>>31,h=a[13]<<12|a[12]>>>20,k=a[12]<<12|a[13]>>>20,aa=a[22]<<10|a[23]>>>22,ba=a[23]<<10|a[22]>>>22,I=a[33]<<13|a[32]>>>19,J=a[32]<<13|a[33]>>>19,oa=a[42]<<2|a[43]>>>30,pa=a[43]<<2|a[42]>>>30,ga=a[5]<<30|a[4]>>>2,ha=a[4]<<30|a[5]>>>2,O=a[14]<<6| -a[15]>>>26,P=a[15]<<6|a[14]>>>26,l=a[25]<<11|a[24]>>>21,m=a[24]<<11|a[25]>>>21,ca=a[34]<<15|a[35]>>>17,da=a[35]<<15|a[34]>>>17,K=a[45]<<29|a[44]>>>3,L=a[44]<<29|a[45]>>>3,t=a[6]<<28|a[7]>>>4,u=a[7]<<28|a[6]>>>4,ia=a[17]<<23|a[16]>>>9,ja=a[16]<<23|a[17]>>>9,Q=a[26]<<25|a[27]>>>7,R=a[27]<<25|a[26]>>>7,n=a[36]<<21|a[37]>>>11,p=a[37]<<21|a[36]>>>11,ea=a[47]<<24|a[46]>>>8,fa=a[46]<<24|a[47]>>>8,W=a[8]<<27|a[9]>>>5,X=a[9]<<27|a[8]>>>5,w=a[18]<<20|a[19]>>>12,x=a[19]<<20|a[18]>>>12,ka=a[29]<<7|a[28]>>>25, -la=a[28]<<7|a[29]>>>25,S=a[38]<<8|a[39]>>>24,T=a[39]<<8|a[38]>>>24,q=a[48]<<14|a[49]>>>18,r=a[49]<<14|a[48]>>>18,a[0]=d^~h&l,a[1]=c^~k&m,a[10]=t^~w&y,a[11]=u^~x&z,a[20]=M^~O&Q,a[21]=N^~P&R,a[30]=W^~Y&aa,a[31]=X^~Z&ba,a[40]=ga^~ia&ka,a[41]=ha^~ja&la,a[2]=h^~l&n,a[3]=k^~m&p,a[12]=w^~y&I,a[13]=x^~z&J,a[22]=O^~Q&S,a[23]=P^~R&T,a[32]=Y^~aa&ca,a[33]=Z^~ba&da,a[42]=ia^~ka&ma,a[43]=ja^~la&na,a[4]=l^~n&q,a[5]=m^~p&r,a[14]=y^~I&K,a[15]=z^~J&L,a[24]=Q^~S&U,a[25]=R^~T&V,a[34]=aa^~ca&ea,a[35]=ba^~da&fa,a[44]= -ka^~ma&oa,a[45]=la^~na&pa,a[6]=n^~q&d,a[7]=p^~r&c,a[16]=I^~K&t,a[17]=J^~L&u,a[26]=S^~U&M,a[27]=T^~V&N,a[36]=ca^~ea&W,a[37]=da^~fa&X,a[46]=ma^~oa&ga,a[47]=na^~pa&ha,a[8]=q^~d&h,a[9]=r^~c&k,a[18]=K^~t&w,a[19]=L^~u&x,a[28]=U^~M&O,a[29]=V^~N&P,a[38]=ea^~W&Y,a[39]=fa^~X&Z,a[48]=oa^~ga&ia,a[49]=pa^~ha&ja,a[0]^=sa[g],a[1]^=sa[g+1]}while(!E);v="";if(Aa)d=a[0],c=a[1],h=a[2],k=a[3],l=a[4],m=a[5],n=a[6],p=a[7],q=a[8],r=a[9],t=a[10],u=a[11],w=a[12],x=a[13],y=a[14],z=a[15],v+=b[d>>4&15]+b[d&15]+b[d>>12&15]+b[d>> -8&15]+b[d>>20&15]+b[d>>16&15]+b[d>>28&15]+b[d>>24&15]+b[c>>4&15]+b[c&15]+b[c>>12&15]+b[c>>8&15]+b[c>>20&15]+b[c>>16&15]+b[c>>28&15]+b[c>>24&15]+b[h>>4&15]+b[h&15]+b[h>>12&15]+b[h>>8&15]+b[h>>20&15]+b[h>>16&15]+b[h>>28&15]+b[h>>24&15]+b[k>>4&15]+b[k&15]+b[k>>12&15]+b[k>>8&15]+b[k>>20&15]+b[k>>16&15]+b[k>>28&15]+b[k>>24&15]+b[l>>4&15]+b[l&15]+b[l>>12&15]+b[l>>8&15]+b[l>>20&15]+b[l>>16&15]+b[l>>28&15]+b[l>>24&15]+b[m>>4&15]+b[m&15]+b[m>>12&15]+b[m>>8&15]+b[m>>20&15]+b[m>>16&15]+b[m>>28&15]+b[m>>24&15]+ -b[n>>4&15]+b[n&15]+b[n>>12&15]+b[n>>8&15]+b[n>>20&15]+b[n>>16&15]+b[n>>28&15]+b[n>>24&15],256<=e&&(v+=b[p>>4&15]+b[p&15]+b[p>>12&15]+b[p>>8&15]+b[p>>20&15]+b[p>>16&15]+b[p>>28&15]+b[p>>24&15]),384<=e&&(v+=b[q>>4&15]+b[q&15]+b[q>>12&15]+b[q>>8&15]+b[q>>20&15]+b[q>>16&15]+b[q>>28&15]+b[q>>24&15]+b[r>>4&15]+b[r&15]+b[r>>12&15]+b[r>>8&15]+b[r>>20&15]+b[r>>16&15]+b[r>>28&15]+b[r>>24&15]+b[t>>4&15]+b[t&15]+b[t>>12&15]+b[t>>8&15]+b[t>>20&15]+b[t>>16&15]+b[t>>28&15]+b[t>>24&15]+b[u>>4&15]+b[u&15]+b[u>>12& -15]+b[u>>8&15]+b[u>>20&15]+b[u>>16&15]+b[u>>28&15]+b[u>>24&15]),512==e&&(v+=b[w>>4&15]+b[w&15]+b[w>>12&15]+b[w>>8&15]+b[w>>20&15]+b[w>>16&15]+b[w>>28&15]+b[w>>24&15]+b[x>>4&15]+b[x&15]+b[x>>12&15]+b[x>>8&15]+b[x>>20&15]+b[x>>16&15]+b[x>>28&15]+b[x>>24&15]+b[y>>4&15]+b[y&15]+b[y>>12&15]+b[y>>8&15]+b[y>>20&15]+b[y>>16&15]+b[y>>28&15]+b[y>>24&15]+b[z>>4&15]+b[z&15]+b[z>>12&15]+b[z>>8&15]+b[z>>20&15]+b[z>>16&15]+b[z>>28&15]+b[z>>24&15]);else for(c=0,g=e/32;c>4&15]+b[d&15]+b[d>>12& -15]+b[d>>8&15]+b[d>>20&15]+b[d>>16&15]+b[d>>28&15]+b[d>>24&15];return v};!e.JS_SHA3_TEST&&E?module.exports={sha3_512:ya,sha3_384:xa,sha3_256:wa,sha3_224:va,keccak_512:A,keccak_384:ua,keccak_256:ta,keccak_224:G}:e&&(e.sha3_512=ya,e.sha3_384=xa,e.sha3_256=wa,e.sha3_224=va,e.keccak_512=A,e.keccak_384=ua,e.keccak_256=ta,e.keccak_224=G)})(this); +(function(f,Aa){var D="undefined"!=typeof module;D&&(f=global,f.JS_SHA3_TEST&&(f.navigator={userAgent:"Chrome"}));var Ba=(f.JS_SHA3_TEST||!D)&&-1!=navigator.userAgent.indexOf("Chrome"),b="0123456789abcdef".split(""),H=[1,256,65536,16777216],E=[6,1536,393216,100663296],C=[0,8,16,24],ta=[1,0,32898,0,32906,2147483648,2147516416,2147483648,32907,0,2147483649,0,2147516545,2147483648,32777,2147483648,138,0,136,0,2147516425,0,2147483658,0,2147516555,0,139,2147483648,32905,2147483648,32771,2147483648,32770, +2147483648,128,2147483648,32778,0,2147483658,2147483648,2147516545,2147483648,32896,2147483648,2147483649,0,2147516424,2147483648],e=[],a=[],G=function(a){return A(a,224,H)},ua=function(a){return A(a,256,H)},va=function(a){return A(a,384,H)},wa=function(a){return A(a,224,E)},xa=function(a){return A(a,256,E)},ya=function(a){return A(a,384,E)},za=function(a){return A(a,512,E)},A=function(h,f,A){var E="string"!=typeof h;E&&h.constructor==ArrayBuffer&&(h=new Uint8Array(h));f===Aa&&(f=512,A=H);var D,g, +G=!1,B=0,ra=0,I=h.length,c,d,k,l,m,n,p,q,r,t,u,v,w,x,y,z,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,aa,ba,ca,da,ea,fa,ga,ha,ia,ja,ka,la,ma,na,oa,pa,qa,F=(1600-2*f)/32,sa=4*F;for(c=0;50>c;++c)a[c]=0;D=0;do{e[0]=D;for(c=1;cc;++B)e[c>>2]|=h[B]<g?e[c>>2]|=g<g?e[c>>2]|=(192|g>>6)<g||57344<=g?e[c>>2]|=(224|g>>12)<>2]|=(240| +g>>18)<>2]|=(128|g>>12&63)<>2]|=(128|g>>6&63)<>2]|=(128|g&63)<>2]|=A[c&3],++B);D=e[F];B>I&&cg;g+=2)k=a[0]^a[10]^a[20]^a[30]^a[40],l=a[1]^a[11]^a[21]^a[31]^a[41],m=a[2]^a[12]^a[22]^a[32]^a[42],n=a[3]^a[13]^a[23]^a[33]^a[43],p=a[4]^a[14]^a[24]^a[34]^a[44],q=a[5]^a[15]^a[25]^a[35]^a[45],r=a[6]^a[16]^a[26]^a[36]^a[46],t=a[7]^a[17]^a[27]^a[37]^a[47],u=a[8]^a[18]^ +a[28]^a[38]^a[48],v=a[9]^a[19]^a[29]^a[39]^a[49],d=u^(m<<1|n>>>31),c=v^(n<<1|m>>>31),a[0]^=d,a[1]^=c,a[10]^=d,a[11]^=c,a[20]^=d,a[21]^=c,a[30]^=d,a[31]^=c,a[40]^=d,a[41]^=c,d=k^(p<<1|q>>>31),c=l^(q<<1|p>>>31),a[2]^=d,a[3]^=c,a[12]^=d,a[13]^=c,a[22]^=d,a[23]^=c,a[32]^=d,a[33]^=c,a[42]^=d,a[43]^=c,d=m^(r<<1|t>>>31),c=n^(t<<1|r>>>31),a[4]^=d,a[5]^=c,a[14]^=d,a[15]^=c,a[24]^=d,a[25]^=c,a[34]^=d,a[35]^=c,a[44]^=d,a[45]^=c,d=p^(u<<1|v>>>31),c=q^(v<<1|u>>>31),a[6]^=d,a[7]^=c,a[16]^=d,a[17]^=c,a[26]^=d,a[27]^= +c,a[36]^=d,a[37]^=c,a[46]^=d,a[47]^=c,d=r^(k<<1|l>>>31),c=t^(l<<1|k>>>31),a[8]^=d,a[9]^=c,a[18]^=d,a[19]^=c,a[28]^=d,a[29]^=c,a[38]^=d,a[39]^=c,a[48]^=d,a[49]^=c,d=a[0],c=a[1],Z=a[11]<<4|a[10]>>>28,aa=a[10]<<4|a[11]>>>28,y=a[20]<<3|a[21]>>>29,z=a[21]<<3|a[20]>>>29,na=a[31]<<9|a[30]>>>23,oa=a[30]<<9|a[31]>>>23,V=a[40]<<18|a[41]>>>14,W=a[41]<<18|a[40]>>>14,N=a[2]<<1|a[3]>>>31,O=a[3]<<1|a[2]>>>31,k=a[13]<<12|a[12]>>>20,l=a[12]<<12|a[13]>>>20,ba=a[22]<<10|a[23]>>>22,ca=a[23]<<10|a[22]>>>22,J=a[33]<<13| +a[32]>>>19,K=a[32]<<13|a[33]>>>19,pa=a[42]<<2|a[43]>>>30,qa=a[43]<<2|a[42]>>>30,ha=a[5]<<30|a[4]>>>2,ia=a[4]<<30|a[5]>>>2,P=a[14]<<6|a[15]>>>26,Q=a[15]<<6|a[14]>>>26,m=a[25]<<11|a[24]>>>21,n=a[24]<<11|a[25]>>>21,da=a[34]<<15|a[35]>>>17,ea=a[35]<<15|a[34]>>>17,L=a[45]<<29|a[44]>>>3,M=a[44]<<29|a[45]>>>3,u=a[6]<<28|a[7]>>>4,v=a[7]<<28|a[6]>>>4,ja=a[17]<<23|a[16]>>>9,ka=a[16]<<23|a[17]>>>9,R=a[26]<<25|a[27]>>>7,S=a[27]<<25|a[26]>>>7,p=a[36]<<21|a[37]>>>11,q=a[37]<<21|a[36]>>>11,fa=a[47]<<24|a[46]>>> +8,ga=a[46]<<24|a[47]>>>8,X=a[8]<<27|a[9]>>>5,Y=a[9]<<27|a[8]>>>5,w=a[18]<<20|a[19]>>>12,x=a[19]<<20|a[18]>>>12,la=a[29]<<7|a[28]>>>25,ma=a[28]<<7|a[29]>>>25,T=a[38]<<8|a[39]>>>24,U=a[39]<<8|a[38]>>>24,r=a[48]<<14|a[49]>>>18,t=a[49]<<14|a[48]>>>18,a[0]=d^~k&m,a[1]=c^~l&n,a[10]=u^~w&y,a[11]=v^~x&z,a[20]=N^~P&R,a[21]=O^~Q&S,a[30]=X^~Z&ba,a[31]=Y^~aa&ca,a[40]=ha^~ja&la,a[41]=ia^~ka&ma,a[2]=k^~m&p,a[3]=l^~n&q,a[12]=w^~y&J,a[13]=x^~z&K,a[22]=P^~R&T,a[23]=Q^~S&U,a[32]=Z^~ba&da,a[33]=aa^~ca&ea,a[42]=ja^~la& +na,a[43]=ka^~ma&oa,a[4]=m^~p&r,a[5]=n^~q&t,a[14]=y^~J&L,a[15]=z^~K&M,a[24]=R^~T&V,a[25]=S^~U&W,a[34]=ba^~da&fa,a[35]=ca^~ea&ga,a[44]=la^~na&pa,a[45]=ma^~oa&qa,a[6]=p^~r&d,a[7]=q^~t&c,a[16]=J^~L&u,a[17]=K^~M&v,a[26]=T^~V&N,a[27]=U^~W&O,a[36]=da^~fa&X,a[37]=ea^~ga&Y,a[46]=na^~pa&ha,a[47]=oa^~qa&ia,a[8]=r^~d&k,a[9]=t^~c&l,a[18]=L^~u&w,a[19]=M^~v&x,a[28]=V^~N&P,a[29]=W^~O&Q,a[38]=fa^~X&Z,a[39]=ga^~Y&aa,a[48]=pa^~ha&ja,a[49]=qa^~ia&ka,a[0]^=ta[g],a[1]^=ta[g+1]}while(!G);h="";if(Ba)d=a[0],c=a[1],k=a[2], +l=a[3],m=a[4],n=a[5],p=a[6],q=a[7],r=a[8],t=a[9],u=a[10],v=a[11],w=a[12],x=a[13],y=a[14],z=a[15],h+=b[d>>4&15]+b[d&15]+b[d>>12&15]+b[d>>8&15]+b[d>>20&15]+b[d>>16&15]+b[d>>28&15]+b[d>>24&15]+b[c>>4&15]+b[c&15]+b[c>>12&15]+b[c>>8&15]+b[c>>20&15]+b[c>>16&15]+b[c>>28&15]+b[c>>24&15]+b[k>>4&15]+b[k&15]+b[k>>12&15]+b[k>>8&15]+b[k>>20&15]+b[k>>16&15]+b[k>>28&15]+b[k>>24&15]+b[l>>4&15]+b[l&15]+b[l>>12&15]+b[l>>8&15]+b[l>>20&15]+b[l>>16&15]+b[l>>28&15]+b[l>>24&15]+b[m>>4&15]+b[m&15]+b[m>>12&15]+b[m>>8&15]+ +b[m>>20&15]+b[m>>16&15]+b[m>>28&15]+b[m>>24&15]+b[n>>4&15]+b[n&15]+b[n>>12&15]+b[n>>8&15]+b[n>>20&15]+b[n>>16&15]+b[n>>28&15]+b[n>>24&15]+b[p>>4&15]+b[p&15]+b[p>>12&15]+b[p>>8&15]+b[p>>20&15]+b[p>>16&15]+b[p>>28&15]+b[p>>24&15],256<=f&&(h+=b[q>>4&15]+b[q&15]+b[q>>12&15]+b[q>>8&15]+b[q>>20&15]+b[q>>16&15]+b[q>>28&15]+b[q>>24&15]),384<=f&&(h+=b[r>>4&15]+b[r&15]+b[r>>12&15]+b[r>>8&15]+b[r>>20&15]+b[r>>16&15]+b[r>>28&15]+b[r>>24&15]+b[t>>4&15]+b[t&15]+b[t>>12&15]+b[t>>8&15]+b[t>>20&15]+b[t>>16&15]+b[t>> +28&15]+b[t>>24&15]+b[u>>4&15]+b[u&15]+b[u>>12&15]+b[u>>8&15]+b[u>>20&15]+b[u>>16&15]+b[u>>28&15]+b[u>>24&15]+b[v>>4&15]+b[v&15]+b[v>>12&15]+b[v>>8&15]+b[v>>20&15]+b[v>>16&15]+b[v>>28&15]+b[v>>24&15]),512==f&&(h+=b[w>>4&15]+b[w&15]+b[w>>12&15]+b[w>>8&15]+b[w>>20&15]+b[w>>16&15]+b[w>>28&15]+b[w>>24&15]+b[x>>4&15]+b[x&15]+b[x>>12&15]+b[x>>8&15]+b[x>>20&15]+b[x>>16&15]+b[x>>28&15]+b[x>>24&15]+b[y>>4&15]+b[y&15]+b[y>>12&15]+b[y>>8&15]+b[y>>20&15]+b[y>>16&15]+b[y>>28&15]+b[y>>24&15]+b[z>>4&15]+b[z&15]+ +b[z>>12&15]+b[z>>8&15]+b[z>>20&15]+b[z>>16&15]+b[z>>28&15]+b[z>>24&15]);else for(c=0,g=f/32;c>4&15]+b[d&15]+b[d>>12&15]+b[d>>8&15]+b[d>>20&15]+b[d>>16&15]+b[d>>28&15]+b[d>>24&15];return h};!f.JS_SHA3_TEST&&D?module.exports={sha3_512:za,sha3_384:ya,sha3_256:xa,sha3_224:wa,keccak_512:A,keccak_384:va,keccak_256:ua,keccak_224:G}:f&&(f.sha3_512=za,f.sha3_384=ya,f.sha3_256=xa,f.sha3_224=wa,f.keccak_512=A,f.keccak_384=va,f.keccak_256=ua,f.keccak_224=G)})(this); diff --git a/package.json b/package.json index 160cf5d..4c939d8 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "js-sha3", - "version": "0.2.0", + "version": "0.3.0", "description": "A simple SHA-3 / Keccak hash function for JavaScript supports UTF-8 encoding.", "main": "src/sha3.js", "devDependencies": { diff --git a/src/sha3.js b/src/sha3.js index 97e8914..4ac1dc9 100644 --- a/src/sha3.js +++ b/src/sha3.js @@ -1,5 +1,5 @@ /* - * js-sha3 v0.2.0 + * js-sha3 v0.3.0 * https://github.com/emn178/js-sha3 * * Copyright 2015, emn178@gmail.com @@ -59,6 +59,11 @@ }; var keccak = function(message, bits, padding) { + var notString = typeof(message) != 'string'; + if(notString && message.constructor == ArrayBuffer) { + message = new Uint8Array(message); + } + if(bits === undefined) { bits = 512; padding = KECCAK_PADDING; @@ -82,23 +87,29 @@ for(i = 1;i < blockCount + 1;++i) { blocks[i] = 0; } - for (i = start;index < length && i < byteCount; ++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]; + if(notString) { + for (i = start;index < length && i < 64; ++index) { + blocks[i >> 2] |= message[index] << SHIFT[i++ & 3]; + } + } else { + for (i = start;index < length && i < byteCount; ++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]; + } } } start = i - byteCount; diff --git a/tests/test-keccak.js b/tests/test-keccak.js index 018432a..16e79a6 100644 --- a/tests/test-keccak.js +++ b/tests/test-keccak.js @@ -1,67 +1,94 @@ (function(keccak_512, keccak_384, keccak_256, keccak_224) { describe('keccak_512', function() { - describe('ascii', function() { - describe('less than 128 bytes', function() { - it('should be successful', function() { + context('when ascii', function() { + context('and less than 128 bytes', function() { + it('should be equal', function() { expect(keccak_512('')).to.be('0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e'); expect(keccak_512('The quick brown fox jumps over the lazy dog')).to.be('d135bb84d0439dbac432247ee573a23ea7d3c9deb2a968eb31d47c4fb45f1ef4422d6c531b5b9bd6f449ebcc449ea94d0a8f05f62130fda612da53c79659f609'); expect(keccak_512('The quick brown fox jumps over the lazy dog.')).to.be('ab7192d2b11f51c7dd744e7b3441febf397ca07bf812cceae122ca4ded6387889064f8db9230f173f6d1ab6e24b6e50f065b039f799f5592360a6558eb52d760'); }); }); - describe('more than 128 bytes', function() { - it('should be successful', function() { + context('and more than 128 bytes', function() { + it('should be equal', function() { expect(keccak_512('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('10dcbf6389980ce3594547939bbc685363d28adbd6a05bc4abd7fc62e7693a1f6e33569fed5a380bfecb56ae811d25939b95823f39bb0f16a08740629d066d43'); }); }); }); - describe('UTF8', function() { - describe('less than 128 bytes', function() { - it('should be successful', function() { + context('when UTF8', function() { + context('and less than 128 bytes', function() { + it('should be equal', function() { expect(keccak_512('中文')).to.be('2f6a1bd50562230229af34b0ccf46b8754b89d23ae2c5bf7840b4acfcef86f87395edc0a00b2bfef53bafebe3b79de2e3e01cbd8169ddbb08bde888dcc893524'); expect(keccak_512('aécio')).to.be('c452ec93e83d4795fcab62a76eed0d88f2231a995ce108ac8f130246f87c4a11cb18a2c1a688a5695906a6f863e71bbe8997c6610319ab97f12d2e5bf0afe458'); expect(keccak_512('𠜎')).to.be('8a2d72022ce19d989dbe6a0017faccbf5dc2e22c162d1c5eb168864d32dd1a71e1b4782652c148cf6ca47b77a72c96fff682e72bdfef0566d4b7cca3c9ccc59d'); }); }); - describe('more than 128 bytes', function() { - it('should be successful', function() { + context('and more than 128 bytes', function() { + it('should be equal', function() { expect(keccak_512('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一')).to.be('6a67c28aa1946ca1be8382b861aac4aaf20052f495db9b6902d13adfa603eaba5d169f8896b86d461b2949283eb98e503c3f0640188ea7d6731526fc06568d37'); expect(keccak_512('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('d04ff5b0e85e9968be2a4d4e133c15c7ccee7497198bb651599a97d11d00bca6048d329ab75aa454566cd532648fa1cb4551985d9d645de9fa43a311a9ee8e4d'); }); }); }); + + context('when special length', function() { + it('should be equal', function() { + expect(keccak_512('012345678901234567890123456789012345678901234567890123456789012345678901')).to.be('90b1d032c3bf06dcc78a46fe52054bab1250600224bfc6dfbfb40a7877c55e89bb982799a2edf198568a4166f6736678b45e76b12fac813cfdf0a76714e5eae8'); + expect(keccak_512('01234567890123456789012345678901234567890123456789012345678901234567890')).to.be('3173e7abc754a0b2909410d78986428a9183e996864af02f421d273d9fa1b4e4a5b14e2998b20767712f53a01ff8f6ae2c3e71e51e2c0f24257b03e6da09eb77'); + }); + }); + + context('when Array', function() { + it('should be equal', function() { + expect(keccak_512([])).to.be('0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e'); + expect(keccak_512([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])).to.be('d135bb84d0439dbac432247ee573a23ea7d3c9deb2a968eb31d47c4fb45f1ef4422d6c531b5b9bd6f449ebcc449ea94d0a8f05f62130fda612da53c79659f609'); + }); + }); + + context('when Uint8Array', function() { + it('should be equal', function() { + expect(keccak_512(new Uint8Array([]))).to.be('0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e'); + expect(keccak_512(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]))).to.be('d135bb84d0439dbac432247ee573a23ea7d3c9deb2a968eb31d47c4fb45f1ef4422d6c531b5b9bd6f449ebcc449ea94d0a8f05f62130fda612da53c79659f609'); + }); + }); + + context('when ArrayBuffer', function() { + it('should be equal', function() { + expect(keccak_512(new ArrayBuffer(0))).to.be('0eab42de4c3ceb9235fc91acffe746b29c29a8c366b7c60e4e67c466f36a4304c00fa9caf9d87976ba469bcbe06713b435f091ef2769fb160cdab33d3670680e'); + }); + }); }); describe('keccak_384', function() { - describe('ascii', function() { - describe('less than 128 bytes', function() { - it('should be successful', function() { + context('when ascii', function() { + context('and less than 128 bytes', function() { + it('should be equal', function() { expect(keccak_384('')).to.be('2c23146a63a29acf99e73b88f8c24eaa7dc60aa771780ccc006afbfa8fe2479b2dd2b21362337441ac12b515911957ff'); expect(keccak_384('The quick brown fox jumps over the lazy dog')).to.be('283990fa9d5fb731d786c5bbee94ea4db4910f18c62c03d173fc0a5e494422e8a0b3da7574dae7fa0baf005e504063b3'); expect(keccak_384('The quick brown fox jumps over the lazy dog.')).to.be('9ad8e17325408eddb6edee6147f13856ad819bb7532668b605a24a2d958f88bd5c169e56dc4b2f89ffd325f6006d820b'); }); }); - describe('more than 128 bytes', function() { - it('should be successful', function() { + context('and more than 128 bytes', function() { + it('should be equal', function() { expect(keccak_384('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('e7ec8976b4d96e43f50ae8ecdcf2d97a56236e6406e8dd00efd0d9abe885659db58a2f4b138a4ecfb1bd0052f6569516'); }); }); }); - describe('UTF8', function() { - describe('less than 128 bytes', function() { - it('should be successful', function() { + context('when UTF8', function() { + context('and less than 128 bytes', function() { + it('should be equal', function() { expect(keccak_384('中文')).to.be('743f64bb7544c6ed923be4741b738dde18b7cee384a3a09c4e01acaaac9f19222cdee137702bd3aa05dc198373d87d6c'); expect(keccak_384('aécio')).to.be('08990555e131af8597687614309da4c5053ce866f348544da0a0c2c78c2cc79680ebb57cfbe238286e78ea133a037897'); expect(keccak_384('𠜎')).to.be('2a80f59abf3111f38a35a3daa25123b495f90e9736bd300e35911d19abdd8806498c581333f198ccbbf2252b57c2925d'); }); }); - describe('more than 128 bytes', function() { - it('should be successful', function() { + context('and more than 128 bytes', function() { + it('should be equal', function() { expect(keccak_384('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一')).to.be('a3b043a2f69e4326a05d478fa4c8aa2bd7612453d775af37665a0b96ef2207cdc74c50cdba1629796a5136fe77300b05'); expect(keccak_384('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('66414c090cc3fe9c396d313cbaa100aefd335e851838b29382568b7f57357ada7c54b8fa8c17f859945bba88b2c2e332'); }); @@ -70,33 +97,33 @@ }); describe('keccak_256', function() { - describe('ascii', function() { - describe('less than 128 bytes', function() { - it('should be successful', function() { + context('when ascii', function() { + context('and less than 128 bytes', function() { + it('should be equal', function() { expect(keccak_256('')).to.be('c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470'); expect(keccak_256('The quick brown fox jumps over the lazy dog')).to.be('4d741b6f1eb29cb2a9b9911c82f56fa8d73b04959d3d9d222895df6c0b28aa15'); expect(keccak_256('The quick brown fox jumps over the lazy dog.')).to.be('578951e24efd62a3d63a86f7cd19aaa53c898fe287d2552133220370240b572d'); }); }); - describe('more than 128 bytes', function() { - it('should be successful', function() { + context('and more than 128 bytes', function() { + it('should be equal', function() { expect(keccak_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('af20018353ffb50d507f1555580f5272eca7fdab4f8295db4b1a9ad832c93f6d'); }); }); }); - describe('UTF8', function() { - describe('less than 128 bytes', function() { - it('should be successful', function() { + context('when UTF8', function() { + context('and less than 128 bytes', function() { + it('should be equal', function() { expect(keccak_256('中文')).to.be('70a2b6579047f0a977fcb5e9120a4e07067bea9abb6916fbc2d13ffb9a4e4eee'); expect(keccak_256('aécio')).to.be('d7d569202f04daf90432810d6163112b2695d7820da979327ebd894efb0276dc'); expect(keccak_256('𠜎')).to.be('16a7cc7a58444cbf7e939611910ddc82e7cba65a99d3e8e08cfcda53180a2180'); }); }); - describe('more than 128 bytes', function() { - it('should be successful', function() { + context('and more than 128 bytes', function() { + it('should be equal', function() { expect(keccak_256('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一')).to.be('d1021d2d4c5c7e88098c40f422af68493b4b64c913cbd68220bf5e6127c37a88'); expect(keccak_256('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('ffabf9bba2127c4928d360c9905cb4911f0ec21b9c3b89f3b242bccc68389e36'); }); @@ -105,44 +132,37 @@ }); describe('keccak_224', function() { - describe('ascii', function() { - describe('less than 128 bytes', function() { - it('should be successful', function() { + context('when ascii', function() { + context('and less than 128 bytes', function() { + it('should be equal', function() { expect(keccak_224('')).to.be('f71837502ba8e10837bdd8d365adb85591895602fc552b48b7390abd'); expect(keccak_224('The quick brown fox jumps over the lazy dog')).to.be('310aee6b30c47350576ac2873fa89fd190cdc488442f3ef654cf23fe'); expect(keccak_224('The quick brown fox jumps over the lazy dog.')).to.be('c59d4eaeac728671c635ff645014e2afa935bebffdb5fbd207ffdeab'); }); }); - describe('more than 128 bytes', function() { - it('should be successful', function() { + context('and more than 128 bytes', function() { + it('should be equal', function() { expect(keccak_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('8dd58b706e3a08ec4f1f202af39295b38c355a39b23308ade7218a21'); }); }); }); - describe('UTF8', function() { - describe('less than 128 bytes', function() { - it('should be successful', function() { + context('when UTF8', function() { + context('and less than 128 bytes', function() { + it('should be equal', function() { expect(keccak_224('中文')).to.be('7bc2a0b6e7e0a055a61e4f731e2944b560f41ff98967dcbf4bbf77a5'); expect(keccak_224('aécio')).to.be('66f3db76bf8cb35726cb278bac412d187c3484ab2083dc50ef5ffb55'); expect(keccak_224('𠜎')).to.be('3bfa94845726f4cd5cf17d19b5eacac17b3694790e13a76d5c81c7c2'); }); }); - describe('more than 128 bytes', function() { - it('should be successful', function() { + context('and more than 128 bytes', function() { + it('should be equal', function() { expect(keccak_224('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一')).to.be('d59eef8f394ef7d96967bb0bde578785c033f7f0a21913d6ba41ed1b'); expect(keccak_224('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('27123a2a3860d1041d4769778c4b078732bf4300f7e1c56536ab2644'); }); }); }); }); - - describe('special length', function() { - it('should be successful', function() { - expect(keccak_512('012345678901234567890123456789012345678901234567890123456789012345678901')).to.be('90b1d032c3bf06dcc78a46fe52054bab1250600224bfc6dfbfb40a7877c55e89bb982799a2edf198568a4166f6736678b45e76b12fac813cfdf0a76714e5eae8'); - expect(keccak_512('01234567890123456789012345678901234567890123456789012345678901234567890')).to.be('3173e7abc754a0b2909410d78986428a9183e996864af02f421d273d9fa1b4e4a5b14e2998b20767712f53a01ff8f6ae2c3e71e51e2c0f24257b03e6da09eb77'); - }); - }); })(keccak_512, keccak_384, keccak_256, keccak_224); diff --git a/tests/test.js b/tests/test.js index b48b0e7..59e059a 100644 --- a/tests/test.js +++ b/tests/test.js @@ -1,67 +1,94 @@ (function(sha3_512, sha3_384, sha3_256, sha3_224) { describe('sha3_512', function() { - describe('ascii', function() { - describe('less than 128 bytes', function() { - it('should be successful', function() { + context('when ascii', function() { + context('and less than 128 bytes', function() { + it('should be equal', function() { expect(sha3_512('')).to.be('a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26'); expect(sha3_512('The quick brown fox jumps over the lazy dog')).to.be('01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450'); expect(sha3_512('The quick brown fox jumps over the lazy dog.')).to.be('18f4f4bd419603f95538837003d9d254c26c23765565162247483f65c50303597bc9ce4d289f21d1c2f1f458828e33dc442100331b35e7eb031b5d38ba6460f8'); }); }); - describe('more than 128 bytes', function() { - it('should be successful', function() { + context('and more than 128 bytes', function() { + it('should be equal', function() { expect(sha3_512('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('4f8bcf3a60d3ee56a0bd405c3e6bb37dac44b6781c41bf76c91a5d8e621d472b7b13b8806d88914af3d97585df996363ebe17566d5dfeb6f4884a7949ba8263d'); }); }); }); - describe('UTF8', function() { - describe('less than 128 bytes', function() { - it('should be successful', function() { + context('when UTF8', function() { + context('and less than 128 bytes', function() { + it('should be equal', function() { expect(sha3_512('中文')).to.be('059bbe2efc50cc30e4d8ec5a96be697e2108fcbf9193e1296192eddabc13b143c0120d059399a13d0d42651efe23a6c1ce2d1efb576c5b207fa2516050505af7'); expect(sha3_512('aécio')).to.be('35dfaf82d2ce4be79393dc90e327b4dd15b1c150d8a30f59d8d1b42ca4fc3c87f50b77da36acccf9dc76494d07fc57cfcc9470e627c38f95bce4deab311b87e0'); expect(sha3_512('𠜎')).to.be('33ef254289f36527c93cd203ef1973aec1eff7475c23fa842c3092b0d30965d13b0805c61d0aa92c51245c56bfe26978c35c00f26eb558a043982043ee8b178c'); }); }); - describe('more than 128 bytes', function() { - it('should be successful', function() { + context('and more than 128 bytes', function() { + it('should be equal', function() { expect(sha3_512('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一')).to.be('accb127bb24b0ffbb7550dc637222d2f78538a8a186c98bc5efdad685b9b396639f34148bf0b94ed470f0e9c3665dc3b4c1cb321bacd32dd317a646295e073d9'); expect(sha3_512('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('5b70eaad083f1b86fd535b6812e02f5f2876a4bd8b43aede8d62ae71bb1743ebd919dc41be56d73ba45b67b2876ff215d0575788560e7b0c92b879f8a2fc3111'); }); }); }); + + context('when special length', function() { + it('should be equal', function() { + expect(sha3_512('012345678901234567890123456789012345678901234567890123456789012345678901')).to.be('bce9da5b408846edd5bec9f26c2dee9bd835215c3f2b3876197067d87bc4d1af0cd97f94fda59761a0d804fe82383be2c6c4886fbb82e005fcf899449029f221'); + expect(sha3_512('01234567890123456789012345678901234567890123456789012345678901234567890')).to.be('8bdcb85e6b52c29fafac0d3daf65492f2e3499e066da1a095a65eb1144849a26b2790a8b39c2a7fb747456f749391d953841a61cb13289f9806f04981c180a86'); + }); + }); + + context('when Array', function() { + it('should be equal', function() { + expect(sha3_512([])).to.be('a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26'); + expect(sha3_512([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])).to.be('01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450'); + }); + }); + + context('when Uint8Array', function() { + it('should be equal', function() { + expect(sha3_512(new Uint8Array([]))).to.be('a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26'); + expect(sha3_512(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]))).to.be('01dedd5de4ef14642445ba5f5b97c15e47b9ad931326e4b0727cd94cefc44fff23f07bf543139939b49128caf436dc1bdee54fcb24023a08d9403f9b4bf0d450'); + }); + }); + + context('when ArrayBuffer', function() { + it('should be equal', function() { + expect(sha3_512(new ArrayBuffer(0))).to.be('a69f73cca23a9ac5c8b567dc185a756e97c982164fe25859e0d1dcc1475c80a615b2123af1f5f94c11e3e9402c3ac558f500199d95b6d3e301758586281dcd26'); + }); + }); }); describe('sha3_384', function() { - describe('ascii', function() { - describe('less than 128 bytes', function() { - it('should be successful', function() { + context('when ascii', function() { + context('and less than 128 bytes', function() { + it('should be equal', function() { expect(sha3_384('')).to.be('0c63a75b845e4f7d01107d852e4c2485c51a50aaaa94fc61995e71bbee983a2ac3713831264adb47fb6bd1e058d5f004'); expect(sha3_384('The quick brown fox jumps over the lazy dog')).to.be('7063465e08a93bce31cd89d2e3ca8f602498696e253592ed26f07bf7e703cf328581e1471a7ba7ab119b1a9ebdf8be41'); expect(sha3_384('The quick brown fox jumps over the lazy dog.')).to.be('1a34d81695b622df178bc74df7124fe12fac0f64ba5250b78b99c1273d4b080168e10652894ecad5f1f4d5b965437fb9'); }); }); - describe('more than 128 bytes', function() { - it('should be successful', function() { + context('and more than 128 bytes', function() { + it('should be equal', function() { expect(sha3_384('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('ca6b121a6060bc85de05e5a8d70577838fad2481b092c8263d6f7bcbe5148740f0c7f9c4dc27061339570496956aaef6'); }); }); }); - describe('UTF8', function() { - describe('less than 128 bytes', function() { - it('should be successful', function() { + context('when UTF8', function() { + context('and less than 128 bytes', function() { + it('should be equal', function() { expect(sha3_384('中文')).to.be('9fb5b99e3c546f2738dcd50a14e9aef9c313800c1bf8cf76bc9b2c3a23307841364c5a2d0794702662c5796fb72f5432'); expect(sha3_384('aécio')).to.be('70b447f1bd5ce5a4753ccf7a3697eca0315954774374bc1042aff19582ccc32d5067f7da6c2bea9d6d344e11924cbe72'); expect(sha3_384('𠜎')).to.be('7add8d544b0a7cf188b54b1697a046f77e49d5f292e7ffe56feeed90a500b0bf026b9b68892888a1bafb9f8cb89ed874'); }); }); - describe('more than 128 bytes', function() { - it('should be successful', function() { + context('and more than 128 bytes', function() { + it('should be equal', function() { expect(sha3_384('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一')).to.be('7d0f80fe5c79a04a2a37a30a440e0cc068eb78fe6c3182246ede29645c144b5d33c44607cb2c3111ba77ffc66107f1cd'); expect(sha3_384('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('e344b95c6a961a27793eff00fa5103ef78b4180fe41c93fc60a31aff49b3b5e95a92c84fda9a6c80fa403b7df58db59f'); }); @@ -70,33 +97,33 @@ }); describe('sha3_256', function() { - describe('ascii', function() { - describe('less than 128 bytes', function() { - it('should be successful', function() { + context('when ascii', function() { + context('and less than 128 bytes', function() { + it('should be equal', function() { expect(sha3_256('')).to.be('a7ffc6f8bf1ed76651c14756a061d662f580ff4de43b49fa82d80a4b80f8434a'); expect(sha3_256('The quick brown fox jumps over the lazy dog')).to.be('69070dda01975c8c120c3aada1b282394e7f032fa9cf32f4cb2259a0897dfc04'); expect(sha3_256('The quick brown fox jumps over the lazy dog.')).to.be('a80f839cd4f83f6c3dafc87feae470045e4eb0d366397d5c6ce34ba1739f734d'); }); }); - describe('more than 128 bytes', function() { - it('should be successful', function() { + context('and more than 128 bytes', function() { + it('should be equal', function() { expect(sha3_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('fa198893674a0bf9fb35980504e8cefb250aabd2311a37e5d2205f07fb023d36'); }); }); }); - describe('UTF8', function() { - describe('less than 128 bytes', function() { - it('should be successful', function() { + context('when UTF8', function() { + context('and less than 128 bytes', function() { + it('should be equal', function() { expect(sha3_256('中文')).to.be('ac5305da3d18be1aed44aa7c70ea548da243a59a5fd546f489348fd5718fb1a0'); expect(sha3_256('aécio')).to.be('65c756408eb6c35a1ffa2d7e09711bdc9f0b28716b1376223844a2b4c52b6718'); expect(sha3_256('𠜎')).to.be('babe9afc555b0311700dfb0b5b6296d49347b3d770480baedfcdc47a4aea6e82'); }); }); - describe('more than 128 bytes', function() { - it('should be successful', function() { + context('and more than 128 bytes', function() { + it('should be equal', function() { expect(sha3_256('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一')).to.be('4b2f36e4320b86e6ead0ad001e47e6d9e7fcf0044cd5a5fd65490a633c0372a4'); expect(sha3_256('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('558a7f843b1ac5e7a8bbef90357876bcce0612992d0dfa2907e95521612f507f'); }); @@ -105,44 +132,37 @@ }); describe('sha3_224', function() { - describe('ascii', function() { - describe('less than 128 bytes', function() { - it('should be successful', function() { + context('when ascii', function() { + context('and less than 128 bytes', function() { + it('should be equal', function() { expect(sha3_224('')).to.be('6b4e03423667dbb73b6e15454f0eb1abd4597f9a1b078e3f5b5a6bc7'); expect(sha3_224('The quick brown fox jumps over the lazy dog')).to.be('d15dadceaa4d5d7bb3b48f446421d542e08ad8887305e28d58335795'); expect(sha3_224('The quick brown fox jumps over the lazy dog.')).to.be('2d0708903833afabdd232a20201176e8b58c5be8a6fe74265ac54db0'); }); }); - describe('more than 128 bytes', function() { - it('should be successful', function() { + context('and more than 128 bytes', function() { + it('should be equal', function() { expect(sha3_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('06885009a28e43e15bf1af718561ad211515a27b542eabc36764a0ca'); }); }); }); - describe('UTF8', function() { - describe('less than 128 bytes', function() { - it('should be successful', function() { + context('when UTF8', function() { + context('and less than 128 bytes', function() { + it('should be equal', function() { expect(sha3_224('中文')).to.be('106d169e10b61c2a2a05554d3e631ec94467f8316640f29545d163ee'); expect(sha3_224('aécio')).to.be('b16bad54608dc01864a5d7510d4c19b09f3a0f39cfc4ba1e53aa952a'); expect(sha3_224('𠜎')).to.be('f59253c41cb87e5cd953311656716cb5b64dbafc9e8155f0dd68123c'); }); }); - describe('more than 128 bytes', function() { - it('should be successful', function() { + context('and more than 128 bytes', function() { + it('should be equal', function() { expect(sha3_224('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一')).to.be('135c13deb71fdf6fb77b52b720c43ddd6ce7467f9147a74248557114'); expect(sha3_224('訊息摘要演算法第五版(英語:Message-Digest Algorithm 5,縮寫為MD5),是當前電腦領域用於確保資訊傳輸完整一致而廣泛使用的雜湊演算法之一(又譯雜湊演算法、摘要演算法等),主流程式語言普遍已有MD5的實作。')).to.be('bd05581e02445c53e05aad2014f6a3819d77a9dff918b8c6bf60bd06'); }); }); }); }); - - describe('special length', function() { - it('should be successful', function() { - expect(sha3_512('012345678901234567890123456789012345678901234567890123456789012345678901')).to.be('bce9da5b408846edd5bec9f26c2dee9bd835215c3f2b3876197067d87bc4d1af0cd97f94fda59761a0d804fe82383be2c6c4886fbb82e005fcf899449029f221'); - expect(sha3_512('01234567890123456789012345678901234567890123456789012345678901234567890')).to.be('8bdcb85e6b52c29fafac0d3daf65492f2e3499e066da1a095a65eb1144849a26b2790a8b39c2a7fb747456f749391d953841a61cb13289f9806f04981c180a86'); - }); - }); })(sha3_512, sha3_384, sha3_256, sha3_224);