megar 0.0.1

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 (51) hide show
  1. data/.gitignore +19 -0
  2. data/.rspec +1 -0
  3. data/.rvmrc +1 -0
  4. data/.travis.yml +11 -0
  5. data/CHANGELOG +5 -0
  6. data/Gemfile +4 -0
  7. data/Guardfile +11 -0
  8. data/LICENSE +22 -0
  9. data/README.rdoc +218 -0
  10. data/Rakefile +33 -0
  11. data/bin/megar +16 -0
  12. data/lib/extensions/math.rb +13 -0
  13. data/lib/js_ref_impl/_README +9 -0
  14. data/lib/js_ref_impl/base64_1.js +83 -0
  15. data/lib/js_ref_impl/crypto_5.js +1795 -0
  16. data/lib/js_ref_impl/download_8.js +867 -0
  17. data/lib/js_ref_impl/hex_1.js +76 -0
  18. data/lib/js_ref_impl/index_9.js +666 -0
  19. data/lib/js_ref_impl/js.manifest +115 -0
  20. data/lib/js_ref_impl/rsa_1.js +456 -0
  21. data/lib/js_ref_impl/sjcl_1.js +1 -0
  22. data/lib/js_ref_impl/upload_10.js +691 -0
  23. data/lib/megar.rb +11 -0
  24. data/lib/megar/catalog.rb +5 -0
  25. data/lib/megar/catalog/catalog_item.rb +90 -0
  26. data/lib/megar/catalog/file.rb +14 -0
  27. data/lib/megar/catalog/files.rb +13 -0
  28. data/lib/megar/catalog/folder.rb +20 -0
  29. data/lib/megar/catalog/folders.rb +28 -0
  30. data/lib/megar/connection.rb +84 -0
  31. data/lib/megar/crypto.rb +399 -0
  32. data/lib/megar/exception.rb +55 -0
  33. data/lib/megar/session.rb +157 -0
  34. data/lib/megar/shell.rb +87 -0
  35. data/lib/megar/version.rb +3 -0
  36. data/megar.gemspec +30 -0
  37. data/spec/fixtures/crypto_expectations/sample_user.json +109 -0
  38. data/spec/spec_helper.rb +24 -0
  39. data/spec/support/crypto_expectations_helper.rb +44 -0
  40. data/spec/support/mocks_helper.rb +22 -0
  41. data/spec/unit/catalog/file_spec.rb +31 -0
  42. data/spec/unit/catalog/files_spec.rb +26 -0
  43. data/spec/unit/catalog/folder_spec.rb +28 -0
  44. data/spec/unit/catalog/folders_spec.rb +49 -0
  45. data/spec/unit/connection_spec.rb +50 -0
  46. data/spec/unit/crypto_spec.rb +476 -0
  47. data/spec/unit/exception_spec.rb +35 -0
  48. data/spec/unit/extensions/math_spec.rb +21 -0
  49. data/spec/unit/session_spec.rb +146 -0
  50. data/spec/unit/shell_spec.rb +18 -0
  51. metadata +238 -0
