-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathjwt.all.asp
218 lines (192 loc) · 62.8 KB
/
jwt.all.asp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
<script language="javascript" runat="server">
"use strict";var sjcl={cipher:{},hash:{},mode:{},misc:{},codec:{},exception:{corrupt:function(b){this.toString=function(){return"CORRUPT: "+this.message};this.message=b},invalid:function(b){this.toString=function(){return"INVALID: "+this.message};this.message=b},bug:function(b){this.toString=function(){return"BUG: "+this.message};this.message=b}}};sjcl.cipher.aes=function(j){this.h[0][0][0]||this.w();var i,p,o,n,m=this.h[0][4],l=this.h[1];i=j.length;var k=1;if(i!==4&&i!==6&&i!==8){throw new sjcl.exception.invalid("invalid aes key size")}this.a=[o=j.slice(0),n=[]];for(j=i;j<4*i+28;j++){p=o[j-1];if(j%i===0||i===8&&j%i===4){p=m[p>>>24]<<24^m[p>>16&255]<<16^m[p>>8&255]<<8^m[p&255];if(j%i===0){p=p<<8^p>>>24^k<<24;k=k<<1^(k>>7)*283}}o[j]=o[j-i]^p}for(i=0;j;i++,j--){p=o[i&3?j:j-4];n[i]=j<=4||i<4?p:l[0][m[p>>>24]]^l[1][m[p>>16&255]]^l[2][m[p>>8&255]]^l[3][m[p&255]]}};sjcl.cipher.aes.prototype={encrypt:function(b){return this.H(b,0)},decrypt:function(b){return this.H(b,1)},h:[[[],[],[],[],[]],[[],[],[],[],[]]],w:function(){var z=this.h[0],y=this.h[1],x=z[4],w=y[4],v,u,t,s=[],r=[],p,q,o,n;for(v=0;v<256;v++){r[(s[v]=v<<1^(v>>7)*283)^v]=v}for(u=t=0;!x[u];u^=p||1,t=r[t]||1){o=t^t<<1^t<<2^t<<3^t<<4;o=o>>8^o&255^99;x[u]=o;w[o]=u;q=s[v=s[p=s[u]]];n=q*16843009^v*65537^p*257^u*16843008;q=s[o]*257^o*16843008;for(v=0;v<4;v++){z[v][u]=q=q<<24^q>>>8;y[v][o]=n=n<<24^n>>>8}}for(v=0;v<5;v++){z[v]=z[v].slice(0);y[v]=y[v].slice(0)}},H:function(J,I){if(J.length!==4){throw new sjcl.exception.invalid("invalid aes block size")}var H=this.a[I],G=J[0]^H[0],F=J[I?3:1]^H[1],E=J[2]^H[2];J=J[I?1:3]^H[3];var D,C,B,z=H.length/4-2,A,y=4,x=[0,0,0,0];D=this.h[I];var w=D[0],v=D[1],u=D[2],t=D[3],s=D[4];for(A=0;A<z;A++){D=w[G>>>24]^v[F>>16&255]^u[E>>8&255]^t[J&255]^H[y];C=w[F>>>24]^v[E>>16&255]^u[J>>8&255]^t[G&255]^H[y+1];B=w[E>>>24]^v[J>>16&255]^u[G>>8&255]^t[F&255]^H[y+2];J=w[J>>>24]^v[G>>16&255]^u[F>>8&255]^t[E&255]^H[y+3];y+=4;G=D;F=C;E=B}for(A=0;A<4;A++){x[I?3&-A:A]=s[G>>>24]<<24^s[F>>16&255]<<16^s[E>>8&255]<<8^s[J&255]^H[y++];D=G;G=F;F=E;E=J;J=D}return x}};sjcl.bitArray={bitSlice:function(e,d,f){e=sjcl.bitArray.P(e.slice(d/32),32-(d&31)).slice(1);return f===undefined?e:sjcl.bitArray.clamp(e,f-d)},concat:function(f,e){if(f.length===0||e.length===0){return f.concat(e)}var h=f[f.length-1],g=sjcl.bitArray.getPartial(h);return g===32?f.concat(e):sjcl.bitArray.P(e,g,h|0,f.slice(0,f.length-1))},bitLength:function(d){var c=d.length;if(c===0){return 0}return(c-1)*32+sjcl.bitArray.getPartial(d[c-1])},clamp:function(e,d){if(e.length*32<d){return e}e=e.slice(0,Math.ceil(d/32));var f=e.length;d&=31;if(f>0&&d){e[f-1]=sjcl.bitArray.partial(d,e[f-1]&2147483648>>d-1,1)}return e},partial:function(e,d,f){if(e===32){return d}return(f?d|0:d<<32-e)+e*1099511627776},getPartial:function(b){return Math.round(b/1099511627776)||32},equal:function(f,e){if(sjcl.bitArray.bitLength(f)!==sjcl.bitArray.bitLength(e)){return false}var h=0,g;for(g=0;g<f.length;g++){h|=f[g]^e[g]}return h===0},P:function(g,f,j,i){var h;h=0;if(i===undefined){i=[]}for(;f>=32;f-=32){i.push(j);j=0}if(f===0){return i.concat(g)}for(h=0;h<g.length;h++){i.push(j|g[h]>>>f);j=g[h]<<32-f}h=g.length?g[g.length-1]:0;g=sjcl.bitArray.getPartial(h);i.push(sjcl.bitArray.partial(f+g&31,f+g>32?j:i.pop(),1));return i},k:function(d,c){return[d[0]^c[0],d[1]^c[1],d[2]^c[2],d[3]^c[3]]}};sjcl.codec.utf8String={fromBits:function(g){var f="",j=sjcl.bitArray.bitLength(g),i,h;for(i=0;i<j/8;i++){if((i&3)===0){h=g[i/4]}f+=String.fromCharCode(h>>>24);h<<=8}return decodeURIComponent(escape(f))},toBits:function(f){f=unescape(encodeURIComponent(f));var e=[],h,g=0;for(h=0;h<f.length;h++){g=g<<8|f.charCodeAt(h);if((h&3)===3){e.push(g);g=0}}h&3&&e.push(sjcl.bitArray.partial(8*(h&3),g));return e}};sjcl.codec.hex={fromBits:function(e){var d="",f;for(f=0;f<e.length;f++){d+=((e[f]|0)+263882790666240).toString(16).substr(4)}return d.substr(0,sjcl.bitArray.bitLength(e)/4)},toBits:function(f){var e,h=[],g;f=f.replace(/\s|0x/g,"");g=f.length;f+="00000000";for(e=0;e<f.length;e+=8){h.push(parseInt(f.substr(e,8),16)^0)}return sjcl.bitArray.clamp(h,g*4)}};sjcl.codec.base64={D:"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/",fromBits:function(j,i){var p="",o,n=0,m=sjcl.codec.base64.D,l=0,k=sjcl.bitArray.bitLength(j);for(o=0;p.length*6<k;){p+=m.charAt((l^j[o]>>>n)>>>26);if(n<6){l=j[o]<<6-n;n+=26;o++}else{l<<=6;n-=6}}for(;p.length&3&&!i;){p+="="}return p},toBits:function(i){i=i.replace(/\s|=/g,"");var h=[],n,m=0,l=sjcl.codec.base64.D,k=0,j;for(n=0;n<i.length;n++){j=l.indexOf(i.charAt(n));if(j<0){throw new sjcl.exception.invalid("this isn't base64!")}if(m>26){m-=26;h.push(k^j>>>m);k=j<<32-m}else{m+=6;k^=j<<32-m}}m&56&&h.push(sjcl.bitArray.partial(m&56,k,1));return h}};sjcl.hash.sha256=function(b){this.a[0]||this.w();if(b){this.n=b.n.slice(0);this.i=b.i.slice(0);this.e=b.e}else{this.reset()}};sjcl.hash.sha256.hash=function(b){return(new sjcl.hash.sha256).update(b).finalize()};sjcl.hash.sha256.prototype={blockSize:512,reset:function(){this.n=this.N.slice(0);this.i=[];this.e=0;return this},update:function(e){if(typeof e==="string"){e=sjcl.codec.utf8String.toBits(e)}var d,f=this.i=sjcl.bitArray.concat(this.i,e);d=this.e;e=this.e=d+sjcl.bitArray.bitLength(e);for(d=512+d&-512;d<=e;d+=512){this.C(f.splice(0,16))}return this},finalize:function(){var e,d=this.i,f=this.n;d=sjcl.bitArray.concat(d,[sjcl.bitArray.partial(1,1)]);for(e=d.length+2;e&15;e++){d.push(0)}d.push(Math.floor(this.e/4294967296));for(d.push(this.e|0);d.length;){this.C(d.splice(0,16))}this.reset();return f},N:[],a:[],w:function(){function f(a){return(a-Math.floor(a))*4294967296|0}var e=0,h=2,g;f:for(;e<64;h++){for(g=2;g*g<=h;g++){if(h%g===0){continue f}}if(e<8){this.N[e]=f(Math.pow(h,0.5))}this.a[e]=f(Math.pow(h,1/3));e++}},C:function(B){var A,z,y=B.slice(0),x=this.n,w=this.a,v=x[0],u=x[1],t=x[2],r=x[3],s=x[4],q=x[5],p=x[6],o=x[7];for(B=0;B<64;B++){if(B<16){A=y[B]}else{A=y[B+1&15];z=y[B+14&15];A=y[B&15]=(A>>>7^A>>>18^A>>>3^A<<25^A<<14)+(z>>>17^z>>>19^z>>>10^z<<15^z<<13)+y[B&15]+y[B+9&15]|0}A=A+o+(s>>>6^s>>>11^s>>>25^s<<26^s<<21^s<<7)+(p^s&(q^p))+w[B];o=p;p=q;q=s;s=r+A|0;r=t;t=u;u=v;v=A+(u&t^r&(u^t))+(u>>>2^u>>>13^u>>>22^u<<30^u<<19^u<<10)|0}x[0]=x[0]+v|0;x[1]=x[1]+u|0;x[2]=x[2]+t|0;x[3]=x[3]+r|0;x[4]=x[4]+s|0;x[5]=x[5]+q|0;x[6]=x[6]+p|0;x[7]=x[7]+o|0}};sjcl.mode.ccm={name:"ccm",encrypt:function(t,s,r,q,p){var o,n=s.slice(0),m=sjcl.bitArray,l=m.bitLength(r)/8,j=m.bitLength(n)/8;p=p||64;q=q||[];if(l<7){throw new sjcl.exception.invalid("ccm: iv must be at least 7 bytes")}for(o=2;o<4&&j>>>8*o;o++){}if(o<15-l){o=15-l}r=m.clamp(r,8*(15-o));s=sjcl.mode.ccm.G(t,s,r,q,p,o);n=sjcl.mode.ccm.I(t,n,r,s,p,o);return m.concat(n.data,n.tag)},decrypt:function(t,s,r,q,p){p=p||64;q=q||[];var o=sjcl.bitArray,n=o.bitLength(r)/8,m=o.bitLength(s),l=o.clamp(s,m-p),j=o.bitSlice(s,m-p);m=(m-p)/8;if(n<7){throw new sjcl.exception.invalid("ccm: iv must be at least 7 bytes")}for(s=2;s<4&&m>>>8*s;s++){}if(s<15-n){s=15-n}r=o.clamp(r,8*(15-s));l=sjcl.mode.ccm.I(t,l,r,j,p,s);t=sjcl.mode.ccm.G(t,l.data,r,q,p,s);if(!o.equal(l.tag,t)){throw new sjcl.exception.corrupt("ccm: tag doesn't match")}return l.data},G:function(r,q,p,o,n,m){var l=[],k=sjcl.bitArray,j=k.k;n/=8;if(n%2||n<4||n>16){throw new sjcl.exception.invalid("ccm: invalid tag length")}if(o.length>4294967295||q.length>4294967295){throw new sjcl.exception.bug("ccm: can't deal with 4GiB or more data")}m=[k.partial(8,(o.length?64:0)|n-2<<2|m-1)];m=k.concat(m,p);m[3]|=k.bitLength(q)/8;m=r.encrypt(m);if(o.length){p=k.bitLength(o)/8;if(p<=65279){l=[k.partial(16,p)]}else{if(p<=4294967295){l=k.concat([k.partial(16,65534)],[p])}}l=k.concat(l,o);for(o=0;o<l.length;o+=4){m=r.encrypt(j(m,l.slice(o,o+4)))}}for(o=0;o<q.length;o+=4){m=r.encrypt(j(m,q.slice(o,o+4)))}return k.clamp(m,n*8)},I:function(t,s,r,q,p,o){var n,m=sjcl.bitArray;n=m.k;var l=s.length,j=m.bitLength(s);r=m.concat([m.partial(8,o-1)],r).concat([0,0,0]).slice(0,4);q=m.bitSlice(n(q,t.encrypt(r)),0,p);if(!l){return{tag:q,data:[]}}for(n=0;n<l;n+=4){r[3]++;p=t.encrypt(r);s[n]^=p[0];s[n+1]^=p[1];s[n+2]^=p[2];s[n+3]^=p[3]}return{tag:q,data:m.clamp(s,j)}}};sjcl.mode.ocb2={name:"ocb2",encrypt:function(z,y,x,w,v,u){if(sjcl.bitArray.bitLength(x)!==128){throw new sjcl.exception.invalid("ocb iv must be 128 bits")}var t,s=sjcl.mode.ocb2.A,r=sjcl.bitArray,p=r.k,q=[0,0,0,0];x=s(z.encrypt(x));var o,n=[];w=w||[];v=v||64;for(t=0;t+4<y.length;t+=4){o=y.slice(t,t+4);q=p(q,o);n=n.concat(p(x,z.encrypt(p(x,o))));x=s(x)}o=y.slice(t);y=r.bitLength(o);t=z.encrypt(p(x,[0,0,0,y]));o=r.clamp(p(o,t),y);q=p(q,p(o,t));q=z.encrypt(p(q,p(x,s(x))));if(w.length){q=p(q,u?w:sjcl.mode.ocb2.pmac(z,w))}return n.concat(r.concat(o,r.clamp(q,v)))},decrypt:function(D,C,B,A,z,y){if(sjcl.bitArray.bitLength(B)!==128){throw new sjcl.exception.invalid("ocb iv must be 128 bits")}z=z||64;var x=sjcl.mode.ocb2.A,w=sjcl.bitArray,v=w.k,t=[0,0,0,0],u=x(D.encrypt(B)),s,r,q=sjcl.bitArray.bitLength(C)-z,p=[];A=A||[];for(B=0;B+4<q/32;B+=4){s=v(u,D.decrypt(v(u,C.slice(B,B+4))));t=v(t,s);p=p.concat(s);u=x(u)}r=q-B*32;s=D.encrypt(v(u,[0,0,0,r]));s=v(s,w.clamp(C.slice(B),r));t=v(t,s);t=D.encrypt(v(t,v(u,x(u))));if(A.length){t=v(t,y?A:sjcl.mode.ocb2.pmac(D,A))}if(!w.equal(w.clamp(t,z),w.bitSlice(C,q))){throw new sjcl.exception.corrupt("ocb: tag doesn't match")}return p.concat(w.clamp(s,r))},pmac:function(j,i){var p,o=sjcl.mode.ocb2.A,n=sjcl.bitArray,m=n.k,l=[0,0,0,0],k=j.encrypt([0,0,0,0]);k=m(k,o(o(k)));for(p=0;p+4<i.length;p+=4){k=o(k);l=m(l,j.encrypt(m(k,i.slice(p,p+4))))}i=i.slice(p);if(n.bitLength(i)<128){k=m(k,o(k));i=n.concat(i,[2147483648|0])}l=m(l,i);return j.encrypt(m(o(m(k,o(k))),l))},A:function(b){return[b[0]<<1^b[1]>>>31,b[1]<<1^b[2]>>>31,b[2]<<1^b[3]>>>31,b[3]<<1^(b[0]>>>31)*135]}};sjcl.misc.hmac=function(f,e){this.M=e=e||sjcl.hash.sha256;var h=[[],[]],g=e.prototype.blockSize/32;this.l=[new e,new e];if(f.length>g){f=e.hash(f)}for(e=0;e<g;e++){h[0][e]=f[e]^909522486;h[1][e]=f[e]^1549556828}this.l[0].update(h[0]);this.l[1].update(h[1])};sjcl.misc.hmac.prototype.encrypt=sjcl.misc.hmac.prototype.mac=function(d,c){d=(new this.M(this.l[0])).update(d,c).finalize();return(new this.M(this.l[1])).update(d).finalize()};sjcl.misc.pbkdf2=function(v,u,t,s,r){t=t||1000;if(s<0||t<0){throw sjcl.exception.invalid("invalid params to pbkdf2")}if(typeof v==="string"){v=sjcl.codec.utf8String.toBits(v)}r=r||sjcl.misc.hmac;v=new r(v);var q,p,o,n,l=[],m=sjcl.bitArray;for(n=1;32*l.length<(s||1);n++){r=q=v.encrypt(m.concat(u,[n]));for(p=1;p<t;p++){q=v.encrypt(q);for(o=0;o<q.length;o++){r[o]^=q[o]}}l=l.concat(r)}if(s){l=m.clamp(l,s)}return l};sjcl.random={randomWords:function(f,e){var h=[];e=this.isReady(e);var g;if(e===0){throw new sjcl.exception.notready("generator isn't seeded")}else{e&2&&this.U(!(e&1))}for(e=0;e<f;e+=4){(e+1)%65536===0&&this.L();g=this.u();h.push(g[0],g[1],g[2],g[3])}this.L();return h.slice(0,f)},setDefaultParanoia:function(b){this.t=b},addEntropy:function(j,i,p){p=p||"user";var o,n,m=(new Date).valueOf(),l=this.q[p],k=this.isReady();o=this.F[p];if(o===undefined){o=this.F[p]=this.R++}if(l===undefined){l=this.q[p]=0}this.q[p]=(this.q[p]+1)%this.b.length;switch(typeof j){case"number":break;case"object":if(i===undefined){for(p=i=0;p<j.length;p++){for(n=j[p];n>0;){i++;n>>>=1}}}this.b[l].update([o,this.J++,2,i,m,j.length].concat(j));break;case"string":if(i===undefined){i=j.length}this.b[l].update([o,this.J++,3,i,m,j.length]);this.b[l].update(j);break;default:throw new sjcl.exception.bug("random: addEntropy only supports number, array or string")}this.j[l]+=i;this.f+=i;if(k===0){this.isReady()!==0&&this.K("seeded",Math.max(this.g,this.f));this.K("progress",this.getProgress())}},isReady:function(b){b=this.B[b!==undefined?b:this.t];return this.g&&this.g>=b?this.j[0]>80&&(new Date).valueOf()>this.O?3:1:this.f>=b?2:0},getProgress:function(b){b=this.B[b?b:this.t];return this.g>=b?1["0"]:this.f>b?1["0"]:this.f/b},startCollectors:function(){if(!this.m){if(window.addEventListener){window.addEventListener("load",this.o,false);window.addEventListener("mousemove",this.p,false)}else{if(document.attachEvent){document.attachEvent("onload",this.o);document.attachEvent("onmousemove",this.p)}else{throw new sjcl.exception.bug("can't attach event")}}this.m=true}},stopCollectors:function(){if(this.m){if(window.removeEventListener){window.removeEventListener("load",this.o);window.removeEventListener("mousemove",this.p)}else{if(window.detachEvent){window.detachEvent("onload",this.o);window.detachEvent("onmousemove",this.p)}}this.m=false}},addEventListener:function(d,c){this.r[d][this.Q++]=c},removeEventListener:function(f,e){var h;f=this.r[f];var g=[];for(h in f){f.hasOwnProperty[h]&&f[h]===e&&g.push(h)}for(e=0;e<g.length;e++){h=g[e];delete f[h]}},b:[new sjcl.hash.sha256],j:[0],z:0,q:{},J:0,F:{},R:0,g:0,f:0,O:0,a:[0,0,0,0,0,0,0,0],d:[0,0,0,0],s:undefined,t:6,m:false,r:{progress:{},seeded:{}},Q:0,B:[0,48,64,96,128,192,256,384,512,768,1024],u:function(){for(var b=0;b<4;b++){this.d[b]=this.d[b]+1|0;if(this.d[b]){break}}return this.s.encrypt(this.d)},L:function(){this.a=this.u().concat(this.u());this.s=new sjcl.cipher.aes(this.a)},T:function(b){this.a=sjcl.hash.sha256.hash(this.a.concat(b));this.s=new sjcl.cipher.aes(this.a);for(b=0;b<4;b++){this.d[b]=this.d[b]+1|0;if(this.d[b]){break}}},U:function(f){var e=[],h=0,g;this.O=e[0]=(new Date).valueOf()+30000;for(g=0;g<16;g++){e.push(Math.random()*4294967296|0)}for(g=0;g<this.b.length;g++){e=e.concat(this.b[g].finalize());h+=this.j[g];this.j[g]=0;if(!f&&this.z&1<<g){break}}if(this.z>=1<<this.b.length){this.b.push(new sjcl.hash.sha256);this.j.push(0)}this.f-=h;if(h>this.g){this.g=h}this.z++;this.T(e)},p:function(b){sjcl.random.addEntropy([b.x||b.clientX||b.offsetX,b.y||b.clientY||b.offsetY],2,"mouse")},o:function(){sjcl.random.addEntropy(new Date,2,"loadtime")},K:function(f,e){var h;f=sjcl.random.r[f];var g=[];for(h in f){f.hasOwnProperty(h)&&g.push(f[h])}for(h=0;h<g.length;h++){g[h](e)}}};sjcl.json={defaults:{v:1,iter:1000,ks:128,ts:64,mode:"ccm",adata:"",cipher:"aes"},encrypt:function(h,g,l,k){l=l||{};k=k||{};var j=sjcl.json,i=j.c({iv:sjcl.random.randomWords(4,0)},j.defaults);j.c(i,l);if(typeof i.salt==="string"){i.salt=sjcl.codec.base64.toBits(i.salt)}if(typeof i.iv==="string"){i.iv=sjcl.codec.base64.toBits(i.iv)}if(!sjcl.mode[i.mode]||!sjcl.cipher[i.cipher]||typeof h==="string"&&i.iter<=100||i.ts!==64&&i.ts!==96&&i.ts!==128||i.ks!==128&&i.ks!==192&&i.ks!==256||i.iv.length<2||i.iv.length>4){throw new sjcl.exception.invalid("json encrypt: invalid parameters")}if(typeof h==="string"){l=sjcl.misc.cachedPbkdf2(h,i);h=l.key.slice(0,i.ks/32);i.salt=l.salt}if(typeof g==="string"){g=sjcl.codec.utf8String.toBits(g)}l=new sjcl.cipher[i.cipher](h);j.c(k,i);k.key=h;i.ct=sjcl.mode[i.mode].encrypt(l,g,i.iv,i.adata,i.tag);return j.encode(j.V(i,j.defaults))},decrypt:function(g,f,j,i){j=j||{};i=i||{};var h=sjcl.json;f=h.c(h.c(h.c({},h.defaults),h.decode(f)),j,true);if(typeof f.salt==="string"){f.salt=sjcl.codec.base64.toBits(f.salt)}if(typeof f.iv==="string"){f.iv=sjcl.codec.base64.toBits(f.iv)}if(!sjcl.mode[f.mode]||!sjcl.cipher[f.cipher]||typeof g==="string"&&f.iter<=100||f.ts!==64&&f.ts!==96&&f.ts!==128||f.ks!==128&&f.ks!==192&&f.ks!==256||!f.iv||f.iv.length<2||f.iv.length>4){throw new sjcl.exception.invalid("json decrypt: invalid parameters")}if(typeof g==="string"){j=sjcl.misc.cachedPbkdf2(g,f);g=j.key.slice(0,f.ks/32);f.salt=j.salt}j=new sjcl.cipher[f.cipher](g);j=sjcl.mode[f.mode].decrypt(j,f.ct,f.iv,f.adata,f.tag);h.c(i,f);i.key=g;return sjcl.codec.utf8String.fromBits(j)},encode:function(f){var e,h="{",g="";for(e in f){if(f.hasOwnProperty(e)){if(!e.match(/^[a-z0-9]+$/i)){throw new sjcl.exception.invalid("json encode: invalid property name")}h+=g+e+":";g=",";switch(typeof f[e]){case"number":case"boolean":h+=f[e];break;case"string":h+='"'+escape(f[e])+'"';break;case"object":h+='"'+sjcl.codec.base64.fromBits(f[e],1)+'"';break;default:throw new sjcl.exception.bug("json encode: unsupported type")}}}return h+"}"},decode:function(f){f=f.replace(/\s/g,"");if(!f.match(/^\{.*\}$/)){throw new sjcl.exception.invalid("json decode: this isn't json!")}f=f.replace(/^\{|\}$/g,"").split(/,/);var e={},h,g;for(h=0;h<f.length;h++){if(!(g=f[h].match(/^([a-z][a-z0-9]*):(?:(\d+)|"([a-z0-9+\/%*_.@=\-]*)")$/i))){throw new sjcl.exception.invalid("json decode: this isn't json!")}e[g[1]]=g[2]?parseInt(g[2],10):g[1].match(/^(ct|salt|iv)$/)?sjcl.codec.base64.toBits(g[3]):unescape(g[3])}return e},c:function(f,e,h){if(f===undefined){f={}}if(e===undefined){return f}var g;for(g in e){if(e.hasOwnProperty(g)){if(h&&f[g]!==undefined&&f[g]!==e[g]){throw new sjcl.exception.invalid("required parameter overridden")}f[g]=e[g]}}return f},V:function(f,e){var h={},g;for(g in f){if(f.hasOwnProperty(g)&&f[g]!==e[g]){h[g]=f[g]}}return h},W:function(f,e){var h={},g;for(g=0;g<e.length;g++){if(f[e[g]]!==undefined){h[e[g]]=f[e[g]]}}return h}};sjcl.encrypt=sjcl.json.encrypt;sjcl.decrypt=sjcl.json.decrypt;sjcl.misc.S={};sjcl.misc.cachedPbkdf2=function(f,e){var h=sjcl.misc.S,g;e=e||{};g=e.iter||1000;h=h[f]=h[f]||{};g=h[g]=h[g]||{firstSalt:e.salt&&e.salt.length?e.salt.slice(0):sjcl.random.randomWords(2,0)};h=e.salt===undefined?g.firstSalt:e.salt;g[h]=g[h]||sjcl.misc.pbkdf2(f,h,e.iter);return{key:g[h].slice(0),salt:h.slice(0)}};
//
// asn1hex.js - Hexadecimal represented ASN.1 string library
//
//
// version: 1.0 (2010-Jun-03)
//
// Copyright (c) 2010 Kenji Urushima ([email protected])
//
// This software is licensed under the terms of the MIT License.
// http://www.opensource.org/licenses/mit-license.php
//
// The above copyright and license notice shall be
// included in all copies or substantial portions of the Software.
//
//
// Depends on:
//
// MEMO:
// f('3082025b02...', 2) ... 82025b ... 3bytes
// f('020100', 2) ... 01 ... 1byte
// f('0203001...', 2) ... 03 ... 1byte
// f('02818003...', 2) ... 8180 ... 2bytes
// f('3080....0000', 2) ... 80 ... -1
//
// Requirements:
// - ASN.1 type octet length MUST be 1.
// (i.e. ASN.1 primitives like SET, SEQUENCE, INTEGER, OCTETSTRING ...)
// -
function _asnhex_getByteLengthOfL_AtObj(b,c){if(b.substring(c+2,c+3)!="8"){return 1}var a=parseInt(b.substring(c+3,c+4));if(a==0){return -1}if(0<a&&a<10){return a+1}return -2}function _asnhex_getHexOfL_AtObj(b,c){var a=_asnhex_getByteLengthOfL_AtObj(b,c);if(a<1){return""}return b.substring(c+2,c+2+a*2)}function _asnhex_getIntOfL_AtObj(c,d){var b=_asnhex_getHexOfL_AtObj(c,d);if(b==""){return -1}var a;if(parseInt(b.substring(0,1))<8){a=parseBigInt(b,16)}else{a=parseBigInt(b.substring(2),16)}return a.intValue()}function _asnhex_getStartPosOfV_AtObj(b,c){var a=_asnhex_getByteLengthOfL_AtObj(b,c);if(a<0){return a}return c+(a+1)*2}function _asnhex_getHexOfV_AtObj(c,d){var b=_asnhex_getStartPosOfV_AtObj(c,d);var a=_asnhex_getIntOfL_AtObj(c,d);return c.substring(b,b+a*2)}function _asnhex_getPosOfNextSibling_AtObj(c,d){var b=_asnhex_getStartPosOfV_AtObj(c,d);var a=_asnhex_getIntOfL_AtObj(c,d);return b+a*2}function _asnhex_getPosArrayOfChildren_AtObj(f,j){var c=new Array();var i=_asnhex_getStartPosOfV_AtObj(f,j);c.push(i);var b=_asnhex_getIntOfL_AtObj(f,j);var g=i;var d=0;while(1){var e=_asnhex_getPosOfNextSibling_AtObj(f,g);if(e==null||(e-i>=(b*2))){break}if(d>=200){break}c.push(e);g=e;d++}return c}
//
// rsa-pem.js - adding function for reading/writing PKCS#1 PEM private key
// to RSAKey class.
//
// version: 1.0 (2010-Jun-03)
//
// Copyright (c) 2010 Kenji Urushima ([email protected])
//
// This software is licensed under the terms of the MIT License.
// http://www.opensource.org/licenses/mit-license.php
//
// The above copyright and license notice shall be
// included in all copies or substantial portions of the Software.
//
//
// Depends on:
//
//
//
// _RSApem_pemToBase64(sPEM)
//
// removing PEM header, PEM footer and space characters including
// new lines from PEM formatted RSA private key string.
//
function _rsapem_pemToBase64(b){var a=b;a=a.replace("-----BEGIN RSA PRIVATE KEY-----","");a=a.replace("-----END RSA PRIVATE KEY-----","");a=a.replace(/[ \n]+/g,"");return a}function _rsapem_getPosArrayOfChildrenFromHex(d){var j=new Array();var k=_asnhex_getStartPosOfV_AtObj(d,0);var f=_asnhex_getPosOfNextSibling_AtObj(d,k);var h=_asnhex_getPosOfNextSibling_AtObj(d,f);var b=_asnhex_getPosOfNextSibling_AtObj(d,h);var l=_asnhex_getPosOfNextSibling_AtObj(d,b);var e=_asnhex_getPosOfNextSibling_AtObj(d,l);var g=_asnhex_getPosOfNextSibling_AtObj(d,e);var c=_asnhex_getPosOfNextSibling_AtObj(d,g);var i=_asnhex_getPosOfNextSibling_AtObj(d,c);j.push(k,f,h,b,l,e,g,c,i);return j}function _rsapem_getHexValueArrayOfChildrenFromHex(i){var o=_rsapem_getPosArrayOfChildrenFromHex(i);var r=_asnhex_getHexOfV_AtObj(i,o[0]);var f=_asnhex_getHexOfV_AtObj(i,o[1]);var j=_asnhex_getHexOfV_AtObj(i,o[2]);var k=_asnhex_getHexOfV_AtObj(i,o[3]);var c=_asnhex_getHexOfV_AtObj(i,o[4]);var b=_asnhex_getHexOfV_AtObj(i,o[5]);var h=_asnhex_getHexOfV_AtObj(i,o[6]);var g=_asnhex_getHexOfV_AtObj(i,o[7]);var l=_asnhex_getHexOfV_AtObj(i,o[8]);var m=new Array();m.push(r,f,j,k,c,b,h,g,l);return m}function _rsapem_readPrivateKeyFromPEMString(e){var c=_rsapem_pemToBase64(e);var d=b64tohex(c);var b=_rsapem_getHexValueArrayOfChildrenFromHex(d);this.setPrivateEx(b[1],b[2],b[3],b[4],b[5],b[6],b[7],b[8])}RSAKey.prototype.readPrivateKeyFromPEMString=_rsapem_readPrivateKeyFromPEMString;
//
// rsa-sign.js - adding signing functions to RSAKey class.
//
//
// version: 1.0 (2010-Jun-03)
//
// Copyright (c) 2010 Kenji Urushima ([email protected])
//
// This software is licensed under the terms of the MIT License.
// http://www.opensource.org/licenses/mit-license.php
//
// The above copyright and license notice shall be
// included in all copies or substantial portions of the Software.
//
// Depends on:
// function sha1.hex(s) of sha1.js
// jsbn.js
// jsbn2.js
// rsa.js
// rsa2.js
//
// keysize / pmstrlen
// 512 / 128
// 1024 / 256
// 2048 / 512
// 4096 / 1024
// As for _RSASGIN_DIHEAD values for each hash algorithm, see PKCS#1 v2.1 spec (p38).
var _RSASIGN_DIHEAD=[];_RSASIGN_DIHEAD.sha1="3021300906052b0e03021a05000414";_RSASIGN_DIHEAD.sha256="3031300d060960864801650304020105000420";var _RSASIGN_HASHHEXFUNC=[];_RSASIGN_HASHHEXFUNC.sha256=function(a){return sjcl.codec.hex.fromBits(sjcl.hash.sha256.hash(a))};function _rsasign_getHexPaddedDigestInfoForString(m,g,b){var d=g/4;var j=_RSASIGN_HASHHEXFUNC[b];var e=j(m);var a="0001";var k="00"+_RSASIGN_DIHEAD[b]+e;var h="";var l=d-a.length-k.length;for(var f=0;f<l;f+=2){h+="ff"}var c=a+h+k;return c}function _rsasign_signString(e,c){var f=_rsasign_getHexPaddedDigestInfoForString(e,this.n.bitLength(),c);var b=parseBigInt(f,16);var d=this.doPrivate(b);var a=d.toString(16);return a}function _rsasign_signStringWithSHA1(d){var e=_rsasign_getHexPaddedDigestInfoForString(d,this.n.bitLength(),"sha1");var b=parseBigInt(e,16);var c=this.doPrivate(b);var a=c.toString(16);return a}function _rsasign_signStringWithSHA256(d){var e=_rsasign_getHexPaddedDigestInfoForString(d,this.n.bitLength(),"sha256");var b=parseBigInt(e,16);var c=this.doPrivate(b);var a=c.toString(16);return a}function _rsasign_getDecryptSignatureBI(a,d,c){var b=new RSAKey();b.setPublic(d,c);var e=b.doPublic(a);return e}function _rsasign_getHexDigestInfoFromSig(a,c,b){var e=_rsasign_getDecryptSignatureBI(a,c,b);var d=e.toString(16).replace(/^1f+00/,"");return d}function _rsasign_getAlgNameAndHashFromHexDisgestInfo(f){for(var e in _RSASIGN_DIHEAD){var d=_RSASIGN_DIHEAD[e];var b=d.length;if(f.substring(0,b)==d){var c=[e,f.substring(b)];return c}}return[]}function _rsasign_verifySignatureWithArgs(f,b,g,j){var e=_rsasign_getHexDigestInfoFromSig(b,g,j);var h=_rsasign_getAlgNameAndHashFromHexDisgestInfo(e);if(h.length==0){return false}var d=h[0];var i=h[1];var a=_RSASIGN_HASHHEXFUNC[d];var c=a(f);return(i==c)}function _rsasign_verifyHexSignatureForMessage(c,b){var d=parseBigInt(c,16);var a=_rsasign_verifySignatureWithArgs(b,d,this.n.toString(16),this.e.toString(16));return a}function _rsasign_verifyString(f,j){j=j.replace(/[ \n]+/g,"");var b=parseBigInt(j,16);var i=this.doPublic(b);var e=i.toString(16).replace(/^1f+00/,"");var g=_rsasign_getAlgNameAndHashFromHexDisgestInfo(e);if(g.length==0){return false}var d=g[0];var h=g[1];var a=_RSASIGN_HASHHEXFUNC[d];var c=a(f);return(h==c)}RSAKey.prototype.signString=_rsasign_signString;RSAKey.prototype.signStringWithSHA1=_rsasign_signStringWithSHA1;RSAKey.prototype.signStringWithSHA256=_rsasign_signStringWithSHA256;RSAKey.prototype.verifyString=_rsasign_verifyString;RSAKey.prototype.verifyHexSignatureForMessage=_rsasign_verifyHexSignatureForMessage;
//
// x509.js - X509 class to read subject public key from certificate.
//
// version: 1.0 (2010-Jun-03)
//
// Copyright (c) 2010 Kenji Urushima ([email protected])
//
// This software is licensed under the terms of the MIT License.
// http://www.opensource.org/licenses/mit-license.php
//
// The above copyright and license notice shall be
// included in all copies or substantial portions of the Software.
//
// Depends:
// base64.js
// rsa.js
function _x509_pemToBase64(a){var b=a;b=b.replace("-----BEGIN CERTIFICATE-----","");b=b.replace("-----END CERTIFICATE-----","");b=b.replace(/[ \n]+/g,"");return b}function _x509_pemToHex(a){var c=_x509_pemToBase64(a);var b=b64tohex(c);return b}function _x509_getHexTbsCertificateFromCert(b){var a=_asnhex_getStartPosOfV_AtObj(b,0);return a}function _x509_getSubjectPublicKeyInfoPosFromCertHex(d){var c=_asnhex_getStartPosOfV_AtObj(d,0);var b=_asnhex_getPosArrayOfChildren_AtObj(d,c);if(b.length<1){return -1}if(d.substring(b[0],b[0]+10)=="a003020102"){if(b.length<6){return -1}return b[6]}else{if(b.length<5){return -1}return b[5]}}function _x509_getSubjectPublicKeyPosFromCertHex(f){var e=_x509_getSubjectPublicKeyInfoPosFromCertHex(f);if(e==-1){return -1}var b=_asnhex_getPosArrayOfChildren_AtObj(f,e);if(b.length!=2){return -1}var d=b[1];if(f.substring(d,d+2)!="03"){return -1}var c=_asnhex_getStartPosOfV_AtObj(f,d);if(f.substring(c,c+2)!="00"){return -1}return c+2}function _x509_getPublicKeyHexArrayFromCertHex(f){var e=_x509_getSubjectPublicKeyPosFromCertHex(f);var b=_asnhex_getPosArrayOfChildren_AtObj(f,e);if(b.length!=2){return[]}var d=_asnhex_getHexOfV_AtObj(f,b[0]);var c=_asnhex_getHexOfV_AtObj(f,b[1]);if(d!=null&&c!=null){return[d,c]}else{return[]}}function _x509_getPublicKeyHexArrayFromCertPEM(c){var d=_x509_pemToHex(c);var b=_x509_getPublicKeyHexArrayFromCertHex(d);return b}function _x509_readCertPEM(c){var e=_x509_pemToHex(c);var b=_x509_getPublicKeyHexArrayFromCertHex(e);var d=new RSAKey();d.setPublic(b[0],b[1]);this.subjectPublicKeyRSA=d;this.subjectPublicKeyRSA_hN=b[0];this.subjectPublicKeyRSA_hE=b[1]}function _x509_readCertPEMWithoutRSAInit(c){var d=_x509_pemToHex(c);var b=_x509_getPublicKeyHexArrayFromCertHex(d);this.subjectPublicKeyRSA.setPublic(b[0],b[1]);this.subjectPublicKeyRSA_hN=b[0];this.subjectPublicKeyRSA_hE=b[1]}function X509(){this.subjectPublicKeyRSA=null;this.subjectPublicKeyRSA_hN=null;this.subjectPublicKeyRSA_hE=null}X509.prototype.readCertPEM=_x509_readCertPEM;X509.prototype.readCertPEMWithoutRSAInit=_x509_readCertPEMWithoutRSAInit;
var b64map="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";var b64pad="=";function hex2b64(d){var b;var e;var a="";for(b=0;b+3<=d.length;b+=3){e=parseInt(d.substring(b,b+3),16);a+=b64map.charAt(e>>6)+b64map.charAt(e&63)}if(b+1==d.length){e=parseInt(d.substring(b,b+1),16);a+=b64map.charAt(e<<2)}else{if(b+2==d.length){e=parseInt(d.substring(b,b+2),16);a+=b64map.charAt(e>>2)+b64map.charAt((e&3)<<4)}}while((a.length&3)>0){a+=b64pad}return a}function b64tohex(f){var d="";var e;var b=0;var c;for(e=0;e<f.length;++e){if(f.charAt(e)==b64pad){break}var a=b64map.indexOf(f.charAt(e));if(a<0){continue}if(b==0){d+=int2char(a>>2);c=a&3;b=1}else{if(b==1){d+=int2char((c<<2)|(a>>4));c=a&15;b=2}else{if(b==2){d+=int2char(c);d+=int2char(a>>2);c=a&3;b=3}else{d+=int2char((c<<2)|(a>>4));d+=int2char(a&15);b=0}}}}if(b==1){d+=int2char(c<<2)}return d}function b64toBA(e){var d=b64tohex(e);var c;var b=new Array();for(c=0;2*c<d.length;++c){b[c]=parseInt(d.substring(2*c,2*c+2),16)}return b};
// Copyright (c) 2005 Tom Wu
// All Rights Reserved.
// See "LICENSE" for details.
// Basic JavaScript BN library - subset useful for RSA encryption.
var dbits;var canary=244837814094590;var j_lm=((canary&16777215)==15715070);function BigInteger(e,d,f){if(e!=null){if("number"==typeof e){this.fromNumber(e,d,f)}else{if(d==null&&"string"!=typeof e){this.fromString(e,256)}else{this.fromString(e,d)}}}}function nbi(){return new BigInteger(null)}function am1(f,a,b,e,h,g){while(--g>=0){var d=a*this[f++]+b[e]+h;h=Math.floor(d/67108864);b[e++]=d&67108863}return h}function am2(f,q,r,e,o,a){var k=q&32767,p=q>>15;while(--a>=0){var d=this[f]&32767;var g=this[f++]>>15;var b=p*d+g*k;d=k*d+((b&32767)<<15)+r[e]+(o&1073741823);o=(d>>>30)+(b>>>15)+p*g+(o>>>30);r[e++]=d&1073741823}return o}function am3(f,q,r,e,o,a){var k=q&16383,p=q>>14;while(--a>=0){var d=this[f]&16383;var g=this[f++]>>14;var b=p*d+g*k;d=k*d+((b&16383)<<14)+r[e]+o;o=(d>>28)+(b>>14)+p*g;r[e++]=d&268435455}return o}BigInteger.prototype.am=am2;dbits=30;BigInteger.prototype.DB=dbits;BigInteger.prototype.DM=((1<<dbits)-1);BigInteger.prototype.DV=(1<<dbits);var BI_FP=52;BigInteger.prototype.FV=Math.pow(2,BI_FP);BigInteger.prototype.F1=BI_FP-dbits;BigInteger.prototype.F2=2*dbits-BI_FP;var BI_RM="0123456789abcdefghijklmnopqrstuvwxyz";var BI_RC=new Array();var rr,vv;rr="0".charCodeAt(0);for(vv=0;vv<=9;++vv){BI_RC[rr++]=vv}rr="a".charCodeAt(0);for(vv=10;vv<36;++vv){BI_RC[rr++]=vv}rr="A".charCodeAt(0);for(vv=10;vv<36;++vv){BI_RC[rr++]=vv}function int2char(a){return BI_RM.charAt(a)}function intAt(b,a){var d=BI_RC[b.charCodeAt(a)];return(d==null)?-1:d}function bnpCopyTo(b){for(var a=this.t-1;a>=0;--a){b[a]=this[a]}b.t=this.t;b.s=this.s}function bnpFromInt(a){this.t=1;this.s=(a<0)?-1:0;if(a>0){this[0]=a}else{if(a<-1){this[0]=a+DV}else{this.t=0}}}function nbv(a){var b=nbi();b.fromInt(a);return b}function bnpFromString(h,c){var e;if(c==16){e=4}else{if(c==8){e=3}else{if(c==256){e=8}else{if(c==2){e=1}else{if(c==32){e=5}else{if(c==4){e=2}else{this.fromRadix(h,c);return}}}}}}this.t=0;this.s=0;var g=h.length,d=false,f=0;while(--g>=0){var a=(e==8)?h[g]&255:intAt(h,g);if(a<0){if(h.charAt(g)=="-"){d=true}continue}d=false;if(f==0){this[this.t++]=a}else{if(f+e>this.DB){this[this.t-1]|=(a&((1<<(this.DB-f))-1))<<f;this[this.t++]=(a>>(this.DB-f))}else{this[this.t-1]|=a<<f}}f+=e;if(f>=this.DB){f-=this.DB}}if(e==8&&(h[0]&128)!=0){this.s=-1;if(f>0){this[this.t-1]|=((1<<(this.DB-f))-1)<<f}}this.clamp();if(d){BigInteger.ZERO.subTo(this,this)}}function bnpClamp(){var a=this.s&this.DM;while(this.t>0&&this[this.t-1]==a){--this.t}}function bnToString(c){if(this.s<0){return"-"+this.negate().toString(c)}var e;if(c==16){e=4}else{if(c==8){e=3}else{if(c==2){e=1}else{if(c==32){e=5}else{if(c==4){e=2}else{return this.toRadix(c)}}}}}var g=(1<<e)-1,l,a=false,h="",f=this.t;var j=this.DB-(f*this.DB)%e;if(f-->0){if(j<this.DB&&(l=this[f]>>j)>0){a=true;h=int2char(l)}while(f>=0){if(j<e){l=(this[f]&((1<<j)-1))<<(e-j);l|=this[--f]>>(j+=this.DB-e)}else{l=(this[f]>>(j-=e))&g;if(j<=0){j+=this.DB;--f}}if(l>0){a=true}if(a){h+=int2char(l)}}}return a?h:"0"}function bnNegate(){var a=nbi();BigInteger.ZERO.subTo(this,a);return a}function bnAbs(){return(this.s<0)?this.negate():this}function bnCompareTo(b){var d=this.s-b.s;if(d!=0){return d}var c=this.t;d=c-b.t;if(d!=0){return d}while(--c>=0){if((d=this[c]-b[c])!=0){return d}}return 0}function nbits(a){var c=1,b;if((b=a>>>16)!=0){a=b;c+=16}if((b=a>>8)!=0){a=b;c+=8}if((b=a>>4)!=0){a=b;c+=4}if((b=a>>2)!=0){a=b;c+=2}if((b=a>>1)!=0){a=b;c+=1}return c}function bnBitLength(){if(this.t<=0){return 0}return this.DB*(this.t-1)+nbits(this[this.t-1]^(this.s&this.DM))}function bnpDLShiftTo(c,b){var a;for(a=this.t-1;a>=0;--a){b[a+c]=this[a]}for(a=c-1;a>=0;--a){b[a]=0}b.t=this.t+c;b.s=this.s}function bnpDRShiftTo(c,b){for(var a=c;a<this.t;++a){b[a-c]=this[a]}b.t=Math.max(this.t-c,0);b.s=this.s}function bnpLShiftTo(j,e){var b=j%this.DB;var a=this.DB-b;var g=(1<<a)-1;var f=Math.floor(j/this.DB),h=(this.s<<b)&this.DM,d;for(d=this.t-1;d>=0;--d){e[d+f+1]=(this[d]>>a)|h;h=(this[d]&g)<<b}for(d=f-1;d>=0;--d){e[d]=0}e[f]=h;e.t=this.t+f+1;e.s=this.s;e.clamp()}function bnpRShiftTo(g,d){d.s=this.s;var e=Math.floor(g/this.DB);if(e>=this.t){d.t=0;return}var b=g%this.DB;var a=this.DB-b;var f=(1<<b)-1;d[0]=this[e]>>b;for(var c=e+1;c<this.t;++c){d[c-e-1]|=(this[c]&f)<<a;d[c-e]=this[c]>>b}if(b>0){d[this.t-e-1]|=(this.s&f)<<a}d.t=this.t-e;d.clamp()}function bnpSubTo(d,f){var e=0,g=0,b=Math.min(d.t,this.t);while(e<b){g+=this[e]-d[e];f[e++]=g&this.DM;g>>=this.DB}if(d.t<this.t){g-=d.s;while(e<this.t){g+=this[e];f[e++]=g&this.DM;g>>=this.DB}g+=this.s}else{g+=this.s;while(e<d.t){g-=d[e];f[e++]=g&this.DM;g>>=this.DB}g-=d.s}f.s=(g<0)?-1:0;if(g<-1){f[e++]=this.DV+g}else{if(g>0){f[e++]=g}}f.t=e;f.clamp()}function bnpMultiplyTo(c,e){var b=this.abs(),f=c.abs();var d=b.t;e.t=d+f.t;while(--d>=0){e[d]=0}for(d=0;d<f.t;++d){e[d+b.t]=b.am(0,f[d],e,d,0,b.t)}e.s=0;e.clamp();if(this.s!=c.s){BigInteger.ZERO.subTo(e,e)}}function bnpSquareTo(d){var a=this.abs();var b=d.t=2*a.t;while(--b>=0){d[b]=0}for(b=0;b<a.t-1;++b){var e=a.am(b,a[b],d,2*b,0,1);if((d[b+a.t]+=a.am(b+1,2*a[b],d,2*b+1,e,a.t-b-1))>=a.DV){d[b+a.t]-=a.DV;d[b+a.t+1]=1}}if(d.t>0){d[d.t-1]+=a.am(b,a[b],d,2*b,0,1)}d.s=0;d.clamp()}function bnpDivRemTo(n,h,g){var w=n.abs();if(w.t<=0){return}var k=this.abs();if(k.t<w.t){if(h!=null){h.fromInt(0)}if(g!=null){this.copyTo(g)}return}if(g==null){g=nbi()}var d=nbi(),a=this.s,l=n.s;var v=this.DB-nbits(w[w.t-1]);if(v>0){w.lShiftTo(v,d);k.lShiftTo(v,g)}else{w.copyTo(d);k.copyTo(g)}var p=d.t;var b=d[p-1];if(b==0){return}var o=b*(1<<this.F1)+((p>1)?d[p-2]>>this.F2:0);var A=this.FV/o,z=(1<<this.F1)/o,x=1<<this.F2;var u=g.t,s=u-p,f=(h==null)?nbi():h;d.dlShiftTo(s,f);if(g.compareTo(f)>=0){g[g.t++]=1;g.subTo(f,g)}BigInteger.ONE.dlShiftTo(p,f);f.subTo(d,d);while(d.t<p){d[d.t++]=0}while(--s>=0){var c=(g[--u]==b)?this.DM:Math.floor(g[u]*A+(g[u-1]+x)*z);if((g[u]+=d.am(0,c,g,s,0,p))<c){d.dlShiftTo(s,f);g.subTo(f,g);while(g[u]<--c){g.subTo(f,g)}}}if(h!=null){g.drShiftTo(p,h);if(a!=l){BigInteger.ZERO.subTo(h,h)}}g.t=p;g.clamp();if(v>0){g.rShiftTo(v,g)}if(a<0){BigInteger.ZERO.subTo(g,g)}}function bnMod(b){var c=nbi();this.abs().divRemTo(b,null,c);if(this.s<0&&c.compareTo(BigInteger.ZERO)>0){b.subTo(c,c)}return c}function Classic(a){this.m=a}function cConvert(a){if(a.s<0||a.compareTo(this.m)>=0){return a.mod(this.m)}else{return a}}function cRevert(a){return a}function cReduce(a){a.divRemTo(this.m,null,a)}function cMulTo(a,c,b){a.multiplyTo(c,b);this.reduce(b)}function cSqrTo(a,b){a.squareTo(b);this.reduce(b)}Classic.prototype.convert=cConvert;Classic.prototype.revert=cRevert;Classic.prototype.reduce=cReduce;Classic.prototype.mulTo=cMulTo;Classic.prototype.sqrTo=cSqrTo;function bnpInvDigit(){if(this.t<1){return 0}var a=this[0];if((a&1)==0){return 0}var b=a&3;b=(b*(2-(a&15)*b))&15;b=(b*(2-(a&255)*b))&255;b=(b*(2-(((a&65535)*b)&65535)))&65535;b=(b*(2-a*b%this.DV))%this.DV;return(b>0)?this.DV-b:-b}function Montgomery(a){this.m=a;this.mp=a.invDigit();this.mpl=this.mp&32767;this.mph=this.mp>>15;this.um=(1<<(a.DB-15))-1;this.mt2=2*a.t}function montConvert(a){var b=nbi();a.abs().dlShiftTo(this.m.t,b);b.divRemTo(this.m,null,b);if(a.s<0&&b.compareTo(BigInteger.ZERO)>0){this.m.subTo(b,b)}return b}function montRevert(a){var b=nbi();a.copyTo(b);this.reduce(b);return b}function montReduce(a){while(a.t<=this.mt2){a[a.t++]=0}for(var c=0;c<this.m.t;++c){var b=a[c]&32767;var d=(b*this.mpl+(((b*this.mph+(a[c]>>15)*this.mpl)&this.um)<<15))&a.DM;b=c+this.m.t;a[b]+=this.m.am(0,d,a,c,0,this.m.t);while(a[b]>=a.DV){a[b]-=a.DV;a[++b]++}}a.clamp();a.drShiftTo(this.m.t,a);if(a.compareTo(this.m)>=0){a.subTo(this.m,a)}}function montSqrTo(a,b){a.squareTo(b);this.reduce(b)}function montMulTo(a,c,b){a.multiplyTo(c,b);this.reduce(b)}Montgomery.prototype.convert=montConvert;Montgomery.prototype.revert=montRevert;Montgomery.prototype.reduce=montReduce;Montgomery.prototype.mulTo=montMulTo;Montgomery.prototype.sqrTo=montSqrTo;function bnpIsEven(){return((this.t>0)?(this[0]&1):this.s)==0}function bnpExp(h,j){if(h>4294967295||h<1){return BigInteger.ONE}var f=nbi(),a=nbi(),d=j.convert(this),c=nbits(h)-1;d.copyTo(f);while(--c>=0){j.sqrTo(f,a);if((h&(1<<c))>0){j.mulTo(a,d,f)}else{var b=f;f=a;a=b}}return j.revert(f)}function bnModPowInt(b,a){var c;if(b<256||a.isEven()){c=new Classic(a)}else{c=new Montgomery(a)}return this.exp(b,c)}BigInteger.prototype.copyTo=bnpCopyTo;BigInteger.prototype.fromInt=bnpFromInt;BigInteger.prototype.fromString=bnpFromString;BigInteger.prototype.clamp=bnpClamp;BigInteger.prototype.dlShiftTo=bnpDLShiftTo;BigInteger.prototype.drShiftTo=bnpDRShiftTo;BigInteger.prototype.lShiftTo=bnpLShiftTo;BigInteger.prototype.rShiftTo=bnpRShiftTo;BigInteger.prototype.subTo=bnpSubTo;BigInteger.prototype.multiplyTo=bnpMultiplyTo;BigInteger.prototype.squareTo=bnpSquareTo;BigInteger.prototype.divRemTo=bnpDivRemTo;BigInteger.prototype.invDigit=bnpInvDigit;BigInteger.prototype.isEven=bnpIsEven;BigInteger.prototype.exp=bnpExp;BigInteger.prototype.toString=bnToString;BigInteger.prototype.negate=bnNegate;BigInteger.prototype.abs=bnAbs;BigInteger.prototype.compareTo=bnCompareTo;BigInteger.prototype.bitLength=bnBitLength;BigInteger.prototype.mod=bnMod;BigInteger.prototype.modPowInt=bnModPowInt;BigInteger.ZERO=nbv(0);BigInteger.ONE=nbv(1);
// Copyright (c) 2005-2009 Tom Wu
// All Rights Reserved.
// See "LICENSE" for details.
// Extended JavaScript BN functions, required for RSA private ops.
// Version 1.1: new BigInteger("0", 10) returns "proper" zero
function bnClone(){var a=nbi();this.copyTo(a);return a}function bnIntValue(){if(this.s<0){if(this.t==1){return this[0]-this.DV}else{if(this.t==0){return -1}}}else{if(this.t==1){return this[0]}else{if(this.t==0){return 0}}}return((this[1]&((1<<(32-this.DB))-1))<<this.DB)|this[0]}function bnByteValue(){return(this.t==0)?this.s:(this[0]<<24)>>24}function bnShortValue(){return(this.t==0)?this.s:(this[0]<<16)>>16}function bnpChunkSize(a){return Math.floor(Math.LN2*this.DB/Math.log(a))}function bnSigNum(){if(this.s<0){return -1}else{if(this.t<=0||(this.t==1&&this[0]<=0)){return 0}else{return 1}}}function bnpToRadix(c){if(c==null){c=10}if(this.signum()==0||c<2||c>36){return"0"}var f=this.chunkSize(c);var e=Math.pow(c,f);var i=nbv(e),j=nbi(),h=nbi(),g="";this.divRemTo(i,j,h);while(j.signum()>0){g=(e+h.intValue()).toString(c).substr(1)+g;j.divRemTo(i,j,h)}return h.intValue().toString(c)+g}function bnpFromRadix(m,h){this.fromInt(0);if(h==null){h=10}var f=this.chunkSize(h);var g=Math.pow(h,f),e=false,a=0,l=0;for(var c=0;c<m.length;++c){var k=intAt(m,c);if(k<0){if(m.charAt(c)=="-"&&this.signum()==0){e=true}continue}l=h*l+k;if(++a>=f){this.dMultiply(g);this.dAddOffset(l,0);a=0;l=0}}if(a>0){this.dMultiply(Math.pow(h,a));this.dAddOffset(l,0)}if(e){BigInteger.ZERO.subTo(this,this)}}function bnpFromNumber(f,e,h){if("number"==typeof e){if(f<2){this.fromInt(1)}else{this.fromNumber(f,h);if(!this.testBit(f-1)){this.bitwiseTo(BigInteger.ONE.shiftLeft(f-1),op_or,this)}if(this.isEven()){this.dAddOffset(1,0)}while(!this.isProbablePrime(e)){this.dAddOffset(2,0);if(this.bitLength()>f){this.subTo(BigInteger.ONE.shiftLeft(f-1),this)}}}}else{var d=new Array(),g=f&7;d.length=(f>>3)+1;e.nextBytes(d);if(g>0){d[0]&=((1<<g)-1)}else{d[0]=0}this.fromString(d,256)}}function bnToByteArray(){var b=this.t,c=new Array();c[0]=this.s;var e=this.DB-(b*this.DB)%8,f,a=0;if(b-->0){if(e<this.DB&&(f=this[b]>>e)!=(this.s&this.DM)>>e){c[a++]=f|(this.s<<(this.DB-e))}while(b>=0){if(e<8){f=(this[b]&((1<<e)-1))<<(8-e);f|=this[--b]>>(e+=this.DB-8)}else{f=(this[b]>>(e-=8))&255;if(e<=0){e+=this.DB;--b}}if((f&128)!=0){f|=-256}if(a==0&&(this.s&128)!=(f&128)){++a}if(a>0||f!=this.s){c[a++]=f}}}return c}function bnEquals(b){return(this.compareTo(b)==0)}function bnMin(b){return(this.compareTo(b)<0)?this:b}function bnMax(b){return(this.compareTo(b)>0)?this:b}function bnpBitwiseTo(c,h,e){var d,g,b=Math.min(c.t,this.t);for(d=0;d<b;++d){e[d]=h(this[d],c[d])}if(c.t<this.t){g=c.s&this.DM;for(d=b;d<this.t;++d){e[d]=h(this[d],g)}e.t=this.t}else{g=this.s&this.DM;for(d=b;d<c.t;++d){e[d]=h(g,c[d])}e.t=c.t}e.s=h(this.s,c.s);e.clamp()}function op_and(a,b){return a&b}function bnAnd(b){var c=nbi();this.bitwiseTo(b,op_and,c);return c}function op_or(a,b){return a|b}function bnOr(b){var c=nbi();this.bitwiseTo(b,op_or,c);return c}function op_xor(a,b){return a^b}function bnXor(b){var c=nbi();this.bitwiseTo(b,op_xor,c);return c}function op_andnot(a,b){return a&~b}function bnAndNot(b){var c=nbi();this.bitwiseTo(b,op_andnot,c);return c}function bnNot(){var b=nbi();for(var a=0;a<this.t;++a){b[a]=this.DM&~this[a]}b.t=this.t;b.s=~this.s;return b}function bnShiftLeft(b){var a=nbi();if(b<0){this.rShiftTo(-b,a)}else{this.lShiftTo(b,a)}return a}function bnShiftRight(b){var a=nbi();if(b<0){this.lShiftTo(-b,a)}else{this.rShiftTo(b,a)}return a}function lbit(a){if(a==0){return -1}var b=0;if((a&65535)==0){a>>=16;b+=16}if((a&255)==0){a>>=8;b+=8}if((a&15)==0){a>>=4;b+=4}if((a&3)==0){a>>=2;b+=2}if((a&1)==0){++b}return b}function bnGetLowestSetBit(){for(var a=0;a<this.t;++a){if(this[a]!=0){return a*this.DB+lbit(this[a])}}if(this.s<0){return this.t*this.DB}return -1}function cbit(a){var b=0;while(a!=0){a&=a-1;++b}return b}function bnBitCount(){var c=0,a=this.s&this.DM;for(var b=0;b<this.t;++b){c+=cbit(this[b]^a)}return c}function bnTestBit(b){var a=Math.floor(b/this.DB);if(a>=this.t){return(this.s!=0)}return((this[a]&(1<<(b%this.DB)))!=0)}function bnpChangeBit(c,b){var a=BigInteger.ONE.shiftLeft(c);this.bitwiseTo(a,b,a);return a}function bnSetBit(a){return this.changeBit(a,op_or)}function bnClearBit(a){return this.changeBit(a,op_andnot)}function bnFlipBit(a){return this.changeBit(a,op_xor)}function bnpAddTo(d,f){var e=0,g=0,b=Math.min(d.t,this.t);while(e<b){g+=this[e]+d[e];f[e++]=g&this.DM;g>>=this.DB}if(d.t<this.t){g+=d.s;while(e<this.t){g+=this[e];f[e++]=g&this.DM;g>>=this.DB}g+=this.s}else{g+=this.s;while(e<d.t){g+=d[e];f[e++]=g&this.DM;g>>=this.DB}g+=d.s}f.s=(g<0)?-1:0;if(g>0){f[e++]=g}else{if(g<-1){f[e++]=this.DV+g}}f.t=e;f.clamp()}function bnAdd(b){var c=nbi();this.addTo(b,c);return c}function bnSubtract(b){var c=nbi();this.subTo(b,c);return c}function bnMultiply(b){var c=nbi();this.multiplyTo(b,c);return c}function bnDivide(b){var c=nbi();this.divRemTo(b,c,null);return c}function bnRemainder(b){var c=nbi();this.divRemTo(b,null,c);return c}function bnDivideAndRemainder(b){var d=nbi(),c=nbi();this.divRemTo(b,d,c);return new Array(d,c)}function bnpDMultiply(a){this[this.t]=this.am(0,a-1,this,0,0,this.t);++this.t;this.clamp()}function bnpDAddOffset(b,a){if(b==0){return}while(this.t<=a){this[this.t++]=0}this[a]+=b;while(this[a]>=this.DV){this[a]-=this.DV;if(++a>=this.t){this[this.t++]=0}++this[a]}}function NullExp(){}function nNop(a){return a}function nMulTo(a,c,b){a.multiplyTo(c,b)}function nSqrTo(a,b){a.squareTo(b)}NullExp.prototype.convert=nNop;NullExp.prototype.revert=nNop;NullExp.prototype.mulTo=nMulTo;NullExp.prototype.sqrTo=nSqrTo;function bnPow(a){return this.exp(a,new NullExp())}function bnpMultiplyLowerTo(b,f,e){var d=Math.min(this.t+b.t,f);e.s=0;e.t=d;while(d>0){e[--d]=0}var c;for(c=e.t-this.t;d<c;++d){e[d+this.t]=this.am(0,b[d],e,d,0,this.t)}for(c=Math.min(b.t,f);d<c;++d){this.am(0,b[d],e,d,0,f-d)}e.clamp()}function bnpMultiplyUpperTo(b,e,d){--e;var c=d.t=this.t+b.t-e;d.s=0;while(--c>=0){d[c]=0}for(c=Math.max(e-this.t,0);c<b.t;++c){d[this.t+c-e]=this.am(e-c,b[c],d,0,0,this.t+c-e)}d.clamp();d.drShiftTo(1,d)}function Barrett(a){this.r2=nbi();this.q3=nbi();BigInteger.ONE.dlShiftTo(2*a.t,this.r2);this.mu=this.r2.divide(a);this.m=a}function barrettConvert(a){if(a.s<0||a.t>2*this.m.t){return a.mod(this.m)}else{if(a.compareTo(this.m)<0){return a}else{var b=nbi();a.copyTo(b);this.reduce(b);return b}}}function barrettRevert(a){return a}function barrettReduce(a){a.drShiftTo(this.m.t-1,this.r2);if(a.t>this.m.t+1){a.t=this.m.t+1;a.clamp()}this.mu.multiplyUpperTo(this.r2,this.m.t+1,this.q3);this.m.multiplyLowerTo(this.q3,this.m.t+1,this.r2);while(a.compareTo(this.r2)<0){a.dAddOffset(1,this.m.t+1)}a.subTo(this.r2,a);while(a.compareTo(this.m)>=0){a.subTo(this.m,a)}}function barrettSqrTo(a,b){a.squareTo(b);this.reduce(b)}function barrettMulTo(a,c,b){a.multiplyTo(c,b);this.reduce(b)}Barrett.prototype.convert=barrettConvert;Barrett.prototype.revert=barrettRevert;Barrett.prototype.reduce=barrettReduce;Barrett.prototype.mulTo=barrettMulTo;Barrett.prototype.sqrTo=barrettSqrTo;function bnModPow(q,f){var o=q.bitLength(),h,b=nbv(1),v;if(o<=0){return b}else{if(o<18){h=1}else{if(o<48){h=3}else{if(o<144){h=4}else{if(o<768){h=5}else{h=6}}}}}if(o<8){v=new Classic(f)}else{if(f.isEven()){v=new Barrett(f)}else{v=new Montgomery(f)}}var p=new Array(),d=3,s=h-1,a=(1<<h)-1;p[1]=v.convert(this);if(h>1){var A=nbi();v.sqrTo(p[1],A);while(d<=a){p[d]=nbi();v.mulTo(A,p[d-2],p[d]);d+=2}}var l=q.t-1,x,u=true,c=nbi(),y;o=nbits(q[l])-1;while(l>=0){if(o>=s){x=(q[l]>>(o-s))&a}else{x=(q[l]&((1<<(o+1))-1))<<(s-o);if(l>0){x|=q[l-1]>>(this.DB+o-s)}}d=h;while((x&1)==0){x>>=1;--d}if((o-=d)<0){o+=this.DB;--l}if(u){p[x].copyTo(b);u=false}else{while(d>1){v.sqrTo(b,c);v.sqrTo(c,b);d-=2}if(d>0){v.sqrTo(b,c)}else{y=b;b=c;c=y}v.mulTo(c,p[x],b)}while(l>=0&&(q[l]&(1<<o))==0){v.sqrTo(b,c);y=b;b=c;c=y;if(--o<0){o=this.DB-1;--l}}}return v.revert(b)}function bnGCD(c){var b=(this.s<0)?this.negate():this.clone();var h=(c.s<0)?c.negate():c.clone();if(b.compareTo(h)<0){var e=b;b=h;h=e}var d=b.getLowestSetBit(),f=h.getLowestSetBit();if(f<0){return b}if(d<f){f=d}if(f>0){b.rShiftTo(f,b);h.rShiftTo(f,h)}while(b.signum()>0){if((d=b.getLowestSetBit())>0){b.rShiftTo(d,b)}if((d=h.getLowestSetBit())>0){h.rShiftTo(d,h)}if(b.compareTo(h)>=0){b.subTo(h,b);b.rShiftTo(1,b)}else{h.subTo(b,h);h.rShiftTo(1,h)}}if(f>0){h.lShiftTo(f,h)}return h}function bnpModInt(e){if(e<=0){return 0}var c=this.DV%e,b=(this.s<0)?e-1:0;if(this.t>0){if(c==0){b=this[0]%e}else{for(var a=this.t-1;a>=0;--a){b=(c*b+this[a])%e}}}return b}function bnModInverse(f){var j=f.isEven();if((this.isEven()&&j)||f.signum()==0){return BigInteger.ZERO}var i=f.clone(),h=this.clone();var g=nbv(1),e=nbv(0),l=nbv(0),k=nbv(1);while(i.signum()!=0){while(i.isEven()){i.rShiftTo(1,i);if(j){if(!g.isEven()||!e.isEven()){g.addTo(this,g);e.subTo(f,e)}g.rShiftTo(1,g)}else{if(!e.isEven()){e.subTo(f,e)}}e.rShiftTo(1,e)}while(h.isEven()){h.rShiftTo(1,h);if(j){if(!l.isEven()||!k.isEven()){l.addTo(this,l);k.subTo(f,k)}l.rShiftTo(1,l)}else{if(!k.isEven()){k.subTo(f,k)}}k.rShiftTo(1,k)}if(i.compareTo(h)>=0){i.subTo(h,i);if(j){g.subTo(l,g)}e.subTo(k,e)}else{h.subTo(i,h);if(j){l.subTo(g,l)}k.subTo(e,k)}}if(h.compareTo(BigInteger.ONE)!=0){return BigInteger.ZERO}if(k.compareTo(f)>=0){return k.subtract(f)}if(k.signum()<0){k.addTo(f,k)}else{return k}if(k.signum()<0){return k.add(f)}else{return k}}var lowprimes=[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71,73,79,83,89,97,101,103,107,109,113,127,131,137,139,149,151,157,163,167,173,179,181,191,193,197,199,211,223,227,229,233,239,241,251,257,263,269,271,277,281,283,293,307,311,313,317,331,337,347,349,353,359,367,373,379,383,389,397,401,409,419,421,431,433,439,443,449,457,461,463,467,479,487,491,499,503,509];var lplim=(1<<26)/lowprimes[lowprimes.length-1];function bnIsProbablePrime(e){var d,b=this.abs();if(b.t==1&&b[0]<=lowprimes[lowprimes.length-1]){for(d=0;d<lowprimes.length;++d){if(b[0]==lowprimes[d]){return true}}return false}if(b.isEven()){return false}d=1;while(d<lowprimes.length){var a=lowprimes[d],c=d+1;while(c<lowprimes.length&&a<lplim){a*=lowprimes[c++]}a=b.modInt(a);while(d<c){if(a%lowprimes[d++]==0){return false}}}return b.millerRabin(e)}function bnpMillerRabin(f){var g=this.subtract(BigInteger.ONE);var c=g.getLowestSetBit();if(c<=0){return false}var h=g.shiftRight(c);f=(f+1)>>1;if(f>lowprimes.length){f=lowprimes.length}var b=nbi();for(var e=0;e<f;++e){b.fromInt(lowprimes[e]);var l=b.modPow(h,this);if(l.compareTo(BigInteger.ONE)!=0&&l.compareTo(g)!=0){var d=1;while(d++<c&&l.compareTo(g)!=0){l=l.modPowInt(2,this);if(l.compareTo(BigInteger.ONE)==0){return false}}if(l.compareTo(g)!=0){return false}}}return true}BigInteger.prototype.chunkSize=bnpChunkSize;BigInteger.prototype.toRadix=bnpToRadix;BigInteger.prototype.fromRadix=bnpFromRadix;BigInteger.prototype.fromNumber=bnpFromNumber;BigInteger.prototype.bitwiseTo=bnpBitwiseTo;BigInteger.prototype.changeBit=bnpChangeBit;BigInteger.prototype.addTo=bnpAddTo;BigInteger.prototype.dMultiply=bnpDMultiply;BigInteger.prototype.dAddOffset=bnpDAddOffset;BigInteger.prototype.multiplyLowerTo=bnpMultiplyLowerTo;BigInteger.prototype.multiplyUpperTo=bnpMultiplyUpperTo;BigInteger.prototype.modInt=bnpModInt;BigInteger.prototype.millerRabin=bnpMillerRabin;BigInteger.prototype.clone=bnClone;BigInteger.prototype.intValue=bnIntValue;BigInteger.prototype.byteValue=bnByteValue;BigInteger.prototype.shortValue=bnShortValue;BigInteger.prototype.signum=bnSigNum;BigInteger.prototype.toByteArray=bnToByteArray;BigInteger.prototype.equals=bnEquals;BigInteger.prototype.min=bnMin;BigInteger.prototype.max=bnMax;BigInteger.prototype.and=bnAnd;BigInteger.prototype.or=bnOr;BigInteger.prototype.xor=bnXor;BigInteger.prototype.andNot=bnAndNot;BigInteger.prototype.not=bnNot;BigInteger.prototype.shiftLeft=bnShiftLeft;BigInteger.prototype.shiftRight=bnShiftRight;BigInteger.prototype.getLowestSetBit=bnGetLowestSetBit;BigInteger.prototype.bitCount=bnBitCount;BigInteger.prototype.testBit=bnTestBit;BigInteger.prototype.setBit=bnSetBit;BigInteger.prototype.clearBit=bnClearBit;BigInteger.prototype.flipBit=bnFlipBit;BigInteger.prototype.add=bnAdd;BigInteger.prototype.subtract=bnSubtract;BigInteger.prototype.multiply=bnMultiply;BigInteger.prototype.divide=bnDivide;BigInteger.prototype.remainder=bnRemainder;BigInteger.prototype.divideAndRemainder=bnDivideAndRemainder;BigInteger.prototype.modPow=bnModPow;BigInteger.prototype.modInverse=bnModInverse;BigInteger.prototype.pow=bnPow;BigInteger.prototype.gcd=bnGCD;BigInteger.prototype.isProbablePrime=bnIsProbablePrime;
// Depends on jsbn.js and rng.js
// Version 1.1: support utf-8 encoding in pkcs1pad2
// convert a (hex) string to a bignum object
function parseBigInt(b,a){return new BigInteger(b,a)}function linebrk(c,d){var a="";var b=0;while(b+d<c.length){a+=c.substring(b,b+d)+"\n";b+=d}return a+c.substring(b,c.length)}function byte2Hex(a){if(a<16){return"0"+a.toString(16)}else{return a.toString(16)}}function pkcs1pad2(e,h){if(h<e.length+11){alert("Message too long for RSA");return null}var g=new Array();var d=e.length-1;while(d>=0&&h>0){var f=e.charCodeAt(d--);if(f<128){g[--h]=f}else{if((f>127)&&(f<2048)){g[--h]=(f&63)|128;g[--h]=(f>>6)|192}else{g[--h]=(f&63)|128;g[--h]=((f>>6)&63)|128;g[--h]=(f>>12)|224}}}g[--h]=0;var b=new SecureRandom();var a=new Array();while(h>2){a[0]=0;while(a[0]==0){b.nextBytes(a)}g[--h]=a[0]}g[--h]=2;g[--h]=0;return new BigInteger(g)}function RSAKey(){this.n=null;this.e=0;this.d=null;this.p=null;this.q=null;this.dmp1=null;this.dmq1=null;this.coeff=null}function RSASetPublic(b,a){if(b!=null&&a!=null&&b.length>0&&a.length>0){this.n=parseBigInt(b,16);this.e=parseInt(a,16)}else{alert("Invalid RSA public key")}}function RSADoPublic(a){return a.modPowInt(this.e,this.n)}function RSAEncrypt(d){var a=pkcs1pad2(d,(this.n.bitLength()+7)>>3);if(a==null){return null}var e=this.doPublic(a);if(e==null){return null}var b=e.toString(16);if((b.length&1)==0){return b}else{return"0"+b}}RSAKey.prototype.doPublic=RSADoPublic;RSAKey.prototype.setPublic=RSASetPublic;RSAKey.prototype.encrypt=RSAEncrypt;
// Depends on rsa.js and jsbn2.js
// Version 1.1: support utf-8 decoding in pkcs1unpad2
// Undo PKCS#1 (type 2, random) padding and, if valid, return the plaintext
function pkcs1unpad2(g,j){var a=g.toByteArray();var f=0;while(f<a.length&&a[f]==0){++f}if(a.length-f!=j-1||a[f]!=2){return null}++f;while(a[f]!=0){if(++f>=a.length){return null}}var e="";while(++f<a.length){var h=a[f]&255;if(h<128){e+=String.fromCharCode(h)}else{if((h>191)&&(h<224)){e+=String.fromCharCode(((h&31)<<6)|(a[f+1]&63));++f}else{e+=String.fromCharCode(((h&15)<<12)|((a[f+1]&63)<<6)|(a[f+2]&63));f+=2}}}return e}function RSASetPrivate(c,a,b){if(c!=null&&a!=null&&c.length>0&&a.length>0){this.n=parseBigInt(c,16);this.e=parseInt(a,16);this.d=parseBigInt(b,16)}else{alert("Invalid RSA private key")}}function RSASetPrivateEx(g,d,e,c,b,a,h,f){if(g!=null&&d!=null&&g.length>0&&d.length>0){this.n=parseBigInt(g,16);this.e=parseInt(d,16);this.d=parseBigInt(e,16);this.p=parseBigInt(c,16);this.q=parseBigInt(b,16);this.dmp1=parseBigInt(a,16);this.dmq1=parseBigInt(h,16);this.coeff=parseBigInt(f,16)}else{alert("Invalid RSA private key")}}function RSAGenerate(b,i){var a=new SecureRandom();var f=b>>1;this.e=parseInt(i,16);var c=new BigInteger(i,16);for(;;){for(;;){this.p=new BigInteger(b-f,1,a);if(this.p.subtract(BigInteger.ONE).gcd(c).compareTo(BigInteger.ONE)==0&&this.p.isProbablePrime(10)){break}}for(;;){this.q=new BigInteger(f,1,a);if(this.q.subtract(BigInteger.ONE).gcd(c).compareTo(BigInteger.ONE)==0&&this.q.isProbablePrime(10)){break}}if(this.p.compareTo(this.q)<=0){var h=this.p;this.p=this.q;this.q=h}var g=this.p.subtract(BigInteger.ONE);var d=this.q.subtract(BigInteger.ONE);var e=g.multiply(d);if(e.gcd(c).compareTo(BigInteger.ONE)==0){this.n=this.p.multiply(this.q);this.d=c.modInverse(e);this.dmp1=this.d.mod(g);this.dmq1=this.d.mod(d);this.coeff=this.q.modInverse(this.p);break}}}function RSADoPrivate(a){if(this.p==null||this.q==null){return a.modPow(this.d,this.n)}var c=a.mod(this.p).modPow(this.dmp1,this.p);var b=a.mod(this.q).modPow(this.dmq1,this.q);while(c.compareTo(b)<0){c=c.add(this.p)}return c.subtract(b).multiply(this.coeff).mod(this.p).multiply(this.q).add(b)}function RSADecrypt(b){var d=parseBigInt(b,16);var a=this.doPrivate(d);if(a==null){return null}return pkcs1unpad2(a,(this.n.bitLength()+7)>>3)}RSAKey.prototype.doPrivate=RSADoPrivate;RSAKey.prototype.setPrivate=RSASetPrivate;RSAKey.prototype.setPrivateEx=RSASetPrivateEx;RSAKey.prototype.generate=RSAGenerate;RSAKey.prototype.decrypt=RSADecrypt;
//Base64
var Base64Encoder={encode:function(a){return base64encode(a)},decode:function(a){return base64decode(a)}};var base64EncodeChars="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";var base64DecodeChars=new Array(-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,-1,62,-1,-1,-1,63,52,53,54,55,56,57,58,59,60,61,-1,-1,-1,-1,-1,-1,-1,0,1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,-1,-1,-1,-1,-1,-1,26,27,28,29,30,31,32,33,34,35,36,37,38,39,40,41,42,43,44,45,46,47,48,49,50,51,-1,-1,-1,-1,-1);function base64encode(g){var c,e,a;var f,d,b;a=g.length;e=0;c="";while(e<a){f=g.charCodeAt(e++)&255;if(e==a){c+=base64EncodeChars.charAt(f>>2);c+=base64EncodeChars.charAt((f&3)<<4);c+="==";break}d=g.charCodeAt(e++);if(e==a){c+=base64EncodeChars.charAt(f>>2);c+=base64EncodeChars.charAt(((f&3)<<4)|((d&240)>>4));c+=base64EncodeChars.charAt((d&15)<<2);c+="=";break}b=g.charCodeAt(e++);c+=base64EncodeChars.charAt(f>>2);c+=base64EncodeChars.charAt(((f&3)<<4)|((d&240)>>4));c+=base64EncodeChars.charAt(((d&15)<<2)|((b&192)>>6));c+=base64EncodeChars.charAt(b&63)}return c}function base64decode(h){var g,f,d,b;var e,a,c;a=h.length;e=0;c="";while(e<a){do{g=base64DecodeChars[h.charCodeAt(e++)&255]}while(e<a&&g==-1);if(g==-1){break}do{f=base64DecodeChars[h.charCodeAt(e++)&255]}while(e<a&&f==-1);if(f==-1){break}c+=String.fromCharCode((g<<2)|((f&48)>>4));do{d=h.charCodeAt(e++)&255;if(d==61){return c}d=base64DecodeChars[d]}while(e<a&&d==-1);if(d==-1){break}c+=String.fromCharCode(((f&15)<<4)|((d&60)>>2));do{b=h.charCodeAt(e++)&255;if(b==61){return c}b=base64DecodeChars[b]}while(e<a&&b==-1);if(b==-1){break}c+=String.fromCharCode(((d&3)<<6)|b)}return c}function utf16to8(e){var b,d,a,f;b="";a=e.length;for(d=0;d<a;d++){f=e.charCodeAt(d);if((f>=1)&&(f<=127)){b+=e.charAt(d)}else{if(f>2047){b+=String.fromCharCode(224|((f>>12)&15));b+=String.fromCharCode(128|((f>>6)&63));b+=String.fromCharCode(128|((f>>0)&63))}else{b+=String.fromCharCode(192|((f>>6)&31));b+=String.fromCharCode(128|((f>>0)&63))}}}return b}function utf8to16(g){var b,e,a,h;var f,d;b="";a=g.length;e=0;while(e<a){h=g.charCodeAt(e++);switch(h>>4){case 0:case 1:case 2:case 3:case 4:case 5:case 6:case 7:b+=g.charAt(e-1);break;case 12:case 13:f=g.charCodeAt(e++);b+=String.fromCharCode(((h&31)<<6)|(f&63));break;case 14:f=g.charCodeAt(e++);d=g.charCodeAt(e++);b+=String.fromCharCode(((h&15)<<12)|((f&63)<<6)|((d&63)<<0));break}}return b};
/****** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* Contributor(s):
* Michael Hanson <[email protected]>
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
var jwt={};var JWTInternals=(function(){var b64urlmap="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_";function b64urltohex(s){var ret="";var i;var k=0;var slop;for(i=0;i<s.length;++i){var v=b64urlmap.indexOf(s.charAt(i));if(v<0){continue}if(k==0){ret+=int2char(v>>2);slop=v&3;k=1}else{if(k==1){ret+=int2char((slop<<2)|(v>>4));slop=v&15;k=2}else{if(k==2){ret+=int2char(slop);ret+=int2char(v>>2);slop=v&3;k=3}else{ret+=int2char((slop<<2)|(v>>4));ret+=int2char(v&15);k=0}}}}if(k==1){ret+=int2char(slop<<2)}return ret}function base64urlencode(arg){var s=base64encode(arg);s=s.split("=")[0];s=s.replace(/\+/g,"-");s=s.replace(/\//g,"_");return s}function base64urldecode(arg){var s=arg;s=s.replace(/-/g,"+");s=s.replace(/_/g,"/");switch(s.length%4){case 0:break;case 2:s+="==";break;case 3:s+="=";break;default:throw new InputException("Illegal base64url string!")}return Base64Encoder.decode(s)}function NoSuchAlgorithmException(message){this.message=message;this.toString=function(){return"No such algorithm: "+this.message}}function NotImplementedException(message){this.message=message;this.toString=function(){return"Not implemented: "+this.message}}function InputException(message){this.message=message;this.toString=function(){return"Malformed input: "+this.message}}function HMACAlgorithm(hash,key){if(hash=="sha256"){this.hash=sjcl.hash.sha256}else{throw new NoSuchAlgorithmException("HMAC does not support hash "+hash)}this.key=sjcl.codec.utf8String.toBits(key)}HMACAlgorithm.prototype={update:function _update(data){this.data=data},finalize:function _finalize(){},sign:function _sign(){var hmac=new sjcl.misc.hmac(this.key,this.hash);var result=hmac.encrypt(this.data);return base64urlencode(base64decode(sjcl.codec.base64.fromBits(result)))},verify:function _verify(sig){var hmac=new sjcl.misc.hmac(this.key,this.hash);var result=hmac.encrypt(this.data);return base64urlencode(base64decode(sjcl.codec.base64.fromBits(result)))==sig}};function RSASHAAlgorithm(hash,keyPEM){if(hash=="sha1"){this.hash="sha1"}else{if(hash=="sha256"){this.hash="sha256"}else{throw new NoSuchAlgorithmException("JWT algorithm: "+hash)}}this.keyPEM=keyPEM}RSASHAAlgorithm.prototype={update:function _update(data){this.data=data},finalize:function _finalize(){},sign:function _sign(){var rsa=new RSAKey();rsa.readPrivateKeyFromPEMString(this.keyPEM);var hSig=rsa.signString(this.data,this.hash);return base64urlencode(base64urldecode(hex2b64(hSig)))},verify:function _verify(sig){var result=this.keyPEM.verifyString(this.data,b64urltohex(sig));return result}};function WebToken(objectStr,algorithm){this.objectStr=objectStr;this.pkAlgorithm=algorithm}var WebTokenParser={parse:function _parse(input){var parts=input.split(".");if(parts.length!=3){throw new MalformedWebToken("Must have three parts")}var token=new WebToken();token.headerSegment=parts[0];token.payloadSegment=parts[1];token.cryptoSegment=parts[2];token.pkAlgorithm=base64urldecode(parts[0]);return token}};function jsonObj(strOrObject){if(typeof strOrObject=="string"){return eval("("+strOrObject+")")}return strOrObject}function constructAlgorithm(jwtAlgStr,key){if("ES256"===jwtAlgStr){throw new NotImplementedException("ECDSA-SHA256 not yet implemented")}else{if("ES384"===jwtAlgStr){throw new NotImplementedException("ECDSA-SHA384 not yet implemented")}else{if("ES512"===jwtAlgStr){throw new NotImplementedException("ECDSA-SHA512 not yet implemented")}else{if("HS256"===jwtAlgStr){return new HMACAlgorithm("sha256",key)}else{if("HS384"===jwtAlgStr){throw new NotImplementedException("HMAC-SHA384 not yet implemented")}else{if("HS512"===jwtAlgStr){throw new NotImplementedException("HMAC-SHA512 not yet implemented")}else{if("RS256"===jwtAlgStr){return new RSASHAAlgorithm("sha256",key)}else{if("RS384"===jwtAlgStr){throw new NotImplementedException("RSA-SHA384 not yet implemented")}else{if("RS512"===jwtAlgStr){throw new NotImplementedException("RSA-SHA512 not yet implemented")}else{throw new NoSuchAlgorithmException("Unknown algorithm: "+jwtAlgStr)}}}}}}}}}}WebToken.prototype={serialize:function _serialize(key){var header=jsonObj(this.pkAlgorithm);var jwtAlgStr=header.alg;var algorithm=constructAlgorithm(jwtAlgStr,key);var algBytes=base64urlencode(this.pkAlgorithm);var jsonBytes=base64urlencode(this.objectStr);var stringToSign=algBytes+"."+jsonBytes;algorithm.update(stringToSign);var digestValue=algorithm.finalize();var signatureValue=algorithm.sign();return algBytes+"."+jsonBytes+"."+signatureValue},verify:function _verify(key){var header=jsonObj(this.pkAlgorithm);var jwtAlgStr=header.alg;var algorithm=constructAlgorithm(jwtAlgStr,key);algorithm.update(this.headerSegment+"."+this.payloadSegment);algorithm.finalize();return algorithm.verify(this.cryptoSegment)}};jwt.WebToken=WebToken;jwt.WebTokenParser=WebTokenParser;jwt.base64urlencode=base64urlencode;jwt.base64urldecode=base64urldecode})();
</script>
<script language="javascript" runat="server">
function duoshuo_getjwt(){
var token = new jwt.WebToken('{"short_name": "'+duoshuo.config.Read("short_name")+'","user_key": '+BlogUser.ID+', "name": "'+duoshuo_jsEncode(BlogUser.FirstName)+'"}', "{\"typ\":\"JWT\",\"alg\":\"HS256\"}");
var signed = token.serialize(duoshuo.config.Read("secret"));
Response.AddHeader("Set-Cookie","duoshuo_token="+signed);
return signed;
}
</script>
<%
Function duoshuo_jsEncode(str)
Dim charmap(127), haystack()
charmap(8) = "\b"
charmap(9) = "\t"
charmap(10) = "\n"
charmap(12) = "\f"
charmap(13) = "\r"
charmap(34) = "\"""
charmap(47) = "\/"
charmap(92) = "\\"
Dim strlen : strlen = Len(str) - 1
ReDim haystack(strlen)
Dim i, charcode
For i = 0 To strlen
haystack(i) = Mid(str, i + 1, 1)
charcode = AscW(haystack(i)) And 65535
If charcode < 127 Then
If Not IsEmpty(charmap(charcode)) Then
haystack(i) = charmap(charcode)
ElseIf charcode < 32 Then
haystack(i) = "\u" & Right("000" & Hex(charcode), 4)
End If
Else
haystack(i) = "\u" & Right("000" & Hex(charcode), 4)
End If
Next
duoshuo_jsEncode = Join(haystack, "")
End Function
%>