carapace 0.1.0

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