@@ -0,0 +1,115 @@
1
+ sh1['lang/af_9.json'] = [-1417274822,-2083361332,-757349909,-256227082,-194525256,1772665401,-562569639,-1824830515];
2
+ sh1['lang/ar_10.json'] = [1235799322,9155442,2110091259,-2136114012,912938604,-808272302,-1795602172,-1201886664];
3
+ sh1['lang/br_9.json'] = [1242538198,1608684115,-59288908,-176878086,1418649338,739302207,965271337,-973738017];
4
+ sh1['lang/bs_9.json'] = [189392850,243571116,1301007014,-1446438592,-630828613,-37643582,-498811045,-689467094];
5
+ sh1['lang/ca_9.json'] = [-444218915,-1314480278,-265134042,-990054495,-2099780978,293235452,-1571699718,1343107885];
6
+ sh1['lang/cn_10.json'] = [1586464771,530181140,243816546,-784300980,1893149622,1558328722,1319347247,-1946086886];
7
+ sh1['lang/ct_10.json'] = [862123335,449522135,-1956260987,956107799,-1261551418,-1248958651,-2026875914,-981536878];
8
+ sh1['lang/cz_11.json'] = [-314078848,1906297063,958692511,-344635755,674625387,-688162136,-1459316586,672132346];
9
+ sh1['lang/de_18.json'] = [942315312,490928411,309437530,1925110338,-921653494,503766771,1408815300,2009507732];
10
+ sh1['lang/dk_9.json'] = [-1171541107,1339402631,451435825,665546601,-90154274,505058228,1506857006,-528270856];
11
+ sh1['lang/en_10.json'] = [1395646144,-432346362,-381776523,1593155331,-1086411019,563567999,-1656688732,-535086718];
12
+ sh1['lang/es_12.json'] = [665910051,-1955775882,-559234723,-803837355,2110501300,497319110,1805558941,557800806];
13
+ sh1['lang/eu_9.json'] = [-1442692119,102552333,50140116,1956924778,-1992982475,-1387358899,-543142937,-877305666];
14
+ sh1['lang/fi_11.json'] = [-1257785185,-795977108,-1741326466,1869929457,-1690730598,-312282688,-1637031797,666885765];
15
+ sh1['lang/fr_12.json'] = [-1482611511,570083086,406155144,2123561076,1389000104,-1467778098,-1599800490,707335320];
16
+ sh1['lang/he_9.json'] = [-1084644158,134495674,-1702906024,-624273163,930295260,1979957744,1424180720,-761247189];
17
+ sh1['lang/hu_8.json'] = [-626335876,1007689330,-529295500,1510273899,581371199,-1902967911,-297445765,-998995668];
18
+ sh1['lang/it_11.json'] = [-717132397,-1265515515,2144685660,-1083029140,194143163,-1890492845,2143453798,-1473804726];
19
+ sh1['lang/jp_13.json'] = [-1660895194,307595636,520579794,-1861404801,1064575997,-1515112197,-11929196,-1495667509];
20
+ sh1['lang/kr_9.json'] = [1602391252,-156781249,-1235260808,1749855881,-134715681,290629394,1476526107,-1565861976];
21
+ sh1['lang/mi_9.json'] = [-1921266744,-2031455335,2082993485,694931044,-1730467013,1490940215,-516142412,916502000];
22
+ sh1['lang/nl_10.json'] = [631898679,-1230686836,2041349814,62910108,-1918380002,1123092150,-14296908,-983410608];
23
+ sh1['lang/no_10.json'] = [-1687122975,1008188103,861009501,116476897,-1061839221,1702136543,-1392454654,-515583843];
24
+ sh1['lang/pl_11.json'] = [-2056280233,1864447692,1654598904,1971482219,445325606,-682713388,1128841617,1504498186];
25
+ sh1['lang/pt_9.json'] = [886385382,1956215302,1534413984,2032462336,-965366753,-343081312,574049850,-923397074];
26
+ sh1['lang/ro_9.json'] = [-514402454,-1312757612,447848157,1121931621,-197279212,-1183046062,1061822919,1419724718];
27
+ sh1['lang/ru_12.json'] = [1212450100,515057698,-1323290342,1964893669,-1628281075,-951402062,393037624,465530221];
28
+ sh1['lang/se_11.json'] = [1880051918,-310573777,-327443094,-346373274,159652734,1000056817,71462234,-210778467];
29
+ sh1['lang/sk_10.json'] = [-179472912,336427433,-1479565582,-1092739223,-705314812,-1398186025,1374113247,1351426577];
30
+ sh1['lang/sl_9.json'] = [-1762650877,-305098928,393780837,-597401058,1885221857,-1025952330,-608056236,-1846365058];
31
+ sh1['lang/tr_10.json'] = [1433782800,1760820256,1007137501,64802347,-1951067245,-500302034,-1791197501,-481990005];
32
+ sh1['lang/vn_9.json'] = [-1763760986,653641479,1904931733,313505685,-829053283,158260617,2042634646,1789230711];
33
+ sh1['json_1.js'] = [260740289,1366618322,-425421514,1426645344,1815060041,-1445234843,-570158005,-1264018126];
34
+ sh1['sjcl_1.js'] = [-171772505,-264193555,357626960,-1993441797,930271978,2002120912,114344546,1757807051];
35
+ sh1['functions_5.js'] = [1807891267,-1588116585,959784928,-202022813,326395229,1318281623,-904527844,-1775791863];
36
+ sh1['countries_1.js'] = [-1429991108,-360077332,2087722987,830335793,940682070,-184269370,-807595443,666823178];
37
+ sh1['rsa_1.js'] = [2001167291,-526423915,1610203002,735871105,-288977147,281057591,529538147,-1392450329];
38
+ sh1['base64_1.js'] = [1377965723,-820855789,-623852925,-619566876,-131428866,-1938828440,320381032,94642301];
39
+ sh1['hex_1.js'] = [-536915967,1050992997,-349767130,933847197,-1115174222,701434549,2060891637,583948869];
40
+ sh1['mouse_2.js'] = [479653427,11848959,2065008764,-220479196,1718261440,-225582138,501675892,-1487275805];
41
+ sh1['keygen_3.js'] = [1782191324,1257655688,1757987168,-20394106,600299018,741954078,-1296374862,880480498];
42
+ sh1['extjs/ext-all_1.js'] = [1262933215,96301843,-1571790653,1392206008,-1368444059,-1176178047,416916212,1743632601];
43
+ sh1['cleartemp_1.js'] = [1956223068,-1532289288,2073658448,-1672893692,-1425892485,-1269642078,-1088621579,740221379];
44
+ sh1['crypto_5.js'] = [742870084,-660161097,-2114019053,-1294453450,-544806641,1071765090,-1511904046,201976673];
45
+ sh1['swfobject_1.js'] = [-2038982885,295488952,720803839,235467288,1225594129,1864506692,807345352,2038327252];
46
+ sh1['user_4.js'] = [945768895,150969340,831343257,232034011,-300876947,-913567226,1206029299,1666935234];
47
+ sh1['upload_10.js'] = [1297305363,-2069320082,-1228724989,-1810609427,2047682434,-675901936,-239522232,-666096561];
48
+ sh1['download_8.js'] = [1995047484,-249618757,-1306466548,40840577,-1159223764,461840606,-381325450,-1897246195];
49
+ sh1['filedrag_1.js'] = [-3771780,1486712778,1679413561,901547761,1464674895,2145986770,-646287617,-2073595498];
50
+ sh1['jquery-min_1.js'] = [267545618,410969040,-257332869,-1568370133,-1372876779,1961677582,-450016788,-533646155];
51
+ sh1['jquery-ui_1.js'] = [-742248723,-305520618,161981736,2090598087,-901854626,-1850226703,932365935,-900440464];
52
+ sh1['tool_5.js'] = [-887281824,-1151929678,-1766512342,-127997671,1710303281,745056990,133956977,1177066920];
53
+ sh1['filetypes_1.js'] = [-789731113,-108604875,-919205671,-597553477,-622600853,-905048523,-934476178,-944058599];
54
+ sh1['pages/top_1.html'] = [-2073564756,-487500319,-1379919808,889678958,2137116139,282908245,-1991242911,-1504710623];
55
+ sh1['pages/topl_2.html'] = [699128006,-472690044,-1047628123,-1081905379,977316143,1418607394,1938259125,1869997200];
56
+ sh1['pages/chrome_1.html'] = [443745202,-2085976064,-1917798229,1623287596,-452987103,-1946265870,197612221,1953691450];
57
+ sh1['pages/investors_1.html'] = [1090312779,-197394799,-611797489,-744420154,562751960,552453508,-1283778437,1638152802];
58
+ sh1['pages/investors_1.js'] = [869139470,389990555,-921764873,1063042907,1815096402,-509011024,-926964454,542506903];
59
+ sh1['pages/chrome_1.js'] = [-549626102,-1722665091,-1378169910,-914300046,-2074490841,-1326803700,1863412438,600703552];
60
+ sh1['pages/bottom_1.html'] = [-1724109664,-66476738,-1091130428,-1694541061,-349670379,-964684688,-145819752,-53932509];
61
+ sh1['pages/fm_12.js'] = [-346968613,903708781,-618121541,6683033,76873838,807942159,451968703,405681683];
62
+ sh1['pages/done_1.html'] = [63746648,-29341517,-1341733970,1258069219,306121526,708042924,231328537,254431042];
63
+ sh1['pages/flash_1.html'] = [-1248343472,1771818726,1432362167,-1845494701,-1544161754,-298890197,11119393,-1848952864];
64
+ sh1['pages/done_1.js'] = [-1494046142,1259099392,1731543890,-1347191732,-204403603,-654770010,-1880035241,-718967266];
65
+ sh1['pages/register_1.html'] = [-887466443,-682667237,-1376302895,48218009,-1884354725,-385823892,-221345039,-637871310];
66
+ sh1['pages/register_2.js'] = [138491732,-2093985117,1692398906,520257893,479180965,-690913499,-445250672,-1456444062];
67
+ sh1['pages/login_2.html'] = [2143622433,-878051980,-3706875,1282317370,-1976282683,-907088042,1021087429,933949964];
68
+ sh1['pages/login_3.js'] = [2133268215,-949088241,322570651,1708081223,-2052214757,-256518879,472384,-1283723554];
69
+ sh1['pages/blog_2.html'] = [454339769,1232817007,1050538266,-614858753,-982157685,-273605740,-826552982,-1632740451];
70
+ sh1['pages/blog_14.js'] = [1870213347,-868831059,373865832,2016046245,1226493656,-380620813,1856593819,-1936837171];
71
+ sh1['pages/blogarticle_1.html'] = [1423174348,1130975021,731646762,-1525243796,-1214088722,-258198075,1976664524,-230602703];
72
+ sh1['pages/blogarticle_2.js'] = [-1641370087,1781042113,914317588,-1139859801,1195295937,-1470786569,-1682589181,559852555];
73
+ sh1['pages/nstart_1.html'] = [1904761052,613539187,-735796997,1577623713,-2108467002,943757633,-1469034746,1316595430];
74
+ sh1['pages/nstart_1.js'] = [918677555,-1872846878,-1288741758,244757304,1202072083,-1059970056,1852965074,831390151];
75
+ sh1['pages/resellers_1.html'] = [1167853423,1418071276,479860629,-1220706590,-1709773171,2062350719,1277432575,248101811];
76
+ sh1['pages/hosting_1.html'] = [-155550596,880800874,-1165690192,2078535341,947619835,82026097,785574197,1362960497];
77
+ sh1['pages/hosting_1.js'] = [-800388995,-860805304,-1925434694,-1238219272,601489914,-769737198,-717747821,2093563239];
78
+ sh1['pages/about_1.html'] = [296528327,-223681735,1535930772,-312042605,-2039866153,-1000283839,-1648393415,395013484];
79
+ sh1['pages/about_1.js'] = [287161143,-323614452,1689772751,174741354,-216816164,1198378298,-45229596,-205224332];
80
+ sh1['pages/key_1.html'] = [801701094,-585072668,-1316686569,-492756437,-579746824,-2030984657,-377978424,-649222237];
81
+ sh1['pages/key_1.js'] = [-744750577,1158061351,1194429549,1076646511,-423688229,-1640714091,-157346893,-151198213];
82
+ sh1['pages/download_3.html'] = [-1731877409,-1686165699,446273092,572159696,652053034,-1736472275,808081153,-372173755];
83
+ sh1['pages/download_6.js'] = [91021255,-193018363,1988998341,295853168,1812080870,-785289366,-1465531523,837611540];
84
+ sh1['pages/copyright_1.html'] = [-1561303273,1974993813,-593739152,803093508,-1372318467,1305771867,-599859667,994858053];
85
+ sh1['pages/account_3.html'] = [2123780517,418524669,-868375462,869005806,1423670235,-895846777,-1860975342,-318892827];
86
+ sh1['pages/account_5.js'] = [257922438,756407627,-1292507598,509298364,-168373273,1983577466,-1151676043,-1860367702];
87
+ sh1['pages/privacy_4.html'] = [2123819213,69866335,-1161794670,-1221177010,-337081820,-1053978870,935459800,-1050359509];
88
+ sh1['pages/terms_1.html'] = [-1716182282,-388600864,1408071139,1383301681,-548677916,-268459886,-83200279,512410474];
89
+ sh1['pages/developers_1.html'] = [-1034728817,-2113097570,-2062084276,-776039411,267323710,-460563795,-1922656440,-1694314488];
90
+ sh1['pages/developers_1.js'] = [-1731862613,-422910779,634277433,-409155613,461328040,1484806805,-399792975,266887385];
91
+ sh1['pages/help_1.html'] = [532797045,1842162164,2052125793,-1696334451,-311768125,288624285,1983543384,1379642726];
92
+ sh1['pages/help_1.js'] = [1718207973,1514667516,-1085676779,-1440347761,701540409,671780040,-208604987,-955640858];
93
+ sh1['ZeroClipboard_2.js'] = [2070800473,740072642,786430495,240860126,-1964841847,707799627,-85560607,1016573827];
94
+ sh1['resources/css/my-ext-theme_1.css'] = [372799983,-1916993762,347261028,-354417760,1723369862,-515180935,1258735240,1564795807];
95
+ sh1['resources/css/ext-custom_2.css'] = [-941797079,1257459560,-1523396776,2075068147,-137927227,1837244283,18297057,46236920];
96
+ sh1['resources/css/style_6.css'] = [-399339067,-733865365,933410316,-1851387750,1107766263,1743806893,-1441294586,-1476494841];
97
+ sh1['pages/helpsub_1.html'] = [-1666417453,-1845165473,1993338738,491673394,1494769704,-576351617,-227568182,1740741195];
98
+ sh1['pages/helpsub_1.js'] = [1161430355,-1860682027,-1301312235,-1226818371,-1748984987,1752253186,-1544336725,-1723861380];
99
+ sh1['pages/contact_1.html'] = [1891524577,1389164150,-26233532,2136416744,-1819000270,815238453,893154656,-1164257118];
100
+ sh1['pages/copyrightnotice_1.html'] = [57585609,-1407177597,1634004563,-2035441394,1348213238,-1806812380,1424634128,-993003593];
101
+ sh1['pages/copyrightnotice_1.js'] = [-1273225133,-1401736409,1495095695,-566562887,2113732902,-1883749972,896046059,123198449];
102
+ sh1['pages/privacycompany_2.html'] = [-1279979971,-505129569,773231605,1755768074,-2056011432,-1489145212,-1921137602,-2042186167];
103
+ sh1['pages/privacycompany_1.js'] = [1684929870,826281319,666054451,636901768,-1873065664,1204141200,-1540028076,-1144499929];
104
+ sh1['pages/pro_1.html'] = [-1139112765,-688745337,-1439284721,2057423418,2012711667,-850934154,-1097696445,818512853];
105
+ sh1['pages/pro_1.js'] = [-760861482,-464263356,1278013738,-204131451,-51832502,-1634557030,2084661232,1932066104];
106
+ sh1['pages/resellerapp_2.html'] = [-628659139,-423890632,-454466580,-296285312,-1079492523,1870567620,537524995,2015774886];
107
+ sh1['pages/resellerapp_1.js'] = [535686301,1940628733,-23199267,138952032,-573086779,1886105930,1269778842,-280815523];
108
+ sh1['pages/resellerintro_1.html'] = [-1439407084,313440834,2041007158,-443877117,1071363056,-2021618866,1989604434,-1138587370];
109
+ sh1['index_9.js'] = [693247143,-1223411349,-1151041439,2030302201,-1190322233,-1280513022,-1197622087,-1879136242];
110
+ sh1['lang/hr_2.json'] = [-180085637,-1541952180,963947765,-1258468677,1384121893,1807079662,710178439,180130624];
111
+ sh1['thumbnail_2.js'] = [-1940913709,76086878,-779961889,-1288584391,-1074089905,1673046495,1380060199,-948885668];
112
+ sh1['pages/forgotpassword_1.html'] = [465408861,-1703370349,1571497590,15938986,1574575076,387549260,-2016883648,-1515340694];
113
+ sh1['pages/forgotpassword_1.js'] = [2079781834,1919101860,-484268578,-1929551079,-1321571298,-238018804,449022272,1076975803];
114
+ sh1['firefox_2.js'] = [667227136,-274806947,-292794554,1158571122,-507548245,1834299029,1079631090,14793777];
115
+ l
@@ -0,0 +1,456 @@
1
+
2
+ /* RSA public key encryption/decryption
3
+ * The following functions are (c) 2000 by John M Hanna and are
4
+ * released under the terms of the Gnu Public License.
5
+ * You must freely redistribute them with their source -- see the
6
+ * GPL for details.
7
+ * -- Latest version found at http://sourceforge.net/projects/shop-js
8
+ *
9
+ * Modifications and GnuPG multi precision integer (mpi) conversion added
10
+ * 2004 by Herbert Hanewinkel, www.haneWIN.de
11
+ */
12
+
13
+ // --- Arbitrary Precision Math ---
14
+ // badd(a,b), bsub(a,b), bsqr(a), bmul(a,b)
15
+ // bdiv(a,b), bmod(a,b), bexpmod(g,e,m), bmodexp(g,e,m)
16
+
17
+ // bs is the shift, bm is the mask
18
+ // set single precision bits to 28
19
+ var bs=28;
20
+ var bx2=1<<bs, bm=bx2-1, bx=bx2>>1, bd=bs>>1, bdm=(1<<bd)-1;
21
+
22
+ var log2=Math.log(2);
23
+
24
+ function zeros(n)
25
+ {
26
+ var r=new Array(n);
27
+
28
+ while(n-->0) r[n]=0;
29
+ return r;
30
+ }
31
+
32
+ function zclip(r)
33
+ {
34
+ var n = r.length;
35
+ if(r[n-1]) return r;
36
+ while(n>1 && r[n-1]==0) n--;
37
+ return r.slice(0,n);
38
+ }
39
+
40
+ // returns bit length of integer x
41
+ function nbits(x)
42
+ {
43
+ var n = 1, t;
44
+ if((t=x>>>16) != 0) { x = t; n += 16; }
45
+ if((t=x>>8) != 0) { x = t; n += 8; }
46
+ if((t=x>>4) != 0) { x = t; n += 4; }
47
+ if((t=x>>2) != 0) { x = t; n += 2; }
48
+ if((t=x>>1) != 0) { x = t; n += 1; }
49
+ return n;
50
+ }
51
+
52
+ function badd(a,b)
53
+ {
54
+ var al=a.length;
55
+ var bl=b.length;
56
+
57
+ if(al < bl) return badd(b,a);
58
+
59
+ var r=new Array(al);
60
+ var c=0, n=0;
61
+
62
+ for(; n<bl; n++)
63
+ {
64
+ c+=a[n]+b[n];
65
+ r[n]=c & bm;
66
+ c>>>=bs;
67
+ }
68
+ for(; n<al; n++)
69
+ {
70
+ c+=a[n];
71
+ r[n]=c & bm;
72
+ c>>>=bs;
73
+ }
74
+ if(c) r[n]=c;
75
+ return r;
76
+ }
77
+
78
+ function bsub(a,b)
79
+ {
80
+ var al=a.length;
81
+ var bl=b.length;
82
+
83
+ if(bl > al) return [];
84
+ if(bl == al)
85
+ {
86
+ if(b[bl-1] > a[bl-1]) return [];
87
+ if(bl==1) return [a[0]-b[0]];
88
+ }
89
+
90
+ var r=new Array(al);
91
+ var c=0;
92
+
93
+ for(var n=0; n<bl; n++)
94
+ {
95
+ c+=a[n]-b[n];
96
+ r[n]=c & bm;
97
+ c>>=bs;
98
+ }
99
+ for(;n<al; n++)
100
+ {
101
+ c+=a[n];
102
+ r[n]=c & bm;
103
+ c>>=bs;
104
+ }
105
+ if(c) return [];
106
+
107
+ return zclip(r);
108
+ }
109
+
110
+ function ip(w, n, x, y, c)
111
+ {
112
+ var xl = x&bdm;
113
+ var xh = x>>bd;
114
+
115
+ var yl = y&bdm;
116
+ var yh = y>>bd;
117
+
118
+ var m = xh*yl+yh*xl;
119
+ var l = xl*yl+((m&bdm)<<bd)+w[n]+c;
120
+ w[n] = l&bm;
121
+ c = xh*yh+(m>>bd)+(l>>bs);
122
+ return c;
123
+ }
124
+
125
+ // Multiple-precision squaring, HAC Algorithm 14.16
126
+
127
+ function bsqr(x)
128
+ {
129
+ var t = x.length;
130
+ var n = 2*t;
131
+ var r = zeros(n);
132
+ var c = 0;
133
+ var i, j;
134
+
135
+ for(i = 0; i < t; i++)
136
+ {
137
+ c = ip(r,2*i,x[i],x[i],0);
138
+ for(j = i+1; j < t; j++)
139
+ {
140
+ c = ip(r,i+j,2*x[j],x[i],c);
141
+ }
142
+ r[i+t] = c;
143
+ }
144
+
145
+ return zclip(r);
146
+ }
147
+
148
+ // Multiple-precision multiplication, HAC Algorithm 14.12
149
+
150
+ function bmul(x,y)
151
+ {
152
+ var n = x.length;
153
+ var t = y.length;
154
+ var r = zeros(n+t-1);
155
+ var c, i, j;
156
+
157
+ for(i = 0; i < t; i++)
158
+ {
159
+ c = 0;
160
+ for(j = 0; j < n; j++)
161
+ {
162
+ c = ip(r,i+j,x[j],y[i],c);
163
+ }
164
+ r[i+n] = c;
165
+ }
166
+
167
+ return zclip(r);
168
+ }
169
+
170
+ function toppart(x,start,len)
171
+ {
172
+ var n=0;
173
+ while(start >= 0 && len-->0) n=n*bx2+x[start--];
174
+ return n;
175
+ }
176
+
177
+ // Multiple-precision division, HAC Algorithm 14.20
178
+
179
+ function bdiv(a,b)
180
+ {
181
+ var n=a.length-1;
182
+ var t=b.length-1;
183
+ var nmt=n-t;
184
+
185
+ // trivial cases; a < b
186
+ if(n < t || n==t && (a[n]<b[n] || n>0 && a[n]==b[n] && a[n-1]<b[n-1]))
187
+ {
188
+ this.q=[0]; this.mod=a;
189
+ return this;
190
+ }
191
+
192
+ // trivial cases; q < 4
193
+ if(n==t && toppart(a,t,2)/toppart(b,t,2) <4)
194
+ {
195
+ var x=a.concat();
196
+ var qq=0;
197
+ var xx;
198
+ for(;;)
199
+ {
200
+ xx=bsub(x,b);
201
+ if(xx.length==0) break;
202
+ x=xx; qq++;
203
+ }
204
+ this.q=[qq]; this.mod=x;
205
+ return this;
206
+ }
207
+
208
+ // normalize
209
+ var shift2=Math.floor(Math.log(b[t])/log2)+1;
210
+ var shift=bs-shift2;
211
+
212
+ var x=a.concat();
213
+ var y=b.concat();
214
+
215
+ if(shift)
216
+ {
217
+ for(i=t; i>0; i--) y[i]=((y[i]<<shift) & bm) | (y[i-1] >> shift2);
218
+ y[0]=(y[0]<<shift) & bm;
219
+ if(x[n] & ((bm <<shift2) & bm))
220
+ {
221
+ x[++n]=0; nmt++;
222
+ }
223
+ for(i=n; i>0; i--) x[i]=((x[i]<<shift) & bm) | (x[i-1] >> shift2);
224
+ x[0]=(x[0]<<shift) & bm;
225
+ }
226
+
227
+ var i, j, x2;
228
+ var q=zeros(nmt+1);
229
+ var y2=zeros(nmt).concat(y);
230
+ for(;;)
231
+ {
232
+ x2=bsub(x,y2);
233
+ if(x2.length==0) break;
234
+ q[nmt]++;
235
+ x=x2;
236
+ }
237
+
238
+ var yt=y[t], top=toppart(y,t,2)
239
+ for(i=n; i>t; i--)
240
+ {
241
+ var m=i-t-1;
242
+ if(i >= x.length) q[m]=1;
243
+ else if(x[i] == yt) q[m]=bm;
244
+ else q[m]=Math.floor(toppart(x,i,2)/yt);
245
+
246
+ var topx=toppart(x,i,3);
247
+ while(q[m] * top > topx) q[m]--;
248
+
249
+ //x-=q[m]*y*b^m
250
+ y2=y2.slice(1);
251
+ x2=bsub(x,bmul([q[m]],y2));
252
+ if(x2.length==0)
253
+ {
254
+ q[m]--;
255
+ x2=bsub(x,bmul([q[m]],y2));
256
+ }
257
+ x=x2;
258
+ }
259
+ // de-normalize
260
+ if(shift)
261
+ {
262
+ for(i=0; i<x.length-1; i++) x[i]=(x[i]>>shift) | ((x[i+1] << shift2) & bm);
263
+ x[x.length-1]>>=shift;
264
+ }
265
+
266
+ this.q = zclip(q);
267
+ this.mod = zclip(x);
268
+ return this;
269
+ }
270
+
271
+ function simplemod(i,m) // returns the mod where m < 2^bd
272
+ {
273
+ var c=0, v;
274
+ for(var n=i.length-1; n>=0; n--)
275
+ {
276
+ v=i[n];
277
+ c=((v >> bd) + (c<<bd)) % m;
278
+ c=((v & bdm) + (c<<bd)) % m;
279
+ }
280
+ return c;
281
+ }
282
+
283
+ function bmod(p,m)
284
+ {
285
+ if(m.length==1)
286
+ {
287
+ if(p.length==1) return [p[0] % m[0]];
288
+ if(m[0] < bdm) return [simplemod(p,m[0])];
289
+ }
290
+
291
+ var r=bdiv(p,m);
292
+ return r.mod;
293
+ }
294
+
295
+ // Barrett's modular reduction, HAC Algorithm 14.42
296
+
297
+ function bmod2(x,m,mu)
298
+ {
299
+ var xl=x.length - (m.length << 1);
300
+ if(xl > 0) return bmod2(x.slice(0,xl).concat(bmod2(x.slice(xl),m,mu)),m,mu);
301
+
302
+ var ml1=m.length+1, ml2=m.length-1,rr;
303
+ //var q1=x.slice(ml2)
304
+ //var q2=bmul(q1,mu)
305
+ var q3=bmul(x.slice(ml2),mu).slice(ml1);
306
+ var r1=x.slice(0,ml1);
307
+ var r2=bmul(q3,m).slice(0,ml1);
308
+ var r=bsub(r1,r2);
309
+
310
+ if(r.length==0)
311
+ {
312
+ r1[ml1]=1;
313
+ r=bsub(r1,r2);
314
+ }
315
+ for(var n=0;;n++)
316
+ {
317
+ rr=bsub(r,m);
318
+ if(rr.length==0) break;
319
+ r=rr;
320
+ if(n>=3) return bmod2(r,m,mu);
321
+ }
322
+ return r;
323
+ }
324
+
325
+ // Modular exponentiation, HAC Algorithm 14.79
326
+
327
+ function bexpmod(g,e,m)
328
+ {
329
+ var a = g.concat();
330
+ var l = e.length-1;
331
+ var n = nbits(e[l])-2;
332
+
333
+ for(; l >= 0; l--)
334
+ {
335
+ for(; n >= 0; n-=1)
336
+ {
337
+ a=bmod(bsqr(a),m);
338
+ if(e[l] & (1<<n)) a=bmod(bmul(a,g),m);
339
+ }
340
+ n = bs-1;
341
+ }
342
+ return a;
343
+ }
344
+
345
+ // Modular exponentiation using Barrett reduction
346
+
347
+ function bmodexp(g,e,m)
348
+ {
349
+ var a=g.concat();
350
+ var l=e.length-1;
351
+ var n=m.length*2;
352
+ var mu=zeros(n+1);
353
+ mu[n]=1;
354
+ mu=bdiv(mu,m).q;
355
+
356
+ n = nbits(e[l])-2;
357
+
358
+ for(; l >= 0; l--)
359
+ {
360
+ for(; n >= 0; n-=1)
361
+ {
362
+ a=bmod2(bsqr(a),m, mu);
363
+ if(e[l] & (1<<n)) a=bmod2(bmul(a,g),m, mu);
364
+ }
365
+ n = bs-1;
366
+ }
367
+ return a;
368
+ }
369
+
370
+ // -----------------------------------------------------
371
+ // Compute s**e mod m for RSA public key operation
372
+
373
+ function RSAencrypt(s, e, m) { return bexpmod(s,e,m); }
374
+
375
+ // Compute m**d mod p*q for RSA private key operations.
376
+
377
+ function RSAdecrypt(m, d, p, q, u)
378
+ {
379
+ var xp = bmodexp(bmod(m,p), bmod(d,bsub(p,[1])), p);
380
+ var xq = bmodexp(bmod(m,q), bmod(d,bsub(q,[1])), q);
381
+
382
+ var t=bsub(xq,xp);
383
+ if(t.length==0)
384
+ {
385
+ t=bsub(xp,xq);
386
+ t=bmod(bmul(t, u), q);
387
+ t=bsub(q,t);
388
+ }
389
+ else
390
+ {
391
+ t=bmod(bmul(t, u), q);
392
+ }
393
+ return badd(bmul(t,p), xp);
394
+ }
395
+
396
+ // -----------------------------------------------------------------
397
+ // conversion functions: num array <-> multi precision integer (mpi)
398
+ // mpi: 2 octets with length in bits + octets in big endian order
399
+
400
+ function mpi2b(s)
401
+ {
402
+ var bn=1, r=[0], rn=0, sb=256;
403
+ var c, sn=s.length;
404
+ if(sn < 2) return 0;
405
+
406
+ var len=(sn-2)*8;
407
+ var bits=s.charCodeAt(0)*256+s.charCodeAt(1);
408
+ if(bits > len || bits < len-8) return 0;
409
+
410
+ for(var n=0; n<len; n++)
411
+ {
412
+ if((sb<<=1) > 255)
413
+ {
414
+ sb=1; c=s.charCodeAt(--sn);
415
+ }
416
+ if(bn > bm)
417
+ {
418
+ bn=1;
419
+ r[++rn]=0;
420
+ }
421
+ if(c & sb) r[rn]|=bn;
422
+ bn<<=1;
423
+ }
424
+ return r;
425
+ }
426
+
427
+ function b2mpi(b)
428
+ {
429
+ var bn=1, bc=0, r=[0], rb=1, rn=0;
430
+ var bits=b.length*bs;
431
+ var n, rr='';
432
+
433
+ for(n=0; n<bits; n++)
434
+ {
435
+ if(b[bc] & bn) r[rn]|=rb;
436
+ if((rb<<=1) > 255)
437
+ {
438
+ rb=1; r[++rn]=0;
439
+ }
440
+ if((bn<<=1) > bm)
441
+ {
442
+ bn=1; bc++;
443
+ }
444
+ }
445
+
446
+ while(rn && r[rn]==0) rn--;
447
+
448
+ bn=256;
449
+ for(bits=8; bits>0; bits--) if(r[rn] & (bn>>=1)) break;
450
+ bits+=rn*8;
451
+
452
+ rr+=String.fromCharCode(bits/256)+String.fromCharCode(bits%256);
453
+ if(bits) for(n=rn; n>=0; n--) rr+=String.fromCharCode(r[n]);
454
+ return rr;
455
+ }
456
+