From 06a3fa8f52563acae2716c68dbc9937c12a4229d Mon Sep 17 00:00:00 2001 From: Yi-Cyuan Date: Mon, 6 Jun 2016 11:23:18 +0800 Subject: [PATCH] * Fixed shake output incorrect in the special length. --- .travis.yml | 2 + CHANGELOG.md | 4 + LICENSE.txt | 4 +- README.md | 2 +- bower.json | 2 +- build/sha3.min.js | 41 +++-- package.json | 5 +- src/sha3.js | 129 +++++++------- tests/index.html | 1 - tests/node-test.js | 33 ++-- tests/test-keccak.js | 180 ------------------- tests/test-shake.js | 98 +++++----- tests/test.js | 414 +++++++++++++++++++++++++++---------------- 13 files changed, 427 insertions(+), 488 deletions(-) delete mode 100644 tests/test-keccak.js diff --git a/.travis.yml b/.travis.yml index 1299958..6d44cb9 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,5 +1,7 @@ language: node_js node_js: + - "4.1" + - "4.0" - "0.12" - "0.11" - "0.10" diff --git a/CHANGELOG.md b/CHANGELOG.md index 820eaa8..a039f16 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,7 @@ +# v0.5.2 / 2016-06-06 + +* Fixed shake output incorrect in the special length. + # v0.5.1 / 2015-10-27 * Update package.json and bower.json. diff --git a/LICENSE.txt b/LICENSE.txt index aeb45cf..5b6aafa 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,6 +1,4 @@ -Copyright (c) 2015 Chen Yi-Cyuan - -MIT License +Copyright 2015-2016 Chen, Yi-Cyuan 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 c4b4e77..26afe51 100644 --- a/README.md +++ b/README.md @@ -281,4 +281,4 @@ The project is released under the [MIT license](http://www.opensource.org/licens ## Contact The project's website is located at https://github.com/emn178/js-sha3 -Author: emn178@gmail.com +Author: Chen, Yi-Cyuan (emn178@gmail.com) diff --git a/bower.json b/bower.json index 9f71844..03d9db3 100644 --- a/bower.json +++ b/bower.json @@ -1,6 +1,6 @@ { "name": "js-sha3", - "version": "0.5.1", + "version": "0.5.2", "main": ["src/sha3.js"], "ignore": [ "samples", diff --git a/build/sha3.min.js b/build/sha3.min.js index f74f7cf..d2b7a6d 100644 --- a/build/sha3.min.js +++ b/build/sha3.min.js @@ -1,23 +1,22 @@ -/* - * js-sha3 v0.5.1 - * https://github.com/emn178/js-sha3 +/** + * [js-sha3]{@link https://github.com/emn178/js-sha3} * - * Copyright 2015, emn178@gmail.com - * - * Licensed under the MIT license: - * http://www.opensource.org/licenses/MIT + * @version 0.5.2 + * @author Chen, Yi-Cyuan [emn178@gmail.com] + * @copyright Chen, Yi-Cyuan 2015-2016 + * @license MIT */ -(function(r,ma){function n(a,b,c){this.blocks=[];this.s=[];this.padding=b;this.outputBits=c;this.reset=!0;this.start=this.block=0;this.blockCount=1600-(a<<1)>>5;this.byteCount=this.blockCount<<2;this.outputBlocks=c>>5;this.extraBytes=(c&31)>>3;for(a=0;50>a;++a)this.s[a]=0}var ga="undefined"!=typeof module;ga&&(r=global,r.JS_SHA3_TEST&&(r.navigator={userAgent:"Chrome"}));for(var l="0123456789abcdef".split(""),p=[0,8,16,24],ha=[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],t=[224,256,384,512],w=["hex","buffer","array"],ia=function(a,b,c){return function(e){return(new n(a,b,a)).update(e)[c]()}},ja=function(a,b,c){return function(e,g){return(new n(a,b,g)).update(e)[c]()}},q=function(a,b){var c=ia(a, -b,"hex");c.create=function(){return new n(a,b,a)};c.update=function(a){return c.create().update(a)};for(var e=0;e>2]|=a[h]<m?e[d>>2]|=m<m?e[d>>2]|=(192|m>>6)<m||57344<=m?e[d>>2]|=(224|m>>12)<>2]|=(240|m>>18)<>2]|=(128|m>>12&63)<>2]|=(128|m>>6&63)<>2]|=(128|m&63)<=g){this.start=d-g;this.block=e[k];for(d=0;d>2]|=this.padding[b&3];if(this.lastByteIndex==this.byteCount)for(a[0]=a[c],b=1;b>4&15]+l[f&15]+l[f>>12&15]+l[f>>8&15]+l[f>> -20&15]+l[f>>16&15]+l[f>>28&15]+l[f>>24&15];0==k%a&&v(b)}e&&(f=b[g],0>4&15]+l[f&15]),1>12&15]+l[f>>8&15]),2>20&15]+l[f>>16&15]));return h};n.prototype.buffer=function(){this.finalize();var a=this.blockCount,b=this.s,c=this.outputBlocks,e=this.extraBytes,g=0,k=0,h=this.outputBits>>3,f;f=e?new ArrayBuffer(c+1<<2):new ArrayBuffer(h);for(var d=new Uint32Array(f);k>8&255,h[f+2]=d>>16&255,h[f+3]=d>>24&255;0==k%a&&v(b)}e&&(f=k<<2,d=b[g],0>8&255),2>16&255));return h};var v=function(a){var b,c,e,g,k,h,f,d,m,l,n,p,q,r,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,aa,ba,ca,da;for(e=0;48>e;e+=2)g=a[0]^ -a[10]^a[20]^a[30]^a[40],k=a[1]^a[11]^a[21]^a[31]^a[41],h=a[2]^a[12]^a[22]^a[32]^a[42],f=a[3]^a[13]^a[23]^a[33]^a[43],d=a[4]^a[14]^a[24]^a[34]^a[44],m=a[5]^a[15]^a[25]^a[35]^a[45],l=a[6]^a[16]^a[26]^a[36]^a[46],n=a[7]^a[17]^a[27]^a[37]^a[47],p=a[8]^a[18]^a[28]^a[38]^a[48],q=a[9]^a[19]^a[29]^a[39]^a[49],b=p^(h<<1|f>>>31),c=q^(f<<1|h>>>31),a[0]^=b,a[1]^=c,a[10]^=b,a[11]^=c,a[20]^=b,a[21]^=c,a[30]^=b,a[31]^=c,a[40]^=b,a[41]^=c,b=g^(d<<1|m>>>31),c=k^(m<<1|d>>>31),a[2]^=b,a[3]^=c,a[12]^=b,a[13]^=c,a[22]^= -b,a[23]^=c,a[32]^=b,a[33]^=c,a[42]^=b,a[43]^=c,b=h^(l<<1|n>>>31),c=f^(n<<1|l>>>31),a[4]^=b,a[5]^=c,a[14]^=b,a[15]^=c,a[24]^=b,a[25]^=c,a[34]^=b,a[35]^=c,a[44]^=b,a[45]^=c,b=d^(p<<1|q>>>31),c=m^(q<<1|p>>>31),a[6]^=b,a[7]^=c,a[16]^=b,a[17]^=c,a[26]^=b,a[27]^=c,a[36]^=b,a[37]^=c,a[46]^=b,a[47]^=c,b=l^(g<<1|k>>>31),c=n^(k<<1|g>>>31),a[8]^=b,a[9]^=c,a[18]^=b,a[19]^=c,a[28]^=b,a[29]^=c,a[38]^=b,a[39]^=c,a[48]^=b,a[49]^=c,b=a[0],c=a[1],M=a[11]<<4|a[10]>>>28,N=a[10]<<4|a[11]>>>28,u=a[20]<<3|a[21]>>>29,v= -a[21]<<3|a[20]>>>29,aa=a[31]<<9|a[30]>>>23,ba=a[30]<<9|a[31]>>>23,I=a[40]<<18|a[41]>>>14,J=a[41]<<18|a[40]>>>14,A=a[2]<<1|a[3]>>>31,B=a[3]<<1|a[2]>>>31,g=a[13]<<12|a[12]>>>20,k=a[12]<<12|a[13]>>>20,O=a[22]<<10|a[23]>>>22,P=a[23]<<10|a[22]>>>22,w=a[33]<<13|a[32]>>>19,x=a[32]<<13|a[33]>>>19,ca=a[42]<<2|a[43]>>>30,da=a[43]<<2|a[42]>>>30,U=a[5]<<30|a[4]>>>2,V=a[4]<<30|a[5]>>>2,C=a[14]<<6|a[15]>>>26,D=a[15]<<6|a[14]>>>26,h=a[25]<<11|a[24]>>>21,f=a[24]<<11|a[25]>>>21,Q=a[34]<<15|a[35]>>>17,R=a[35]<<15| -a[34]>>>17,y=a[45]<<29|a[44]>>>3,z=a[44]<<29|a[45]>>>3,p=a[6]<<28|a[7]>>>4,q=a[7]<<28|a[6]>>>4,W=a[17]<<23|a[16]>>>9,X=a[16]<<23|a[17]>>>9,E=a[26]<<25|a[27]>>>7,F=a[27]<<25|a[26]>>>7,d=a[36]<<21|a[37]>>>11,m=a[37]<<21|a[36]>>>11,S=a[47]<<24|a[46]>>>8,T=a[46]<<24|a[47]>>>8,K=a[8]<<27|a[9]>>>5,L=a[9]<<27|a[8]>>>5,r=a[18]<<20|a[19]>>>12,t=a[19]<<20|a[18]>>>12,Y=a[29]<<7|a[28]>>>25,Z=a[28]<<7|a[29]>>>25,G=a[38]<<8|a[39]>>>24,H=a[39]<<8|a[38]>>>24,l=a[48]<<14|a[49]>>>18,n=a[49]<<14|a[48]>>>18,a[0]=b^~g& -h,a[1]=c^~k&f,a[10]=p^~r&u,a[11]=q^~t&v,a[20]=A^~C&E,a[21]=B^~D&F,a[30]=K^~M&O,a[31]=L^~N&P,a[40]=U^~W&Y,a[41]=V^~X&Z,a[2]=g^~h&d,a[3]=k^~f&m,a[12]=r^~u&w,a[13]=t^~v&x,a[22]=C^~E&G,a[23]=D^~F&H,a[32]=M^~O&Q,a[33]=N^~P&R,a[42]=W^~Y&aa,a[43]=X^~Z&ba,a[4]=h^~d&l,a[5]=f^~m&n,a[14]=u^~w&y,a[15]=v^~x&z,a[24]=E^~G&I,a[25]=F^~H&J,a[34]=O^~Q&S,a[35]=P^~R&T,a[44]=Y^~aa&ca,a[45]=Z^~ba&da,a[6]=d^~l&b,a[7]=m^~n&c,a[16]=w^~y&p,a[17]=x^~z&q,a[26]=G^~I&A,a[27]=H^~J&B,a[36]=Q^~S&K,a[37]=R^~T&L,a[46]=aa^~ca&U,a[47]= -ba^~da&V,a[8]=l^~b&g,a[9]=n^~c&k,a[18]=y^~p&r,a[19]=z^~q&t,a[28]=I^~A&C,a[29]=J^~B&D,a[38]=S^~K&M,a[39]=T^~L&N,a[48]=ca^~U&W,a[49]=da^~V&X,a[0]^=ha[e],a[1]^=ha[e+1]};if(!r.JS_SHA3_TEST&&ga)module.exports=x;else if(r)for(var ka in x)r[ka]=x[ka]})(this); +(function(r){function n(a,b,c){this.blocks=[];this.s=[];this.padding=b;this.outputBits=c;this.reset=!0;this.start=this.block=0;this.blockCount=1600-(a<<1)>>5;this.byteCount=this.blockCount<<2;this.outputBlocks=c>>5;this.extraBytes=(c&31)>>3;for(a=0;50>a;++a)this.s[a]=0}var ga="undefined"!=typeof module;ga&&(r=global);for(var l="0123456789abcdef".split(""),p=[0,8,16,24],ha=[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],t=[224,256,384,512],w=["hex","buffer","array"],ia=function(a,b,c){return function(e){return(new n(a,b,a)).update(e)[c]()}},ja=function(a,b,c){return function(e,g){return(new n(a,b,g)).update(e)[c]()}},q=function(a,b){var c=ia(a,b,"hex");c.create=function(){return new n(a,b,a)};c.update= +function(a){return c.create().update(a)};for(var e=0;e>2]|=a[h]<m?e[d>>2]|=m<m?e[d>>2]|=(192|m>>6)<m||57344<=m?e[d>>2]|=(224|m>>12)<>2]|=(240|m>>18)<>2]|=(128|m>>12&63)<>2]|=(128|m>>6&63)<>2]|=(128|m&63)<=g){this.start=d-g;this.block=e[k];for(d=0;d>2]|=this.padding[b&3];if(this.lastByteIndex==this.byteCount)for(a[0]=a[c],b=1;b>4&15]+l[f&15]+l[f>>12&15]+l[f>>8&15]+l[f>>20&15]+l[f>>16&15]+l[f>>28&15]+l[f>>24&15];0==k%a&& +(v(b),g=0)}e&&(f=b[g],0>4&15]+l[f&15]),1>12&15]+l[f>>8&15]),2>20&15]+l[f>>16&15]));return h};n.prototype.buffer=function(){this.finalize();var a=this.blockCount,b=this.s,c=this.outputBlocks,e=this.extraBytes,g=0,k=0,h=this.outputBits>>3,f;f=e?new ArrayBuffer(c+1<<2):new ArrayBuffer(h);for(var d=new Uint32Array(f);k>8&255,h[f+2]=d>>16&255,h[f+3]=d>>24&255;0==k%a&&v(b)}e&&(f=k<<2,d=b[g],0>8&255),2>16&255));return h};var v=function(a){var b,c,e,g,k,h,f,d,m,l,n,p,q,r,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,aa,ba,ca,da;for(e=0;48>e;e+=2)g=a[0]^a[10]^a[20]^a[30]^a[40],k=a[1]^a[11]^a[21]^a[31]^ +a[41],h=a[2]^a[12]^a[22]^a[32]^a[42],f=a[3]^a[13]^a[23]^a[33]^a[43],d=a[4]^a[14]^a[24]^a[34]^a[44],m=a[5]^a[15]^a[25]^a[35]^a[45],l=a[6]^a[16]^a[26]^a[36]^a[46],n=a[7]^a[17]^a[27]^a[37]^a[47],p=a[8]^a[18]^a[28]^a[38]^a[48],q=a[9]^a[19]^a[29]^a[39]^a[49],b=p^(h<<1|f>>>31),c=q^(f<<1|h>>>31),a[0]^=b,a[1]^=c,a[10]^=b,a[11]^=c,a[20]^=b,a[21]^=c,a[30]^=b,a[31]^=c,a[40]^=b,a[41]^=c,b=g^(d<<1|m>>>31),c=k^(m<<1|d>>>31),a[2]^=b,a[3]^=c,a[12]^=b,a[13]^=c,a[22]^=b,a[23]^=c,a[32]^=b,a[33]^=c,a[42]^=b,a[43]^=c, +b=h^(l<<1|n>>>31),c=f^(n<<1|l>>>31),a[4]^=b,a[5]^=c,a[14]^=b,a[15]^=c,a[24]^=b,a[25]^=c,a[34]^=b,a[35]^=c,a[44]^=b,a[45]^=c,b=d^(p<<1|q>>>31),c=m^(q<<1|p>>>31),a[6]^=b,a[7]^=c,a[16]^=b,a[17]^=c,a[26]^=b,a[27]^=c,a[36]^=b,a[37]^=c,a[46]^=b,a[47]^=c,b=l^(g<<1|k>>>31),c=n^(k<<1|g>>>31),a[8]^=b,a[9]^=c,a[18]^=b,a[19]^=c,a[28]^=b,a[29]^=c,a[38]^=b,a[39]^=c,a[48]^=b,a[49]^=c,b=a[0],c=a[1],M=a[11]<<4|a[10]>>>28,N=a[10]<<4|a[11]>>>28,u=a[20]<<3|a[21]>>>29,v=a[21]<<3|a[20]>>>29,aa=a[31]<<9|a[30]>>>23,ba=a[30]<< +9|a[31]>>>23,I=a[40]<<18|a[41]>>>14,J=a[41]<<18|a[40]>>>14,A=a[2]<<1|a[3]>>>31,B=a[3]<<1|a[2]>>>31,g=a[13]<<12|a[12]>>>20,k=a[12]<<12|a[13]>>>20,O=a[22]<<10|a[23]>>>22,P=a[23]<<10|a[22]>>>22,w=a[33]<<13|a[32]>>>19,x=a[32]<<13|a[33]>>>19,ca=a[42]<<2|a[43]>>>30,da=a[43]<<2|a[42]>>>30,U=a[5]<<30|a[4]>>>2,V=a[4]<<30|a[5]>>>2,C=a[14]<<6|a[15]>>>26,D=a[15]<<6|a[14]>>>26,h=a[25]<<11|a[24]>>>21,f=a[24]<<11|a[25]>>>21,Q=a[34]<<15|a[35]>>>17,R=a[35]<<15|a[34]>>>17,y=a[45]<<29|a[44]>>>3,z=a[44]<<29|a[45]>>> +3,p=a[6]<<28|a[7]>>>4,q=a[7]<<28|a[6]>>>4,W=a[17]<<23|a[16]>>>9,X=a[16]<<23|a[17]>>>9,E=a[26]<<25|a[27]>>>7,F=a[27]<<25|a[26]>>>7,d=a[36]<<21|a[37]>>>11,m=a[37]<<21|a[36]>>>11,S=a[47]<<24|a[46]>>>8,T=a[46]<<24|a[47]>>>8,K=a[8]<<27|a[9]>>>5,L=a[9]<<27|a[8]>>>5,r=a[18]<<20|a[19]>>>12,t=a[19]<<20|a[18]>>>12,Y=a[29]<<7|a[28]>>>25,Z=a[28]<<7|a[29]>>>25,G=a[38]<<8|a[39]>>>24,H=a[39]<<8|a[38]>>>24,l=a[48]<<14|a[49]>>>18,n=a[49]<<14|a[48]>>>18,a[0]=b^~g&h,a[1]=c^~k&f,a[10]=p^~r&u,a[11]=q^~t&v,a[20]=A^~C& +E,a[21]=B^~D&F,a[30]=K^~M&O,a[31]=L^~N&P,a[40]=U^~W&Y,a[41]=V^~X&Z,a[2]=g^~h&d,a[3]=k^~f&m,a[12]=r^~u&w,a[13]=t^~v&x,a[22]=C^~E&G,a[23]=D^~F&H,a[32]=M^~O&Q,a[33]=N^~P&R,a[42]=W^~Y&aa,a[43]=X^~Z&ba,a[4]=h^~d&l,a[5]=f^~m&n,a[14]=u^~w&y,a[15]=v^~x&z,a[24]=E^~G&I,a[25]=F^~H&J,a[34]=O^~Q&S,a[35]=P^~R&T,a[44]=Y^~aa&ca,a[45]=Z^~ba&da,a[6]=d^~l&b,a[7]=m^~n&c,a[16]=w^~y&p,a[17]=x^~z&q,a[26]=G^~I&A,a[27]=H^~J&B,a[36]=Q^~S&K,a[37]=R^~T&L,a[46]=aa^~ca&U,a[47]=ba^~da&V,a[8]=l^~b&g,a[9]=n^~c&k,a[18]=y^~p&r,a[19]= +z^~q&t,a[28]=I^~A&C,a[29]=J^~B&D,a[38]=S^~K&M,a[39]=T^~L&N,a[48]=ca^~U&W,a[49]=da^~V&X,a[0]^=ha[e],a[1]^=ha[e+1]};if(!r.JS_SHA3_TEST&&ga)module.exports=x;else if(r)for(var ka in x)r[ka]=x[ka]})(this); diff --git a/package.json b/package.json index a2e309a..fa8ce9c 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "js-sha3", - "version": "0.5.1", + "version": "0.5.2", "description": "A simple SHA-3 / Keccak / Shake hash function for JavaScript supports UTF-8 encoding.", "main": "src/sha3.js", "devDependencies": { @@ -19,13 +19,14 @@ "keywords": [ "sha3", "keccak", + "shake", "hash", "encryption", "cryptography", "HMAC" ], "license": "MIT", - "author": "emn178 ", + "author": "Chen, Yi-Cyuan ", "homepage": "https://github.com/emn178/js-sha3", "bugs": { "url": "https://github.com/emn178/js-sha3/issues" diff --git a/src/sha3.js b/src/sha3.js index dcf3d53..daa6f46 100644 --- a/src/sha3.js +++ b/src/sha3.js @@ -1,21 +1,17 @@ -/* - * js-sha3 v0.5.1 - * https://github.com/emn178/js-sha3 +/** + * [js-sha3]{@link https://github.com/emn178/js-sha3} * - * Copyright 2015, emn178@gmail.com - * - * Licensed under the MIT license: - * http://www.opensource.org/licenses/MIT + * @version 0.5.2 + * @author Chen, Yi-Cyuan [emn178@gmail.com] + * @copyright Chen, Yi-Cyuan 2015-2016 + * @license MIT */ -;(function(root, undefined) { +(function (root) { 'use strict'; var NODE_JS = typeof(module) != 'undefined'; - if(NODE_JS) { + if (NODE_JS) { root = global; - if(root.JS_SHA3_TEST) { - root.navigator = { userAgent: 'Chrome'}; - } } var HEX_CHARS = '0123456789abcdef'.split(''); var SHAKE_PADDING = [31, 7936, 2031616, 520093696]; @@ -31,42 +27,42 @@ var SHAKE_BITS = [128, 256]; var OUTPUT_TYPES = ['hex', 'buffer', 'array']; - var createOutputMethod = function(bits, padding, outputType) { - return function(message) { + var createOutputMethod = function (bits, padding, outputType) { + return function (message) { return new Keccak(bits, padding, bits).update(message)[outputType](); } }; - var createShakeOutputMethod = function(bits, padding, outputType) { - return function(message, outputBits) { + var createShakeOutputMethod = function (bits, padding, outputType) { + return function (message, outputBits) { return new Keccak(bits, padding, outputBits).update(message)[outputType](); } }; - var createMethod = function(bits, padding) { + var createMethod = function (bits, padding) { var method = createOutputMethod(bits, padding, 'hex'); - method.create = function() { + method.create = function () { return new Keccak(bits, padding, bits); }; - method.update = function(message) { + method.update = function (message) { return method.create().update(message); }; - for(var i = 0;i < OUTPUT_TYPES.length;++i) { + for (var i = 0;i < OUTPUT_TYPES.length;++i) { var type = OUTPUT_TYPES[i]; method[type] = createOutputMethod(bits, padding, type); } return method; }; - var createShakeMethod = function(bits, padding) { + var createShakeMethod = function (bits, padding) { var method = createShakeOutputMethod(bits, padding, 'hex'); - method.create = function(outputBits) { + method.create = function (outputBits) { return new Keccak(bits, padding, outputBits); }; - method.update = function(message, outputBits) { + method.update = function (message, outputBits) { return method.create(outputBits).update(message); }; - for(var i = 0;i < OUTPUT_TYPES.length;++i) { + for (var i = 0;i < OUTPUT_TYPES.length;++i) { var type = OUTPUT_TYPES[i]; method[type] = createShakeOutputMethod(bits, padding, type); } @@ -81,11 +77,11 @@ var methods = {}; - for(var i = 0;i < algorithms.length;++i) { + for (var i = 0;i < algorithms.length;++i) { var algorithm = algorithms[i]; var bits = algorithm.bits; var createMethod = algorithm.createMethod; - for(var j = 0;j < bits.length;++j) { + for (var j = 0;j < bits.length;++j) { var method = algorithm.createMethod(bits[j], algorithm.padding); methods[algorithm.name +'_' + bits[j]] = method; } @@ -104,28 +100,28 @@ this.outputBlocks = outputBits >> 5; this.extraBytes = (outputBits & 31) >> 3; - for(var i = 0;i < 50;++i) { + for (var i = 0;i < 50;++i) { this.s[i] = 0; } }; - Keccak.prototype.update = function(message) { + Keccak.prototype.update = function (message) { var notString = typeof(message) != 'string'; - if(notString && message.constructor == root.ArrayBuffer) { + if (notString && message.constructor == root.ArrayBuffer) { message = new Uint8Array(message); } var length = message.length, blocks = this.blocks, byteCount = this.byteCount, blockCount = this.blockCount, index = 0, s = this.s, i, code; - while(index < length) { - if(this.reset) { + while (index < length) { + if (this.reset) { this.reset = false; blocks[0] = this.block; - for(i = 1;i < blockCount + 1;++i) { + for (i = 1;i < blockCount + 1;++i) { blocks[i] = 0; } } - if(notString) { + if (notString) { for (i = this.start;index < length && i < byteCount; ++index) { blocks[i >> 2] |= message[index] << SHIFT[i++ & 3]; } @@ -151,10 +147,10 @@ } } this.lastByteIndex = i; - if(i >= byteCount) { + if (i >= byteCount) { this.start = i - byteCount; this.block = blocks[blockCount]; - for(i = 0;i < blockCount;++i) { + for (i = 0;i < blockCount;++i) { s[i] ^= blocks[i]; } f(s); @@ -166,91 +162,92 @@ return this; }; - Keccak.prototype.finalize = function() { + Keccak.prototype.finalize = function () { var blocks = this.blocks, i = this.lastByteIndex, blockCount = this.blockCount, s = this.s; blocks[i >> 2] |= this.padding[i & 3]; - if(this.lastByteIndex == this.byteCount) { + if (this.lastByteIndex == this.byteCount) { blocks[0] = blocks[blockCount]; - for(i = 1;i < blockCount + 1;++i) { + for (i = 1;i < blockCount + 1;++i) { blocks[i] = 0; } } blocks[blockCount - 1] |= 0x80000000; - for(i = 0;i < blockCount;++i) { + for (i = 0;i < blockCount;++i) { s[i] ^= blocks[i]; } f(s); }; - Keccak.prototype.toString = Keccak.prototype.hex = function() { + Keccak.prototype.toString = Keccak.prototype.hex = function () { this.finalize(); var blockCount = this.blockCount, s = this.s, outputBlocks = this.outputBlocks, extraBytes = this.extraBytes, i = 0, j = 0; var hex = '', block; - while(j < outputBlocks) { - for(i = 0;i < blockCount && j < outputBlocks;++i, ++j) { + while (j < outputBlocks) { + for (i = 0;i < blockCount && j < outputBlocks;++i, ++j) { block = s[i]; hex += HEX_CHARS[(block >> 4) & 0x0F] + HEX_CHARS[block & 0x0F] + HEX_CHARS[(block >> 12) & 0x0F] + HEX_CHARS[(block >> 8) & 0x0F] + HEX_CHARS[(block >> 20) & 0x0F] + HEX_CHARS[(block >> 16) & 0x0F] + HEX_CHARS[(block >> 28) & 0x0F] + HEX_CHARS[(block >> 24) & 0x0F]; } - if(j % blockCount == 0) { + if (j % blockCount == 0) { f(s); + i = 0; } } - if(extraBytes) { + if (extraBytes) { block = s[i]; - if(extraBytes > 0) { + if (extraBytes > 0) { hex += HEX_CHARS[(block >> 4) & 0x0F] + HEX_CHARS[block & 0x0F]; } - if(extraBytes > 1) { + if (extraBytes > 1) { hex += HEX_CHARS[(block >> 12) & 0x0F] + HEX_CHARS[(block >> 8) & 0x0F]; } - if(extraBytes > 2) { + if (extraBytes > 2) { hex += HEX_CHARS[(block >> 20) & 0x0F] + HEX_CHARS[(block >> 16) & 0x0F]; } } return hex; }; - Keccak.prototype.buffer = function() { + Keccak.prototype.buffer = function () { this.finalize(); var blockCount = this.blockCount, s = this.s, outputBlocks = this.outputBlocks, extraBytes = this.extraBytes, i = 0, j = 0; var bytes = this.outputBits >> 3; var buffer; - if(extraBytes) { + if (extraBytes) { buffer = new ArrayBuffer((outputBlocks + 1) << 2); } else { buffer = new ArrayBuffer(bytes); } var array = new Uint32Array(buffer); - while(j < outputBlocks) { - for(i = 0;i < blockCount && j < outputBlocks;++i, ++j) { + while (j < outputBlocks) { + for (i = 0;i < blockCount && j < outputBlocks;++i, ++j) { array[j] = s[i]; } - if(j % blockCount == 0) { + if (j % blockCount == 0) { f(s); } } - if(extraBytes) { + if (extraBytes) { array[i] = s[i]; buffer = buffer.slice(0, bytes); } return buffer; }; - Keccak.prototype.digest = Keccak.prototype.array = function() { + Keccak.prototype.digest = Keccak.prototype.array = function () { this.finalize(); var blockCount = this.blockCount, s = this.s, outputBlocks = this.outputBlocks, extraBytes = this.extraBytes, i = 0, j = 0; var array = [], offset, block; - while(j < outputBlocks) { - for(i = 0;i < blockCount && j < outputBlocks;++i, ++j) { + while (j < outputBlocks) { + for (i = 0;i < blockCount && j < outputBlocks;++i, ++j) { offset = j << 2; block = s[i]; array[offset] = block & 0xFF; @@ -258,32 +255,32 @@ array[offset + 2] = (block >> 16) & 0xFF; array[offset + 3] = (block >> 24) & 0xFF; } - if(j % blockCount == 0) { + if (j % blockCount == 0) { f(s); } } - if(extraBytes) { + if (extraBytes) { offset = j << 2; block = s[i]; - if(extraBytes > 0) { + if (extraBytes > 0) { array[offset] = block & 0xFF; } - if(extraBytes > 1) { + if (extraBytes > 1) { array[offset + 1] = (block >> 8) & 0xFF; } - if(extraBytes > 2) { + if (extraBytes > 2) { array[offset + 2] = (block >> 16) & 0xFF; } } return array; }; - var f = function(s) { + var f = function (s) { var h, l, n, c0, c1, c2, c3, c4, c5, c6, c7, c8, c9, b0, b1, b2, b3, b4, b5, b6, b7, b8, b9, b10, b11, b12, b13, b14, b15, b16, b17, b18, b19, b20, b21, b22, b23, b24, b25, b26, b27, b28, b29, b30, b31, b32, b33, b34, b35, b36, b37, b38, b39, b40, b41, b42, b43, b44, b45, b46, b47, b48, b49; - for(n = 0; n < 48; n += 2) { + for (n = 0; n < 48; n += 2) { c0 = s[0] ^ s[10] ^ s[20] ^ s[30] ^ s[40]; c1 = s[1] ^ s[11] ^ s[21] ^ s[31] ^ s[41]; c2 = s[2] ^ s[12] ^ s[22] ^ s[32] ^ s[42]; @@ -463,10 +460,10 @@ } } - if(!root.JS_SHA3_TEST && NODE_JS) { + if (!root.JS_SHA3_TEST && NODE_JS) { module.exports = methods; - } else if(root) { - for(var key in methods) { + } else if (root) { + for (var key in methods) { root[key] = methods[key]; } } diff --git a/tests/index.html b/tests/index.html index 4874a72..7f46290 100644 --- a/tests/index.html +++ b/tests/index.html @@ -14,7 +14,6 @@ mocha.setup('bdd'); -