fix sjcl issue on npm install

...by including sjcl locally
This commit is contained in:
Ryan X. Charles 2014-06-06 11:49:58 -07:00
parent 1e97ab8c6d
commit 338158ca46
7 changed files with 176 additions and 118 deletions

View File

@ -31,7 +31,7 @@ requireWhenAccessed('ECIES', './lib/ECIES');
requireWhenAccessed('log', './util/log');
requireWhenAccessed('networks', './networks');
requireWhenAccessed('SecureRandom', './lib/SecureRandom');
requireWhenAccessed('sjcl', 'sjcl');
requireWhenAccessed('sjcl', './lib/sjcl');
requireWhenAccessed('util', './util/util');
requireWhenAccessed('EncodedData', './util/EncodedData');
requireWhenAccessed('VersionedData', './util/VersionedData');

View File

@ -11,9 +11,6 @@ var puts = function(error, stdout, stderr) {
//sys.puts(stderr);
};
//compile sjcl
exec('cd node_modules/sjcl && ./configure --without-all --with-aes --with-convenience --with-cbc --with-codecHex --with-codecBase64 --with-sha512 --with-hmac && make && cd ../..;', puts);
var pack = function (params) {
var file = require.resolve('soop');
var dir = file.substr(0, file.length - String('soop.js').length);
@ -51,6 +48,7 @@ var modules = [
'lib/ScriptInterpreter',
'lib/SecureRandom',
'lib/Sign',
'lib/sjcl',
'lib/Transaction',
'lib/TransactionBuilder',
'lib/Wallet',

File diff suppressed because one or more lines are too long

2
buildsjcl.sh Executable file
View File

@ -0,0 +1,2 @@
#!/bin/bash
cd node_modules/sjcl && ./configure --without-all --with-aes --with-convenience --with-cbc --with-codecHex --with-codecBase64 --with-sha512 --with-hmac && make && cd ../.. && cp node_modules/sjcl/sjcl.js lib/

View File

@ -1,6 +1,6 @@
'use strict';
var imports = require('soop').imports();
var sjcl = require('sjcl');
var sjcl = require('../sjcl');
var ECIES = require('../common/ECIES');
ECIES.symmetricEncrypt = function(key, iv, message) {

56
lib/sjcl.js Normal file
View File

@ -0,0 +1,56 @@
"use strict";function l(a){throw a;}var s=void 0,v=!1;var sjcl={cipher:{},hash:{},keyexchange:{},mode:{},misc:{},codec:{},exception:{corrupt:function(a){this.toString=function(){return"CORRUPT: "+this.message};this.message=a},invalid:function(a){this.toString=function(){return"INVALID: "+this.message};this.message=a},bug:function(a){this.toString=function(){return"BUG: "+this.message};this.message=a},notReady:function(a){this.toString=function(){return"NOT READY: "+this.message};this.message=a}}};
"undefined"!==typeof module&&module.exports&&(module.exports=sjcl);
sjcl.cipher.aes=function(a){this.m[0][0][0]||this.q();var b,c,d,e,f=this.m[0][4],g=this.m[1];b=a.length;var h=1;4!==b&&(6!==b&&8!==b)&&l(new sjcl.exception.invalid("invalid aes key size"));this.a=[d=a.slice(0),e=[]];for(a=b;a<4*b+28;a++){c=d[a-1];if(0===a%b||8===b&&4===a%b)c=f[c>>>24]<<24^f[c>>16&255]<<16^f[c>>8&255]<<8^f[c&255],0===a%b&&(c=c<<8^c>>>24^h<<24,h=h<<1^283*(h>>7));d[a]=d[a-b]^c}for(b=0;a;b++,a--)c=d[b&3?a:a-4],e[b]=4>=a||4>b?c:g[0][f[c>>>24]]^g[1][f[c>>16&255]]^g[2][f[c>>8&255]]^g[3][f[c&
255]]};
sjcl.cipher.aes.prototype={encrypt:function(a){return aa(this,a,0)},decrypt:function(a){return aa(this,a,1)},m:[[[],[],[],[],[]],[[],[],[],[],[]]],q:function(){var a=this.m[0],b=this.m[1],c=a[4],d=b[4],e,f,g,h=[],k=[],m,p,n,q;for(e=0;0x100>e;e++)k[(h[e]=e<<1^283*(e>>7))^e]=e;for(f=g=0;!c[f];f^=m||1,g=k[g]||1){n=g^g<<1^g<<2^g<<3^g<<4;n=n>>8^n&255^99;c[f]=n;d[n]=f;p=h[e=h[m=h[f]]];q=0x1010101*p^0x10001*e^0x101*m^0x1010100*f;p=0x101*h[n]^0x1010100*n;for(e=0;4>e;e++)a[e][f]=p=p<<24^p>>>8,b[e][n]=q=q<<24^q>>>8}for(e=
0;5>e;e++)a[e]=a[e].slice(0),b[e]=b[e].slice(0)}};
function aa(a,b,c){4!==b.length&&l(new sjcl.exception.invalid("invalid aes block size"));var d=a.a[c],e=b[0]^d[0],f=b[c?3:1]^d[1],g=b[2]^d[2];b=b[c?1:3]^d[3];var h,k,m,p=d.length/4-2,n,q=4,w=[0,0,0,0];h=a.m[c];a=h[0];var D=h[1],B=h[2],E=h[3],C=h[4];for(n=0;n<p;n++)h=a[e>>>24]^D[f>>16&255]^B[g>>8&255]^E[b&255]^d[q],k=a[f>>>24]^D[g>>16&255]^B[b>>8&255]^E[e&255]^d[q+1],m=a[g>>>24]^D[b>>16&255]^B[e>>8&255]^E[f&255]^d[q+2],b=a[b>>>24]^D[e>>16&255]^B[f>>8&255]^E[g&255]^d[q+3],q+=4,e=h,f=k,g=m;for(n=0;4>
n;n++)w[c?3&-n:n]=C[e>>>24]<<24^C[f>>16&255]<<16^C[g>>8&255]<<8^C[b&255]^d[q++],h=e,e=f,f=g,g=b,b=h;return w}
sjcl.bitArray={bitSlice:function(a,b,c){a=sjcl.bitArray.O(a.slice(b/32),32-(b&31)).slice(1);return c===s?a:sjcl.bitArray.clamp(a,c-b)},extract:function(a,b,c){var d=Math.floor(-b-c&31);return((b+c-1^b)&-32?a[b/32|0]<<32-d^a[b/32+1|0]>>>d:a[b/32|0]>>>d)&(1<<c)-1},concat:function(a,b){if(0===a.length||0===b.length)return a.concat(b);var c=a[a.length-1],d=sjcl.bitArray.getPartial(c);return 32===d?a.concat(b):sjcl.bitArray.O(b,d,c|0,a.slice(0,a.length-1))},bitLength:function(a){var b=a.length;return 0===
b?0:32*(b-1)+sjcl.bitArray.getPartial(a[b-1])},clamp:function(a,b){if(32*a.length<b)return a;a=a.slice(0,Math.ceil(b/32));var c=a.length;b&=31;0<c&&b&&(a[c-1]=sjcl.bitArray.partial(b,a[c-1]&2147483648>>b-1,1));return a},partial:function(a,b,c){return 32===a?b:(c?b|0:b<<32-a)+0x10000000000*a},getPartial:function(a){return Math.round(a/0x10000000000)||32},equal:function(a,b){if(sjcl.bitArray.bitLength(a)!==sjcl.bitArray.bitLength(b))return v;var c=0,d;for(d=0;d<a.length;d++)c|=a[d]^b[d];return 0===
c},O:function(a,b,c,d){var e;e=0;for(d===s&&(d=[]);32<=b;b-=32)d.push(c),c=0;if(0===b)return d.concat(a);for(e=0;e<a.length;e++)d.push(c|a[e]>>>b),c=a[e]<<32-b;e=a.length?a[a.length-1]:0;a=sjcl.bitArray.getPartial(e);d.push(sjcl.bitArray.partial(b+a&31,32<b+a?c:d.pop(),1));return d},t:function(a,b){return[a[0]^b[0],a[1]^b[1],a[2]^b[2],a[3]^b[3]]}};
sjcl.codec.utf8String={fromBits:function(a){var b="",c=sjcl.bitArray.bitLength(a),d,e;for(d=0;d<c/8;d++)0===(d&3)&&(e=a[d/4]),b+=String.fromCharCode(e>>>24),e<<=8;return decodeURIComponent(escape(b))},toBits:function(a){a=unescape(encodeURIComponent(a));var b=[],c,d=0;for(c=0;c<a.length;c++)d=d<<8|a.charCodeAt(c),3===(c&3)&&(b.push(d),d=0);c&3&&b.push(sjcl.bitArray.partial(8*(c&3),d));return b}};
sjcl.codec.hex={fromBits:function(a){var b="",c;for(c=0;c<a.length;c++)b+=((a[c]|0)+0xf00000000000).toString(16).substr(4);return b.substr(0,sjcl.bitArray.bitLength(a)/4)},toBits:function(a){var b,c=[],d;a=a.replace(/\s|0x/g,"");d=a.length;a+="00000000";for(b=0;b<a.length;b+=8)c.push(parseInt(a.substr(b,8),16)^0);return sjcl.bitArray.clamp(c,4*d)}};
sjcl.codec.base64={I:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",fromBits:function(a,b,c){var d="",e=0,f=sjcl.codec.base64.I,g=0,h=sjcl.bitArray.bitLength(a);c&&(f=f.substr(0,62)+"-_");for(c=0;6*d.length<h;)d+=f.charAt((g^a[c]>>>e)>>>26),6>e?(g=a[c]<<6-e,e+=26,c++):(g<<=6,e-=6);for(;d.length&3&&!b;)d+="=";return d},toBits:function(a,b){a=a.replace(/\s|=/g,"");var c=[],d,e=0,f=sjcl.codec.base64.I,g=0,h;b&&(f=f.substr(0,62)+"-_");for(d=0;d<a.length;d++)h=f.indexOf(a.charAt(d)),
0>h&&l(new sjcl.exception.invalid("this isn't base64!")),26<e?(e-=26,c.push(g^h>>>e),g=h<<32-e):(e+=6,g^=h<<32-e);e&56&&c.push(sjcl.bitArray.partial(e&56,g,1));return c}};sjcl.codec.base64url={fromBits:function(a){return sjcl.codec.base64.fromBits(a,1,1)},toBits:function(a){return sjcl.codec.base64.toBits(a,1)}};sjcl.hash.sha256=function(a){this.a[0]||this.q();a?(this.e=a.e.slice(0),this.d=a.d.slice(0),this.c=a.c):this.reset()};sjcl.hash.sha256.hash=function(a){return(new sjcl.hash.sha256).update(a).finalize()};
sjcl.hash.sha256.prototype={blockSize:512,reset:function(){this.e=this.j.slice(0);this.d=[];this.c=0;return this},update:function(a){"string"===typeof a&&(a=sjcl.codec.utf8String.toBits(a));var b,c=this.d=sjcl.bitArray.concat(this.d,a);b=this.c;a=this.c=b+sjcl.bitArray.bitLength(a);for(b=512+b&-512;b<=a;b+=512)this.p(c.splice(0,16));return this},finalize:function(){var a,b=this.d,c=this.e,b=sjcl.bitArray.concat(b,[sjcl.bitArray.partial(1,1)]);for(a=b.length+2;a&15;a++)b.push(0);b.push(Math.floor(this.c/
4294967296));for(b.push(this.c|0);b.length;)this.p(b.splice(0,16));this.reset();return c},j:[],a:[],q:function(){function a(a){return 0x100000000*(a-Math.floor(a))|0}var b=0,c=2,d;a:for(;64>b;c++){for(d=2;d*d<=c;d++)if(0===c%d)continue a;8>b&&(this.j[b]=a(Math.pow(c,0.5)));this.a[b]=a(Math.pow(c,1/3));b++}},p:function(a){var b,c,d=a.slice(0),e=this.e,f=this.a,g=e[0],h=e[1],k=e[2],m=e[3],p=e[4],n=e[5],q=e[6],w=e[7];for(a=0;64>a;a++)16>a?b=d[a]:(b=d[a+1&15],c=d[a+14&15],b=d[a&15]=(b>>>7^b>>>18^b>>>3^
b<<25^b<<14)+(c>>>17^c>>>19^c>>>10^c<<15^c<<13)+d[a&15]+d[a+9&15]|0),b=b+w+(p>>>6^p>>>11^p>>>25^p<<26^p<<21^p<<7)+(q^p&(n^q))+f[a],w=q,q=n,n=p,p=m+b|0,m=k,k=h,h=g,g=b+(h&k^m&(h^k))+(h>>>2^h>>>13^h>>>22^h<<30^h<<19^h<<10)|0;e[0]=e[0]+g|0;e[1]=e[1]+h|0;e[2]=e[2]+k|0;e[3]=e[3]+m|0;e[4]=e[4]+p|0;e[5]=e[5]+n|0;e[6]=e[6]+q|0;e[7]=e[7]+w|0}};sjcl.hash.sha512=function(a){this.a[0]||this.q();a?(this.e=a.e.slice(0),this.d=a.d.slice(0),this.c=a.c):this.reset()};sjcl.hash.sha512.hash=function(a){return(new sjcl.hash.sha512).update(a).finalize()};
sjcl.hash.sha512.prototype={blockSize:1024,reset:function(){this.e=this.j.slice(0);this.d=[];this.c=0;return this},update:function(a){"string"===typeof a&&(a=sjcl.codec.utf8String.toBits(a));var b,c=this.d=sjcl.bitArray.concat(this.d,a);b=this.c;a=this.c=b+sjcl.bitArray.bitLength(a);for(b=1024+b&-1024;b<=a;b+=1024)this.p(c.splice(0,32));return this},finalize:function(){var a,b=this.d,c=this.e,b=sjcl.bitArray.concat(b,[sjcl.bitArray.partial(1,1)]);for(a=b.length+4;a&31;a++)b.push(0);b.push(0);b.push(0);
b.push(Math.floor(this.c/0x100000000));for(b.push(this.c|0);b.length;)this.p(b.splice(0,32));this.reset();return c},j:[],Y:[12372232,13281083,9762859,1914609,15106769,4090911,4308331,8266105],a:[],$:[2666018,15689165,5061423,9034684,4764984,380953,1658779,7176472,197186,7368638,14987916,16757986,8096111,1480369,13046325,6891156,15813330,5187043,9229749,11312229,2818677,10937475,4324308,1135541,6741931,11809296,16458047,15666916,11046850,698149,229999,945776,13774844,2541862,12856045,9810911,11494366,
7844520,15576806,8533307,15795044,4337665,16291729,5553712,15684120,6662416,7413802,12308920,13816008,4303699,9366425,10176680,13195875,4295371,6546291,11712675,15708924,1519456,15772530,6568428,6495784,8568297,13007125,7492395,2515356,12632583,14740254,7262584,1535930,13146278,16321966,1853211,294276,13051027,13221564,1051980,4080310,6651434,14088940,4675607],q:function(){function a(a){return 0x100000000*(a-Math.floor(a))|0}function b(a){return 0x10000000000*(a-Math.floor(a))&255}var c=0,d=2,e;a:for(;80>
c;d++){for(e=2;e*e<=d;e++)if(0===d%e)continue a;8>c&&(this.j[2*c]=a(Math.pow(d,0.5)),this.j[2*c+1]=b(Math.pow(d,0.5))<<24|this.Y[c]);this.a[2*c]=a(Math.pow(d,1/3));this.a[2*c+1]=b(Math.pow(d,1/3))<<24|this.$[c];c++}},p:function(a){var b,c,d=a.slice(0),e=this.e,f=this.a,g=e[0],h=e[1],k=e[2],m=e[3],p=e[4],n=e[5],q=e[6],w=e[7],D=e[8],B=e[9],E=e[10],C=e[11],fa=e[12],O=e[13],ga=e[14],P=e[15],x=g,t=h,H=k,F=m,I=p,G=n,W=q,J=w,y=D,u=B,Q=E,K=C,R=fa,L=O,X=ga,M=P;for(a=0;80>a;a++){if(16>a)b=d[2*a],c=d[2*a+1];
else{c=d[2*(a-15)];var r=d[2*(a-15)+1];b=(r<<31|c>>>1)^(r<<24|c>>>8)^c>>>7;var z=(c<<31|r>>>1)^(c<<24|r>>>8)^(c<<25|r>>>7);c=d[2*(a-2)];var A=d[2*(a-2)+1],r=(A<<13|c>>>19)^(c<<3|A>>>29)^c>>>6,A=(c<<13|A>>>19)^(A<<3|c>>>29)^(c<<26|A>>>6),Y=d[2*(a-7)],Z=d[2*(a-16)],N=d[2*(a-16)+1];c=z+d[2*(a-7)+1];b=b+Y+(c>>>0<z>>>0?1:0);c+=A;b+=r+(c>>>0<A>>>0?1:0);c+=N;b+=Z+(c>>>0<N>>>0?1:0)}d[2*a]=b|=0;d[2*a+1]=c|=0;var Y=y&Q^~y&R,ha=u&K^~u&L,A=x&H^x&I^H&I,la=t&F^t&G^F&G,Z=(t<<4|x>>>28)^(x<<30|t>>>2)^(x<<25|t>>>7),
N=(x<<4|t>>>28)^(t<<30|x>>>2)^(t<<25|x>>>7),ma=f[2*a],ia=f[2*a+1],r=M+((y<<18|u>>>14)^(y<<14|u>>>18)^(u<<23|y>>>9)),z=X+((u<<18|y>>>14)^(u<<14|y>>>18)^(y<<23|u>>>9))+(r>>>0<M>>>0?1:0),r=r+ha,z=z+(Y+(r>>>0<ha>>>0?1:0)),r=r+ia,z=z+(ma+(r>>>0<ia>>>0?1:0)),r=r+c,z=z+(b+(r>>>0<c>>>0?1:0));c=N+la;b=Z+A+(c>>>0<N>>>0?1:0);X=R;M=L;R=Q;L=K;Q=y;K=u;u=J+r|0;y=W+z+(u>>>0<J>>>0?1:0)|0;W=I;J=G;I=H;G=F;H=x;F=t;t=r+c|0;x=z+b+(t>>>0<r>>>0?1:0)|0}h=e[1]=h+t|0;e[0]=g+x+(h>>>0<t>>>0?1:0)|0;m=e[3]=m+F|0;e[2]=k+H+(m>>>
0<F>>>0?1:0)|0;n=e[5]=n+G|0;e[4]=p+I+(n>>>0<G>>>0?1:0)|0;w=e[7]=w+J|0;e[6]=q+W+(w>>>0<J>>>0?1:0)|0;B=e[9]=B+u|0;e[8]=D+y+(B>>>0<u>>>0?1:0)|0;C=e[11]=C+K|0;e[10]=E+Q+(C>>>0<K>>>0?1:0)|0;O=e[13]=O+L|0;e[12]=fa+R+(O>>>0<L>>>0?1:0)|0;P=e[15]=P+M|0;e[14]=ga+X+(P>>>0<M>>>0?1:0)|0}};
sjcl.mode.ccm={name:"ccm",encrypt:function(a,b,c,d,e){var f,g=b.slice(0),h=sjcl.bitArray,k=h.bitLength(c)/8,m=h.bitLength(g)/8;e=e||64;d=d||[];7>k&&l(new sjcl.exception.invalid("ccm: iv must be at least 7 bytes"));for(f=2;4>f&&m>>>8*f;f++);f<15-k&&(f=15-k);c=h.clamp(c,8*(15-f));b=sjcl.mode.ccm.K(a,b,c,d,e,f);g=sjcl.mode.ccm.L(a,g,c,b,e,f);return h.concat(g.data,g.tag)},decrypt:function(a,b,c,d,e){e=e||64;d=d||[];var f=sjcl.bitArray,g=f.bitLength(c)/8,h=f.bitLength(b),k=f.clamp(b,h-e),m=f.bitSlice(b,
h-e),h=(h-e)/8;7>g&&l(new sjcl.exception.invalid("ccm: iv must be at least 7 bytes"));for(b=2;4>b&&h>>>8*b;b++);b<15-g&&(b=15-g);c=f.clamp(c,8*(15-b));k=sjcl.mode.ccm.L(a,k,c,m,e,b);a=sjcl.mode.ccm.K(a,k.data,c,d,e,b);f.equal(k.tag,a)||l(new sjcl.exception.corrupt("ccm: tag doesn't match"));return k.data},K:function(a,b,c,d,e,f){var g=[],h=sjcl.bitArray,k=h.t;e/=8;(e%2||4>e||16<e)&&l(new sjcl.exception.invalid("ccm: invalid tag length"));(0xffffffff<d.length||0xffffffff<b.length)&&l(new sjcl.exception.bug("ccm: can't deal with 4GiB or more data"));
f=[h.partial(8,(d.length?64:0)|e-2<<2|f-1)];f=h.concat(f,c);f[3]|=h.bitLength(b)/8;f=a.encrypt(f);if(d.length){c=h.bitLength(d)/8;65279>=c?g=[h.partial(16,c)]:0xffffffff>=c&&(g=h.concat([h.partial(16,65534)],[c]));g=h.concat(g,d);for(d=0;d<g.length;d+=4)f=a.encrypt(k(f,g.slice(d,d+4).concat([0,0,0])))}for(d=0;d<b.length;d+=4)f=a.encrypt(k(f,b.slice(d,d+4).concat([0,0,0])));return h.clamp(f,8*e)},L:function(a,b,c,d,e,f){var g,h=sjcl.bitArray;g=h.t;var k=b.length,m=h.bitLength(b);c=h.concat([h.partial(8,
f-1)],c).concat([0,0,0]).slice(0,4);d=h.bitSlice(g(d,a.encrypt(c)),0,e);if(!k)return{tag:d,data:[]};for(g=0;g<k;g+=4)c[3]++,e=a.encrypt(c),b[g]^=e[0],b[g+1]^=e[1],b[g+2]^=e[2],b[g+3]^=e[3];return{tag:d,data:h.clamp(b,m)}}};sjcl.beware===s&&(sjcl.beware={});
sjcl.beware["CBC mode is dangerous because it doesn't protect message integrity."]=function(){sjcl.mode.cbc={name:"cbc",encrypt:function(a,b,c,d){d&&d.length&&l(new sjcl.exception.invalid("cbc can't authenticate data"));128!==sjcl.bitArray.bitLength(c)&&l(new sjcl.exception.invalid("cbc iv must be 128 bits"));var e=sjcl.bitArray,f=e.t,g=e.bitLength(b),h=0,k=[];g&7&&l(new sjcl.exception.invalid("pkcs#5 padding only works for multiples of a byte"));for(d=0;h+128<=g;d+=4,h+=128)c=a.encrypt(f(c,b.slice(d,
d+4))),k.splice(d,0,c[0],c[1],c[2],c[3]);g=0x1010101*(16-(g>>3&15));c=a.encrypt(f(c,e.concat(b,[g,g,g,g]).slice(d,d+4)));k.splice(d,0,c[0],c[1],c[2],c[3]);return k},decrypt:function(a,b,c,d){d&&d.length&&l(new sjcl.exception.invalid("cbc can't authenticate data"));128!==sjcl.bitArray.bitLength(c)&&l(new sjcl.exception.invalid("cbc iv must be 128 bits"));(sjcl.bitArray.bitLength(b)&127||!b.length)&&l(new sjcl.exception.corrupt("cbc ciphertext must be a positive multiple of the block size"));var e=sjcl.bitArray,
f=e.t,g,h=[];for(d=0;d<b.length;d+=4)g=b.slice(d,d+4),c=f(c,a.decrypt(g)),h.splice(d,0,c[0],c[1],c[2],c[3]),c=g;g=h[d-1]&255;(0===g||16<g)&&l(new sjcl.exception.corrupt("pkcs#5 padding corrupt"));c=0x1010101*g;e.equal(e.bitSlice([c,c,c,c],0,8*g),e.bitSlice(h,32*h.length-8*g,32*h.length))||l(new sjcl.exception.corrupt("pkcs#5 padding corrupt"));return e.bitSlice(h,0,32*h.length-8*g)}}};
sjcl.misc.hmac=function(a,b){this.M=b=b||sjcl.hash.sha256;var c=[[],[]],d,e=b.prototype.blockSize/32;this.o=[new b,new b];a.length>e&&(a=b.hash(a));for(d=0;d<e;d++)c[0][d]=a[d]^909522486,c[1][d]=a[d]^1549556828;this.o[0].update(c[0]);this.o[1].update(c[1]);this.G=new b(this.o[0])};sjcl.misc.hmac.prototype.encrypt=sjcl.misc.hmac.prototype.mac=function(a){this.P&&l(new sjcl.exception.invalid("encrypt on already updated hmac called!"));this.update(a);return this.digest(a)};
sjcl.misc.hmac.prototype.reset=function(){this.G=new this.M(this.o[0]);this.P=v};sjcl.misc.hmac.prototype.update=function(a){this.P=!0;this.G.update(a)};sjcl.misc.hmac.prototype.digest=function(){var a=this.G.finalize(),a=(new this.M(this.o[1])).update(a).finalize();this.reset();return a};
sjcl.misc.pbkdf2=function(a,b,c,d,e){c=c||1E3;(0>d||0>c)&&l(sjcl.exception.invalid("invalid params to pbkdf2"));"string"===typeof a&&(a=sjcl.codec.utf8String.toBits(a));"string"===typeof b&&(b=sjcl.codec.utf8String.toBits(b));e=e||sjcl.misc.hmac;a=new e(a);var f,g,h,k,m=[],p=sjcl.bitArray;for(k=1;32*m.length<(d||1);k++){e=f=a.encrypt(p.concat(b,[k]));for(g=1;g<c;g++){f=a.encrypt(f);for(h=0;h<f.length;h++)e[h]^=f[h]}m=m.concat(e)}d&&(m=p.clamp(m,d));return m};
sjcl.prng=function(a){this.f=[new sjcl.hash.sha256];this.k=[0];this.F=0;this.s={};this.D=0;this.J={};this.N=this.g=this.l=this.V=0;this.a=[0,0,0,0,0,0,0,0];this.i=[0,0,0,0];this.B=s;this.C=a;this.r=v;this.A={progress:{},seeded:{}};this.n=this.U=0;this.u=1;this.w=2;this.R=0x10000;this.H=[0,48,64,96,128,192,0x100,384,512,768,1024];this.S=3E4;this.Q=80};
sjcl.prng.prototype={randomWords:function(a,b){var c=[],d;d=this.isReady(b);var e;d===this.n&&l(new sjcl.exception.notReady("generator isn't seeded"));if(d&this.w){d=!(d&this.u);e=[];var f=0,g;this.N=e[0]=(new Date).valueOf()+this.S;for(g=0;16>g;g++)e.push(0x100000000*Math.random()|0);for(g=0;g<this.f.length&&!(e=e.concat(this.f[g].finalize()),f+=this.k[g],this.k[g]=0,!d&&this.F&1<<g);g++);this.F>=1<<this.f.length&&(this.f.push(new sjcl.hash.sha256),this.k.push(0));this.g-=f;f>this.l&&(this.l=f);this.F++;
this.a=sjcl.hash.sha256.hash(this.a.concat(e));this.B=new sjcl.cipher.aes(this.a);for(d=0;4>d&&!(this.i[d]=this.i[d]+1|0,this.i[d]);d++);}for(d=0;d<a;d+=4)0===(d+1)%this.R&&ba(this),e=S(this),c.push(e[0],e[1],e[2],e[3]);ba(this);return c.slice(0,a)},setDefaultParanoia:function(a,b){0===a&&"Setting paranoia=0 will ruin your security; use it only for testing"!==b&&l("Setting paranoia=0 will ruin your security; use it only for testing");this.C=a},addEntropy:function(a,b,c){c=c||"user";var d,e,f=(new Date).valueOf(),
g=this.s[c],h=this.isReady(),k=0;d=this.J[c];d===s&&(d=this.J[c]=this.V++);g===s&&(g=this.s[c]=0);this.s[c]=(this.s[c]+1)%this.f.length;switch(typeof a){case "number":b===s&&(b=1);this.f[g].update([d,this.D++,1,b,f,1,a|0]);break;case "object":c=Object.prototype.toString.call(a);if("[object Uint32Array]"===c){e=[];for(c=0;c<a.length;c++)e.push(a[c]);a=e}else{"[object Array]"!==c&&(k=1);for(c=0;c<a.length&&!k;c++)"number"!==typeof a[c]&&(k=1)}if(!k){if(b===s)for(c=b=0;c<a.length;c++)for(e=a[c];0<e;)b++,
e>>>=1;this.f[g].update([d,this.D++,2,b,f,a.length].concat(a))}break;case "string":b===s&&(b=a.length);this.f[g].update([d,this.D++,3,b,f,a.length]);this.f[g].update(a);break;default:k=1}k&&l(new sjcl.exception.bug("random: addEntropy only supports number, array of numbers or string"));this.k[g]+=b;this.g+=b;h===this.n&&(this.isReady()!==this.n&&ca("seeded",Math.max(this.l,this.g)),ca("progress",this.getProgress()))},isReady:function(a){a=this.H[a!==s?a:this.C];return this.l&&this.l>=a?this.k[0]>
this.Q&&(new Date).valueOf()>this.N?this.w|this.u:this.u:this.g>=a?this.w|this.n:this.n},getProgress:function(a){a=this.H[a?a:this.C];return this.l>=a?1:this.g>a?1:this.g/a},startCollectors:function(){this.r||(this.b={loadTimeCollector:T(this,this.aa),mouseCollector:T(this,this.ba),keyboardCollector:T(this,this.Z),accelerometerCollector:T(this,this.T)},window.addEventListener?(window.addEventListener("load",this.b.loadTimeCollector,v),window.addEventListener("mousemove",this.b.mouseCollector,v),window.addEventListener("keypress",
this.b.keyboardCollector,v),window.addEventListener("devicemotion",this.b.accelerometerCollector,v)):document.attachEvent?(document.attachEvent("onload",this.b.loadTimeCollector),document.attachEvent("onmousemove",this.b.mouseCollector),document.attachEvent("keypress",this.b.keyboardCollector)):l(new sjcl.exception.bug("can't attach event")),this.r=!0)},stopCollectors:function(){this.r&&(window.removeEventListener?(window.removeEventListener("load",this.b.loadTimeCollector,v),window.removeEventListener("mousemove",
this.b.mouseCollector,v),window.removeEventListener("keypress",this.b.keyboardCollector,v),window.removeEventListener("devicemotion",this.b.accelerometerCollector,v)):document.detachEvent&&(document.detachEvent("onload",this.b.loadTimeCollector),document.detachEvent("onmousemove",this.b.mouseCollector),document.detachEvent("keypress",this.b.keyboardCollector)),this.r=v)},addEventListener:function(a,b){this.A[a][this.U++]=b},removeEventListener:function(a,b){var c,d,e=this.A[a],f=[];for(d in e)e.hasOwnProperty(d)&&
e[d]===b&&f.push(d);for(c=0;c<f.length;c++)d=f[c],delete e[d]},Z:function(){U(1)},ba:function(a){sjcl.random.addEntropy([a.x||a.clientX||a.offsetX||0,a.y||a.clientY||a.offsetY||0],2,"mouse");U(0)},aa:function(){U(2)},T:function(a){a=a.accelerationIncludingGravity.x||a.accelerationIncludingGravity.y||a.accelerationIncludingGravity.z;if(window.orientation){var b=window.orientation;"number"===typeof b&&sjcl.random.addEntropy(b,1,"accelerometer")}a&&sjcl.random.addEntropy(a,2,"accelerometer");U(0)}};
function ca(a,b){var c,d=sjcl.random.A[a],e=[];for(c in d)d.hasOwnProperty(c)&&e.push(d[c]);for(c=0;c<e.length;c++)e[c](b)}function U(a){window&&window.performance&&"function"===typeof window.performance.now?sjcl.random.addEntropy(window.performance.now(),a,"loadtime"):sjcl.random.addEntropy((new Date).valueOf(),a,"loadtime")}function ba(a){a.a=S(a).concat(S(a));a.B=new sjcl.cipher.aes(a.a)}function S(a){for(var b=0;4>b&&!(a.i[b]=a.i[b]+1|0,a.i[b]);b++);return a.B.encrypt(a.i)}
function T(a,b){return function(){b.apply(a,arguments)}}sjcl.random=new sjcl.prng(6);
a:try{var V,da,$,ea;if(ea="undefined"!==typeof module){var ja;if(ja=module.exports){var ka;try{ka=require("crypto")}catch(na){ka=null}ja=(da=ka)&&da.randomBytes}ea=ja}if(ea)V=da.randomBytes(128),V=new Uint32Array((new Uint8Array(V)).buffer),sjcl.random.addEntropy(V,1024,"crypto['randomBytes']");else if(window&&Uint32Array){$=new Uint32Array(32);if(window.crypto&&window.crypto.getRandomValues)window.crypto.getRandomValues($);else if(window.msCrypto&&window.msCrypto.getRandomValues)window.msCrypto.getRandomValues($);
else break a;sjcl.random.addEntropy($,1024,"crypto['getRandomValues']")}}catch(oa){"undefined"!==typeof window&&window.console&&(console.log("There was an error collecting entropy from the browser:"),console.log(oa))}
sjcl.json={defaults:{v:1,iter:1E3,ks:128,ts:64,mode:"ccm",adata:"",cipher:"aes"},X:function(a,b,c,d){c=c||{};d=d||{};var e=sjcl.json,f=e.h({iv:sjcl.random.randomWords(4,0)},e.defaults),g;e.h(f,c);c=f.adata;"string"===typeof f.salt&&(f.salt=sjcl.codec.base64.toBits(f.salt));"string"===typeof f.iv&&(f.iv=sjcl.codec.base64.toBits(f.iv));(!sjcl.mode[f.mode]||!sjcl.cipher[f.cipher]||"string"===typeof a&&100>=f.iter||64!==f.ts&&96!==f.ts&&128!==f.ts||128!==f.ks&&192!==f.ks&&0x100!==f.ks||2>f.iv.length||4<
f.iv.length)&&l(new sjcl.exception.invalid("json encrypt: invalid parameters"));"string"===typeof a?(g=sjcl.misc.cachedPbkdf2(a,f),a=g.key.slice(0,f.ks/32),f.salt=g.salt):sjcl.ecc&&a instanceof sjcl.ecc.elGamal.publicKey&&(g=a.kem(),f.kemtag=g.tag,a=g.key.slice(0,f.ks/32));"string"===typeof b&&(b=sjcl.codec.utf8String.toBits(b));"string"===typeof c&&(c=sjcl.codec.utf8String.toBits(c));g=new sjcl.cipher[f.cipher](a);e.h(d,f);d.key=a;f.ct=sjcl.mode[f.mode].encrypt(g,b,f.iv,c,f.ts);return f},encrypt:function(a,
b,c,d){var e=sjcl.json,f=e.X.apply(e,arguments);return e.encode(f)},W:function(a,b,c,d){c=c||{};d=d||{};var e=sjcl.json;b=e.h(e.h(e.h({},e.defaults),b),c,!0);var f;c=b.adata;"string"===typeof b.salt&&(b.salt=sjcl.codec.base64.toBits(b.salt));"string"===typeof b.iv&&(b.iv=sjcl.codec.base64.toBits(b.iv));(!sjcl.mode[b.mode]||!sjcl.cipher[b.cipher]||"string"===typeof a&&100>=b.iter||64!==b.ts&&96!==b.ts&&128!==b.ts||128!==b.ks&&192!==b.ks&&0x100!==b.ks||!b.iv||2>b.iv.length||4<b.iv.length)&&l(new sjcl.exception.invalid("json decrypt: invalid parameters"));
"string"===typeof a?(f=sjcl.misc.cachedPbkdf2(a,b),a=f.key.slice(0,b.ks/32),b.salt=f.salt):sjcl.ecc&&a instanceof sjcl.ecc.elGamal.secretKey&&(a=a.unkem(sjcl.codec.base64.toBits(b.kemtag)).slice(0,b.ks/32));"string"===typeof c&&(c=sjcl.codec.utf8String.toBits(c));f=new sjcl.cipher[b.cipher](a);c=sjcl.mode[b.mode].decrypt(f,b.ct,b.iv,c,b.ts);e.h(d,b);d.key=a;return sjcl.codec.utf8String.fromBits(c)},decrypt:function(a,b,c,d){var e=sjcl.json;return e.W(a,e.decode(b),c,d)},encode:function(a){var b,c=
"{",d="";for(b in a)if(a.hasOwnProperty(b))switch(b.match(/^[a-z0-9]+$/i)||l(new sjcl.exception.invalid("json encode: invalid property name")),c+=d+'"'+b+'":',d=",",typeof a[b]){case "number":case "boolean":c+=a[b];break;case "string":c+='"'+escape(a[b])+'"';break;case "object":c+='"'+sjcl.codec.base64.fromBits(a[b],0)+'"';break;default:l(new sjcl.exception.bug("json encode: unsupported type"))}return c+"}"},decode:function(a){a=a.replace(/\s/g,"");a.match(/^\{.*\}$/)||l(new sjcl.exception.invalid("json decode: this isn't json!"));
a=a.replace(/^\{|\}$/g,"").split(/,/);var b={},c,d;for(c=0;c<a.length;c++)(d=a[c].match(/^(?:(["']?)([a-z][a-z0-9]*)\1):(?:(\d+)|"([a-z0-9+\/%*_.@=\-]*)")$/i))||l(new sjcl.exception.invalid("json decode: this isn't json!")),b[d[2]]=d[3]?parseInt(d[3],10):d[2].match(/^(ct|salt|iv)$/)?sjcl.codec.base64.toBits(d[4]):unescape(d[4]);return b},h:function(a,b,c){a===s&&(a={});if(b===s)return a;for(var d in b)b.hasOwnProperty(d)&&(c&&(a[d]!==s&&a[d]!==b[d])&&l(new sjcl.exception.invalid("required parameter overridden")),
a[d]=b[d]);return a},ea:function(a,b){var c={},d;for(d in a)a.hasOwnProperty(d)&&a[d]!==b[d]&&(c[d]=a[d]);return c},da:function(a,b){var c={},d;for(d=0;d<b.length;d++)a[b[d]]!==s&&(c[b[d]]=a[b[d]]);return c}};sjcl.encrypt=sjcl.json.encrypt;sjcl.decrypt=sjcl.json.decrypt;sjcl.misc.ca={};
sjcl.misc.cachedPbkdf2=function(a,b){var c=sjcl.misc.ca,d;b=b||{};d=b.iter||1E3;c=c[a]=c[a]||{};d=c[d]=c[d]||{firstSalt:b.salt&&b.salt.length?b.salt.slice(0):sjcl.random.randomWords(2,0)};c=b.salt===s?d.firstSalt:b.salt;d[c]=d[c]||sjcl.misc.pbkdf2(a,c,b.iter);return{key:d[c].slice(0),salt:c.slice(0)}};

View File

@ -3,7 +3,7 @@ var bignum = require('bignum');
var Binary = require('binary');
var Put = require('bufferput');
var buffertools = require('buffertools');
var sjcl = require('sjcl');
var sjcl = require('../lib/sjcl');
var browser;
var inBrowser = !process.versions;
if (inBrowser) {