JSEncrypt.js 139 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758
  1. function createJSEncrypt() {
  2. // lib/lib/jsbn/util.js
  3. var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
  4. function int2char(n) {
  5. return BI_RM.charAt(n);
  6. }
  7. function op_and(x, y) {
  8. return x & y;
  9. }
  10. function op_or(x, y) {
  11. return x | y;
  12. }
  13. function op_xor(x, y) {
  14. return x ^ y;
  15. }
  16. function op_andnot(x, y) {
  17. return x & ~y;
  18. }
  19. function lbit(x) {
  20. if (x == 0) {
  21. return -1;
  22. }
  23. var r = 0;
  24. if ((x & 65535) == 0) {
  25. x >>= 16;
  26. r += 16;
  27. }
  28. if ((x & 255) == 0) {
  29. x >>= 8;
  30. r += 8;
  31. }
  32. if ((x & 15) == 0) {
  33. x >>= 4;
  34. r += 4;
  35. }
  36. if ((x & 3) == 0) {
  37. x >>= 2;
  38. r += 2;
  39. }
  40. if ((x & 1) == 0) {
  41. ++r;
  42. }
  43. return r;
  44. }
  45. function cbit(x) {
  46. var r = 0;
  47. while (x != 0) {
  48. x &= x - 1;
  49. ++r;
  50. }
  51. return r;
  52. }
  53. // lib/lib/jsbn/base64.js
  54. var b64map = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  55. var b64pad = "=";
  56. function hex2b64(h) {
  57. var i;
  58. var c;
  59. var ret = "";
  60. for (i = 0; i + 3 <= h.length; i += 3) {
  61. c = parseInt(h.substring(i, i + 3), 16);
  62. ret += b64map.charAt(c >> 6) + b64map.charAt(c & 63);
  63. }
  64. if (i + 1 == h.length) {
  65. c = parseInt(h.substring(i, i + 1), 16);
  66. ret += b64map.charAt(c << 2);
  67. } else if (i + 2 == h.length) {
  68. c = parseInt(h.substring(i, i + 2), 16);
  69. ret += b64map.charAt(c >> 2) + b64map.charAt((c & 3) << 4);
  70. }
  71. while ((ret.length & 3) > 0) {
  72. ret += b64pad;
  73. }
  74. return ret;
  75. }
  76. function b64tohex(s) {
  77. var ret = "";
  78. var i;
  79. var k = 0;
  80. var slop = 0;
  81. for (i = 0; i < s.length; ++i) {
  82. if (s.charAt(i) == b64pad) {
  83. break;
  84. }
  85. var v = b64map.indexOf(s.charAt(i));
  86. if (v < 0) {
  87. continue;
  88. }
  89. if (k == 0) {
  90. ret += int2char(v >> 2);
  91. slop = v & 3;
  92. k = 1;
  93. } else if (k == 1) {
  94. ret += int2char(slop << 2 | v >> 4);
  95. slop = v & 15;
  96. k = 2;
  97. } else if (k == 2) {
  98. ret += int2char(slop);
  99. ret += int2char(v >> 2);
  100. slop = v & 3;
  101. k = 3;
  102. } else {
  103. ret += int2char(slop << 2 | v >> 4);
  104. ret += int2char(v & 15);
  105. k = 0;
  106. }
  107. }
  108. if (k == 1) {
  109. ret += int2char(slop << 2);
  110. }
  111. return ret;
  112. }
  113. // lib/lib/asn1js/hex.js
  114. var decoder;
  115. var Hex = {
  116. decode: function (a) {
  117. var i;
  118. if (decoder === void 0) {
  119. var hex = "0123456789ABCDEF";
  120. var ignore = " \f\n\r\t \u2028\u2029";
  121. decoder = {};
  122. for (i = 0; i < 16; ++i) {
  123. decoder[hex.charAt(i)] = i;
  124. }
  125. hex = hex.toLowerCase();
  126. for (i = 10; i < 16; ++i) {
  127. decoder[hex.charAt(i)] = i;
  128. }
  129. for (i = 0; i < ignore.length; ++i) {
  130. decoder[ignore.charAt(i)] = -1;
  131. }
  132. }
  133. var out = [];
  134. var bits = 0;
  135. var char_count = 0;
  136. for (i = 0; i < a.length; ++i) {
  137. var c = a.charAt(i);
  138. if (c == "=") {
  139. break;
  140. }
  141. c = decoder[c];
  142. if (c == -1) {
  143. continue;
  144. }
  145. if (c === void 0) {
  146. throw new Error("Illegal character at offset " + i);
  147. }
  148. bits |= c;
  149. if (++char_count >= 2) {
  150. out[out.length] = bits;
  151. bits = 0;
  152. char_count = 0;
  153. } else {
  154. bits <<= 4;
  155. }
  156. }
  157. if (char_count) {
  158. throw new Error("Hex encoding incomplete: 4 bits missing");
  159. }
  160. return out;
  161. }
  162. };
  163. // lib/lib/asn1js/base64.js
  164. var decoder2;
  165. var Base64 = {
  166. decode: function (a) {
  167. var i;
  168. if (decoder2 === void 0) {
  169. var b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  170. var ignore = "= \f\n\r\t \u2028\u2029";
  171. decoder2 = Object.create(null);
  172. for (i = 0; i < 64; ++i) {
  173. decoder2[b64.charAt(i)] = i;
  174. }
  175. decoder2["-"] = 62;
  176. decoder2["_"] = 63;
  177. for (i = 0; i < ignore.length; ++i) {
  178. decoder2[ignore.charAt(i)] = -1;
  179. }
  180. }
  181. var out = [];
  182. var bits = 0;
  183. var char_count = 0;
  184. for (i = 0; i < a.length; ++i) {
  185. var c = a.charAt(i);
  186. if (c == "=") {
  187. break;
  188. }
  189. c = decoder2[c];
  190. if (c == -1) {
  191. continue;
  192. }
  193. if (c === void 0) {
  194. throw new Error("Illegal character at offset " + i);
  195. }
  196. bits |= c;
  197. if (++char_count >= 4) {
  198. out[out.length] = bits >> 16;
  199. out[out.length] = bits >> 8 & 255;
  200. out[out.length] = bits & 255;
  201. bits = 0;
  202. char_count = 0;
  203. } else {
  204. bits <<= 6;
  205. }
  206. }
  207. switch (char_count) {
  208. case 1:
  209. throw new Error("Base64 encoding incomplete: at least 2 bits missing");
  210. case 2:
  211. out[out.length] = bits >> 10;
  212. break;
  213. case 3:
  214. out[out.length] = bits >> 16;
  215. out[out.length] = bits >> 8 & 255;
  216. break;
  217. }
  218. return out;
  219. },
  220. re: /-----BEGIN [^-]+-----([A-Za-z0-9+\/=\s]+)-----END [^-]+-----|begin-base64[^\n]+\n([A-Za-z0-9+\/=\s]+)====/,
  221. unarmor: function (a) {
  222. var m = Base64.re.exec(a);
  223. if (m) {
  224. if (m[1]) {
  225. a = m[1];
  226. } else if (m[2]) {
  227. a = m[2];
  228. } else {
  229. throw new Error("RegExp out of sync");
  230. }
  231. }
  232. return Base64.decode(a);
  233. }
  234. };
  235. // lib/lib/asn1js/int10.js
  236. var max = 1e13;
  237. var Int10 =
  238. /** @class */ function () {
  239. function Int102(value) {
  240. this.buf = [+value || 0];
  241. }
  242. Int102.prototype.mulAdd = function (m, c) {
  243. var b = this.buf;
  244. var l = b.length;
  245. var i;
  246. var t;
  247. for (i = 0; i < l; ++i) {
  248. t = b[i] * m + c;
  249. if (t < max) {
  250. c = 0;
  251. } else {
  252. c = 0 | t / max;
  253. t -= c * max;
  254. }
  255. b[i] = t;
  256. }
  257. if (c > 0) {
  258. b[i] = c;
  259. }
  260. };
  261. Int102.prototype.sub = function (c) {
  262. var b = this.buf;
  263. var l = b.length;
  264. var i;
  265. var t;
  266. for (i = 0; i < l; ++i) {
  267. t = b[i] - c;
  268. if (t < 0) {
  269. t += max;
  270. c = 1;
  271. } else {
  272. c = 0;
  273. }
  274. b[i] = t;
  275. }
  276. while (b[b.length - 1] === 0) {
  277. b.pop();
  278. }
  279. };
  280. Int102.prototype.toString = function (base) {
  281. if ((base || 10) != 10) {
  282. throw new Error("only base 10 is supported");
  283. }
  284. var b = this.buf;
  285. var s = b[b.length - 1].toString();
  286. for (var i = b.length - 2; i >= 0; --i) {
  287. s += (max + b[i]).toString().substring(1);
  288. }
  289. return s;
  290. };
  291. Int102.prototype.valueOf = function () {
  292. var b = this.buf;
  293. var v = 0;
  294. for (var i = b.length - 1; i >= 0; --i) {
  295. v = v * max + b[i];
  296. }
  297. return v;
  298. };
  299. Int102.prototype.simplify = function () {
  300. var b = this.buf;
  301. return b.length == 1 ? b[0] : this;
  302. };
  303. return Int102;
  304. }();
  305. // lib/lib/asn1js/asn1.js
  306. var ellipsis = "…";
  307. var reTimeS = /^(\d\d)(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])([01]\d|2[0-3])(?:([0-5]\d)(?:([0-5]\d)(?:[.,](\d{1,3}))?)?)?(Z|[-+](?:[0]\d|1[0-2])([0-5]\d)?)?$/;
  308. var reTimeL = /^(\d\d\d\d)(0[1-9]|1[0-2])(0[1-9]|[12]\d|3[01])([01]\d|2[0-3])(?:([0-5]\d)(?:([0-5]\d)(?:[.,](\d{1,3}))?)?)?(Z|[-+](?:[0]\d|1[0-2])([0-5]\d)?)?$/;
  309. function stringCut(str, len) {
  310. if (str.length > len) {
  311. str = str.substring(0, len) + ellipsis;
  312. }
  313. return str;
  314. }
  315. var Stream =
  316. /** @class */ function () {
  317. function Stream2(enc, pos) {
  318. this.hexDigits = "0123456789ABCDEF";
  319. if (enc instanceof Stream2) {
  320. this.enc = enc.enc;
  321. this.pos = enc.pos;
  322. } else {
  323. this.enc = enc;
  324. this.pos = pos;
  325. }
  326. }
  327. Stream2.prototype.get = function (pos) {
  328. if (pos === void 0) {
  329. pos = this.pos++;
  330. }
  331. if (pos >= this.enc.length) {
  332. throw new Error("Requesting byte offset ".concat(pos, " on a stream of length ").concat(this.enc.length));
  333. }
  334. return "string" === typeof this.enc ? this.enc.charCodeAt(pos) : this.enc[pos];
  335. };
  336. Stream2.prototype.hexByte = function (b) {
  337. return this.hexDigits.charAt(b >> 4 & 15) + this.hexDigits.charAt(b & 15);
  338. };
  339. Stream2.prototype.hexDump = function (start, end, raw) {
  340. var s = "";
  341. for (var i = start; i < end; ++i) {
  342. s += this.hexByte(this.get(i));
  343. if (raw !== true) {
  344. switch (i & 15) {
  345. case 7:
  346. s += " ";
  347. break;
  348. case 15:
  349. s += "\n";
  350. break;
  351. default:
  352. s += " ";
  353. }
  354. }
  355. }
  356. return s;
  357. };
  358. Stream2.prototype.isASCII = function (start, end) {
  359. for (var i = start; i < end; ++i) {
  360. var c = this.get(i);
  361. if (c < 32 || c > 176) {
  362. return false;
  363. }
  364. }
  365. return true;
  366. };
  367. Stream2.prototype.parseStringISO = function (start, end) {
  368. var s = "";
  369. for (var i = start; i < end; ++i) {
  370. s += String.fromCharCode(this.get(i));
  371. }
  372. return s;
  373. };
  374. Stream2.prototype.parseStringUTF = function (start, end) {
  375. var s = "";
  376. for (var i = start; i < end;) {
  377. var c = this.get(i++);
  378. if (c < 128) {
  379. s += String.fromCharCode(c);
  380. } else if (c > 191 && c < 224) {
  381. s += String.fromCharCode((c & 31) << 6 | this.get(i++) & 63);
  382. } else {
  383. s += String.fromCharCode((c & 15) << 12 | (this.get(i++) & 63) << 6 | this.get(i++) & 63);
  384. }
  385. }
  386. return s;
  387. };
  388. Stream2.prototype.parseStringBMP = function (start, end) {
  389. var str = "";
  390. var hi;
  391. var lo;
  392. for (var i = start; i < end;) {
  393. hi = this.get(i++);
  394. lo = this.get(i++);
  395. str += String.fromCharCode(hi << 8 | lo);
  396. }
  397. return str;
  398. };
  399. Stream2.prototype.parseTime = function (start, end, shortYear) {
  400. var s = this.parseStringISO(start, end);
  401. var m = (shortYear ? reTimeS : reTimeL).exec(s);
  402. if (!m) {
  403. return "Unrecognized time: " + s;
  404. }
  405. if (shortYear) {
  406. m[1] = +m[1];
  407. m[1] += +m[1] < 70 ? 2e3 : 1900;
  408. }
  409. s = m[1] + "-" + m[2] + "-" + m[3] + " " + m[4];
  410. if (m[5]) {
  411. s += ":" + m[5];
  412. if (m[6]) {
  413. s += ":" + m[6];
  414. if (m[7]) {
  415. s += "." + m[7];
  416. }
  417. }
  418. }
  419. if (m[8]) {
  420. s += " UTC";
  421. if (m[8] != "Z") {
  422. s += m[8];
  423. if (m[9]) {
  424. s += ":" + m[9];
  425. }
  426. }
  427. }
  428. return s;
  429. };
  430. Stream2.prototype.parseInteger = function (start, end) {
  431. var v = this.get(start);
  432. var neg = v > 127;
  433. var pad = neg ? 255 : 0;
  434. var len;
  435. var s = "";
  436. while (v == pad && ++start < end) {
  437. v = this.get(start);
  438. }
  439. len = end - start;
  440. if (len === 0) {
  441. return neg ? -1 : 0;
  442. }
  443. if (len > 4) {
  444. s = v;
  445. len <<= 3;
  446. while (((+s ^ pad) & 128) == 0) {
  447. s = +s << 1;
  448. --len;
  449. }
  450. s = "(" + len + " bit)\n";
  451. }
  452. if (neg) {
  453. v = v - 256;
  454. }
  455. var n = new Int10(v);
  456. for (var i = start + 1; i < end; ++i) {
  457. n.mulAdd(256, this.get(i));
  458. }
  459. return s + n.toString();
  460. };
  461. Stream2.prototype.parseBitString = function (start, end, maxLength) {
  462. var unusedBit = this.get(start);
  463. var lenBit = (end - start - 1 << 3) - unusedBit;
  464. var intro = "(" + lenBit + " bit)\n";
  465. var s = "";
  466. for (var i = start + 1; i < end; ++i) {
  467. var b = this.get(i);
  468. var skip = i == end - 1 ? unusedBit : 0;
  469. for (var j = 7; j >= skip; --j) {
  470. s += b >> j & 1 ? "1" : "0";
  471. }
  472. if (s.length > maxLength) {
  473. return intro + stringCut(s, maxLength);
  474. }
  475. }
  476. return intro + s;
  477. };
  478. Stream2.prototype.parseOctetString = function (start, end, maxLength) {
  479. if (this.isASCII(start, end)) {
  480. return stringCut(this.parseStringISO(start, end), maxLength);
  481. }
  482. var len = end - start;
  483. var s = "(" + len + " byte)\n";
  484. maxLength /= 2;
  485. if (len > maxLength) {
  486. end = start + maxLength;
  487. }
  488. for (var i = start; i < end; ++i) {
  489. s += this.hexByte(this.get(i));
  490. }
  491. if (len > maxLength) {
  492. s += ellipsis;
  493. }
  494. return s;
  495. };
  496. Stream2.prototype.parseOID = function (start, end, maxLength) {
  497. var s = "";
  498. var n = new Int10();
  499. var bits = 0;
  500. for (var i = start; i < end; ++i) {
  501. var v = this.get(i);
  502. n.mulAdd(128, v & 127);
  503. bits += 7;
  504. if (!(v & 128)) {
  505. if (s === "") {
  506. n = n.simplify();
  507. if (n instanceof Int10) {
  508. n.sub(80);
  509. s = "2." + n.toString();
  510. } else {
  511. var m = n < 80 ? n < 40 ? 0 : 1 : 2;
  512. s = m + "." + (n - m * 40);
  513. }
  514. } else {
  515. s += "." + n.toString();
  516. }
  517. if (s.length > maxLength) {
  518. return stringCut(s, maxLength);
  519. }
  520. n = new Int10();
  521. bits = 0;
  522. }
  523. }
  524. if (bits > 0) {
  525. s += ".incomplete";
  526. }
  527. return s;
  528. };
  529. return Stream2;
  530. }();
  531. var ASN1 =
  532. /** @class */ function () {
  533. function ASN12(stream, header, length, tag, sub) {
  534. if (!(tag instanceof ASN1Tag)) {
  535. throw new Error("Invalid tag value.");
  536. }
  537. this.stream = stream;
  538. this.header = header;
  539. this.length = length;
  540. this.tag = tag;
  541. this.sub = sub;
  542. }
  543. ASN12.prototype.typeName = function () {
  544. switch (this.tag.tagClass) {
  545. case 0:
  546. switch (this.tag.tagNumber) {
  547. case 0:
  548. return "EOC";
  549. case 1:
  550. return "BOOLEAN";
  551. case 2:
  552. return "INTEGER";
  553. case 3:
  554. return "BIT_STRING";
  555. case 4:
  556. return "OCTET_STRING";
  557. case 5:
  558. return "NULL";
  559. case 6:
  560. return "OBJECT_IDENTIFIER";
  561. case 7:
  562. return "ObjectDescriptor";
  563. case 8:
  564. return "EXTERNAL";
  565. case 9:
  566. return "REAL";
  567. case 10:
  568. return "ENUMERATED";
  569. case 11:
  570. return "EMBEDDED_PDV";
  571. case 12:
  572. return "UTF8String";
  573. case 16:
  574. return "SEQUENCE";
  575. case 17:
  576. return "SET";
  577. case 18:
  578. return "NumericString";
  579. case 19:
  580. return "PrintableString";
  581. case 20:
  582. return "TeletexString";
  583. case 21:
  584. return "VideotexString";
  585. case 22:
  586. return "IA5String";
  587. case 23:
  588. return "UTCTime";
  589. case 24:
  590. return "GeneralizedTime";
  591. case 25:
  592. return "GraphicString";
  593. case 26:
  594. return "VisibleString";
  595. case 27:
  596. return "GeneralString";
  597. case 28:
  598. return "UniversalString";
  599. case 30:
  600. return "BMPString";
  601. }
  602. return "Universal_" + this.tag.tagNumber.toString();
  603. case 1:
  604. return "Application_" + this.tag.tagNumber.toString();
  605. case 2:
  606. return "[" + this.tag.tagNumber.toString() + "]";
  607. case 3:
  608. return "Private_" + this.tag.tagNumber.toString();
  609. }
  610. };
  611. ASN12.prototype.content = function (maxLength) {
  612. if (this.tag === void 0) {
  613. return null;
  614. }
  615. if (maxLength === void 0) {
  616. maxLength = Infinity;
  617. }
  618. var content = this.posContent();
  619. var len = Math.abs(this.length);
  620. if (!this.tag.isUniversal()) {
  621. if (this.sub !== null) {
  622. return "(" + this.sub.length + " elem)";
  623. }
  624. return this.stream.parseOctetString(content, content + len, maxLength);
  625. }
  626. switch (this.tag.tagNumber) {
  627. case 1:
  628. return this.stream.get(content) === 0 ? "false" : "true";
  629. case 2:
  630. return this.stream.parseInteger(content, content + len);
  631. case 3:
  632. return this.sub ? "(" + this.sub.length + " elem)" : this.stream.parseBitString(content, content + len, maxLength);
  633. case 4:
  634. return this.sub ? "(" + this.sub.length + " elem)" : this.stream.parseOctetString(content, content + len, maxLength);
  635. case 6:
  636. return this.stream.parseOID(content, content + len, maxLength);
  637. case 16:
  638. case 17:
  639. if (this.sub !== null) {
  640. return "(" + this.sub.length + " elem)";
  641. } else {
  642. return "(no elem)";
  643. }
  644. case 12:
  645. return stringCut(this.stream.parseStringUTF(content, content + len), maxLength);
  646. case 18:
  647. case 19:
  648. case 20:
  649. case 21:
  650. case 22:
  651. case 26:
  652. return stringCut(this.stream.parseStringISO(content, content + len), maxLength);
  653. case 30:
  654. return stringCut(this.stream.parseStringBMP(content, content + len), maxLength);
  655. case 23:
  656. case 24:
  657. return this.stream.parseTime(content, content + len, this.tag.tagNumber == 23);
  658. }
  659. return null;
  660. };
  661. ASN12.prototype.toString = function () {
  662. return this.typeName() + "@" + this.stream.pos + "[header:" + this.header + ",length:" + this.length + ",sub:" + (this.sub === null ? "null" : this.sub.length) + "]";
  663. };
  664. ASN12.prototype.toPrettyString = function (indent) {
  665. if (indent === void 0) {
  666. indent = "";
  667. }
  668. var s = indent + this.typeName() + " @" + this.stream.pos;
  669. if (this.length >= 0) {
  670. s += "+";
  671. }
  672. s += this.length;
  673. if (this.tag.tagConstructed) {
  674. s += " (constructed)";
  675. } else if (this.tag.isUniversal() && (this.tag.tagNumber == 3 || this.tag.tagNumber == 4) && this.sub !== null) {
  676. s += " (encapsulates)";
  677. }
  678. s += "\n";
  679. if (this.sub !== null) {
  680. indent += " ";
  681. for (var i = 0, max2 = this.sub.length; i < max2; ++i) {
  682. s += this.sub[i].toPrettyString(indent);
  683. }
  684. }
  685. return s;
  686. };
  687. ASN12.prototype.posStart = function () {
  688. return this.stream.pos;
  689. };
  690. ASN12.prototype.posContent = function () {
  691. return this.stream.pos + this.header;
  692. };
  693. ASN12.prototype.posEnd = function () {
  694. return this.stream.pos + this.header + Math.abs(this.length);
  695. };
  696. ASN12.prototype.toHexString = function () {
  697. return this.stream.hexDump(this.posStart(), this.posEnd(), true);
  698. };
  699. ASN12.decodeLength = function (stream) {
  700. var buf = stream.get();
  701. var len = buf & 127;
  702. if (len == buf) {
  703. return len;
  704. }
  705. if (len > 6) {
  706. throw new Error("Length over 48 bits not supported at position " + (stream.pos - 1));
  707. }
  708. if (len === 0) {
  709. return null;
  710. }
  711. buf = 0;
  712. for (var i = 0; i < len; ++i) {
  713. buf = buf * 256 + stream.get();
  714. }
  715. return buf;
  716. };
  717. ASN12.prototype.getHexStringValue = function () {
  718. var hexString = this.toHexString();
  719. var offset = this.header * 2;
  720. var length = this.length * 2;
  721. return hexString.substr(offset, length);
  722. };
  723. ASN12.decode = function (str) {
  724. var stream;
  725. if (!(str instanceof Stream)) {
  726. stream = new Stream(str, 0);
  727. } else {
  728. stream = str;
  729. }
  730. var streamStart = new Stream(stream);
  731. var tag = new ASN1Tag(stream);
  732. var len = ASN12.decodeLength(stream);
  733. var start = stream.pos;
  734. var header = start - streamStart.pos;
  735. var sub = null;
  736. var getSub = function () {
  737. var ret = [];
  738. if (len !== null) {
  739. var end = start + len;
  740. while (stream.pos < end) {
  741. ret[ret.length] = ASN12.decode(stream);
  742. }
  743. if (stream.pos != end) {
  744. throw new Error("Content size is not correct for container starting at offset " + start);
  745. }
  746. } else {
  747. try {
  748. for (; ;) {
  749. var s = ASN12.decode(stream);
  750. if (s.tag.isEOC()) {
  751. break;
  752. }
  753. ret[ret.length] = s;
  754. }
  755. len = start - stream.pos;
  756. } catch (e) {
  757. throw new Error("Exception while decoding undefined length content: " + e);
  758. }
  759. }
  760. return ret;
  761. };
  762. if (tag.tagConstructed) {
  763. sub = getSub();
  764. } else if (tag.isUniversal() && (tag.tagNumber == 3 || tag.tagNumber == 4)) {
  765. try {
  766. if (tag.tagNumber == 3) {
  767. if (stream.get() != 0) {
  768. throw new Error("BIT STRINGs with unused bits cannot encapsulate.");
  769. }
  770. }
  771. sub = getSub();
  772. for (var i = 0; i < sub.length; ++i) {
  773. if (sub[i].tag.isEOC()) {
  774. throw new Error("EOC is not supposed to be actual content.");
  775. }
  776. }
  777. } catch (e) {
  778. sub = null;
  779. }
  780. }
  781. if (sub === null) {
  782. if (len === null) {
  783. throw new Error("We can't skip over an invalid tag with undefined length at offset " + start);
  784. }
  785. stream.pos = start + Math.abs(len);
  786. }
  787. return new ASN12(streamStart, header, len, tag, sub);
  788. };
  789. return ASN12;
  790. }();
  791. var ASN1Tag =
  792. /** @class */ function () {
  793. function ASN1Tag2(stream) {
  794. var buf = stream.get();
  795. this.tagClass = buf >> 6;
  796. this.tagConstructed = (buf & 32) !== 0;
  797. this.tagNumber = buf & 31;
  798. if (this.tagNumber == 31) {
  799. var n = new Int10();
  800. do {
  801. buf = stream.get();
  802. n.mulAdd(128, buf & 127);
  803. } while (buf & 128);
  804. this.tagNumber = n.simplify();
  805. }
  806. }
  807. ASN1Tag2.prototype.isUniversal = function () {
  808. return this.tagClass === 0;
  809. };
  810. ASN1Tag2.prototype.isEOC = function () {
  811. return this.tagClass === 0 && this.tagNumber === 0;
  812. };
  813. return ASN1Tag2;
  814. }();
  815. // lib/lib/jsbn/jsbn.js
  816. var dbits;
  817. var canary = 0xdeadbeefcafe;
  818. var j_lm = (canary & 16777215) == 15715070;
  819. 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, 521, 523, 541, 547, 557, 563, 569, 571, 577, 587, 593, 599, 601, 607, 613, 617, 619, 631, 641, 643, 647, 653, 659, 661, 673, 677, 683, 691, 701, 709, 719, 727, 733, 739, 743, 751, 757, 761, 769, 773, 787, 797, 809, 811, 821, 823, 827, 829, 839, 853, 857, 859, 863, 877, 881, 883, 887, 907, 911, 919, 929, 937, 941, 947, 953, 967, 971, 977, 983, 991, 997];
  820. var lplim = (1 << 26) / lowprimes[lowprimes.length - 1];
  821. var BigInteger =
  822. /** @class */ function () {
  823. function BigInteger2(a, b, c) {
  824. if (a != null) {
  825. if ("number" == typeof a) {
  826. this.fromNumber(a, b, c);
  827. } else if (b == null && "string" != typeof a) {
  828. this.fromString(a, 256);
  829. } else {
  830. this.fromString(a, b);
  831. }
  832. }
  833. }
  834. BigInteger2.prototype.toString = function (b) {
  835. if (this.s < 0) {
  836. return "-" + this.negate().toString(b);
  837. }
  838. var k;
  839. if (b == 16) {
  840. k = 4;
  841. } else if (b == 8) {
  842. k = 3;
  843. } else if (b == 2) {
  844. k = 1;
  845. } else if (b == 32) {
  846. k = 5;
  847. } else if (b == 4) {
  848. k = 2;
  849. } else {
  850. return this.toRadix(b);
  851. }
  852. var km = (1 << k) - 1;
  853. var d;
  854. var m = false;
  855. var r = "";
  856. var i = this.t;
  857. var p = this.DB - i * this.DB % k;
  858. if (i-- > 0) {
  859. if (p < this.DB && (d = this[i] >> p) > 0) {
  860. m = true;
  861. r = int2char(d);
  862. }
  863. while (i >= 0) {
  864. if (p < k) {
  865. d = (this[i] & (1 << p) - 1) << k - p;
  866. d |= this[--i] >> (p += this.DB - k);
  867. } else {
  868. d = this[i] >> (p -= k) & km;
  869. if (p <= 0) {
  870. p += this.DB;
  871. --i;
  872. }
  873. }
  874. if (d > 0) {
  875. m = true;
  876. }
  877. if (m) {
  878. r += int2char(d);
  879. }
  880. }
  881. }
  882. return m ? r : "0";
  883. };
  884. BigInteger2.prototype.negate = function () {
  885. var r = nbi();
  886. BigInteger2.ZERO.subTo(this, r);
  887. return r;
  888. };
  889. BigInteger2.prototype.abs = function () {
  890. return this.s < 0 ? this.negate() : this;
  891. };
  892. BigInteger2.prototype.compareTo = function (a) {
  893. var r = this.s - a.s;
  894. if (r != 0) {
  895. return r;
  896. }
  897. var i = this.t;
  898. r = i - a.t;
  899. if (r != 0) {
  900. return this.s < 0 ? -r : r;
  901. }
  902. while (--i >= 0) {
  903. if ((r = this[i] - a[i]) != 0) {
  904. return r;
  905. }
  906. }
  907. return 0;
  908. };
  909. BigInteger2.prototype.bitLength = function () {
  910. if (this.t <= 0) {
  911. return 0;
  912. }
  913. return this.DB * (this.t - 1) + nbits(this[this.t - 1] ^ this.s & this.DM);
  914. };
  915. BigInteger2.prototype.mod = function (a) {
  916. var r = nbi();
  917. this.abs().divRemTo(a, null, r);
  918. if (this.s < 0 && r.compareTo(BigInteger2.ZERO) > 0) {
  919. a.subTo(r, r);
  920. }
  921. return r;
  922. };
  923. BigInteger2.prototype.modPowInt = function (e, m) {
  924. var z;
  925. if (e < 256 || m.isEven()) {
  926. z = new Classic(m);
  927. } else {
  928. z = new Montgomery(m);
  929. }
  930. return this.exp(e, z);
  931. };
  932. BigInteger2.prototype.clone = function () {
  933. var r = nbi();
  934. this.copyTo(r);
  935. return r;
  936. };
  937. BigInteger2.prototype.intValue = function () {
  938. if (this.s < 0) {
  939. if (this.t == 1) {
  940. return this[0] - this.DV;
  941. } else if (this.t == 0) {
  942. return -1;
  943. }
  944. } else if (this.t == 1) {
  945. return this[0];
  946. } else if (this.t == 0) {
  947. return 0;
  948. }
  949. return (this[1] & (1 << 32 - this.DB) - 1) << this.DB | this[0];
  950. };
  951. BigInteger2.prototype.byteValue = function () {
  952. return this.t == 0 ? this.s : this[0] << 24 >> 24;
  953. };
  954. BigInteger2.prototype.shortValue = function () {
  955. return this.t == 0 ? this.s : this[0] << 16 >> 16;
  956. };
  957. BigInteger2.prototype.signum = function () {
  958. if (this.s < 0) {
  959. return -1;
  960. } else if (this.t <= 0 || this.t == 1 && this[0] <= 0) {
  961. return 0;
  962. } else {
  963. return 1;
  964. }
  965. };
  966. BigInteger2.prototype.toByteArray = function () {
  967. var i = this.t;
  968. var r = [];
  969. r[0] = this.s;
  970. var p = this.DB - i * this.DB % 8;
  971. var d;
  972. var k = 0;
  973. if (i-- > 0) {
  974. if (p < this.DB && (d = this[i] >> p) != (this.s & this.DM) >> p) {
  975. r[k++] = d | this.s << this.DB - p;
  976. }
  977. while (i >= 0) {
  978. if (p < 8) {
  979. d = (this[i] & (1 << p) - 1) << 8 - p;
  980. d |= this[--i] >> (p += this.DB - 8);
  981. } else {
  982. d = this[i] >> (p -= 8) & 255;
  983. if (p <= 0) {
  984. p += this.DB;
  985. --i;
  986. }
  987. }
  988. if ((d & 128) != 0) {
  989. d |= -256;
  990. }
  991. if (k == 0 && (this.s & 128) != (d & 128)) {
  992. ++k;
  993. }
  994. if (k > 0 || d != this.s) {
  995. r[k++] = d;
  996. }
  997. }
  998. }
  999. return r;
  1000. };
  1001. BigInteger2.prototype.equals = function (a) {
  1002. return this.compareTo(a) == 0;
  1003. };
  1004. BigInteger2.prototype.min = function (a) {
  1005. return this.compareTo(a) < 0 ? this : a;
  1006. };
  1007. BigInteger2.prototype.max = function (a) {
  1008. return this.compareTo(a) > 0 ? this : a;
  1009. };
  1010. BigInteger2.prototype.and = function (a) {
  1011. var r = nbi();
  1012. this.bitwiseTo(a, op_and, r);
  1013. return r;
  1014. };
  1015. BigInteger2.prototype.or = function (a) {
  1016. var r = nbi();
  1017. this.bitwiseTo(a, op_or, r);
  1018. return r;
  1019. };
  1020. BigInteger2.prototype.xor = function (a) {
  1021. var r = nbi();
  1022. this.bitwiseTo(a, op_xor, r);
  1023. return r;
  1024. };
  1025. BigInteger2.prototype.andNot = function (a) {
  1026. var r = nbi();
  1027. this.bitwiseTo(a, op_andnot, r);
  1028. return r;
  1029. };
  1030. BigInteger2.prototype.not = function () {
  1031. var r = nbi();
  1032. for (var i = 0; i < this.t; ++i) {
  1033. r[i] = this.DM & ~this[i];
  1034. }
  1035. r.t = this.t;
  1036. r.s = ~this.s;
  1037. return r;
  1038. };
  1039. BigInteger2.prototype.shiftLeft = function (n) {
  1040. var r = nbi();
  1041. if (n < 0) {
  1042. this.rShiftTo(-n, r);
  1043. } else {
  1044. this.lShiftTo(n, r);
  1045. }
  1046. return r;
  1047. };
  1048. BigInteger2.prototype.shiftRight = function (n) {
  1049. var r = nbi();
  1050. if (n < 0) {
  1051. this.lShiftTo(-n, r);
  1052. } else {
  1053. this.rShiftTo(n, r);
  1054. }
  1055. return r;
  1056. };
  1057. BigInteger2.prototype.getLowestSetBit = function () {
  1058. for (var i = 0; i < this.t; ++i) {
  1059. if (this[i] != 0) {
  1060. return i * this.DB + lbit(this[i]);
  1061. }
  1062. }
  1063. if (this.s < 0) {
  1064. return this.t * this.DB;
  1065. }
  1066. return -1;
  1067. };
  1068. BigInteger2.prototype.bitCount = function () {
  1069. var r = 0;
  1070. var x = this.s & this.DM;
  1071. for (var i = 0; i < this.t; ++i) {
  1072. r += cbit(this[i] ^ x);
  1073. }
  1074. return r;
  1075. };
  1076. BigInteger2.prototype.testBit = function (n) {
  1077. var j = Math.floor(n / this.DB);
  1078. if (j >= this.t) {
  1079. return this.s != 0;
  1080. }
  1081. return (this[j] & 1 << n % this.DB) != 0;
  1082. };
  1083. BigInteger2.prototype.setBit = function (n) {
  1084. return this.changeBit(n, op_or);
  1085. };
  1086. BigInteger2.prototype.clearBit = function (n) {
  1087. return this.changeBit(n, op_andnot);
  1088. };
  1089. BigInteger2.prototype.flipBit = function (n) {
  1090. return this.changeBit(n, op_xor);
  1091. };
  1092. BigInteger2.prototype.add = function (a) {
  1093. var r = nbi();
  1094. this.addTo(a, r);
  1095. return r;
  1096. };
  1097. BigInteger2.prototype.subtract = function (a) {
  1098. var r = nbi();
  1099. this.subTo(a, r);
  1100. return r;
  1101. };
  1102. BigInteger2.prototype.multiply = function (a) {
  1103. var r = nbi();
  1104. this.multiplyTo(a, r);
  1105. return r;
  1106. };
  1107. BigInteger2.prototype.divide = function (a) {
  1108. var r = nbi();
  1109. this.divRemTo(a, r, null);
  1110. return r;
  1111. };
  1112. BigInteger2.prototype.remainder = function (a) {
  1113. var r = nbi();
  1114. this.divRemTo(a, null, r);
  1115. return r;
  1116. };
  1117. BigInteger2.prototype.divideAndRemainder = function (a) {
  1118. var q = nbi();
  1119. var r = nbi();
  1120. this.divRemTo(a, q, r);
  1121. return [q, r];
  1122. };
  1123. BigInteger2.prototype.modPow = function (e, m) {
  1124. var i = e.bitLength();
  1125. var k;
  1126. var r = nbv(1);
  1127. var z;
  1128. if (i <= 0) {
  1129. return r;
  1130. } else if (i < 18) {
  1131. k = 1;
  1132. } else if (i < 48) {
  1133. k = 3;
  1134. } else if (i < 144) {
  1135. k = 4;
  1136. } else if (i < 768) {
  1137. k = 5;
  1138. } else {
  1139. k = 6;
  1140. }
  1141. if (i < 8) {
  1142. z = new Classic(m);
  1143. } else if (m.isEven()) {
  1144. z = new Barrett(m);
  1145. } else {
  1146. z = new Montgomery(m);
  1147. }
  1148. var g = [];
  1149. var n = 3;
  1150. var k1 = k - 1;
  1151. var km = (1 << k) - 1;
  1152. g[1] = z.convert(this);
  1153. if (k > 1) {
  1154. var g2 = nbi();
  1155. z.sqrTo(g[1], g2);
  1156. while (n <= km) {
  1157. g[n] = nbi();
  1158. z.mulTo(g2, g[n - 2], g[n]);
  1159. n += 2;
  1160. }
  1161. }
  1162. var j = e.t - 1;
  1163. var w;
  1164. var is1 = true;
  1165. var r2 = nbi();
  1166. var t;
  1167. i = nbits(e[j]) - 1;
  1168. while (j >= 0) {
  1169. if (i >= k1) {
  1170. w = e[j] >> i - k1 & km;
  1171. } else {
  1172. w = (e[j] & (1 << i + 1) - 1) << k1 - i;
  1173. if (j > 0) {
  1174. w |= e[j - 1] >> this.DB + i - k1;
  1175. }
  1176. }
  1177. n = k;
  1178. while ((w & 1) == 0) {
  1179. w >>= 1;
  1180. --n;
  1181. }
  1182. if ((i -= n) < 0) {
  1183. i += this.DB;
  1184. --j;
  1185. }
  1186. if (is1) {
  1187. g[w].copyTo(r);
  1188. is1 = false;
  1189. } else {
  1190. while (n > 1) {
  1191. z.sqrTo(r, r2);
  1192. z.sqrTo(r2, r);
  1193. n -= 2;
  1194. }
  1195. if (n > 0) {
  1196. z.sqrTo(r, r2);
  1197. } else {
  1198. t = r;
  1199. r = r2;
  1200. r2 = t;
  1201. }
  1202. z.mulTo(r2, g[w], r);
  1203. }
  1204. while (j >= 0 && (e[j] & 1 << i) == 0) {
  1205. z.sqrTo(r, r2);
  1206. t = r;
  1207. r = r2;
  1208. r2 = t;
  1209. if (--i < 0) {
  1210. i = this.DB - 1;
  1211. --j;
  1212. }
  1213. }
  1214. }
  1215. return z.revert(r);
  1216. };
  1217. BigInteger2.prototype.modInverse = function (m) {
  1218. var ac = m.isEven();
  1219. if (this.isEven() && ac || m.signum() == 0) {
  1220. return BigInteger2.ZERO;
  1221. }
  1222. var u = m.clone();
  1223. var v = this.clone();
  1224. var a = nbv(1);
  1225. var b = nbv(0);
  1226. var c = nbv(0);
  1227. var d = nbv(1);
  1228. while (u.signum() != 0) {
  1229. while (u.isEven()) {
  1230. u.rShiftTo(1, u);
  1231. if (ac) {
  1232. if (!a.isEven() || !b.isEven()) {
  1233. a.addTo(this, a);
  1234. b.subTo(m, b);
  1235. }
  1236. a.rShiftTo(1, a);
  1237. } else if (!b.isEven()) {
  1238. b.subTo(m, b);
  1239. }
  1240. b.rShiftTo(1, b);
  1241. }
  1242. while (v.isEven()) {
  1243. v.rShiftTo(1, v);
  1244. if (ac) {
  1245. if (!c.isEven() || !d.isEven()) {
  1246. c.addTo(this, c);
  1247. d.subTo(m, d);
  1248. }
  1249. c.rShiftTo(1, c);
  1250. } else if (!d.isEven()) {
  1251. d.subTo(m, d);
  1252. }
  1253. d.rShiftTo(1, d);
  1254. }
  1255. if (u.compareTo(v) >= 0) {
  1256. u.subTo(v, u);
  1257. if (ac) {
  1258. a.subTo(c, a);
  1259. }
  1260. b.subTo(d, b);
  1261. } else {
  1262. v.subTo(u, v);
  1263. if (ac) {
  1264. c.subTo(a, c);
  1265. }
  1266. d.subTo(b, d);
  1267. }
  1268. }
  1269. if (v.compareTo(BigInteger2.ONE) != 0) {
  1270. return BigInteger2.ZERO;
  1271. }
  1272. if (d.compareTo(m) >= 0) {
  1273. return d.subtract(m);
  1274. }
  1275. if (d.signum() < 0) {
  1276. d.addTo(m, d);
  1277. } else {
  1278. return d;
  1279. }
  1280. if (d.signum() < 0) {
  1281. return d.add(m);
  1282. } else {
  1283. return d;
  1284. }
  1285. };
  1286. BigInteger2.prototype.pow = function (e) {
  1287. return this.exp(e, new NullExp());
  1288. };
  1289. BigInteger2.prototype.gcd = function (a) {
  1290. var x = this.s < 0 ? this.negate() : this.clone();
  1291. var y = a.s < 0 ? a.negate() : a.clone();
  1292. if (x.compareTo(y) < 0) {
  1293. var t = x;
  1294. x = y;
  1295. y = t;
  1296. }
  1297. var i = x.getLowestSetBit();
  1298. var g = y.getLowestSetBit();
  1299. if (g < 0) {
  1300. return x;
  1301. }
  1302. if (i < g) {
  1303. g = i;
  1304. }
  1305. if (g > 0) {
  1306. x.rShiftTo(g, x);
  1307. y.rShiftTo(g, y);
  1308. }
  1309. while (x.signum() > 0) {
  1310. if ((i = x.getLowestSetBit()) > 0) {
  1311. x.rShiftTo(i, x);
  1312. }
  1313. if ((i = y.getLowestSetBit()) > 0) {
  1314. y.rShiftTo(i, y);
  1315. }
  1316. if (x.compareTo(y) >= 0) {
  1317. x.subTo(y, x);
  1318. x.rShiftTo(1, x);
  1319. } else {
  1320. y.subTo(x, y);
  1321. y.rShiftTo(1, y);
  1322. }
  1323. }
  1324. if (g > 0) {
  1325. y.lShiftTo(g, y);
  1326. }
  1327. return y;
  1328. };
  1329. BigInteger2.prototype.isProbablePrime = function (t) {
  1330. var i;
  1331. var x = this.abs();
  1332. if (x.t == 1 && x[0] <= lowprimes[lowprimes.length - 1]) {
  1333. for (i = 0; i < lowprimes.length; ++i) {
  1334. if (x[0] == lowprimes[i]) {
  1335. return true;
  1336. }
  1337. }
  1338. return false;
  1339. }
  1340. if (x.isEven()) {
  1341. return false;
  1342. }
  1343. i = 1;
  1344. while (i < lowprimes.length) {
  1345. var m = lowprimes[i];
  1346. var j = i + 1;
  1347. while (j < lowprimes.length && m < lplim) {
  1348. m *= lowprimes[j++];
  1349. }
  1350. m = x.modInt(m);
  1351. while (i < j) {
  1352. if (m % lowprimes[i++] == 0) {
  1353. return false;
  1354. }
  1355. }
  1356. }
  1357. return x.millerRabin(t);
  1358. };
  1359. BigInteger2.prototype.copyTo = function (r) {
  1360. for (var i = this.t - 1; i >= 0; --i) {
  1361. r[i] = this[i];
  1362. }
  1363. r.t = this.t;
  1364. r.s = this.s;
  1365. };
  1366. BigInteger2.prototype.fromInt = function (x) {
  1367. this.t = 1;
  1368. this.s = x < 0 ? -1 : 0;
  1369. if (x > 0) {
  1370. this[0] = x;
  1371. } else if (x < -1) {
  1372. this[0] = x + this.DV;
  1373. } else {
  1374. this.t = 0;
  1375. }
  1376. };
  1377. BigInteger2.prototype.fromString = function (s, b) {
  1378. var k;
  1379. if (b == 16) {
  1380. k = 4;
  1381. } else if (b == 8) {
  1382. k = 3;
  1383. } else if (b == 256) {
  1384. k = 8;
  1385. } else if (b == 2) {
  1386. k = 1;
  1387. } else if (b == 32) {
  1388. k = 5;
  1389. } else if (b == 4) {
  1390. k = 2;
  1391. } else {
  1392. this.fromRadix(s, b);
  1393. return;
  1394. }
  1395. this.t = 0;
  1396. this.s = 0;
  1397. var i = s.length;
  1398. var mi = false;
  1399. var sh = 0;
  1400. while (--i >= 0) {
  1401. var x = k == 8 ? +s[i] & 255 : intAt(s, i);
  1402. if (x < 0) {
  1403. if (s.charAt(i) == "-") {
  1404. mi = true;
  1405. }
  1406. continue;
  1407. }
  1408. mi = false;
  1409. if (sh == 0) {
  1410. this[this.t++] = x;
  1411. } else if (sh + k > this.DB) {
  1412. this[this.t - 1] |= (x & (1 << this.DB - sh) - 1) << sh;
  1413. this[this.t++] = x >> this.DB - sh;
  1414. } else {
  1415. this[this.t - 1] |= x << sh;
  1416. }
  1417. sh += k;
  1418. if (sh >= this.DB) {
  1419. sh -= this.DB;
  1420. }
  1421. }
  1422. if (k == 8 && (+s[0] & 128) != 0) {
  1423. this.s = -1;
  1424. if (sh > 0) {
  1425. this[this.t - 1] |= (1 << this.DB - sh) - 1 << sh;
  1426. }
  1427. }
  1428. this.clamp();
  1429. if (mi) {
  1430. BigInteger2.ZERO.subTo(this, this);
  1431. }
  1432. };
  1433. BigInteger2.prototype.clamp = function () {
  1434. var c = this.s & this.DM;
  1435. while (this.t > 0 && this[this.t - 1] == c) {
  1436. --this.t;
  1437. }
  1438. };
  1439. BigInteger2.prototype.dlShiftTo = function (n, r) {
  1440. var i;
  1441. for (i = this.t - 1; i >= 0; --i) {
  1442. r[i + n] = this[i];
  1443. }
  1444. for (i = n - 1; i >= 0; --i) {
  1445. r[i] = 0;
  1446. }
  1447. r.t = this.t + n;
  1448. r.s = this.s;
  1449. };
  1450. BigInteger2.prototype.drShiftTo = function (n, r) {
  1451. for (var i = n; i < this.t; ++i) {
  1452. r[i - n] = this[i];
  1453. }
  1454. r.t = Math.max(this.t - n, 0);
  1455. r.s = this.s;
  1456. };
  1457. BigInteger2.prototype.lShiftTo = function (n, r) {
  1458. var bs = n % this.DB;
  1459. var cbs = this.DB - bs;
  1460. var bm = (1 << cbs) - 1;
  1461. var ds = Math.floor(n / this.DB);
  1462. var c = this.s << bs & this.DM;
  1463. for (var i = this.t - 1; i >= 0; --i) {
  1464. r[i + ds + 1] = this[i] >> cbs | c;
  1465. c = (this[i] & bm) << bs;
  1466. }
  1467. for (var i = ds - 1; i >= 0; --i) {
  1468. r[i] = 0;
  1469. }
  1470. r[ds] = c;
  1471. r.t = this.t + ds + 1;
  1472. r.s = this.s;
  1473. r.clamp();
  1474. };
  1475. BigInteger2.prototype.rShiftTo = function (n, r) {
  1476. r.s = this.s;
  1477. var ds = Math.floor(n / this.DB);
  1478. if (ds >= this.t) {
  1479. r.t = 0;
  1480. return;
  1481. }
  1482. var bs = n % this.DB;
  1483. var cbs = this.DB - bs;
  1484. var bm = (1 << bs) - 1;
  1485. r[0] = this[ds] >> bs;
  1486. for (var i = ds + 1; i < this.t; ++i) {
  1487. r[i - ds - 1] |= (this[i] & bm) << cbs;
  1488. r[i - ds] = this[i] >> bs;
  1489. }
  1490. if (bs > 0) {
  1491. r[this.t - ds - 1] |= (this.s & bm) << cbs;
  1492. }
  1493. r.t = this.t - ds;
  1494. r.clamp();
  1495. };
  1496. BigInteger2.prototype.subTo = function (a, r) {
  1497. var i = 0;
  1498. var c = 0;
  1499. var m = Math.min(a.t, this.t);
  1500. while (i < m) {
  1501. c += this[i] - a[i];
  1502. r[i++] = c & this.DM;
  1503. c >>= this.DB;
  1504. }
  1505. if (a.t < this.t) {
  1506. c -= a.s;
  1507. while (i < this.t) {
  1508. c += this[i];
  1509. r[i++] = c & this.DM;
  1510. c >>= this.DB;
  1511. }
  1512. c += this.s;
  1513. } else {
  1514. c += this.s;
  1515. while (i < a.t) {
  1516. c -= a[i];
  1517. r[i++] = c & this.DM;
  1518. c >>= this.DB;
  1519. }
  1520. c -= a.s;
  1521. }
  1522. r.s = c < 0 ? -1 : 0;
  1523. if (c < -1) {
  1524. r[i++] = this.DV + c;
  1525. } else if (c > 0) {
  1526. r[i++] = c;
  1527. }
  1528. r.t = i;
  1529. r.clamp();
  1530. };
  1531. BigInteger2.prototype.multiplyTo = function (a, r) {
  1532. var x = this.abs();
  1533. var y = a.abs();
  1534. var i = x.t;
  1535. r.t = i + y.t;
  1536. while (--i >= 0) {
  1537. r[i] = 0;
  1538. }
  1539. for (i = 0; i < y.t; ++i) {
  1540. r[i + x.t] = x.am(0, y[i], r, i, 0, x.t);
  1541. }
  1542. r.s = 0;
  1543. r.clamp();
  1544. if (this.s != a.s) {
  1545. BigInteger2.ZERO.subTo(r, r);
  1546. }
  1547. };
  1548. BigInteger2.prototype.squareTo = function (r) {
  1549. var x = this.abs();
  1550. var i = r.t = 2 * x.t;
  1551. while (--i >= 0) {
  1552. r[i] = 0;
  1553. }
  1554. for (i = 0; i < x.t - 1; ++i) {
  1555. var c = x.am(i, x[i], r, 2 * i, 0, 1);
  1556. if ((r[i + x.t] += x.am(i + 1, 2 * x[i], r, 2 * i + 1, c, x.t - i - 1)) >= x.DV) {
  1557. r[i + x.t] -= x.DV;
  1558. r[i + x.t + 1] = 1;
  1559. }
  1560. }
  1561. if (r.t > 0) {
  1562. r[r.t - 1] += x.am(i, x[i], r, 2 * i, 0, 1);
  1563. }
  1564. r.s = 0;
  1565. r.clamp();
  1566. };
  1567. BigInteger2.prototype.divRemTo = function (m, q, r) {
  1568. var pm = m.abs();
  1569. if (pm.t <= 0) {
  1570. return;
  1571. }
  1572. var pt = this.abs();
  1573. if (pt.t < pm.t) {
  1574. if (q != null) {
  1575. q.fromInt(0);
  1576. }
  1577. if (r != null) {
  1578. this.copyTo(r);
  1579. }
  1580. return;
  1581. }
  1582. if (r == null) {
  1583. r = nbi();
  1584. }
  1585. var y = nbi();
  1586. var ts = this.s;
  1587. var ms = m.s;
  1588. var nsh = this.DB - nbits(pm[pm.t - 1]);
  1589. if (nsh > 0) {
  1590. pm.lShiftTo(nsh, y);
  1591. pt.lShiftTo(nsh, r);
  1592. } else {
  1593. pm.copyTo(y);
  1594. pt.copyTo(r);
  1595. }
  1596. var ys = y.t;
  1597. var y0 = y[ys - 1];
  1598. if (y0 == 0) {
  1599. return;
  1600. }
  1601. var yt = y0 * (1 << this.F1) + (ys > 1 ? y[ys - 2] >> this.F2 : 0);
  1602. var d1 = this.FV / yt;
  1603. var d2 = (1 << this.F1) / yt;
  1604. var e = 1 << this.F2;
  1605. var i = r.t;
  1606. var j = i - ys;
  1607. var t = q == null ? nbi() : q;
  1608. y.dlShiftTo(j, t);
  1609. if (r.compareTo(t) >= 0) {
  1610. r[r.t++] = 1;
  1611. r.subTo(t, r);
  1612. }
  1613. BigInteger2.ONE.dlShiftTo(ys, t);
  1614. t.subTo(y, y);
  1615. while (y.t < ys) {
  1616. y[y.t++] = 0;
  1617. }
  1618. while (--j >= 0) {
  1619. var qd = r[--i] == y0 ? this.DM : Math.floor(r[i] * d1 + (r[i - 1] + e) * d2);
  1620. if ((r[i] += y.am(0, qd, r, j, 0, ys)) < qd) {
  1621. y.dlShiftTo(j, t);
  1622. r.subTo(t, r);
  1623. while (r[i] < --qd) {
  1624. r.subTo(t, r);
  1625. }
  1626. }
  1627. }
  1628. if (q != null) {
  1629. r.drShiftTo(ys, q);
  1630. if (ts != ms) {
  1631. BigInteger2.ZERO.subTo(q, q);
  1632. }
  1633. }
  1634. r.t = ys;
  1635. r.clamp();
  1636. if (nsh > 0) {
  1637. r.rShiftTo(nsh, r);
  1638. }
  1639. if (ts < 0) {
  1640. BigInteger2.ZERO.subTo(r, r);
  1641. }
  1642. };
  1643. BigInteger2.prototype.invDigit = function () {
  1644. if (this.t < 1) {
  1645. return 0;
  1646. }
  1647. var x = this[0];
  1648. if ((x & 1) == 0) {
  1649. return 0;
  1650. }
  1651. var y = x & 3;
  1652. y = y * (2 - (x & 15) * y) & 15;
  1653. y = y * (2 - (x & 255) * y) & 255;
  1654. y = y * (2 - ((x & 65535) * y & 65535)) & 65535;
  1655. y = y * (2 - x * y % this.DV) % this.DV;
  1656. return y > 0 ? this.DV - y : -y;
  1657. };
  1658. BigInteger2.prototype.isEven = function () {
  1659. return (this.t > 0 ? this[0] & 1 : this.s) == 0;
  1660. };
  1661. BigInteger2.prototype.exp = function (e, z) {
  1662. if (e > 4294967295 || e < 1) {
  1663. return BigInteger2.ONE;
  1664. }
  1665. var r = nbi();
  1666. var r2 = nbi();
  1667. var g = z.convert(this);
  1668. var i = nbits(e) - 1;
  1669. g.copyTo(r);
  1670. while (--i >= 0) {
  1671. z.sqrTo(r, r2);
  1672. if ((e & 1 << i) > 0) {
  1673. z.mulTo(r2, g, r);
  1674. } else {
  1675. var t = r;
  1676. r = r2;
  1677. r2 = t;
  1678. }
  1679. }
  1680. return z.revert(r);
  1681. };
  1682. BigInteger2.prototype.chunkSize = function (r) {
  1683. return Math.floor(Math.LN2 * this.DB / Math.log(r));
  1684. };
  1685. BigInteger2.prototype.toRadix = function (b) {
  1686. if (b == null) {
  1687. b = 10;
  1688. }
  1689. if (this.signum() == 0 || b < 2 || b > 36) {
  1690. return "0";
  1691. }
  1692. var cs = this.chunkSize(b);
  1693. var a = Math.pow(b, cs);
  1694. var d = nbv(a);
  1695. var y = nbi();
  1696. var z = nbi();
  1697. var r = "";
  1698. this.divRemTo(d, y, z);
  1699. while (y.signum() > 0) {
  1700. r = (a + z.intValue()).toString(b).substr(1) + r;
  1701. y.divRemTo(d, y, z);
  1702. }
  1703. return z.intValue().toString(b) + r;
  1704. };
  1705. BigInteger2.prototype.fromRadix = function (s, b) {
  1706. this.fromInt(0);
  1707. if (b == null) {
  1708. b = 10;
  1709. }
  1710. var cs = this.chunkSize(b);
  1711. var d = Math.pow(b, cs);
  1712. var mi = false;
  1713. var j = 0;
  1714. var w = 0;
  1715. for (var i = 0; i < s.length; ++i) {
  1716. var x = intAt(s, i);
  1717. if (x < 0) {
  1718. if (s.charAt(i) == "-" && this.signum() == 0) {
  1719. mi = true;
  1720. }
  1721. continue;
  1722. }
  1723. w = b * w + x;
  1724. if (++j >= cs) {
  1725. this.dMultiply(d);
  1726. this.dAddOffset(w, 0);
  1727. j = 0;
  1728. w = 0;
  1729. }
  1730. }
  1731. if (j > 0) {
  1732. this.dMultiply(Math.pow(b, j));
  1733. this.dAddOffset(w, 0);
  1734. }
  1735. if (mi) {
  1736. BigInteger2.ZERO.subTo(this, this);
  1737. }
  1738. };
  1739. BigInteger2.prototype.fromNumber = function (a, b, c) {
  1740. if ("number" == typeof b) {
  1741. if (a < 2) {
  1742. this.fromInt(1);
  1743. } else {
  1744. this.fromNumber(a, c);
  1745. if (!this.testBit(a - 1)) {
  1746. this.bitwiseTo(BigInteger2.ONE.shiftLeft(a - 1), op_or, this);
  1747. }
  1748. if (this.isEven()) {
  1749. this.dAddOffset(1, 0);
  1750. }
  1751. while (!this.isProbablePrime(b)) {
  1752. this.dAddOffset(2, 0);
  1753. if (this.bitLength() > a) {
  1754. this.subTo(BigInteger2.ONE.shiftLeft(a - 1), this);
  1755. }
  1756. }
  1757. }
  1758. } else {
  1759. var x = [];
  1760. var t = a & 7;
  1761. x.length = (a >> 3) + 1;
  1762. b.nextBytes(x);
  1763. if (t > 0) {
  1764. x[0] &= (1 << t) - 1;
  1765. } else {
  1766. x[0] = 0;
  1767. }
  1768. this.fromString(x, 256);
  1769. }
  1770. };
  1771. BigInteger2.prototype.bitwiseTo = function (a, op, r) {
  1772. var i;
  1773. var f;
  1774. var m = Math.min(a.t, this.t);
  1775. for (i = 0; i < m; ++i) {
  1776. r[i] = op(this[i], a[i]);
  1777. }
  1778. if (a.t < this.t) {
  1779. f = a.s & this.DM;
  1780. for (i = m; i < this.t; ++i) {
  1781. r[i] = op(this[i], f);
  1782. }
  1783. r.t = this.t;
  1784. } else {
  1785. f = this.s & this.DM;
  1786. for (i = m; i < a.t; ++i) {
  1787. r[i] = op(f, a[i]);
  1788. }
  1789. r.t = a.t;
  1790. }
  1791. r.s = op(this.s, a.s);
  1792. r.clamp();
  1793. };
  1794. BigInteger2.prototype.changeBit = function (n, op) {
  1795. var r = BigInteger2.ONE.shiftLeft(n);
  1796. this.bitwiseTo(r, op, r);
  1797. return r;
  1798. };
  1799. BigInteger2.prototype.addTo = function (a, r) {
  1800. var i = 0;
  1801. var c = 0;
  1802. var m = Math.min(a.t, this.t);
  1803. while (i < m) {
  1804. c += this[i] + a[i];
  1805. r[i++] = c & this.DM;
  1806. c >>= this.DB;
  1807. }
  1808. if (a.t < this.t) {
  1809. c += a.s;
  1810. while (i < this.t) {
  1811. c += this[i];
  1812. r[i++] = c & this.DM;
  1813. c >>= this.DB;
  1814. }
  1815. c += this.s;
  1816. } else {
  1817. c += this.s;
  1818. while (i < a.t) {
  1819. c += a[i];
  1820. r[i++] = c & this.DM;
  1821. c >>= this.DB;
  1822. }
  1823. c += a.s;
  1824. }
  1825. r.s = c < 0 ? -1 : 0;
  1826. if (c > 0) {
  1827. r[i++] = c;
  1828. } else if (c < -1) {
  1829. r[i++] = this.DV + c;
  1830. }
  1831. r.t = i;
  1832. r.clamp();
  1833. };
  1834. BigInteger2.prototype.dMultiply = function (n) {
  1835. this[this.t] = this.am(0, n - 1, this, 0, 0, this.t);
  1836. ++this.t;
  1837. this.clamp();
  1838. };
  1839. BigInteger2.prototype.dAddOffset = function (n, w) {
  1840. if (n == 0) {
  1841. return;
  1842. }
  1843. while (this.t <= w) {
  1844. this[this.t++] = 0;
  1845. }
  1846. this[w] += n;
  1847. while (this[w] >= this.DV) {
  1848. this[w] -= this.DV;
  1849. if (++w >= this.t) {
  1850. this[this.t++] = 0;
  1851. }
  1852. ++this[w];
  1853. }
  1854. };
  1855. BigInteger2.prototype.multiplyLowerTo = function (a, n, r) {
  1856. var i = Math.min(this.t + a.t, n);
  1857. r.s = 0;
  1858. r.t = i;
  1859. while (i > 0) {
  1860. r[--i] = 0;
  1861. }
  1862. for (var j = r.t - this.t; i < j; ++i) {
  1863. r[i + this.t] = this.am(0, a[i], r, i, 0, this.t);
  1864. }
  1865. for (var j = Math.min(a.t, n); i < j; ++i) {
  1866. this.am(0, a[i], r, i, 0, n - i);
  1867. }
  1868. r.clamp();
  1869. };
  1870. BigInteger2.prototype.multiplyUpperTo = function (a, n, r) {
  1871. --n;
  1872. var i = r.t = this.t + a.t - n;
  1873. r.s = 0;
  1874. while (--i >= 0) {
  1875. r[i] = 0;
  1876. }
  1877. for (i = Math.max(n - this.t, 0); i < a.t; ++i) {
  1878. r[this.t + i - n] = this.am(n - i, a[i], r, 0, 0, this.t + i - n);
  1879. }
  1880. r.clamp();
  1881. r.drShiftTo(1, r);
  1882. };
  1883. BigInteger2.prototype.modInt = function (n) {
  1884. if (n <= 0) {
  1885. return 0;
  1886. }
  1887. var d = this.DV % n;
  1888. var r = this.s < 0 ? n - 1 : 0;
  1889. if (this.t > 0) {
  1890. if (d == 0) {
  1891. r = this[0] % n;
  1892. } else {
  1893. for (var i = this.t - 1; i >= 0; --i) {
  1894. r = (d * r + this[i]) % n;
  1895. }
  1896. }
  1897. }
  1898. return r;
  1899. };
  1900. BigInteger2.prototype.millerRabin = function (t) {
  1901. var n1 = this.subtract(BigInteger2.ONE);
  1902. var k = n1.getLowestSetBit();
  1903. if (k <= 0) {
  1904. return false;
  1905. }
  1906. var r = n1.shiftRight(k);
  1907. t = t + 1 >> 1;
  1908. if (t > lowprimes.length) {
  1909. t = lowprimes.length;
  1910. }
  1911. var a = nbi();
  1912. for (var i = 0; i < t; ++i) {
  1913. a.fromInt(lowprimes[Math.floor(Math.random() * lowprimes.length)]);
  1914. var y = a.modPow(r, this);
  1915. if (y.compareTo(BigInteger2.ONE) != 0 && y.compareTo(n1) != 0) {
  1916. var j = 1;
  1917. while (j++ < k && y.compareTo(n1) != 0) {
  1918. y = y.modPowInt(2, this);
  1919. if (y.compareTo(BigInteger2.ONE) == 0) {
  1920. return false;
  1921. }
  1922. }
  1923. if (y.compareTo(n1) != 0) {
  1924. return false;
  1925. }
  1926. }
  1927. }
  1928. return true;
  1929. };
  1930. BigInteger2.prototype.square = function () {
  1931. var r = nbi();
  1932. this.squareTo(r);
  1933. return r;
  1934. };
  1935. BigInteger2.prototype.gcda = function (a, callback) {
  1936. var x = this.s < 0 ? this.negate() : this.clone();
  1937. var y = a.s < 0 ? a.negate() : a.clone();
  1938. if (x.compareTo(y) < 0) {
  1939. var t = x;
  1940. x = y;
  1941. y = t;
  1942. }
  1943. var i = x.getLowestSetBit();
  1944. var g = y.getLowestSetBit();
  1945. if (g < 0) {
  1946. callback(x);
  1947. return;
  1948. }
  1949. if (i < g) {
  1950. g = i;
  1951. }
  1952. if (g > 0) {
  1953. x.rShiftTo(g, x);
  1954. y.rShiftTo(g, y);
  1955. }
  1956. var gcda1 = function () {
  1957. if ((i = x.getLowestSetBit()) > 0) {
  1958. x.rShiftTo(i, x);
  1959. }
  1960. if ((i = y.getLowestSetBit()) > 0) {
  1961. y.rShiftTo(i, y);
  1962. }
  1963. if (x.compareTo(y) >= 0) {
  1964. x.subTo(y, x);
  1965. x.rShiftTo(1, x);
  1966. } else {
  1967. y.subTo(x, y);
  1968. y.rShiftTo(1, y);
  1969. }
  1970. if (!(x.signum() > 0)) {
  1971. if (g > 0) {
  1972. y.lShiftTo(g, y);
  1973. }
  1974. setTimeout(function () {
  1975. callback(y);
  1976. }, 0);
  1977. } else {
  1978. setTimeout(gcda1, 0);
  1979. }
  1980. };
  1981. setTimeout(gcda1, 10);
  1982. };
  1983. BigInteger2.prototype.fromNumberAsync = function (a, b, c, callback) {
  1984. if ("number" == typeof b) {
  1985. if (a < 2) {
  1986. this.fromInt(1);
  1987. } else {
  1988. this.fromNumber(a, c);
  1989. if (!this.testBit(a - 1)) {
  1990. this.bitwiseTo(BigInteger2.ONE.shiftLeft(a - 1), op_or, this);
  1991. }
  1992. if (this.isEven()) {
  1993. this.dAddOffset(1, 0);
  1994. }
  1995. var bnp_1 = this;
  1996. var bnpfn1_1 = function () {
  1997. bnp_1.dAddOffset(2, 0);
  1998. if (bnp_1.bitLength() > a) {
  1999. bnp_1.subTo(BigInteger2.ONE.shiftLeft(a - 1), bnp_1);
  2000. }
  2001. if (bnp_1.isProbablePrime(b)) {
  2002. setTimeout(function () {
  2003. callback();
  2004. }, 0);
  2005. } else {
  2006. setTimeout(bnpfn1_1, 0);
  2007. }
  2008. };
  2009. setTimeout(bnpfn1_1, 0);
  2010. }
  2011. } else {
  2012. var x = [];
  2013. var t = a & 7;
  2014. x.length = (a >> 3) + 1;
  2015. b.nextBytes(x);
  2016. if (t > 0) {
  2017. x[0] &= (1 << t) - 1;
  2018. } else {
  2019. x[0] = 0;
  2020. }
  2021. this.fromString(x, 256);
  2022. }
  2023. };
  2024. return BigInteger2;
  2025. }();
  2026. var NullExp =
  2027. /** @class */ function () {
  2028. function NullExp2() { }
  2029. NullExp2.prototype.convert = function (x) {
  2030. return x;
  2031. };
  2032. NullExp2.prototype.revert = function (x) {
  2033. return x;
  2034. };
  2035. NullExp2.prototype.mulTo = function (x, y, r) {
  2036. x.multiplyTo(y, r);
  2037. };
  2038. NullExp2.prototype.sqrTo = function (x, r) {
  2039. x.squareTo(r);
  2040. };
  2041. return NullExp2;
  2042. }();
  2043. var Classic =
  2044. /** @class */ function () {
  2045. function Classic2(m) {
  2046. this.m = m;
  2047. }
  2048. Classic2.prototype.convert = function (x) {
  2049. if (x.s < 0 || x.compareTo(this.m) >= 0) {
  2050. return x.mod(this.m);
  2051. } else {
  2052. return x;
  2053. }
  2054. };
  2055. Classic2.prototype.revert = function (x) {
  2056. return x;
  2057. };
  2058. Classic2.prototype.reduce = function (x) {
  2059. x.divRemTo(this.m, null, x);
  2060. };
  2061. Classic2.prototype.mulTo = function (x, y, r) {
  2062. x.multiplyTo(y, r);
  2063. this.reduce(r);
  2064. };
  2065. Classic2.prototype.sqrTo = function (x, r) {
  2066. x.squareTo(r);
  2067. this.reduce(r);
  2068. };
  2069. return Classic2;
  2070. }();
  2071. var Montgomery =
  2072. /** @class */ function () {
  2073. function Montgomery2(m) {
  2074. this.m = m;
  2075. this.mp = m.invDigit();
  2076. this.mpl = this.mp & 32767;
  2077. this.mph = this.mp >> 15;
  2078. this.um = (1 << m.DB - 15) - 1;
  2079. this.mt2 = 2 * m.t;
  2080. }
  2081. Montgomery2.prototype.convert = function (x) {
  2082. var r = nbi();
  2083. x.abs().dlShiftTo(this.m.t, r);
  2084. r.divRemTo(this.m, null, r);
  2085. if (x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) {
  2086. this.m.subTo(r, r);
  2087. }
  2088. return r;
  2089. };
  2090. Montgomery2.prototype.revert = function (x) {
  2091. var r = nbi();
  2092. x.copyTo(r);
  2093. this.reduce(r);
  2094. return r;
  2095. };
  2096. Montgomery2.prototype.reduce = function (x) {
  2097. while (x.t <= this.mt2) {
  2098. x[x.t++] = 0;
  2099. }
  2100. for (var i = 0; i < this.m.t; ++i) {
  2101. var j = x[i] & 32767;
  2102. var u0 = j * this.mpl + ((j * this.mph + (x[i] >> 15) * this.mpl & this.um) << 15) & x.DM;
  2103. j = i + this.m.t;
  2104. x[j] += this.m.am(0, u0, x, i, 0, this.m.t);
  2105. while (x[j] >= x.DV) {
  2106. x[j] -= x.DV;
  2107. x[++j]++;
  2108. }
  2109. }
  2110. x.clamp();
  2111. x.drShiftTo(this.m.t, x);
  2112. if (x.compareTo(this.m) >= 0) {
  2113. x.subTo(this.m, x);
  2114. }
  2115. };
  2116. Montgomery2.prototype.mulTo = function (x, y, r) {
  2117. x.multiplyTo(y, r);
  2118. this.reduce(r);
  2119. };
  2120. Montgomery2.prototype.sqrTo = function (x, r) {
  2121. x.squareTo(r);
  2122. this.reduce(r);
  2123. };
  2124. return Montgomery2;
  2125. }();
  2126. var Barrett =
  2127. /** @class */ function () {
  2128. function Barrett2(m) {
  2129. this.m = m;
  2130. this.r2 = nbi();
  2131. this.q3 = nbi();
  2132. BigInteger.ONE.dlShiftTo(2 * m.t, this.r2);
  2133. this.mu = this.r2.divide(m);
  2134. }
  2135. Barrett2.prototype.convert = function (x) {
  2136. if (x.s < 0 || x.t > 2 * this.m.t) {
  2137. return x.mod(this.m);
  2138. } else if (x.compareTo(this.m) < 0) {
  2139. return x;
  2140. } else {
  2141. var r = nbi();
  2142. x.copyTo(r);
  2143. this.reduce(r);
  2144. return r;
  2145. }
  2146. };
  2147. Barrett2.prototype.revert = function (x) {
  2148. return x;
  2149. };
  2150. Barrett2.prototype.reduce = function (x) {
  2151. x.drShiftTo(this.m.t - 1, this.r2);
  2152. if (x.t > this.m.t + 1) {
  2153. x.t = this.m.t + 1;
  2154. x.clamp();
  2155. }
  2156. this.mu.multiplyUpperTo(this.r2, this.m.t + 1, this.q3);
  2157. this.m.multiplyLowerTo(this.q3, this.m.t + 1, this.r2);
  2158. while (x.compareTo(this.r2) < 0) {
  2159. x.dAddOffset(1, this.m.t + 1);
  2160. }
  2161. x.subTo(this.r2, x);
  2162. while (x.compareTo(this.m) >= 0) {
  2163. x.subTo(this.m, x);
  2164. }
  2165. };
  2166. Barrett2.prototype.mulTo = function (x, y, r) {
  2167. x.multiplyTo(y, r);
  2168. this.reduce(r);
  2169. };
  2170. Barrett2.prototype.sqrTo = function (x, r) {
  2171. x.squareTo(r);
  2172. this.reduce(r);
  2173. };
  2174. return Barrett2;
  2175. }();
  2176. function nbi() {
  2177. return new BigInteger(null);
  2178. }
  2179. function parseBigInt(str, r) {
  2180. return new BigInteger(str, r);
  2181. }
  2182. var inBrowser = typeof navigator !== "undefined";
  2183. if (inBrowser && j_lm && navigator.appName == "Microsoft Internet Explorer") {
  2184. BigInteger.prototype.am = function am2(i, x, w, j, c, n) {
  2185. var xl = x & 32767;
  2186. var xh = x >> 15;
  2187. while (--n >= 0) {
  2188. var l = this[i] & 32767;
  2189. var h = this[i++] >> 15;
  2190. var m = xh * l + h * xl;
  2191. l = xl * l + ((m & 32767) << 15) + w[j] + (c & 1073741823);
  2192. c = (l >>> 30) + (m >>> 15) + xh * h + (c >>> 30);
  2193. w[j++] = l & 1073741823;
  2194. }
  2195. return c;
  2196. };
  2197. dbits = 30;
  2198. } else if (inBrowser && j_lm && navigator.appName != "Netscape") {
  2199. BigInteger.prototype.am = function am1(i, x, w, j, c, n) {
  2200. while (--n >= 0) {
  2201. var v = x * this[i++] + w[j] + c;
  2202. c = Math.floor(v / 67108864);
  2203. w[j++] = v & 67108863;
  2204. }
  2205. return c;
  2206. };
  2207. dbits = 26;
  2208. } else {
  2209. BigInteger.prototype.am = function am3(i, x, w, j, c, n) {
  2210. var xl = x & 16383;
  2211. var xh = x >> 14;
  2212. while (--n >= 0) {
  2213. var l = this[i] & 16383;
  2214. var h = this[i++] >> 14;
  2215. var m = xh * l + h * xl;
  2216. l = xl * l + ((m & 16383) << 14) + w[j] + c;
  2217. c = (l >> 28) + (m >> 14) + xh * h;
  2218. w[j++] = l & 268435455;
  2219. }
  2220. return c;
  2221. };
  2222. dbits = 28;
  2223. }
  2224. BigInteger.prototype.DB = dbits;
  2225. BigInteger.prototype.DM = (1 << dbits) - 1;
  2226. BigInteger.prototype.DV = 1 << dbits;
  2227. var BI_FP = 52;
  2228. BigInteger.prototype.FV = Math.pow(2, BI_FP);
  2229. BigInteger.prototype.F1 = BI_FP - dbits;
  2230. BigInteger.prototype.F2 = 2 * dbits - BI_FP;
  2231. var BI_RC = [];
  2232. var rr;
  2233. var vv;
  2234. rr = "0".charCodeAt(0);
  2235. for (vv = 0; vv <= 9; ++vv) {
  2236. BI_RC[rr++] = vv;
  2237. }
  2238. rr = "a".charCodeAt(0);
  2239. for (vv = 10; vv < 36; ++vv) {
  2240. BI_RC[rr++] = vv;
  2241. }
  2242. rr = "A".charCodeAt(0);
  2243. for (vv = 10; vv < 36; ++vv) {
  2244. BI_RC[rr++] = vv;
  2245. }
  2246. function intAt(s, i) {
  2247. var c = BI_RC[s.charCodeAt(i)];
  2248. return c == null ? -1 : c;
  2249. }
  2250. function nbv(i) {
  2251. var r = nbi();
  2252. r.fromInt(i);
  2253. return r;
  2254. }
  2255. function nbits(x) {
  2256. var r = 1;
  2257. var t;
  2258. if ((t = x >>> 16) != 0) {
  2259. x = t;
  2260. r += 16;
  2261. }
  2262. if ((t = x >> 8) != 0) {
  2263. x = t;
  2264. r += 8;
  2265. }
  2266. if ((t = x >> 4) != 0) {
  2267. x = t;
  2268. r += 4;
  2269. }
  2270. if ((t = x >> 2) != 0) {
  2271. x = t;
  2272. r += 2;
  2273. }
  2274. if ((t = x >> 1) != 0) {
  2275. x = t;
  2276. r += 1;
  2277. }
  2278. return r;
  2279. }
  2280. BigInteger.ZERO = nbv(0);
  2281. BigInteger.ONE = nbv(1);
  2282. // lib/lib/jsbn/prng4.js
  2283. var Arcfour =
  2284. /** @class */ function () {
  2285. function Arcfour2() {
  2286. this.i = 0;
  2287. this.j = 0;
  2288. this.S = [];
  2289. }
  2290. Arcfour2.prototype.init = function (key) {
  2291. var i;
  2292. var j;
  2293. var t;
  2294. for (i = 0; i < 256; ++i) {
  2295. this.S[i] = i;
  2296. }
  2297. j = 0;
  2298. for (i = 0; i < 256; ++i) {
  2299. j = j + this.S[i] + key[i % key.length] & 255;
  2300. t = this.S[i];
  2301. this.S[i] = this.S[j];
  2302. this.S[j] = t;
  2303. }
  2304. this.i = 0;
  2305. this.j = 0;
  2306. };
  2307. Arcfour2.prototype.next = function () {
  2308. var t;
  2309. this.i = this.i + 1 & 255;
  2310. this.j = this.j + this.S[this.i] & 255;
  2311. t = this.S[this.i];
  2312. this.S[this.i] = this.S[this.j];
  2313. this.S[this.j] = t;
  2314. return this.S[t + this.S[this.i] & 255];
  2315. };
  2316. return Arcfour2;
  2317. }();
  2318. function prng_newstate() {
  2319. return new Arcfour();
  2320. }
  2321. var rng_psize = 256;
  2322. // lib/lib/jsbn/rng.js
  2323. var rng_state;
  2324. var rng_pool = null;
  2325. var rng_pptr;
  2326. if (rng_pool == null) {
  2327. rng_pool = [];
  2328. rng_pptr = 0;
  2329. t = void 0;
  2330. if (typeof window !== "undefined" && window.crypto && window.crypto.getRandomValues) {
  2331. z = new Uint32Array(256);
  2332. window.crypto.getRandomValues(z);
  2333. for (t = 0; t < z.length; ++t) {
  2334. rng_pool[rng_pptr++] = z[t] & 255;
  2335. }
  2336. }
  2337. count = 0;
  2338. onMouseMoveListener_1 = function (ev) {
  2339. count = count || 0;
  2340. if (count >= 256 || rng_pptr >= rng_psize) {
  2341. if (window.removeEventListener) {
  2342. window.removeEventListener("mousemove", onMouseMoveListener_1, false);
  2343. } else if (window.detachEvent) {
  2344. window.detachEvent("onmousemove", onMouseMoveListener_1);
  2345. }
  2346. return;
  2347. }
  2348. try {
  2349. var mouseCoordinates = ev.x + ev.y;
  2350. rng_pool[rng_pptr++] = mouseCoordinates & 255;
  2351. count += 1;
  2352. } catch (e) { }
  2353. };
  2354. if (typeof window !== "undefined") {
  2355. if (window.addEventListener) {
  2356. window.addEventListener("mousemove", onMouseMoveListener_1, false);
  2357. } else if (window.attachEvent) {
  2358. window.attachEvent("onmousemove", onMouseMoveListener_1);
  2359. }
  2360. }
  2361. }
  2362. var t;
  2363. var z;
  2364. var count;
  2365. var onMouseMoveListener_1;
  2366. function rng_get_byte() {
  2367. if (rng_state == null) {
  2368. rng_state = prng_newstate();
  2369. while (rng_pptr < rng_psize) {
  2370. var random = Math.floor(65536 * Math.random());
  2371. rng_pool[rng_pptr++] = random & 255;
  2372. }
  2373. rng_state.init(rng_pool);
  2374. for (rng_pptr = 0; rng_pptr < rng_pool.length; ++rng_pptr) {
  2375. rng_pool[rng_pptr] = 0;
  2376. }
  2377. rng_pptr = 0;
  2378. }
  2379. return rng_state.next();
  2380. }
  2381. var SecureRandom =
  2382. /** @class */ function () {
  2383. function SecureRandom2() { }
  2384. SecureRandom2.prototype.nextBytes = function (ba) {
  2385. for (var i = 0; i < ba.length; ++i) {
  2386. ba[i] = rng_get_byte();
  2387. }
  2388. };
  2389. return SecureRandom2;
  2390. }();
  2391. // lib/lib/jsbn/rsa.js
  2392. function pkcs1pad1(s, n) {
  2393. if (n < s.length + 22) {
  2394. console.error("Message too long for RSA");
  2395. return null;
  2396. }
  2397. var len = n - s.length - 6;
  2398. var filler = "";
  2399. for (var f = 0; f < len; f += 2) {
  2400. filler += "ff";
  2401. }
  2402. var m = "0001" + filler + "00" + s;
  2403. return parseBigInt(m, 16);
  2404. }
  2405. function pkcs1pad2(s, n) {
  2406. if (n < s.length + 11) {
  2407. console.error("Message too long for RSA");
  2408. return null;
  2409. }
  2410. var ba = [];
  2411. var i = s.length - 1;
  2412. while (i >= 0 && n > 0) {
  2413. var c = s.charCodeAt(i--);
  2414. if (c < 128) {
  2415. ba[--n] = c;
  2416. } else if (c > 127 && c < 2048) {
  2417. ba[--n] = c & 63 | 128;
  2418. ba[--n] = c >> 6 | 192;
  2419. } else {
  2420. ba[--n] = c & 63 | 128;
  2421. ba[--n] = c >> 6 & 63 | 128;
  2422. ba[--n] = c >> 12 | 224;
  2423. }
  2424. }
  2425. ba[--n] = 0;
  2426. var rng = new SecureRandom();
  2427. var x = [];
  2428. while (n > 2) {
  2429. x[0] = 0;
  2430. while (x[0] == 0) {
  2431. rng.nextBytes(x);
  2432. }
  2433. ba[--n] = x[0];
  2434. }
  2435. ba[--n] = 2;
  2436. ba[--n] = 0;
  2437. return new BigInteger(ba);
  2438. }
  2439. var RSAKey =
  2440. /** @class */ function () {
  2441. function RSAKey2() {
  2442. this.n = null;
  2443. this.e = 0;
  2444. this.d = null;
  2445. this.p = null;
  2446. this.q = null;
  2447. this.dmp1 = null;
  2448. this.dmq1 = null;
  2449. this.coeff = null;
  2450. }
  2451. RSAKey2.prototype.doPublic = function (x) {
  2452. return x.modPowInt(this.e, this.n);
  2453. };
  2454. RSAKey2.prototype.doPrivate = function (x) {
  2455. if (this.p == null || this.q == null) {
  2456. return x.modPow(this.d, this.n);
  2457. }
  2458. var xp = x.mod(this.p).modPow(this.dmp1, this.p);
  2459. var xq = x.mod(this.q).modPow(this.dmq1, this.q);
  2460. while (xp.compareTo(xq) < 0) {
  2461. xp = xp.add(this.p);
  2462. }
  2463. return xp.subtract(xq).multiply(this.coeff).mod(this.p).multiply(this.q).add(xq);
  2464. };
  2465. RSAKey2.prototype.setPublic = function (N, E) {
  2466. if (N != null && E != null && N.length > 0 && E.length > 0) {
  2467. this.n = parseBigInt(N, 16);
  2468. this.e = parseInt(E, 16);
  2469. } else {
  2470. console.error("Invalid RSA public key");
  2471. }
  2472. };
  2473. RSAKey2.prototype.encrypt = function (text) {
  2474. var maxLength = this.n.bitLength() + 7 >> 3;
  2475. var m = pkcs1pad2(text, maxLength);
  2476. if (m == null) {
  2477. return null;
  2478. }
  2479. var c = this.doPublic(m);
  2480. if (c == null) {
  2481. return null;
  2482. }
  2483. var h = c.toString(16);
  2484. var length = h.length;
  2485. for (var i = 0; i < maxLength * 2 - length; i++) {
  2486. h = "0" + h;
  2487. }
  2488. return h;
  2489. };
  2490. RSAKey2.prototype.setPrivate = function (N, E, D) {
  2491. if (N != null && E != null && N.length > 0 && E.length > 0) {
  2492. this.n = parseBigInt(N, 16);
  2493. this.e = parseInt(E, 16);
  2494. this.d = parseBigInt(D, 16);
  2495. } else {
  2496. console.error("Invalid RSA private key");
  2497. }
  2498. };
  2499. RSAKey2.prototype.setPrivateEx = function (N, E, D, P, Q, DP, DQ, C) {
  2500. if (N != null && E != null && N.length > 0 && E.length > 0) {
  2501. this.n = parseBigInt(N, 16);
  2502. this.e = parseInt(E, 16);
  2503. this.d = parseBigInt(D, 16);
  2504. this.p = parseBigInt(P, 16);
  2505. this.q = parseBigInt(Q, 16);
  2506. this.dmp1 = parseBigInt(DP, 16);
  2507. this.dmq1 = parseBigInt(DQ, 16);
  2508. this.coeff = parseBigInt(C, 16);
  2509. } else {
  2510. console.error("Invalid RSA private key");
  2511. }
  2512. };
  2513. RSAKey2.prototype.generate = function (B, E) {
  2514. var rng = new SecureRandom();
  2515. var qs = B >> 1;
  2516. this.e = parseInt(E, 16);
  2517. var ee = new BigInteger(E, 16);
  2518. for (; ;) {
  2519. for (; ;) {
  2520. this.p = new BigInteger(B - qs, 1, rng);
  2521. if (this.p.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) == 0 && this.p.isProbablePrime(10)) {
  2522. break;
  2523. }
  2524. }
  2525. for (; ;) {
  2526. this.q = new BigInteger(qs, 1, rng);
  2527. if (this.q.subtract(BigInteger.ONE).gcd(ee).compareTo(BigInteger.ONE) == 0 && this.q.isProbablePrime(10)) {
  2528. break;
  2529. }
  2530. }
  2531. if (this.p.compareTo(this.q) <= 0) {
  2532. var t = this.p;
  2533. this.p = this.q;
  2534. this.q = t;
  2535. }
  2536. var p1 = this.p.subtract(BigInteger.ONE);
  2537. var q1 = this.q.subtract(BigInteger.ONE);
  2538. var phi = p1.multiply(q1);
  2539. if (phi.gcd(ee).compareTo(BigInteger.ONE) == 0) {
  2540. this.n = this.p.multiply(this.q);
  2541. this.d = ee.modInverse(phi);
  2542. this.dmp1 = this.d.mod(p1);
  2543. this.dmq1 = this.d.mod(q1);
  2544. this.coeff = this.q.modInverse(this.p);
  2545. break;
  2546. }
  2547. }
  2548. };
  2549. RSAKey2.prototype.decrypt = function (ctext) {
  2550. var c = parseBigInt(ctext, 16);
  2551. var m = this.doPrivate(c);
  2552. if (m == null) {
  2553. return null;
  2554. }
  2555. return pkcs1unpad2(m, this.n.bitLength() + 7 >> 3);
  2556. };
  2557. RSAKey2.prototype.generateAsync = function (B, E, callback) {
  2558. var rng = new SecureRandom();
  2559. var qs = B >> 1;
  2560. this.e = parseInt(E, 16);
  2561. var ee = new BigInteger(E, 16);
  2562. var rsa = this;
  2563. var loop1 = function () {
  2564. var loop4 = function () {
  2565. if (rsa.p.compareTo(rsa.q) <= 0) {
  2566. var t = rsa.p;
  2567. rsa.p = rsa.q;
  2568. rsa.q = t;
  2569. }
  2570. var p1 = rsa.p.subtract(BigInteger.ONE);
  2571. var q1 = rsa.q.subtract(BigInteger.ONE);
  2572. var phi = p1.multiply(q1);
  2573. if (phi.gcd(ee).compareTo(BigInteger.ONE) == 0) {
  2574. rsa.n = rsa.p.multiply(rsa.q);
  2575. rsa.d = ee.modInverse(phi);
  2576. rsa.dmp1 = rsa.d.mod(p1);
  2577. rsa.dmq1 = rsa.d.mod(q1);
  2578. rsa.coeff = rsa.q.modInverse(rsa.p);
  2579. setTimeout(function () {
  2580. callback();
  2581. }, 0);
  2582. } else {
  2583. setTimeout(loop1, 0);
  2584. }
  2585. };
  2586. var loop3 = function () {
  2587. rsa.q = nbi();
  2588. rsa.q.fromNumberAsync(qs, 1, rng, function () {
  2589. rsa.q.subtract(BigInteger.ONE).gcda(ee, function (r) {
  2590. if (r.compareTo(BigInteger.ONE) == 0 && rsa.q.isProbablePrime(10)) {
  2591. setTimeout(loop4, 0);
  2592. } else {
  2593. setTimeout(loop3, 0);
  2594. }
  2595. });
  2596. });
  2597. };
  2598. var loop2 = function () {
  2599. rsa.p = nbi();
  2600. rsa.p.fromNumberAsync(B - qs, 1, rng, function () {
  2601. rsa.p.subtract(BigInteger.ONE).gcda(ee, function (r) {
  2602. if (r.compareTo(BigInteger.ONE) == 0 && rsa.p.isProbablePrime(10)) {
  2603. setTimeout(loop3, 0);
  2604. } else {
  2605. setTimeout(loop2, 0);
  2606. }
  2607. });
  2608. });
  2609. };
  2610. setTimeout(loop2, 0);
  2611. };
  2612. setTimeout(loop1, 0);
  2613. };
  2614. RSAKey2.prototype.sign = function (text, digestMethod, digestName) {
  2615. var header = getDigestHeader(digestName);
  2616. var digest = header + digestMethod(text).toString();
  2617. var m = pkcs1pad1(digest, this.n.bitLength() / 4);
  2618. if (m == null) {
  2619. return null;
  2620. }
  2621. var c = this.doPrivate(m);
  2622. if (c == null) {
  2623. return null;
  2624. }
  2625. var h = c.toString(16);
  2626. if ((h.length & 1) == 0) {
  2627. return h;
  2628. } else {
  2629. return "0" + h;
  2630. }
  2631. };
  2632. RSAKey2.prototype.verify = function (text, signature, digestMethod) {
  2633. var c = parseBigInt(signature, 16);
  2634. var m = this.doPublic(c);
  2635. if (m == null) {
  2636. return null;
  2637. }
  2638. var unpadded = m.toString(16).replace(/^1f+00/, "");
  2639. var digest = removeDigestHeader(unpadded);
  2640. return digest == digestMethod(text).toString();
  2641. };
  2642. return RSAKey2;
  2643. }();
  2644. function pkcs1unpad2(d, n) {
  2645. var b = d.toByteArray();
  2646. var i = 0;
  2647. while (i < b.length && b[i] == 0) {
  2648. ++i;
  2649. }
  2650. if (b.length - i != n - 1 || b[i] != 2) {
  2651. return null;
  2652. }
  2653. ++i;
  2654. while (b[i] != 0) {
  2655. if (++i >= b.length) {
  2656. return null;
  2657. }
  2658. }
  2659. var ret = "";
  2660. while (++i < b.length) {
  2661. var c = b[i] & 255;
  2662. if (c < 128) {
  2663. ret += String.fromCharCode(c);
  2664. } else if (c > 191 && c < 224) {
  2665. ret += String.fromCharCode((c & 31) << 6 | b[i + 1] & 63);
  2666. ++i;
  2667. } else {
  2668. ret += String.fromCharCode((c & 15) << 12 | (b[i + 1] & 63) << 6 | b[i + 2] & 63);
  2669. i += 2;
  2670. }
  2671. }
  2672. return ret;
  2673. }
  2674. var DIGEST_HEADERS = {
  2675. md2: "3020300c06082a864886f70d020205000410",
  2676. md5: "3020300c06082a864886f70d020505000410",
  2677. sha1: "3021300906052b0e03021a05000414",
  2678. sha224: "302d300d06096086480165030402040500041c",
  2679. sha256: "3031300d060960864801650304020105000420",
  2680. sha384: "3041300d060960864801650304020205000430",
  2681. sha512: "3051300d060960864801650304020305000440",
  2682. ripemd160: "3021300906052b2403020105000414"
  2683. };
  2684. function getDigestHeader(name) {
  2685. return DIGEST_HEADERS[name] || "";
  2686. }
  2687. function removeDigestHeader(str) {
  2688. for (var name_1 in DIGEST_HEADERS) {
  2689. if (DIGEST_HEADERS.hasOwnProperty(name_1)) {
  2690. var header = DIGEST_HEADERS[name_1];
  2691. var len = header.length;
  2692. if (str.substr(0, len) == header) {
  2693. return str.substr(len);
  2694. }
  2695. }
  2696. }
  2697. return str;
  2698. }
  2699. // lib/lib/jsrsasign/yahoo.js
  2700. var YAHOO = {};
  2701. YAHOO.lang = {
  2702. /**
  2703. * Utility to set up the prototype, constructor and superclass properties to
  2704. * support an inheritance strategy that can chain constructors and methods.
  2705. * Static members will not be inherited.
  2706. *
  2707. * @method extend
  2708. * @static
  2709. * @param {Function} subc the object to modify
  2710. * @param {Function} superc the object to inherit
  2711. * @param {Object} overrides additional properties/methods to add to the
  2712. * subclass prototype. These will override the
  2713. * matching items obtained from the superclass
  2714. * if present.
  2715. */
  2716. extend: function (subc, superc, overrides) {
  2717. if (!superc || !subc) {
  2718. throw new Error("YAHOO.lang.extend failed, please check that all dependencies are included.");
  2719. }
  2720. var F = function () { };
  2721. F.prototype = superc.prototype;
  2722. subc.prototype = new F();
  2723. subc.prototype.constructor = subc;
  2724. subc.superclass = superc.prototype;
  2725. if (superc.prototype.constructor == Object.prototype.constructor) {
  2726. superc.prototype.constructor = superc;
  2727. }
  2728. if (overrides) {
  2729. var i;
  2730. for (i in overrides) {
  2731. subc.prototype[i] = overrides[i];
  2732. }
  2733. var _IEEnumFix = function () { }, ADD = ["toString", "valueOf"];
  2734. try {
  2735. if (/MSIE/.test(navigator.userAgent)) {
  2736. _IEEnumFix = function (r, s) {
  2737. for (i = 0; i < ADD.length; i = i + 1) {
  2738. var fname = ADD[i], f = s[fname];
  2739. if (typeof f === "function" && f != Object.prototype[fname]) {
  2740. r[fname] = f;
  2741. }
  2742. }
  2743. };
  2744. }
  2745. } catch (ex) { }
  2746. _IEEnumFix(subc.prototype, overrides);
  2747. }
  2748. }
  2749. };
  2750. // lib/lib/jsrsasign/asn1-1.0.js
  2751. var KJUR = {};
  2752. if (typeof KJUR.asn1 == "undefined" || !KJUR.asn1) KJUR.asn1 = {};
  2753. KJUR.asn1.ASN1Util = new function () {
  2754. this.integerToByteHex = function (i) {
  2755. var h = i.toString(16);
  2756. if (h.length % 2 == 1) h = "0" + h;
  2757. return h;
  2758. };
  2759. this.bigIntToMinTwosComplementsHex = function (bigIntegerValue) {
  2760. var h = bigIntegerValue.toString(16);
  2761. if (h.substr(0, 1) != "-") {
  2762. if (h.length % 2 == 1) {
  2763. h = "0" + h;
  2764. } else {
  2765. if (!h.match(/^[0-7]/)) {
  2766. h = "00" + h;
  2767. }
  2768. }
  2769. } else {
  2770. var hPos = h.substr(1);
  2771. var xorLen = hPos.length;
  2772. if (xorLen % 2 == 1) {
  2773. xorLen += 1;
  2774. } else {
  2775. if (!h.match(/^[0-7]/)) {
  2776. xorLen += 2;
  2777. }
  2778. }
  2779. var hMask = "";
  2780. for (var i = 0; i < xorLen; i++) {
  2781. hMask += "f";
  2782. }
  2783. var biMask = new BigInteger(hMask, 16);
  2784. var biNeg = biMask.xor(bigIntegerValue).add(BigInteger.ONE);
  2785. h = biNeg.toString(16).replace(/^-/, "");
  2786. }
  2787. return h;
  2788. };
  2789. this.getPEMStringFromHex = function (dataHex, pemHeader) {
  2790. return hextopem(dataHex, pemHeader);
  2791. };
  2792. this.newObject = function (param) {
  2793. var _KJUR = KJUR, _KJUR_asn1 = _KJUR.asn1, _DERBoolean = _KJUR_asn1.DERBoolean, _DERInteger = _KJUR_asn1.DERInteger, _DERBitString = _KJUR_asn1.DERBitString, _DEROctetString = _KJUR_asn1.DEROctetString, _DERNull = _KJUR_asn1.DERNull, _DERObjectIdentifier = _KJUR_asn1.DERObjectIdentifier, _DEREnumerated = _KJUR_asn1.DEREnumerated, _DERUTF8String = _KJUR_asn1.DERUTF8String, _DERNumericString = _KJUR_asn1.DERNumericString, _DERPrintableString = _KJUR_asn1.DERPrintableString, _DERTeletexString = _KJUR_asn1.DERTeletexString, _DERIA5String = _KJUR_asn1.DERIA5String, _DERUTCTime = _KJUR_asn1.DERUTCTime, _DERGeneralizedTime = _KJUR_asn1.DERGeneralizedTime, _DERSequence = _KJUR_asn1.DERSequence, _DERSet = _KJUR_asn1.DERSet, _DERTaggedObject = _KJUR_asn1.DERTaggedObject, _newObject = _KJUR_asn1.ASN1Util.newObject;
  2794. var keys = Object.keys(param);
  2795. if (keys.length != 1) throw "key of param shall be only one.";
  2796. var key = keys[0];
  2797. if (":bool:int:bitstr:octstr:null:oid:enum:utf8str:numstr:prnstr:telstr:ia5str:utctime:gentime:seq:set:tag:".indexOf(":" + key + ":") == -1) throw "undefined key: " + key;
  2798. if (key == "bool") return new _DERBoolean(param[key]);
  2799. if (key == "int") return new _DERInteger(param[key]);
  2800. if (key == "bitstr") return new _DERBitString(param[key]);
  2801. if (key == "octstr") return new _DEROctetString(param[key]);
  2802. if (key == "null") return new _DERNull(param[key]);
  2803. if (key == "oid") return new _DERObjectIdentifier(param[key]);
  2804. if (key == "enum") return new _DEREnumerated(param[key]);
  2805. if (key == "utf8str") return new _DERUTF8String(param[key]);
  2806. if (key == "numstr") return new _DERNumericString(param[key]);
  2807. if (key == "prnstr") return new _DERPrintableString(param[key]);
  2808. if (key == "telstr") return new _DERTeletexString(param[key]);
  2809. if (key == "ia5str") return new _DERIA5String(param[key]);
  2810. if (key == "utctime") return new _DERUTCTime(param[key]);
  2811. if (key == "gentime") return new _DERGeneralizedTime(param[key]);
  2812. if (key == "seq") {
  2813. var paramList = param[key];
  2814. var a = [];
  2815. for (var i = 0; i < paramList.length; i++) {
  2816. var asn1Obj = _newObject(paramList[i]);
  2817. a.push(asn1Obj);
  2818. }
  2819. return new _DERSequence({
  2820. array: a
  2821. });
  2822. }
  2823. if (key == "set") {
  2824. var paramList = param[key];
  2825. var a = [];
  2826. for (var i = 0; i < paramList.length; i++) {
  2827. var asn1Obj = _newObject(paramList[i]);
  2828. a.push(asn1Obj);
  2829. }
  2830. return new _DERSet({
  2831. array: a
  2832. });
  2833. }
  2834. if (key == "tag") {
  2835. var tagParam = param[key];
  2836. if (Object.prototype.toString.call(tagParam) === "[object Array]" && tagParam.length == 3) {
  2837. var obj = _newObject(tagParam[2]);
  2838. return new _DERTaggedObject({
  2839. tag: tagParam[0],
  2840. explicit: tagParam[1],
  2841. obj: obj
  2842. });
  2843. } else {
  2844. var newParam = {};
  2845. if (tagParam.explicit !== void 0) newParam.explicit = tagParam.explicit;
  2846. if (tagParam.tag !== void 0) newParam.tag = tagParam.tag;
  2847. if (tagParam.obj === void 0) throw "obj shall be specified for 'tag'.";
  2848. newParam.obj = _newObject(tagParam.obj);
  2849. return new _DERTaggedObject(newParam);
  2850. }
  2851. }
  2852. };
  2853. this.jsonToASN1HEX = function (param) {
  2854. var asn1Obj = this.newObject(param);
  2855. return asn1Obj.getEncodedHex();
  2856. };
  2857. }();
  2858. KJUR.asn1.ASN1Util.oidHexToInt = function (hex) {
  2859. var s = "";
  2860. var i01 = parseInt(hex.substr(0, 2), 16);
  2861. var i0 = Math.floor(i01 / 40);
  2862. var i1 = i01 % 40;
  2863. var s = i0 + "." + i1;
  2864. var binbuf = "";
  2865. for (var i = 2; i < hex.length; i += 2) {
  2866. var value = parseInt(hex.substr(i, 2), 16);
  2867. var bin = ("00000000" + value.toString(2)).slice(-8);
  2868. binbuf = binbuf + bin.substr(1, 7);
  2869. if (bin.substr(0, 1) == "0") {
  2870. var bi = new BigInteger(binbuf, 2);
  2871. s = s + "." + bi.toString(10);
  2872. binbuf = "";
  2873. }
  2874. }
  2875. return s;
  2876. };
  2877. KJUR.asn1.ASN1Util.oidIntToHex = function (oidString) {
  2878. var itox = function (i2) {
  2879. var h2 = i2.toString(16);
  2880. if (h2.length == 1) h2 = "0" + h2;
  2881. return h2;
  2882. };
  2883. var roidtox = function (roid) {
  2884. var h2 = "";
  2885. var bi = new BigInteger(roid, 10);
  2886. var b = bi.toString(2);
  2887. var padLen = 7 - b.length % 7;
  2888. if (padLen == 7) padLen = 0;
  2889. var bPad = "";
  2890. for (var i2 = 0; i2 < padLen; i2++) bPad += "0";
  2891. b = bPad + b;
  2892. for (var i2 = 0; i2 < b.length - 1; i2 += 7) {
  2893. var b8 = b.substr(i2, 7);
  2894. if (i2 != b.length - 7) b8 = "1" + b8;
  2895. h2 += itox(parseInt(b8, 2));
  2896. }
  2897. return h2;
  2898. };
  2899. if (!oidString.match(/^[0-9.]+$/)) {
  2900. throw "malformed oid string: " + oidString;
  2901. }
  2902. var h = "";
  2903. var a = oidString.split(".");
  2904. var i0 = parseInt(a[0]) * 40 + parseInt(a[1]);
  2905. h += itox(i0);
  2906. a.splice(0, 2);
  2907. for (var i = 0; i < a.length; i++) {
  2908. h += roidtox(a[i]);
  2909. }
  2910. return h;
  2911. };
  2912. KJUR.asn1.ASN1Object = function () {
  2913. var isModified = true;
  2914. var hTLV = null;
  2915. var hT = "00";
  2916. var hL = "00";
  2917. var hV = "";
  2918. this.getLengthHexFromValue = function () {
  2919. if (typeof this.hV == "undefined" || this.hV == null) {
  2920. throw "this.hV is null or undefined.";
  2921. }
  2922. if (this.hV.length % 2 == 1) {
  2923. throw "value hex must be even length: n=" + hV.length + ",v=" + this.hV;
  2924. }
  2925. var n = this.hV.length / 2;
  2926. var hN = n.toString(16);
  2927. if (hN.length % 2 == 1) {
  2928. hN = "0" + hN;
  2929. }
  2930. if (n < 128) {
  2931. return hN;
  2932. } else {
  2933. var hNlen = hN.length / 2;
  2934. if (hNlen > 15) {
  2935. throw "ASN.1 length too long to represent by 8x: n = " + n.toString(16);
  2936. }
  2937. var head = 128 + hNlen;
  2938. return head.toString(16) + hN;
  2939. }
  2940. };
  2941. this.getEncodedHex = function () {
  2942. if (this.hTLV == null || this.isModified) {
  2943. this.hV = this.getFreshValueHex();
  2944. this.hL = this.getLengthHexFromValue();
  2945. this.hTLV = this.hT + this.hL + this.hV;
  2946. this.isModified = false;
  2947. }
  2948. return this.hTLV;
  2949. };
  2950. this.getValueHex = function () {
  2951. this.getEncodedHex();
  2952. return this.hV;
  2953. };
  2954. this.getFreshValueHex = function () {
  2955. return "";
  2956. };
  2957. };
  2958. KJUR.asn1.DERAbstractString = function (params) {
  2959. KJUR.asn1.DERAbstractString.superclass.constructor.call(this);
  2960. var s = null;
  2961. var hV = null;
  2962. this.getString = function () {
  2963. return this.s;
  2964. };
  2965. this.setString = function (newS) {
  2966. this.hTLV = null;
  2967. this.isModified = true;
  2968. this.s = newS;
  2969. this.hV = stohex(this.s);
  2970. };
  2971. this.setStringHex = function (newHexString) {
  2972. this.hTLV = null;
  2973. this.isModified = true;
  2974. this.s = null;
  2975. this.hV = newHexString;
  2976. };
  2977. this.getFreshValueHex = function () {
  2978. return this.hV;
  2979. };
  2980. if (typeof params != "undefined") {
  2981. if (typeof params == "string") {
  2982. this.setString(params);
  2983. } else if (typeof params["str"] != "undefined") {
  2984. this.setString(params["str"]);
  2985. } else if (typeof params["hex"] != "undefined") {
  2986. this.setStringHex(params["hex"]);
  2987. }
  2988. }
  2989. };
  2990. YAHOO.lang.extend(KJUR.asn1.DERAbstractString, KJUR.asn1.ASN1Object);
  2991. KJUR.asn1.DERAbstractTime = function (params) {
  2992. KJUR.asn1.DERAbstractTime.superclass.constructor.call(this);
  2993. var s = null;
  2994. var date = null;
  2995. this.localDateToUTC = function (d) {
  2996. utc = d.getTime() + d.getTimezoneOffset() * 6e4;
  2997. var utcDate = new Date(utc);
  2998. return utcDate;
  2999. };
  3000. this.formatDate = function (dateObject, type, withMillis) {
  3001. var pad = this.zeroPadding;
  3002. var d = this.localDateToUTC(dateObject);
  3003. var year = String(d.getFullYear());
  3004. if (type == "utc") year = year.substr(2, 2);
  3005. var month = pad(String(d.getMonth() + 1), 2);
  3006. var day = pad(String(d.getDate()), 2);
  3007. var hour = pad(String(d.getHours()), 2);
  3008. var min = pad(String(d.getMinutes()), 2);
  3009. var sec = pad(String(d.getSeconds()), 2);
  3010. var s2 = year + month + day + hour + min + sec;
  3011. if (withMillis === true) {
  3012. var millis = d.getMilliseconds();
  3013. if (millis != 0) {
  3014. var sMillis = pad(String(millis), 3);
  3015. sMillis = sMillis.replace(/[0]+$/, "");
  3016. s2 = s2 + "." + sMillis;
  3017. }
  3018. }
  3019. return s2 + "Z";
  3020. };
  3021. this.zeroPadding = function (s2, len) {
  3022. if (s2.length >= len) return s2;
  3023. return new Array(len - s2.length + 1).join("0") + s2;
  3024. };
  3025. this.getString = function () {
  3026. return this.s;
  3027. };
  3028. this.setString = function (newS) {
  3029. this.hTLV = null;
  3030. this.isModified = true;
  3031. this.s = newS;
  3032. this.hV = stohex(newS);
  3033. };
  3034. this.setByDateValue = function (year, month, day, hour, min, sec) {
  3035. var dateObject = new Date(Date.UTC(year, month - 1, day, hour, min, sec, 0));
  3036. this.setByDate(dateObject);
  3037. };
  3038. this.getFreshValueHex = function () {
  3039. return this.hV;
  3040. };
  3041. };
  3042. YAHOO.lang.extend(KJUR.asn1.DERAbstractTime, KJUR.asn1.ASN1Object);
  3043. KJUR.asn1.DERAbstractStructured = function (params) {
  3044. KJUR.asn1.DERAbstractString.superclass.constructor.call(this);
  3045. var asn1Array = null;
  3046. this.setByASN1ObjectArray = function (asn1ObjectArray) {
  3047. this.hTLV = null;
  3048. this.isModified = true;
  3049. this.asn1Array = asn1ObjectArray;
  3050. };
  3051. this.appendASN1Object = function (asn1Object) {
  3052. this.hTLV = null;
  3053. this.isModified = true;
  3054. this.asn1Array.push(asn1Object);
  3055. };
  3056. this.asn1Array = new Array();
  3057. if (typeof params != "undefined") {
  3058. if (typeof params["array"] != "undefined") {
  3059. this.asn1Array = params["array"];
  3060. }
  3061. }
  3062. };
  3063. YAHOO.lang.extend(KJUR.asn1.DERAbstractStructured, KJUR.asn1.ASN1Object);
  3064. KJUR.asn1.DERBoolean = function () {
  3065. KJUR.asn1.DERBoolean.superclass.constructor.call(this);
  3066. this.hT = "01";
  3067. this.hTLV = "0101ff";
  3068. };
  3069. YAHOO.lang.extend(KJUR.asn1.DERBoolean, KJUR.asn1.ASN1Object);
  3070. KJUR.asn1.DERInteger = function (params) {
  3071. KJUR.asn1.DERInteger.superclass.constructor.call(this);
  3072. this.hT = "02";
  3073. this.setByBigInteger = function (bigIntegerValue) {
  3074. this.hTLV = null;
  3075. this.isModified = true;
  3076. this.hV = KJUR.asn1.ASN1Util.bigIntToMinTwosComplementsHex(bigIntegerValue);
  3077. };
  3078. this.setByInteger = function (intValue) {
  3079. var bi = new BigInteger(String(intValue), 10);
  3080. this.setByBigInteger(bi);
  3081. };
  3082. this.setValueHex = function (newHexString) {
  3083. this.hV = newHexString;
  3084. };
  3085. this.getFreshValueHex = function () {
  3086. return this.hV;
  3087. };
  3088. if (typeof params != "undefined") {
  3089. if (typeof params["bigint"] != "undefined") {
  3090. this.setByBigInteger(params["bigint"]);
  3091. } else if (typeof params["int"] != "undefined") {
  3092. this.setByInteger(params["int"]);
  3093. } else if (typeof params == "number") {
  3094. this.setByInteger(params);
  3095. } else if (typeof params["hex"] != "undefined") {
  3096. this.setValueHex(params["hex"]);
  3097. }
  3098. }
  3099. };
  3100. YAHOO.lang.extend(KJUR.asn1.DERInteger, KJUR.asn1.ASN1Object);
  3101. KJUR.asn1.DERBitString = function (params) {
  3102. if (params !== void 0 && typeof params.obj !== "undefined") {
  3103. var o = KJUR.asn1.ASN1Util.newObject(params.obj);
  3104. params.hex = "00" + o.getEncodedHex();
  3105. }
  3106. KJUR.asn1.DERBitString.superclass.constructor.call(this);
  3107. this.hT = "03";
  3108. this.setHexValueIncludingUnusedBits = function (newHexStringIncludingUnusedBits) {
  3109. this.hTLV = null;
  3110. this.isModified = true;
  3111. this.hV = newHexStringIncludingUnusedBits;
  3112. };
  3113. this.setUnusedBitsAndHexValue = function (unusedBits, hValue) {
  3114. if (unusedBits < 0 || 7 < unusedBits) {
  3115. throw "unused bits shall be from 0 to 7: u = " + unusedBits;
  3116. }
  3117. var hUnusedBits = "0" + unusedBits;
  3118. this.hTLV = null;
  3119. this.isModified = true;
  3120. this.hV = hUnusedBits + hValue;
  3121. };
  3122. this.setByBinaryString = function (binaryString) {
  3123. binaryString = binaryString.replace(/0+$/, "");
  3124. var unusedBits = 8 - binaryString.length % 8;
  3125. if (unusedBits == 8) unusedBits = 0;
  3126. for (var i = 0; i <= unusedBits; i++) {
  3127. binaryString += "0";
  3128. }
  3129. var h = "";
  3130. for (var i = 0; i < binaryString.length - 1; i += 8) {
  3131. var b = binaryString.substr(i, 8);
  3132. var x = parseInt(b, 2).toString(16);
  3133. if (x.length == 1) x = "0" + x;
  3134. h += x;
  3135. }
  3136. this.hTLV = null;
  3137. this.isModified = true;
  3138. this.hV = "0" + unusedBits + h;
  3139. };
  3140. this.setByBooleanArray = function (booleanArray) {
  3141. var s = "";
  3142. for (var i = 0; i < booleanArray.length; i++) {
  3143. if (booleanArray[i] == true) {
  3144. s += "1";
  3145. } else {
  3146. s += "0";
  3147. }
  3148. }
  3149. this.setByBinaryString(s);
  3150. };
  3151. this.newFalseArray = function (nLength) {
  3152. var a = new Array(nLength);
  3153. for (var i = 0; i < nLength; i++) {
  3154. a[i] = false;
  3155. }
  3156. return a;
  3157. };
  3158. this.getFreshValueHex = function () {
  3159. return this.hV;
  3160. };
  3161. if (typeof params != "undefined") {
  3162. if (typeof params == "string" && params.toLowerCase().match(/^[0-9a-f]+$/)) {
  3163. this.setHexValueIncludingUnusedBits(params);
  3164. } else if (typeof params["hex"] != "undefined") {
  3165. this.setHexValueIncludingUnusedBits(params["hex"]);
  3166. } else if (typeof params["bin"] != "undefined") {
  3167. this.setByBinaryString(params["bin"]);
  3168. } else if (typeof params["array"] != "undefined") {
  3169. this.setByBooleanArray(params["array"]);
  3170. }
  3171. }
  3172. };
  3173. YAHOO.lang.extend(KJUR.asn1.DERBitString, KJUR.asn1.ASN1Object);
  3174. KJUR.asn1.DEROctetString = function (params) {
  3175. if (params !== void 0 && typeof params.obj !== "undefined") {
  3176. var o = KJUR.asn1.ASN1Util.newObject(params.obj);
  3177. params.hex = o.getEncodedHex();
  3178. }
  3179. KJUR.asn1.DEROctetString.superclass.constructor.call(this, params);
  3180. this.hT = "04";
  3181. };
  3182. YAHOO.lang.extend(KJUR.asn1.DEROctetString, KJUR.asn1.DERAbstractString);
  3183. KJUR.asn1.DERNull = function () {
  3184. KJUR.asn1.DERNull.superclass.constructor.call(this);
  3185. this.hT = "05";
  3186. this.hTLV = "0500";
  3187. };
  3188. YAHOO.lang.extend(KJUR.asn1.DERNull, KJUR.asn1.ASN1Object);
  3189. KJUR.asn1.DERObjectIdentifier = function (params) {
  3190. var itox = function (i) {
  3191. var h = i.toString(16);
  3192. if (h.length == 1) h = "0" + h;
  3193. return h;
  3194. };
  3195. var roidtox = function (roid) {
  3196. var h = "";
  3197. var bi = new BigInteger(roid, 10);
  3198. var b = bi.toString(2);
  3199. var padLen = 7 - b.length % 7;
  3200. if (padLen == 7) padLen = 0;
  3201. var bPad = "";
  3202. for (var i = 0; i < padLen; i++) bPad += "0";
  3203. b = bPad + b;
  3204. for (var i = 0; i < b.length - 1; i += 7) {
  3205. var b8 = b.substr(i, 7);
  3206. if (i != b.length - 7) b8 = "1" + b8;
  3207. h += itox(parseInt(b8, 2));
  3208. }
  3209. return h;
  3210. };
  3211. KJUR.asn1.DERObjectIdentifier.superclass.constructor.call(this);
  3212. this.hT = "06";
  3213. this.setValueHex = function (newHexString) {
  3214. this.hTLV = null;
  3215. this.isModified = true;
  3216. this.s = null;
  3217. this.hV = newHexString;
  3218. };
  3219. this.setValueOidString = function (oidString) {
  3220. if (!oidString.match(/^[0-9.]+$/)) {
  3221. throw "malformed oid string: " + oidString;
  3222. }
  3223. var h = "";
  3224. var a = oidString.split(".");
  3225. var i0 = parseInt(a[0]) * 40 + parseInt(a[1]);
  3226. h += itox(i0);
  3227. a.splice(0, 2);
  3228. for (var i = 0; i < a.length; i++) {
  3229. h += roidtox(a[i]);
  3230. }
  3231. this.hTLV = null;
  3232. this.isModified = true;
  3233. this.s = null;
  3234. this.hV = h;
  3235. };
  3236. this.setValueName = function (oidName) {
  3237. var oid = KJUR.asn1.x509.OID.name2oid(oidName);
  3238. if (oid !== "") {
  3239. this.setValueOidString(oid);
  3240. } else {
  3241. throw "DERObjectIdentifier oidName undefined: " + oidName;
  3242. }
  3243. };
  3244. this.getFreshValueHex = function () {
  3245. return this.hV;
  3246. };
  3247. if (params !== void 0) {
  3248. if (typeof params === "string") {
  3249. if (params.match(/^[0-2].[0-9.]+$/)) {
  3250. this.setValueOidString(params);
  3251. } else {
  3252. this.setValueName(params);
  3253. }
  3254. } else if (params.oid !== void 0) {
  3255. this.setValueOidString(params.oid);
  3256. } else if (params.hex !== void 0) {
  3257. this.setValueHex(params.hex);
  3258. } else if (params.name !== void 0) {
  3259. this.setValueName(params.name);
  3260. }
  3261. }
  3262. };
  3263. YAHOO.lang.extend(KJUR.asn1.DERObjectIdentifier, KJUR.asn1.ASN1Object);
  3264. KJUR.asn1.DEREnumerated = function (params) {
  3265. KJUR.asn1.DEREnumerated.superclass.constructor.call(this);
  3266. this.hT = "0a";
  3267. this.setByBigInteger = function (bigIntegerValue) {
  3268. this.hTLV = null;
  3269. this.isModified = true;
  3270. this.hV = KJUR.asn1.ASN1Util.bigIntToMinTwosComplementsHex(bigIntegerValue);
  3271. };
  3272. this.setByInteger = function (intValue) {
  3273. var bi = new BigInteger(String(intValue), 10);
  3274. this.setByBigInteger(bi);
  3275. };
  3276. this.setValueHex = function (newHexString) {
  3277. this.hV = newHexString;
  3278. };
  3279. this.getFreshValueHex = function () {
  3280. return this.hV;
  3281. };
  3282. if (typeof params != "undefined") {
  3283. if (typeof params["int"] != "undefined") {
  3284. this.setByInteger(params["int"]);
  3285. } else if (typeof params == "number") {
  3286. this.setByInteger(params);
  3287. } else if (typeof params["hex"] != "undefined") {
  3288. this.setValueHex(params["hex"]);
  3289. }
  3290. }
  3291. };
  3292. YAHOO.lang.extend(KJUR.asn1.DEREnumerated, KJUR.asn1.ASN1Object);
  3293. KJUR.asn1.DERUTF8String = function (params) {
  3294. KJUR.asn1.DERUTF8String.superclass.constructor.call(this, params);
  3295. this.hT = "0c";
  3296. };
  3297. YAHOO.lang.extend(KJUR.asn1.DERUTF8String, KJUR.asn1.DERAbstractString);
  3298. KJUR.asn1.DERNumericString = function (params) {
  3299. KJUR.asn1.DERNumericString.superclass.constructor.call(this, params);
  3300. this.hT = "12";
  3301. };
  3302. YAHOO.lang.extend(KJUR.asn1.DERNumericString, KJUR.asn1.DERAbstractString);
  3303. KJUR.asn1.DERPrintableString = function (params) {
  3304. KJUR.asn1.DERPrintableString.superclass.constructor.call(this, params);
  3305. this.hT = "13";
  3306. };
  3307. YAHOO.lang.extend(KJUR.asn1.DERPrintableString, KJUR.asn1.DERAbstractString);
  3308. KJUR.asn1.DERTeletexString = function (params) {
  3309. KJUR.asn1.DERTeletexString.superclass.constructor.call(this, params);
  3310. this.hT = "14";
  3311. };
  3312. YAHOO.lang.extend(KJUR.asn1.DERTeletexString, KJUR.asn1.DERAbstractString);
  3313. KJUR.asn1.DERIA5String = function (params) {
  3314. KJUR.asn1.DERIA5String.superclass.constructor.call(this, params);
  3315. this.hT = "16";
  3316. };
  3317. YAHOO.lang.extend(KJUR.asn1.DERIA5String, KJUR.asn1.DERAbstractString);
  3318. KJUR.asn1.DERUTCTime = function (params) {
  3319. KJUR.asn1.DERUTCTime.superclass.constructor.call(this, params);
  3320. this.hT = "17";
  3321. this.setByDate = function (dateObject) {
  3322. this.hTLV = null;
  3323. this.isModified = true;
  3324. this.date = dateObject;
  3325. this.s = this.formatDate(this.date, "utc");
  3326. this.hV = stohex(this.s);
  3327. };
  3328. this.getFreshValueHex = function () {
  3329. if (typeof this.date == "undefined" && typeof this.s == "undefined") {
  3330. this.date = new Date();
  3331. this.s = this.formatDate(this.date, "utc");
  3332. this.hV = stohex(this.s);
  3333. }
  3334. return this.hV;
  3335. };
  3336. if (params !== void 0) {
  3337. if (params.str !== void 0) {
  3338. this.setString(params.str);
  3339. } else if (typeof params == "string" && params.match(/^[0-9]{12}Z$/)) {
  3340. this.setString(params);
  3341. } else if (params.hex !== void 0) {
  3342. this.setStringHex(params.hex);
  3343. } else if (params.date !== void 0) {
  3344. this.setByDate(params.date);
  3345. }
  3346. }
  3347. };
  3348. YAHOO.lang.extend(KJUR.asn1.DERUTCTime, KJUR.asn1.DERAbstractTime);
  3349. KJUR.asn1.DERGeneralizedTime = function (params) {
  3350. KJUR.asn1.DERGeneralizedTime.superclass.constructor.call(this, params);
  3351. this.hT = "18";
  3352. this.withMillis = false;
  3353. this.setByDate = function (dateObject) {
  3354. this.hTLV = null;
  3355. this.isModified = true;
  3356. this.date = dateObject;
  3357. this.s = this.formatDate(this.date, "gen", this.withMillis);
  3358. this.hV = stohex(this.s);
  3359. };
  3360. this.getFreshValueHex = function () {
  3361. if (this.date === void 0 && this.s === void 0) {
  3362. this.date = new Date();
  3363. this.s = this.formatDate(this.date, "gen", this.withMillis);
  3364. this.hV = stohex(this.s);
  3365. }
  3366. return this.hV;
  3367. };
  3368. if (params !== void 0) {
  3369. if (params.str !== void 0) {
  3370. this.setString(params.str);
  3371. } else if (typeof params == "string" && params.match(/^[0-9]{14}Z$/)) {
  3372. this.setString(params);
  3373. } else if (params.hex !== void 0) {
  3374. this.setStringHex(params.hex);
  3375. } else if (params.date !== void 0) {
  3376. this.setByDate(params.date);
  3377. }
  3378. if (params.millis === true) {
  3379. this.withMillis = true;
  3380. }
  3381. }
  3382. };
  3383. YAHOO.lang.extend(KJUR.asn1.DERGeneralizedTime, KJUR.asn1.DERAbstractTime);
  3384. KJUR.asn1.DERSequence = function (params) {
  3385. KJUR.asn1.DERSequence.superclass.constructor.call(this, params);
  3386. this.hT = "30";
  3387. this.getFreshValueHex = function () {
  3388. var h = "";
  3389. for (var i = 0; i < this.asn1Array.length; i++) {
  3390. var asn1Obj = this.asn1Array[i];
  3391. h += asn1Obj.getEncodedHex();
  3392. }
  3393. this.hV = h;
  3394. return this.hV;
  3395. };
  3396. };
  3397. YAHOO.lang.extend(KJUR.asn1.DERSequence, KJUR.asn1.DERAbstractStructured);
  3398. KJUR.asn1.DERSet = function (params) {
  3399. KJUR.asn1.DERSet.superclass.constructor.call(this, params);
  3400. this.hT = "31";
  3401. this.sortFlag = true;
  3402. this.getFreshValueHex = function () {
  3403. var a = new Array();
  3404. for (var i = 0; i < this.asn1Array.length; i++) {
  3405. var asn1Obj = this.asn1Array[i];
  3406. a.push(asn1Obj.getEncodedHex());
  3407. }
  3408. if (this.sortFlag == true) a.sort();
  3409. this.hV = a.join("");
  3410. return this.hV;
  3411. };
  3412. if (typeof params != "undefined") {
  3413. if (typeof params.sortflag != "undefined" && params.sortflag == false) this.sortFlag = false;
  3414. }
  3415. };
  3416. YAHOO.lang.extend(KJUR.asn1.DERSet, KJUR.asn1.DERAbstractStructured);
  3417. KJUR.asn1.DERTaggedObject = function (params) {
  3418. KJUR.asn1.DERTaggedObject.superclass.constructor.call(this);
  3419. this.hT = "a0";
  3420. this.hV = "";
  3421. this.isExplicit = true;
  3422. this.asn1Object = null;
  3423. this.setASN1Object = function (isExplicitFlag, tagNoHex, asn1Object) {
  3424. this.hT = tagNoHex;
  3425. this.isExplicit = isExplicitFlag;
  3426. this.asn1Object = asn1Object;
  3427. if (this.isExplicit) {
  3428. this.hV = this.asn1Object.getEncodedHex();
  3429. this.hTLV = null;
  3430. this.isModified = true;
  3431. } else {
  3432. this.hV = null;
  3433. this.hTLV = asn1Object.getEncodedHex();
  3434. this.hTLV = this.hTLV.replace(/^../, tagNoHex);
  3435. this.isModified = false;
  3436. }
  3437. };
  3438. this.getFreshValueHex = function () {
  3439. return this.hV;
  3440. };
  3441. if (typeof params != "undefined") {
  3442. if (typeof params["tag"] != "undefined") {
  3443. this.hT = params["tag"];
  3444. }
  3445. if (typeof params["explicit"] != "undefined") {
  3446. this.isExplicit = params["explicit"];
  3447. }
  3448. if (typeof params["obj"] != "undefined") {
  3449. this.asn1Object = params["obj"];
  3450. this.setASN1Object(this.isExplicit, this.hT, this.asn1Object);
  3451. }
  3452. }
  3453. };
  3454. YAHOO.lang.extend(KJUR.asn1.DERTaggedObject, KJUR.asn1.ASN1Object);
  3455. // lib/JSEncryptRSAKey.js
  3456. var __extends = function () {
  3457. var extendStatics = function (d, b) {
  3458. extendStatics = Object.setPrototypeOf || {
  3459. __proto__: []
  3460. } instanceof Array && function (d2, b2) {
  3461. d2.__proto__ = b2;
  3462. } || function (d2, b2) {
  3463. for (var p in b2) if (Object.prototype.hasOwnProperty.call(b2, p)) d2[p] = b2[p];
  3464. };
  3465. return extendStatics(d, b);
  3466. };
  3467. return function (d, b) {
  3468. if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  3469. extendStatics(d, b);
  3470. function __() {
  3471. this.constructor = d;
  3472. }
  3473. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype,
  3474. new __());
  3475. };
  3476. }();
  3477. var JSEncryptRSAKey =
  3478. /** @class */ function (_super) {
  3479. __extends(JSEncryptRSAKey2, _super);
  3480. function JSEncryptRSAKey2(key) {
  3481. var _this = _super.call(this) || this;
  3482. if (key) {
  3483. if (typeof key === "string") {
  3484. _this.parseKey(key);
  3485. } else if (JSEncryptRSAKey2.hasPrivateKeyProperty(key) || JSEncryptRSAKey2.hasPublicKeyProperty(key)) {
  3486. _this.parsePropertiesFrom(key);
  3487. }
  3488. }
  3489. return _this;
  3490. }
  3491. JSEncryptRSAKey2.prototype.parseKey = function (pem) {
  3492. try {
  3493. var modulus = 0;
  3494. var public_exponent = 0;
  3495. var reHex = /^\s*(?:[0-9A-Fa-f][0-9A-Fa-f]\s*)+$/;
  3496. var der = reHex.test(pem) ? Hex.decode(pem) : Base64.unarmor(pem);
  3497. var asn1 = ASN1.decode(der);
  3498. if (asn1.sub.length === 3) {
  3499. asn1 = asn1.sub[2].sub[0];
  3500. }
  3501. if (asn1.sub.length === 9) {
  3502. modulus = asn1.sub[1].getHexStringValue();
  3503. this.n = parseBigInt(modulus, 16);
  3504. public_exponent = asn1.sub[2].getHexStringValue();
  3505. this.e = parseInt(public_exponent, 16);
  3506. var private_exponent = asn1.sub[3].getHexStringValue();
  3507. this.d = parseBigInt(private_exponent, 16);
  3508. var prime1 = asn1.sub[4].getHexStringValue();
  3509. this.p = parseBigInt(prime1, 16);
  3510. var prime2 = asn1.sub[5].getHexStringValue();
  3511. this.q = parseBigInt(prime2, 16);
  3512. var exponent1 = asn1.sub[6].getHexStringValue();
  3513. this.dmp1 = parseBigInt(exponent1, 16);
  3514. var exponent2 = asn1.sub[7].getHexStringValue();
  3515. this.dmq1 = parseBigInt(exponent2, 16);
  3516. var coefficient = asn1.sub[8].getHexStringValue();
  3517. this.coeff = parseBigInt(coefficient, 16);
  3518. } else if (asn1.sub.length === 2) {
  3519. if (asn1.sub[0].sub) {
  3520. var bit_string = asn1.sub[1];
  3521. var sequence = bit_string.sub[0];
  3522. modulus = sequence.sub[0].getHexStringValue();
  3523. this.n = parseBigInt(modulus, 16);
  3524. public_exponent = sequence.sub[1].getHexStringValue();
  3525. this.e = parseInt(public_exponent, 16);
  3526. } else {
  3527. modulus = asn1.sub[0].getHexStringValue();
  3528. this.n = parseBigInt(modulus, 16);
  3529. public_exponent = asn1.sub[1].getHexStringValue();
  3530. this.e = parseInt(public_exponent, 16);
  3531. }
  3532. } else {
  3533. return false;
  3534. }
  3535. return true;
  3536. } catch (ex) {
  3537. return false;
  3538. }
  3539. };
  3540. JSEncryptRSAKey2.prototype.getPrivateBaseKey = function () {
  3541. var options = {
  3542. array: [new KJUR.asn1.DERInteger({
  3543. int: 0
  3544. }), new KJUR.asn1.DERInteger({
  3545. bigint: this.n
  3546. }), new KJUR.asn1.DERInteger({
  3547. int: this.e
  3548. }), new KJUR.asn1.DERInteger({
  3549. bigint: this.d
  3550. }), new KJUR.asn1.DERInteger({
  3551. bigint: this.p
  3552. }), new KJUR.asn1.DERInteger({
  3553. bigint: this.q
  3554. }), new KJUR.asn1.DERInteger({
  3555. bigint: this.dmp1
  3556. }), new KJUR.asn1.DERInteger({
  3557. bigint: this.dmq1
  3558. }), new KJUR.asn1.DERInteger({
  3559. bigint: this.coeff
  3560. })]
  3561. };
  3562. var seq = new KJUR.asn1.DERSequence(options);
  3563. return seq.getEncodedHex();
  3564. };
  3565. JSEncryptRSAKey2.prototype.getPrivateBaseKeyB64 = function () {
  3566. return hex2b64(this.getPrivateBaseKey());
  3567. };
  3568. JSEncryptRSAKey2.prototype.getPublicBaseKey = function () {
  3569. var first_sequence = new KJUR.asn1.DERSequence({
  3570. array: [new KJUR.asn1.DERObjectIdentifier({
  3571. oid: "1.2.840.113549.1.1.1"
  3572. }), new KJUR.asn1.DERNull()]
  3573. });
  3574. var second_sequence = new KJUR.asn1.DERSequence({
  3575. array: [new KJUR.asn1.DERInteger({
  3576. bigint: this.n
  3577. }), new KJUR.asn1.DERInteger({
  3578. int: this.e
  3579. })]
  3580. });
  3581. var bit_string = new KJUR.asn1.DERBitString({
  3582. hex: "00" + second_sequence.getEncodedHex()
  3583. });
  3584. var seq = new KJUR.asn1.DERSequence({
  3585. array: [first_sequence, bit_string]
  3586. });
  3587. return seq.getEncodedHex();
  3588. };
  3589. JSEncryptRSAKey2.prototype.getPublicBaseKeyB64 = function () {
  3590. return hex2b64(this.getPublicBaseKey());
  3591. };
  3592. JSEncryptRSAKey2.wordwrap = function (str, width) {
  3593. width = width || 64;
  3594. if (!str) {
  3595. return str;
  3596. }
  3597. var regex = "(.{1," + width + "})( +|$\n?)|(.{1," + width + "})";
  3598. return str.match(RegExp(regex, "g")).join("\n");
  3599. };
  3600. JSEncryptRSAKey2.prototype.getPrivateKey = function () {
  3601. var key = "-----BEGIN RSA PRIVATE KEY-----\n";
  3602. key += JSEncryptRSAKey2.wordwrap(this.getPrivateBaseKeyB64()) + "\n";
  3603. key += "-----END RSA PRIVATE KEY-----";
  3604. return key;
  3605. };
  3606. JSEncryptRSAKey2.prototype.getPublicKey = function () {
  3607. var key = "-----BEGIN PUBLIC KEY-----\n";
  3608. key += JSEncryptRSAKey2.wordwrap(this.getPublicBaseKeyB64()) + "\n";
  3609. key += "-----END PUBLIC KEY-----";
  3610. return key;
  3611. };
  3612. JSEncryptRSAKey2.hasPublicKeyProperty = function (obj) {
  3613. obj = obj || {};
  3614. return obj.hasOwnProperty("n") && obj.hasOwnProperty("e");
  3615. };
  3616. JSEncryptRSAKey2.hasPrivateKeyProperty = function (obj) {
  3617. obj = obj || {};
  3618. return obj.hasOwnProperty("n") && obj.hasOwnProperty("e") && obj.hasOwnProperty("d") && obj.hasOwnProperty("p") && obj.hasOwnProperty("q") && obj.hasOwnProperty("dmp1") && obj.hasOwnProperty("dmq1") && obj.hasOwnProperty("coeff");
  3619. };
  3620. JSEncryptRSAKey2.prototype.parsePropertiesFrom = function (obj) {
  3621. this.n = obj.n;
  3622. this.e = obj.e;
  3623. if (obj.hasOwnProperty("d")) {
  3624. this.d = obj.d;
  3625. this.p = obj.p;
  3626. this.q = obj.q;
  3627. this.dmp1 = obj.dmp1;
  3628. this.dmq1 = obj.dmq1;
  3629. this.coeff = obj.coeff;
  3630. }
  3631. };
  3632. return JSEncryptRSAKey2;
  3633. }(RSAKey);
  3634. // lib/JSEncrypt.js
  3635. var _a;
  3636. var version = typeof process !== "undefined" ? (_a = process.env) === null || _a === void 0 ? void 0 : _a.npm_package_version : void 0;
  3637. var JSEncrypt =
  3638. /** @class */ function () {
  3639. function JSEncrypt2(options) {
  3640. if (options === void 0) {
  3641. options = {};
  3642. }
  3643. options = options || {};
  3644. this.default_key_size = options.default_key_size ? parseInt(options.default_key_size, 10) : 1024;
  3645. this.default_public_exponent = options.default_public_exponent || "010001";
  3646. this.log = options.log || false;
  3647. this.key = null;
  3648. }
  3649. JSEncrypt2.prototype.setKey = function (key) {
  3650. if (this.log && this.key) {
  3651. console.warn("A key was already set, overriding existing.");
  3652. }
  3653. this.key = new JSEncryptRSAKey(key);
  3654. };
  3655. JSEncrypt2.prototype.setPrivateKey = function (privkey) {
  3656. this.setKey(privkey);
  3657. };
  3658. JSEncrypt2.prototype.setPublicKey = function (pubkey) {
  3659. this.setKey(pubkey);
  3660. };
  3661. JSEncrypt2.prototype.decrypt = function (str) {
  3662. try {
  3663. return this.getKey().decrypt(b64tohex(str));
  3664. } catch (ex) {
  3665. return false;
  3666. }
  3667. };
  3668. JSEncrypt2.prototype.encrypt = function (str) {
  3669. try {
  3670. return hex2b64(this.getKey().encrypt(str));
  3671. } catch (ex) {
  3672. return false;
  3673. }
  3674. };
  3675. JSEncrypt2.prototype.sign = function (str, digestMethod, digestName) {
  3676. try {
  3677. return hex2b64(this.getKey().sign(str, digestMethod, digestName));
  3678. } catch (ex) {
  3679. return false;
  3680. }
  3681. };
  3682. JSEncrypt2.prototype.verify = function (str, signature, digestMethod) {
  3683. try {
  3684. return this.getKey().verify(str, b64tohex(signature), digestMethod);
  3685. } catch (ex) {
  3686. return false;
  3687. }
  3688. };
  3689. JSEncrypt2.prototype.getKey = function (cb) {
  3690. if (!this.key) {
  3691. this.key = new JSEncryptRSAKey();
  3692. if (cb && {}.toString.call(cb) === "[object Function]") {
  3693. this.key.generateAsync(this.default_key_size, this.default_public_exponent, cb);
  3694. return;
  3695. }
  3696. this.key.generate(this.default_key_size, this.default_public_exponent);
  3697. }
  3698. return this.key;
  3699. };
  3700. JSEncrypt2.prototype.getPrivateKey = function () {
  3701. return this.getKey().getPrivateKey();
  3702. };
  3703. JSEncrypt2.prototype.getPrivateKeyB64 = function () {
  3704. return this.getKey().getPrivateBaseKeyB64();
  3705. };
  3706. JSEncrypt2.prototype.getPublicKey = function () {
  3707. return this.getKey().getPublicKey();
  3708. };
  3709. JSEncrypt2.prototype.getPublicKeyB64 = function () {
  3710. return this.getKey().getPublicBaseKeyB64();
  3711. };
  3712. JSEncrypt2.version = version;
  3713. return JSEncrypt2;
  3714. }();
  3715. return JSEncrypt;
  3716. }