carapace 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (61) hide show
  1. data/CHANGELOG.md +13 -0
  2. data/LICENSE +24 -0
  3. data/LICENSE_JSBN +40 -0
  4. data/README.md +96 -0
  5. data/Rakefile +8 -0
  6. data/lib/carapace.rb +124 -0
  7. data/rails_generators/USAGE +7 -0
  8. data/rails_generators/carapace_generator.rb +7 -0
  9. data/rails_generators/templates/carapace.js +848 -0
  10. data/test/rails_app/README.md +130 -0
  11. data/test/rails_app/Rakefile +10 -0
  12. data/test/rails_app/app/controllers/application.rb +10 -0
  13. data/test/rails_app/app/controllers/message_controller.rb +13 -0
  14. data/test/rails_app/app/helpers/application_helper.rb +3 -0
  15. data/test/rails_app/app/helpers/message_helper.rb +2 -0
  16. data/test/rails_app/app/views/message/index.html.erb +24 -0
  17. data/test/rails_app/config/boot.rb +109 -0
  18. data/test/rails_app/config/database.yml +19 -0
  19. data/test/rails_app/config/environment.rb +60 -0
  20. data/test/rails_app/config/environments/development.rb +18 -0
  21. data/test/rails_app/config/environments/production.rb +19 -0
  22. data/test/rails_app/config/environments/test.rb +22 -0
  23. data/test/rails_app/config/initializers/inflections.rb +10 -0
  24. data/test/rails_app/config/initializers/mime_types.rb +5 -0
  25. data/test/rails_app/config/routes.rb +35 -0
  26. data/test/rails_app/doc/README_FOR_APP +2 -0
  27. data/test/rails_app/log/development.log +162 -0
  28. data/test/rails_app/log/production.log +0 -0
  29. data/test/rails_app/log/server.log +0 -0
  30. data/test/rails_app/log/test.log +24 -0
  31. data/test/rails_app/public/404.html +30 -0
  32. data/test/rails_app/public/422.html +30 -0
  33. data/test/rails_app/public/500.html +30 -0
  34. data/test/rails_app/public/dispatch.cgi +10 -0
  35. data/test/rails_app/public/dispatch.fcgi +24 -0
  36. data/test/rails_app/public/dispatch.rb +10 -0
  37. data/test/rails_app/public/favicon.ico +0 -0
  38. data/test/rails_app/public/images/rails.png +0 -0
  39. data/test/rails_app/public/javascripts/application.js +2 -0
  40. data/test/rails_app/public/javascripts/carapace.js +844 -0
  41. data/test/rails_app/public/javascripts/controls.js +963 -0
  42. data/test/rails_app/public/javascripts/dragdrop.js +972 -0
  43. data/test/rails_app/public/javascripts/effects.js +1120 -0
  44. data/test/rails_app/public/javascripts/prototype.js +4225 -0
  45. data/test/rails_app/public/robots.txt +5 -0
  46. data/test/rails_app/script/about +3 -0
  47. data/test/rails_app/script/console +3 -0
  48. data/test/rails_app/script/destroy +3 -0
  49. data/test/rails_app/script/generate +3 -0
  50. data/test/rails_app/script/performance/benchmarker +3 -0
  51. data/test/rails_app/script/performance/profiler +3 -0
  52. data/test/rails_app/script/performance/request +3 -0
  53. data/test/rails_app/script/plugin +3 -0
  54. data/test/rails_app/script/process/inspector +3 -0
  55. data/test/rails_app/script/process/reaper +3 -0
  56. data/test/rails_app/script/process/spawner +3 -0
  57. data/test/rails_app/script/runner +3 -0
  58. data/test/rails_app/script/server +3 -0
  59. data/test/rails_app/test/functional/message_controller_test.rb +11 -0
  60. data/test/rails_app/test/test_helper.rb +38 -0
  61. metadata +127 -0
@@ -0,0 +1,844 @@
1
+ // Carapace.js
2
+ // Copyright (c) 2007 John Lane (www.starfry.com)
3
+ // Portions copyright Tom Wu as identified
4
+
5
+
6
+ // Copyright (c) 2005 Tom Wu
7
+ // All Rights Reserved.
8
+ // See "LICENSE" for details.
9
+
10
+ // Basic JavaScript BN library - subset useful for RSA encryption.
11
+
12
+ // Bits per digit
13
+ var dbits;
14
+
15
+ // JavaScript engine analysis
16
+ var canary = 0xdeadbeefcafe;
17
+ var j_lm = ((canary&0xffffff)==0xefcafe);
18
+
19
+ // (public) Constructor
20
+ function BigInteger(a,b,c) {
21
+ if(a != null)
22
+ if("number" == typeof a) this.fromNumber(a,b,c);
23
+ else if(b == null && "string" != typeof a) this.fromString(a,256);
24
+ else this.fromString(a,b);
25
+ }
26
+
27
+ // return new, unset BigInteger
28
+ function nbi() { return new BigInteger(null); }
29
+
30
+ // am: Compute w_j += (x*this_i), propagate carries,
31
+ // c is initial carry, returns final carry.
32
+ // c < 3*dvalue, x < 2*dvalue, this_i < dvalue
33
+ // We need to select the fastest one that works in this environment.
34
+
35
+ // am1: use a single mult and divide to get the high bits,
36
+ // max digit bits should be 26 because
37
+ // max internal value = 2*dvalue^2-2*dvalue (< 2^53)
38
+ function am1(i,x,w,j,c,n) {
39
+ while(--n >= 0) {
40
+ var v = x*this[i++]+w[j]+c;
41
+ c = Math.floor(v/0x4000000);
42
+ w[j++] = v&0x3ffffff;
43
+ }
44
+ return c;
45
+ }
46
+ // am2 avoids a big mult-and-extract completely.
47
+ // Max digit bits should be <= 30 because we do bitwise ops
48
+ // on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
49
+ function am2(i,x,w,j,c,n) {
50
+ var xl = x&0x7fff, xh = x>>15;
51
+ while(--n >= 0) {
52
+ var l = this[i]&0x7fff;
53
+ var h = this[i++]>>15;
54
+ var m = xh*l+h*xl;
55
+ l = xl*l+((m&0x7fff)<<15)+w[j]+(c&0x3fffffff);
56
+ c = (l>>>30)+(m>>>15)+xh*h+(c>>>30);
57
+ w[j++] = l&0x3fffffff;
58
+ }
59
+ return c;
60
+ }
61
+ // Alternately, set max digit bits to 28 since some
62
+ // browsers slow down when dealing with 32-bit numbers.
63
+ function am3(i,x,w,j,c,n) {
64
+ var xl = x&0x3fff, xh = x>>14;
65
+ while(--n >= 0) {
66
+ var l = this[i]&0x3fff;
67
+ var h = this[i++]>>14;
68
+ var m = xh*l+h*xl;
69
+ l = xl*l+((m&0x3fff)<<14)+w[j]+c;
70
+ c = (l>>28)+(m>>14)+xh*h;
71
+ w[j++] = l&0xfffffff;
72
+ }
73
+ return c;
74
+ }
75
+ if(j_lm && (navigator.appName == "Microsoft Internet Explorer")) {
76
+ BigInteger.prototype.am = am2;
77
+ dbits = 30;
78
+ }
79
+ else if(j_lm && (navigator.appName != "Netscape")) {
80
+ BigInteger.prototype.am = am1;
81
+ dbits = 26;
82
+ }
83
+ else { // Mozilla/Netscape seems to prefer am3
84
+ BigInteger.prototype.am = am3;
85
+ dbits = 28;
86
+ }
87
+
88
+ BigInteger.prototype.DB = dbits;
89
+ BigInteger.prototype.DM = ((1<<dbits)-1);
90
+ BigInteger.prototype.DV = (1<<dbits);
91
+
92
+ var BI_FP = 52;
93
+ BigInteger.prototype.FV = Math.pow(2,BI_FP);
94
+ BigInteger.prototype.F1 = BI_FP-dbits;
95
+ BigInteger.prototype.F2 = 2*dbits-BI_FP;
96
+
97
+ // Digit conversions
98
+ var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
99
+ var BI_RC = new Array();
100
+ var rr,vv;
101
+ rr = "0".charCodeAt(0);
102
+ for(vv = 0; vv <= 9; ++vv) BI_RC[rr++] = vv;
103
+ rr = "a".charCodeAt(0);
104
+ for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
105
+ rr = "A".charCodeAt(0);
106
+ for(vv = 10; vv < 36; ++vv) BI_RC[rr++] = vv;
107
+
108
+ function int2char(n) { return BI_RM.charAt(n); }
109
+ function intAt(s,i) {
110
+ var c = BI_RC[s.charCodeAt(i)];
111
+ return (c==null)?-1:c;
112
+ }
113
+
114
+ // (protected) copy this to r
115
+ function bnpCopyTo(r) {
116
+ for(var i = this.t-1; i >= 0; --i) r[i] = this[i];
117
+ r.t = this.t;
118
+ r.s = this.s;
119
+ }
120
+
121
+ // (protected) set from integer value x, -DV <= x < DV
122
+ function bnpFromInt(x) {
123
+ this.t = 1;
124
+ this.s = (x<0)?-1:0;
125
+ if(x > 0) this[0] = x;
126
+ else if(x < -1) this[0] = x+DV;
127
+ else this.t = 0;
128
+ }
129
+
130
+ // return bigint initialized to value
131
+ function nbv(i) { var r = nbi(); r.fromInt(i); return r; }
132
+
133
+ // (protected) set from string and radix
134
+ function bnpFromString(s,b) {
135
+ var k;
136
+ if(b == 16) k = 4;
137
+ else if(b == 8) k = 3;
138
+ else if(b == 256) k = 8; // byte array
139
+ else if(b == 2) k = 1;
140
+ else if(b == 32) k = 5;
141
+ else if(b == 4) k = 2;
142
+ else { this.fromRadix(s,b); return; }
143
+ this.t = 0;
144
+ this.s = 0;
145
+ var i = s.length, mi = false, sh = 0;
146
+ while(--i >= 0) {
147
+ var x = (k==8)?s[i]&0xff:intAt(s,i);
148
+ if(x < 0) {
149
+ if(s.charAt(i) == "-") mi = true;
150
+ continue;
151
+ }
152
+ mi = false;
153
+ if(sh == 0)
154
+ this[this.t++] = x;
155
+ else if(sh+k > this.DB) {
156
+ this[this.t-1] |= (x&((1<<(this.DB-sh))-1))<<sh;
157
+ this[this.t++] = (x>>(this.DB-sh));
158
+ }
159
+ else
160
+ this[this.t-1] |= x<<sh;
161
+ sh += k;
162
+ if(sh >= this.DB) sh -= this.DB;
163
+ }
164
+ if(k == 8 && (s[0]&0x80) != 0) {
165
+ this.s = -1;
166
+ if(sh > 0) this[this.t-1] |= ((1<<(this.DB-sh))-1)<<sh;
167
+ }
168
+ this.clamp();
169
+ if(mi) BigInteger.ZERO.subTo(this,this);
170
+ }
171
+
172
+ // (protected) clamp off excess high words
173
+ function bnpClamp() {
174
+ var c = this.s&this.DM;
175
+ while(this.t > 0 && this[this.t-1] == c) --this.t;
176
+ }
177
+
178
+ // (public) return string representation in given radix
179
+ function bnToString(b) {
180
+ if(this.s < 0) return "-"+this.negate().toString(b);
181
+ var k;
182
+ if(b == 16) k = 4;
183
+ else if(b == 8) k = 3;
184
+ else if(b == 2) k = 1;
185
+ else if(b == 32) k = 5;
186
+ else if(b == 4) k = 2;
187
+ else return this.toRadix(b);
188
+ var km = (1<<k)-1, d, m = false, r = "", i = this.t;
189
+ var p = this.DB-(i*this.DB)%k;
190
+ if(i-- > 0) {
191
+ if(p < this.DB && (d = this[i]>>p) > 0) { m = true; r = int2char(d); }
192
+ while(i >= 0) {
193
+ if(p < k) {
194
+ d = (this[i]&((1<<p)-1))<<(k-p);
195
+ d |= this[--i]>>(p+=this.DB-k);
196
+ }
197
+ else {
198
+ d = (this[i]>>(p-=k))&km;
199
+ if(p <= 0) { p += this.DB; --i; }
200
+ }
201
+ if(d > 0) m = true;
202
+ if(m) r += int2char(d);
203
+ }
204
+ }
205
+ return m?r:"0";
206
+ }
207
+
208
+ // (public) -this
209
+ function bnNegate() { var r = nbi(); BigInteger.ZERO.subTo(this,r); return r; }
210
+
211
+ // (public) |this|
212
+ function bnAbs() { return (this.s<0)?this.negate():this; }
213
+
214
+ // (public) return + if this > a, - if this < a, 0 if equal
215
+ function bnCompareTo(a) {
216
+ var r = this.s-a.s;
217
+ if(r != 0) return r;
218
+ var i = this.t;
219
+ r = i-a.t;
220
+ if(r != 0) return r;
221
+ while(--i >= 0) if((r=this[i]-a[i]) != 0) return r;
222
+ return 0;
223
+ }
224
+
225
+ // returns bit length of the integer x
226
+ function nbits(x) {
227
+ var r = 1, t;
228
+ if((t=x>>>16) != 0) { x = t; r += 16; }
229
+ if((t=x>>8) != 0) { x = t; r += 8; }
230
+ if((t=x>>4) != 0) { x = t; r += 4; }
231
+ if((t=x>>2) != 0) { x = t; r += 2; }
232
+ if((t=x>>1) != 0) { x = t; r += 1; }
233
+ return r;
234
+ }
235
+
236
+ // (public) return the number of bits in "this"
237
+ function bnBitLength() {
238
+ if(this.t <= 0) return 0;
239
+ return this.DB*(this.t-1)+nbits(this[this.t-1]^(this.s&this.DM));
240
+ }
241
+
242
+ // (protected) r = this << n*DB
243
+ function bnpDLShiftTo(n,r) {
244
+ var i;
245
+ for(i = this.t-1; i >= 0; --i) r[i+n] = this[i];
246
+ for(i = n-1; i >= 0; --i) r[i] = 0;
247
+ r.t = this.t+n;
248
+ r.s = this.s;
249
+ }
250
+
251
+ // (protected) r = this >> n*DB
252
+ function bnpDRShiftTo(n,r) {
253
+ for(var i = n; i < this.t; ++i) r[i-n] = this[i];
254
+ r.t = Math.max(this.t-n,0);
255
+ r.s = this.s;
256
+ }
257
+
258
+ // (protected) r = this << n
259
+ function bnpLShiftTo(n,r) {
260
+ var bs = n%this.DB;
261
+ var cbs = this.DB-bs;
262
+ var bm = (1<<cbs)-1;
263
+ var ds = Math.floor(n/this.DB), c = (this.s<<bs)&this.DM, i;
264
+ for(i = this.t-1; i >= 0; --i) {
265
+ r[i+ds+1] = (this[i]>>cbs)|c;
266
+ c = (this[i]&bm)<<bs;
267
+ }
268
+ for(i = ds-1; i >= 0; --i) r[i] = 0;
269
+ r[ds] = c;
270
+ r.t = this.t+ds+1;
271
+ r.s = this.s;
272
+ r.clamp();
273
+ }
274
+
275
+ // (protected) r = this >> n
276
+ function bnpRShiftTo(n,r) {
277
+ r.s = this.s;
278
+ var ds = Math.floor(n/this.DB);
279
+ if(ds >= this.t) { r.t = 0; return; }
280
+ var bs = n%this.DB;
281
+ var cbs = this.DB-bs;
282
+ var bm = (1<<bs)-1;
283
+ r[0] = this[ds]>>bs;
284
+ for(var i = ds+1; i < this.t; ++i) {
285
+ r[i-ds-1] |= (this[i]&bm)<<cbs;
286
+ r[i-ds] = this[i]>>bs;
287
+ }
288
+ if(bs > 0) r[this.t-ds-1] |= (this.s&bm)<<cbs;
289
+ r.t = this.t-ds;
290
+ r.clamp();
291
+ }
292
+
293
+ // (protected) r = this - a
294
+ function bnpSubTo(a,r) {
295
+ var i = 0, c = 0, m = Math.min(a.t,this.t);
296
+ while(i < m) {
297
+ c += this[i]-a[i];
298
+ r[i++] = c&this.DM;
299
+ c >>= this.DB;
300
+ }
301
+ if(a.t < this.t) {
302
+ c -= a.s;
303
+ while(i < this.t) {
304
+ c += this[i];
305
+ r[i++] = c&this.DM;
306
+ c >>= this.DB;
307
+ }
308
+ c += this.s;
309
+ }
310
+ else {
311
+ c += this.s;
312
+ while(i < a.t) {
313
+ c -= a[i];
314
+ r[i++] = c&this.DM;
315
+ c >>= this.DB;
316
+ }
317
+ c -= a.s;
318
+ }
319
+ r.s = (c<0)?-1:0;
320
+ if(c < -1) r[i++] = this.DV+c;
321
+ else if(c > 0) r[i++] = c;
322
+ r.t = i;
323
+ r.clamp();
324
+ }
325
+
326
+ // (protected) r = this * a, r != this,a (HAC 14.12)
327
+ // "this" should be the larger one if appropriate.
328
+ function bnpMultiplyTo(a,r) {
329
+ var x = this.abs(), y = a.abs();
330
+ var i = x.t;
331
+ r.t = i+y.t;
332
+ while(--i >= 0) r[i] = 0;
333
+ for(i = 0; i < y.t; ++i) r[i+x.t] = x.am(0,y[i],r,i,0,x.t);
334
+ r.s = 0;
335
+ r.clamp();
336
+ if(this.s != a.s) BigInteger.ZERO.subTo(r,r);
337
+ }
338
+
339
+ // (protected) r = this^2, r != this (HAC 14.16)
340
+ function bnpSquareTo(r) {
341
+ var x = this.abs();
342
+ var i = r.t = 2*x.t;
343
+ while(--i >= 0) r[i] = 0;
344
+ for(i = 0; i < x.t-1; ++i) {
345
+ var c = x.am(i,x[i],r,2*i,0,1);
346
+ if((r[i+x.t]+=x.am(i+1,2*x[i],r,2*i+1,c,x.t-i-1)) >= x.DV) {
347
+ r[i+x.t] -= x.DV;
348
+ r[i+x.t+1] = 1;
349
+ }
350
+ }
351
+ if(r.t > 0) r[r.t-1] += x.am(i,x[i],r,2*i,0,1);
352
+ r.s = 0;
353
+ r.clamp();
354
+ }
355
+
356
+ // (protected) divide this by m, quotient and remainder to q, r (HAC 14.20)
357
+ // r != q, this != m. q or r may be null.
358
+ function bnpDivRemTo(m,q,r) {
359
+ var pm = m.abs();
360
+ if(pm.t <= 0) return;
361
+ var pt = this.abs();
362
+ if(pt.t < pm.t) {
363
+ if(q != null) q.fromInt(0);
364
+ if(r != null) this.copyTo(r);
365
+ return;
366
+ }
367
+ if(r == null) r = nbi();
368
+ var y = nbi(), ts = this.s, ms = m.s;
369
+ var nsh = this.DB-nbits(pm[pm.t-1]); // normalize modulus
370
+ if(nsh > 0) { pm.lShiftTo(nsh,y); pt.lShiftTo(nsh,r); }
371
+ else { pm.copyTo(y); pt.copyTo(r); }
372
+ var ys = y.t;
373
+ var y0 = y[ys-1];
374
+ if(y0 == 0) return;
375
+ var yt = y0*(1<<this.F1)+((ys>1)?y[ys-2]>>this.F2:0);
376
+ var d1 = this.FV/yt, d2 = (1<<this.F1)/yt, e = 1<<this.F2;
377
+ var i = r.t, j = i-ys, t = (q==null)?nbi():q;
378
+ y.dlShiftTo(j,t);
379
+ if(r.compareTo(t) >= 0) {
380
+ r[r.t++] = 1;
381
+ r.subTo(t,r);
382
+ }
383
+ BigInteger.ONE.dlShiftTo(ys,t);
384
+ t.subTo(y,y); // "negative" y so we can replace sub with am later
385
+ while(y.t < ys) y[y.t++] = 0;
386
+ while(--j >= 0) {
387
+ // Estimate quotient digit
388
+ var qd = (r[--i]==y0)?this.DM:Math.floor(r[i]*d1+(r[i-1]+e)*d2);
389
+ if((r[i]+=y.am(0,qd,r,j,0,ys)) < qd) { // Try it out
390
+ y.dlShiftTo(j,t);
391
+ r.subTo(t,r);
392
+ while(r[i] < --qd) r.subTo(t,r);
393
+ }
394
+ }
395
+ if(q != null) {
396
+ r.drShiftTo(ys,q);
397
+ if(ts != ms) BigInteger.ZERO.subTo(q,q);
398
+ }
399
+ r.t = ys;
400
+ r.clamp();
401
+ if(nsh > 0) r.rShiftTo(nsh,r); // Denormalize remainder
402
+ if(ts < 0) BigInteger.ZERO.subTo(r,r);
403
+ }
404
+
405
+ // (public) this mod a
406
+ function bnMod(a) {
407
+ var r = nbi();
408
+ this.abs().divRemTo(a,null,r);
409
+ if(this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r,r);
410
+ return r;
411
+ }
412
+
413
+ // Modular reduction using "classic" algorithm
414
+ function Classic(m) { this.m = m; }
415
+ function cConvert(x) {
416
+ if(x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m);
417
+ else return x;
418
+ }
419
+ function cRevert(x) { return x; }
420
+ function cReduce(x) { x.divRemTo(this.m,null,x); }
421
+ function cMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
422
+ function cSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
423
+
424
+ Classic.prototype.convert = cConvert;
425
+ Classic.prototype.revert = cRevert;
426
+ Classic.prototype.reduce = cReduce;
427
+ Classic.prototype.mulTo = cMulTo;
428
+ Classic.prototype.sqrTo = cSqrTo;
429
+
430
+ // (protected) return "-1/this % 2^DB"; useful for Mont. reduction
431
+ // justification:
432
+ // xy == 1 (mod m)
433
+ // xy = 1+km
434
+ // xy(2-xy) = (1+km)(1-km)
435
+ // x[y(2-xy)] = 1-k^2m^2
436
+ // x[y(2-xy)] == 1 (mod m^2)
437
+ // if y is 1/x mod m, then y(2-xy) is 1/x mod m^2
438
+ // should reduce x and y(2-xy) by m^2 at each step to keep size bounded.
439
+ // JS multiply "overflows" differently from C/C++, so care is needed here.
440
+ function bnpInvDigit() {
441
+ if(this.t < 1) return 0;
442
+ var x = this[0];
443
+ if((x&1) == 0) return 0;
444
+ var y = x&3; // y == 1/x mod 2^2
445
+ y = (y*(2-(x&0xf)*y))&0xf; // y == 1/x mod 2^4
446
+ y = (y*(2-(x&0xff)*y))&0xff; // y == 1/x mod 2^8
447
+ y = (y*(2-(((x&0xffff)*y)&0xffff)))&0xffff; // y == 1/x mod 2^16
448
+ // last step - calculate inverse mod DV directly;
449
+ // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints
450
+ y = (y*(2-x*y%this.DV))%this.DV; // y == 1/x mod 2^dbits
451
+ // we really want the negative inverse, and -DV < y < DV
452
+ return (y>0)?this.DV-y:-y;
453
+ }
454
+
455
+ // Montgomery reduction
456
+ function Montgomery(m) {
457
+ this.m = m;
458
+ this.mp = m.invDigit();
459
+ this.mpl = this.mp&0x7fff;
460
+ this.mph = this.mp>>15;
461
+ this.um = (1<<(m.DB-15))-1;
462
+ this.mt2 = 2*m.t;
463
+ }
464
+
465
+ // xR mod m
466
+ function montConvert(x) {
467
+ var r = nbi();
468
+ x.abs().dlShiftTo(this.m.t,r);
469
+ r.divRemTo(this.m,null,r);
470
+ if(x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r,r);
471
+ return r;
472
+ }
473
+
474
+ // x/R mod m
475
+ function montRevert(x) {
476
+ var r = nbi();
477
+ x.copyTo(r);
478
+ this.reduce(r);
479
+ return r;
480
+ }
481
+
482
+ // x = x/R mod m (HAC 14.32)
483
+ function montReduce(x) {
484
+ while(x.t <= this.mt2) // pad x so am has enough room later
485
+ x[x.t++] = 0;
486
+ for(var i = 0; i < this.m.t; ++i) {
487
+ // faster way of calculating u0 = x[i]*mp mod DV
488
+ var j = x[i]&0x7fff;
489
+ var u0 = (j*this.mpl+(((j*this.mph+(x[i]>>15)*this.mpl)&this.um)<<15))&x.DM;
490
+ // use am to combine the multiply-shift-add into one call
491
+ j = i+this.m.t;
492
+ x[j] += this.m.am(0,u0,x,i,0,this.m.t);
493
+ // propagate carry
494
+ while(x[j] >= x.DV) { x[j] -= x.DV; x[++j]++; }
495
+ }
496
+ x.clamp();
497
+ x.drShiftTo(this.m.t,x);
498
+ if(x.compareTo(this.m) >= 0) x.subTo(this.m,x);
499
+ }
500
+
501
+ // r = "x^2/R mod m"; x != r
502
+ function montSqrTo(x,r) { x.squareTo(r); this.reduce(r); }
503
+
504
+ // r = "xy/R mod m"; x,y != r
505
+ function montMulTo(x,y,r) { x.multiplyTo(y,r); this.reduce(r); }
506
+
507
+ Montgomery.prototype.convert = montConvert;
508
+ Montgomery.prototype.revert = montRevert;
509
+ Montgomery.prototype.reduce = montReduce;
510
+ Montgomery.prototype.mulTo = montMulTo;
511
+ Montgomery.prototype.sqrTo = montSqrTo;
512
+
513
+ // (protected) true iff this is even
514
+ function bnpIsEven() { return ((this.t>0)?(this[0]&1):this.s) == 0; }
515
+
516
+ // (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79)
517
+ function bnpExp(e,z) {
518
+ if(e > 0xffffffff || e < 1) return BigInteger.ONE;
519
+ var r = nbi(), r2 = nbi(), g = z.convert(this), i = nbits(e)-1;
520
+ g.copyTo(r);
521
+ while(--i >= 0) {
522
+ z.sqrTo(r,r2);
523
+ if((e&(1<<i)) > 0) z.mulTo(r2,g,r);
524
+ else { var t = r; r = r2; r2 = t; }
525
+ }
526
+ return z.revert(r);
527
+ }
528
+
529
+ // (public) this^e % m, 0 <= e < 2^32
530
+ function bnModPowInt(e,m) {
531
+ var z;
532
+ if(e < 256 || m.isEven()) z = new Classic(m); else z = new Montgomery(m);
533
+ return this.exp(e,z);
534
+ }
535
+
536
+ // protected
537
+ BigInteger.prototype.copyTo = bnpCopyTo;
538
+ BigInteger.prototype.fromInt = bnpFromInt;
539
+ BigInteger.prototype.fromString = bnpFromString;
540
+ BigInteger.prototype.clamp = bnpClamp;
541
+ BigInteger.prototype.dlShiftTo = bnpDLShiftTo;
542
+ BigInteger.prototype.drShiftTo = bnpDRShiftTo;
543
+ BigInteger.prototype.lShiftTo = bnpLShiftTo;
544
+ BigInteger.prototype.rShiftTo = bnpRShiftTo;
545
+ BigInteger.prototype.subTo = bnpSubTo;
546
+ BigInteger.prototype.multiplyTo = bnpMultiplyTo;
547
+ BigInteger.prototype.squareTo = bnpSquareTo;
548
+ BigInteger.prototype.divRemTo = bnpDivRemTo;
549
+ BigInteger.prototype.invDigit = bnpInvDigit;
550
+ BigInteger.prototype.isEven = bnpIsEven;
551
+ BigInteger.prototype.exp = bnpExp;
552
+
553
+ // public
554
+ BigInteger.prototype.toString = bnToString;
555
+ BigInteger.prototype.negate = bnNegate;
556
+ BigInteger.prototype.abs = bnAbs;
557
+ BigInteger.prototype.compareTo = bnCompareTo;
558
+ BigInteger.prototype.bitLength = bnBitLength;
559
+ BigInteger.prototype.mod = bnMod;
560
+ BigInteger.prototype.modPowInt = bnModPowInt;
561
+
562
+ // "constants"
563
+ BigInteger.ZERO = nbv(0);
564
+ BigInteger.ONE = nbv(1);
565
+ // prng4.js - uses Arcfour as a PRNG
566
+
567
+ function Arcfour() {
568
+ this.i = 0;
569
+ this.j = 0;
570
+ this.S = new Array();
571
+ }
572
+
573
+ // Initialize arcfour context from key, an array of ints, each from [0..255]
574
+ function ARC4init(key) {
575
+ var i, j, t;
576
+ for(i = 0; i < 256; ++i)
577
+ this.S[i] = i;
578
+ j = 0;
579
+ for(i = 0; i < 256; ++i) {
580
+ j = (j + this.S[i] + key[i % key.length]) & 255;
581
+ t = this.S[i];
582
+ this.S[i] = this.S[j];
583
+ this.S[j] = t;
584
+ }
585
+ this.i = 0;
586
+ this.j = 0;
587
+ }
588
+
589
+ function ARC4next() {
590
+ var t;
591
+ this.i = (this.i + 1) & 255;
592
+ this.j = (this.j + this.S[this.i]) & 255;
593
+ t = this.S[this.i];
594
+ this.S[this.i] = this.S[this.j];
595
+ this.S[this.j] = t;
596
+ return this.S[(t + this.S[this.i]) & 255];
597
+ }
598
+
599
+ Arcfour.prototype.init = ARC4init;
600
+ Arcfour.prototype.next = ARC4next;
601
+
602
+ // Plug in your RNG constructor here
603
+ function prng_newstate() {
604
+ return new Arcfour();
605
+ }
606
+
607
+ // Pool size must be a multiple of 4 and greater than 32.
608
+ // An array of bytes the size of the pool will be passed to init()
609
+ var rng_psize = 256;
610
+ // Random number generator - requires a PRNG backend, e.g. prng4.js
611
+
612
+ // For best results, put code like
613
+ // <body onClick='rng_seed_time();' onKeyPress='rng_seed_time();'>
614
+ // in your main HTML document.
615
+
616
+ var rng_state;
617
+ var rng_pool;
618
+ var rng_pptr;
619
+
620
+ // Mix in a 32-bit integer into the pool
621
+ function rng_seed_int(x) {
622
+ rng_pool[rng_pptr++] ^= x & 255;
623
+ rng_pool[rng_pptr++] ^= (x >> 8) & 255;
624
+ rng_pool[rng_pptr++] ^= (x >> 16) & 255;
625
+ rng_pool[rng_pptr++] ^= (x >> 24) & 255;
626
+ if(rng_pptr >= rng_psize) rng_pptr -= rng_psize;
627
+ }
628
+
629
+ // Mix in the current time (w/milliseconds) into the pool
630
+ function rng_seed_time() {
631
+ rng_seed_int(new Date().getTime());
632
+ }
633
+
634
+ // Initialize the pool with junk if needed.
635
+ if(rng_pool == null) {
636
+ rng_pool = new Array();
637
+ rng_pptr = 0;
638
+ var t;
639
+ if(navigator.appName == "Netscape" && navigator.appVersion < "5" && window.crypto) {
640
+ // Extract entropy (256 bits) from NS4 RNG if available
641
+ var z = window.crypto.random(32);
642
+ for(t = 0; t < z.length; ++t)
643
+ rng_pool[rng_pptr++] = z.charCodeAt(t) & 255;
644
+ }
645
+ while(rng_pptr < rng_psize) { // extract some randomness from Math.random()
646
+ t = Math.floor(65536 * Math.random());
647
+ rng_pool[rng_pptr++] = t >>> 8;
648
+ rng_pool[rng_pptr++] = t & 255;
649
+ }
650
+ rng_pptr = 0;
651
+ rng_seed_time();
652
+ //rng_seed_int(window.screenX);
653
+ //rng_seed_int(window.screenY);
654
+ }
655
+
656
+ function rng_get_byte() {
657
+ if(rng_state == null) {
658
+ rng_seed_time();
659
+ rng_state = prng_newstate();
660
+ rng_state.init(rng_pool);
661
+ for(rng_pptr = 0; rng_pptr < rng_pool.length; ++rng_pptr)
662
+ rng_pool[rng_pptr] = 0;
663
+ rng_pptr = 0;
664
+ //rng_pool = null;
665
+ }
666
+ // TODO: allow reseeding after first request
667
+ return rng_state.next();
668
+ }
669
+
670
+ function rng_get_bytes(ba) {
671
+ var i;
672
+ for(i = 0; i < ba.length; ++i) ba[i] = rng_get_byte();
673
+ }
674
+
675
+ function SecureRandom() {}
676
+
677
+ SecureRandom.prototype.nextBytes = rng_get_bytes;
678
+ // Depends on jsbn.js and rng.js
679
+
680
+ // convert a (hex) string to a bignum object
681
+ function parseBigInt(str,r) {
682
+ return new BigInteger(str,r);
683
+ }
684
+
685
+ function linebrk(s,n) {
686
+ var ret = "";
687
+ var i = 0;
688
+ while(i + n < s.length) {
689
+ ret += s.substring(i,i+n) + "\n";
690
+ i += n;
691
+ }
692
+ return ret + s.substring(i,s.length);
693
+ }
694
+
695
+ function byte2Hex(b) {
696
+ if(b < 0x10)
697
+ return "0" + b.toString(16);
698
+ else
699
+ return b.toString(16);
700
+ }
701
+
702
+ // PKCS#1 (type 2, random) pad input string s to n bytes, and return a bigint
703
+ function pkcs1pad2(s,n) {
704
+ if(n < s.length + 11) {
705
+ alert("Message too long for RSA");
706
+ return null;
707
+ }
708
+ var ba = new Array();
709
+ var i = s.length - 1;
710
+ while(i >= 0 && n > 0) ba[--n] = s.charCodeAt(i--);
711
+ ba[--n] = 0;
712
+ var rng = new SecureRandom();
713
+ var x = new Array();
714
+ while(n > 2) { // random non-zero pad
715
+ x[0] = 0;
716
+ while(x[0] == 0) rng.nextBytes(x);
717
+ ba[--n] = x[0];
718
+ }
719
+ ba[--n] = 2;
720
+ ba[--n] = 0;
721
+ return new BigInteger(ba);
722
+ }
723
+
724
+ // "empty" RSA key constructor
725
+ function RSAKey() {
726
+ this.n = null;
727
+ this.e = 0;
728
+ this.d = null;
729
+ this.p = null;
730
+ this.q = null;
731
+ this.dmp1 = null;
732
+ this.dmq1 = null;
733
+ this.coeff = null;
734
+ }
735
+
736
+ // Set the public key fields N and e from hex strings
737
+ function RSASetPublic(N,E) {
738
+ if(N != null && E != null && N.length > 0 && E.length > 0) {
739
+ this.n = parseBigInt(N,16);
740
+ this.e = parseInt(E,16);
741
+ }
742
+ else
743
+ alert("Invalid RSA public key");
744
+ }
745
+
746
+ // Perform raw public operation on "x": return x^e (mod n)
747
+ function RSADoPublic(x) {
748
+ return x.modPowInt(this.e, this.n);
749
+ }
750
+
751
+ // Return the PKCS#1 RSA encryption of "text" as an even-length hex string
752
+ function RSAEncrypt(text) {
753
+ var m = pkcs1pad2(text,(this.n.bitLength()+7)>>3);
754
+ if(m == null) return null;
755
+ var c = this.doPublic(m);
756
+ if(c == null) return null;
757
+ var h = c.toString(16);
758
+ if((h.length & 1) == 0) return h; else return "0" + h;
759
+ }
760
+
761
+ // Return the PKCS#1 RSA encryption of "text" as a Base64-encoded string
762
+ //function RSAEncryptB64(text) {
763
+ // var h = this.encrypt(text);
764
+ // if(h) return hex2b64(h); else return null;
765
+ //}
766
+
767
+ // protected
768
+ RSAKey.prototype.doPublic = RSADoPublic;
769
+
770
+ // public
771
+ RSAKey.prototype.setPublic = RSASetPublic;
772
+ RSAKey.prototype.encrypt = RSAEncrypt;
773
+ //RSAKey.prototype.encrypt_b64 = RSAEncryptB64;
774
+ var b64map="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
775
+ var b64pad="=";
776
+
777
+ function hex2b64(h) {
778
+ var i;
779
+ var c;
780
+ var ret = "";
781
+ for(i = 0; i+3 <= h.length; i+=3) {
782
+ c = parseInt(h.substring(i,i+3),16);
783
+ ret += b64map.charAt(c >> 6) + b64map.charAt(c & 63);
784
+ }
785
+ if(i+1 == h.length) {
786
+ c = parseInt(h.substring(i,i+1),16);
787
+ ret += b64map.charAt(c << 2);
788
+ }
789
+ else if(i+2 == h.length) {
790
+ c = parseInt(h.substring(i,i+2),16);
791
+ ret += b64map.charAt(c >> 2) + b64map.charAt((c & 3) << 4);
792
+ }
793
+ while((ret.length & 3) > 0) ret += b64pad;
794
+ return ret;
795
+ }
796
+
797
+ // convert a base64 string to hex
798
+ function b64tohex(s) {
799
+ var ret = ""
800
+ var i;
801
+ var k = 0; // b64 state, 0-3
802
+ var slop;
803
+ for(i = 0; i < s.length; ++i) {
804
+ if(s.charAt(i) == b64pad) break;
805
+ v = b64map.indexOf(s.charAt(i));
806
+ if(v < 0) continue;
807
+ if(k == 0) {
808
+ ret += int2char(v >> 2);
809
+ slop = v & 3;
810
+ k = 1;
811
+ }
812
+ else if(k == 1) {
813
+ ret += int2char((slop << 2) | (v >> 4));
814
+ slop = v & 0xf;
815
+ k = 2;
816
+ }
817
+ else if(k == 2) {
818
+ ret += int2char(slop);
819
+ ret += int2char(v >> 2);
820
+ slop = v & 3;
821
+ k = 3;
822
+ }
823
+ else {
824
+ ret += int2char((slop << 2) | (v >> 4));
825
+ ret += int2char(v & 0xf);
826
+ k = 0;
827
+ }
828
+ }
829
+ if(k == 1)
830
+ ret += int2char(slop << 2);
831
+ return ret;
832
+ }
833
+
834
+ // convert a base64 string to a byte/number array
835
+ function b64toBA(s) {
836
+ //piggyback on b64tohex for now, optimize later
837
+ var h = b64tohex(s);
838
+ var i;
839
+ var a = new Array();
840
+ for(i = 0; 2*i < h.length; ++i) {
841
+ a[i] = parseInt(h.substring(2*i,2*i+2),16);
842
+ }
843
+ return a;
844
+ }