sm2.js 135 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722
  1. function SM2Cipher(a) {
  2. this.ct = 1;
  3. this.sm3c3 = this.sm3keybase = this.p2 = null;
  4. this.key = Array(32);
  5. this.keyOff = 0;
  6. this.cipherMode = "undefined" != typeof a ? a : SM2CipherMode.C1C3C2
  7. }
  8. (function (global, undefined) {
  9. "use strict";
  10. var SM2CipherMode = {
  11. C1C2C3: "0",
  12. C1C3C2: "1"
  13. };
  14. (function () {
  15. function a(a, c) {
  16. var b = (this._lBlock >>> a ^ this._rBlock) & c;
  17. this._rBlock ^= b;
  18. this._lBlock ^= b << a
  19. }
  20. function b(a, c) {
  21. var b = (this._rBlock >>> a ^ this._lBlock) & c;
  22. this._lBlock ^= b;
  23. this._rBlock ^= b << a
  24. }
  25. var c = CryptoJS,
  26. d = c.lib,
  27. e = d.WordArray,
  28. d = d.BlockCipher,
  29. f = c.algo,
  30. g = [57, 49, 41, 33, 25, 17, 9, 1, 58, 50, 42, 34, 26, 18, 10, 2, 59, 51, 43, 35, 27, 19, 11, 3, 60, 52, 44, 36, 63, 55, 47, 39, 31, 23, 15, 7, 62, 54, 46, 38, 30, 22, 14, 6, 61, 53, 45, 37, 29, 21, 13, 5, 28, 20, 12, 4],
  31. h = [14, 17, 11, 24, 1, 5, 3, 28, 15, 6, 21, 10, 23, 19, 12, 4, 26, 8, 16, 7, 27, 20, 13, 2, 41, 52, 31, 37, 47, 55, 30, 40, 51, 45, 33, 48, 44, 49, 39, 56, 34, 53, 46, 42, 50, 36, 29, 32],
  32. k = [1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28],
  33. l = [{
  34. 0: 8421888,
  35. 268435456: 32768,
  36. 536870912: 8421378,
  37. 805306368: 2,
  38. 1073741824: 512,
  39. 1342177280: 8421890,
  40. 1610612736: 8389122,
  41. 1879048192: 8388608,
  42. 2147483648: 514,
  43. 2415919104: 8389120,
  44. 2684354560: 33280,
  45. 2952790016: 8421376,
  46. 3221225472: 32770,
  47. 3489660928: 8388610,
  48. 3758096384: 0,
  49. 4026531840: 33282,
  50. 134217728: 0,
  51. 402653184: 8421890,
  52. 671088640: 33282,
  53. 939524096: 32768,
  54. 1207959552: 8421888,
  55. 1476395008: 512,
  56. 1744830464: 8421378,
  57. 2013265920: 2,
  58. 2281701376: 8389120,
  59. 2550136832: 33280,
  60. 2818572288: 8421376,
  61. 3087007744: 8389122,
  62. 3355443200: 8388610,
  63. 3623878656: 32770,
  64. 3892314112: 514,
  65. 4160749568: 8388608,
  66. 1: 32768,
  67. 268435457: 2,
  68. 536870913: 8421888,
  69. 805306369: 8388608,
  70. 1073741825: 8421378,
  71. 1342177281: 33280,
  72. 1610612737: 512,
  73. 1879048193: 8389122,
  74. 2147483649: 8421890,
  75. 2415919105: 8421376,
  76. 2684354561: 8388610,
  77. 2952790017: 33282,
  78. 3221225473: 514,
  79. 3489660929: 8389120,
  80. 3758096385: 32770,
  81. 4026531841: 0,
  82. 134217729: 8421890,
  83. 402653185: 8421376,
  84. 671088641: 8388608,
  85. 939524097: 512,
  86. 1207959553: 32768,
  87. 1476395009: 8388610,
  88. 1744830465: 2,
  89. 2013265921: 33282,
  90. 2281701377: 32770,
  91. 2550136833: 8389122,
  92. 2818572289: 514,
  93. 3087007745: 8421888,
  94. 3355443201: 8389120,
  95. 3623878657: 0,
  96. 3892314113: 33280,
  97. 4160749569: 8421378
  98. }, {
  99. 0: 1074282512,
  100. 16777216: 16384,
  101. 33554432: 524288,
  102. 50331648: 1074266128,
  103. 67108864: 1073741840,
  104. 83886080: 1074282496,
  105. 100663296: 1073758208,
  106. 117440512: 16,
  107. 134217728: 540672,
  108. 150994944: 1073758224,
  109. 167772160: 1073741824,
  110. 184549376: 540688,
  111. 201326592: 524304,
  112. 218103808: 0,
  113. 234881024: 16400,
  114. 251658240: 1074266112,
  115. 8388608: 1073758208,
  116. 25165824: 540688,
  117. 41943040: 16,
  118. 58720256: 1073758224,
  119. 75497472: 1074282512,
  120. 92274688: 1073741824,
  121. 109051904: 524288,
  122. 125829120: 1074266128,
  123. 142606336: 524304,
  124. 159383552: 0,
  125. 176160768: 16384,
  126. 192937984: 1074266112,
  127. 209715200: 1073741840,
  128. 226492416: 540672,
  129. 243269632: 1074282496,
  130. 260046848: 16400,
  131. 268435456: 0,
  132. 285212672: 1074266128,
  133. 301989888: 1073758224,
  134. 318767104: 1074282496,
  135. 335544320: 1074266112,
  136. 352321536: 16,
  137. 369098752: 540688,
  138. 385875968: 16384,
  139. 402653184: 16400,
  140. 419430400: 524288,
  141. 436207616: 524304,
  142. 452984832: 1073741840,
  143. 469762048: 540672,
  144. 486539264: 1073758208,
  145. 503316480: 1073741824,
  146. 520093696: 1074282512,
  147. 276824064: 540688,
  148. 293601280: 524288,
  149. 310378496: 1074266112,
  150. 327155712: 16384,
  151. 343932928: 1073758208,
  152. 360710144: 1074282512,
  153. 377487360: 16,
  154. 394264576: 1073741824,
  155. 411041792: 1074282496,
  156. 427819008: 1073741840,
  157. 444596224: 1073758224,
  158. 461373440: 524304,
  159. 478150656: 0,
  160. 494927872: 16400,
  161. 511705088: 1074266128,
  162. 528482304: 540672
  163. }, {
  164. 0: 260,
  165. 1048576: 0,
  166. 2097152: 67109120,
  167. 3145728: 65796,
  168. 4194304: 65540,
  169. 5242880: 67108868,
  170. 6291456: 67174660,
  171. 7340032: 67174400,
  172. 8388608: 67108864,
  173. 9437184: 67174656,
  174. 10485760: 65792,
  175. 11534336: 67174404,
  176. 12582912: 67109124,
  177. 13631488: 65536,
  178. 14680064: 4,
  179. 15728640: 256,
  180. 524288: 67174656,
  181. 1572864: 67174404,
  182. 2621440: 0,
  183. 3670016: 67109120,
  184. 4718592: 67108868,
  185. 5767168: 65536,
  186. 6815744: 65540,
  187. 7864320: 260,
  188. 8912896: 4,
  189. 9961472: 256,
  190. 11010048: 67174400,
  191. 12058624: 65796,
  192. 13107200: 65792,
  193. 14155776: 67109124,
  194. 15204352: 67174660,
  195. 16252928: 67108864,
  196. 16777216: 67174656,
  197. 17825792: 65540,
  198. 18874368: 65536,
  199. 19922944: 67109120,
  200. 20971520: 256,
  201. 22020096: 67174660,
  202. 23068672: 67108868,
  203. 24117248: 0,
  204. 25165824: 67109124,
  205. 26214400: 67108864,
  206. 27262976: 4,
  207. 28311552: 65792,
  208. 29360128: 67174400,
  209. 30408704: 260,
  210. 31457280: 65796,
  211. 32505856: 67174404,
  212. 17301504: 67108864,
  213. 18350080: 260,
  214. 19398656: 67174656,
  215. 20447232: 0,
  216. 21495808: 65540,
  217. 22544384: 67109120,
  218. 23592960: 256,
  219. 24641536: 67174404,
  220. 25690112: 65536,
  221. 26738688: 67174660,
  222. 27787264: 65796,
  223. 28835840: 67108868,
  224. 29884416: 67109124,
  225. 30932992: 67174400,
  226. 31981568: 4,
  227. 33030144: 65792
  228. }, {
  229. 0: 2151682048,
  230. 65536: 2147487808,
  231. 131072: 4198464,
  232. 196608: 2151677952,
  233. 262144: 0,
  234. 327680: 4198400,
  235. 393216: 2147483712,
  236. 458752: 4194368,
  237. 524288: 2147483648,
  238. 589824: 4194304,
  239. 655360: 64,
  240. 720896: 2147487744,
  241. 786432: 2151678016,
  242. 851968: 4160,
  243. 917504: 4096,
  244. 983040: 2151682112,
  245. 32768: 2147487808,
  246. 98304: 64,
  247. 163840: 2151678016,
  248. 229376: 2147487744,
  249. 294912: 4198400,
  250. 360448: 2151682112,
  251. 425984: 0,
  252. 491520: 2151677952,
  253. 557056: 4096,
  254. 622592: 2151682048,
  255. 688128: 4194304,
  256. 753664: 4160,
  257. 819200: 2147483648,
  258. 884736: 4194368,
  259. 950272: 4198464,
  260. 1015808: 2147483712,
  261. 1048576: 4194368,
  262. 1114112: 4198400,
  263. 1179648: 2147483712,
  264. 1245184: 0,
  265. 1310720: 4160,
  266. 1376256: 2151678016,
  267. 1441792: 2151682048,
  268. 1507328: 2147487808,
  269. 1572864: 2151682112,
  270. 1638400: 2147483648,
  271. 1703936: 2151677952,
  272. 1769472: 4198464,
  273. 1835008: 2147487744,
  274. 1900544: 4194304,
  275. 1966080: 64,
  276. 2031616: 4096,
  277. 1081344: 2151677952,
  278. 1146880: 2151682112,
  279. 1212416: 0,
  280. 1277952: 4198400,
  281. 1343488: 4194368,
  282. 1409024: 2147483648,
  283. 1474560: 2147487808,
  284. 1540096: 64,
  285. 1605632: 2147483712,
  286. 1671168: 4096,
  287. 1736704: 2147487744,
  288. 1802240: 2151678016,
  289. 1867776: 4160,
  290. 1933312: 2151682048,
  291. 1998848: 4194304,
  292. 2064384: 4198464
  293. }, {
  294. 0: 128,
  295. 4096: 17039360,
  296. 8192: 262144,
  297. 12288: 536870912,
  298. 16384: 537133184,
  299. 20480: 16777344,
  300. 24576: 553648256,
  301. 28672: 262272,
  302. 32768: 16777216,
  303. 36864: 537133056,
  304. 40960: 536871040,
  305. 45056: 553910400,
  306. 49152: 553910272,
  307. 53248: 0,
  308. 57344: 17039488,
  309. 61440: 553648128,
  310. 2048: 17039488,
  311. 6144: 553648256,
  312. 10240: 128,
  313. 14336: 17039360,
  314. 18432: 262144,
  315. 22528: 537133184,
  316. 26624: 553910272,
  317. 30720: 536870912,
  318. 34816: 537133056,
  319. 38912: 0,
  320. 43008: 553910400,
  321. 47104: 16777344,
  322. 51200: 536871040,
  323. 55296: 553648128,
  324. 59392: 16777216,
  325. 63488: 262272,
  326. 65536: 262144,
  327. 69632: 128,
  328. 73728: 536870912,
  329. 77824: 553648256,
  330. 81920: 16777344,
  331. 86016: 553910272,
  332. 90112: 537133184,
  333. 94208: 16777216,
  334. 98304: 553910400,
  335. 102400: 553648128,
  336. 106496: 17039360,
  337. 110592: 537133056,
  338. 114688: 262272,
  339. 118784: 536871040,
  340. 122880: 0,
  341. 126976: 17039488,
  342. 67584: 553648256,
  343. 71680: 16777216,
  344. 75776: 17039360,
  345. 79872: 537133184,
  346. 83968: 536870912,
  347. 88064: 17039488,
  348. 92160: 128,
  349. 96256: 553910272,
  350. 100352: 262272,
  351. 104448: 553910400,
  352. 108544: 0,
  353. 112640: 553648128,
  354. 116736: 16777344,
  355. 120832: 262144,
  356. 124928: 537133056,
  357. 129024: 536871040
  358. }, {
  359. 0: 268435464,
  360. 256: 8192,
  361. 512: 270532608,
  362. 768: 270540808,
  363. 1024: 268443648,
  364. 1280: 2097152,
  365. 1536: 2097160,
  366. 1792: 268435456,
  367. 2048: 0,
  368. 2304: 268443656,
  369. 2560: 2105344,
  370. 2816: 8,
  371. 3072: 270532616,
  372. 3328: 2105352,
  373. 3584: 8200,
  374. 3840: 270540800,
  375. 128: 270532608,
  376. 384: 270540808,
  377. 640: 8,
  378. 896: 2097152,
  379. 1152: 2105352,
  380. 1408: 268435464,
  381. 1664: 268443648,
  382. 1920: 8200,
  383. 2176: 2097160,
  384. 2432: 8192,
  385. 2688: 268443656,
  386. 2944: 270532616,
  387. 3200: 0,
  388. 3456: 270540800,
  389. 3712: 2105344,
  390. 3968: 268435456,
  391. 4096: 268443648,
  392. 4352: 270532616,
  393. 4608: 270540808,
  394. 4864: 8200,
  395. 5120: 2097152,
  396. 5376: 268435456,
  397. 5632: 268435464,
  398. 5888: 2105344,
  399. 6144: 2105352,
  400. 6400: 0,
  401. 6656: 8,
  402. 6912: 270532608,
  403. 7168: 8192,
  404. 7424: 268443656,
  405. 7680: 270540800,
  406. 7936: 2097160,
  407. 4224: 8,
  408. 4480: 2105344,
  409. 4736: 2097152,
  410. 4992: 268435464,
  411. 5248: 268443648,
  412. 5504: 8200,
  413. 5760: 270540808,
  414. 6016: 270532608,
  415. 6272: 270540800,
  416. 6528: 270532616,
  417. 6784: 8192,
  418. 7040: 2105352,
  419. 7296: 2097160,
  420. 7552: 0,
  421. 7808: 268435456,
  422. 8064: 268443656
  423. }, {
  424. 0: 1048576,
  425. 16: 33555457,
  426. 32: 1024,
  427. 48: 1049601,
  428. 64: 34604033,
  429. 80: 0,
  430. 96: 1,
  431. 112: 34603009,
  432. 128: 33555456,
  433. 144: 1048577,
  434. 160: 33554433,
  435. 176: 34604032,
  436. 192: 34603008,
  437. 208: 1025,
  438. 224: 1049600,
  439. 240: 33554432,
  440. 8: 34603009,
  441. 24: 0,
  442. 40: 33555457,
  443. 56: 34604032,
  444. 72: 1048576,
  445. 88: 33554433,
  446. 104: 33554432,
  447. 120: 1025,
  448. 136: 1049601,
  449. 152: 33555456,
  450. 168: 34603008,
  451. 184: 1048577,
  452. 200: 1024,
  453. 216: 34604033,
  454. 232: 1,
  455. 248: 1049600,
  456. 256: 33554432,
  457. 272: 1048576,
  458. 288: 33555457,
  459. 304: 34603009,
  460. 320: 1048577,
  461. 336: 33555456,
  462. 352: 34604032,
  463. 368: 1049601,
  464. 384: 1025,
  465. 400: 34604033,
  466. 416: 1049600,
  467. 432: 1,
  468. 448: 0,
  469. 464: 34603008,
  470. 480: 33554433,
  471. 496: 1024,
  472. 264: 1049600,
  473. 280: 33555457,
  474. 296: 34603009,
  475. 312: 1,
  476. 328: 33554432,
  477. 344: 1048576,
  478. 360: 1025,
  479. 376: 34604032,
  480. 392: 33554433,
  481. 408: 34603008,
  482. 424: 0,
  483. 440: 34604033,
  484. 456: 1049601,
  485. 472: 1024,
  486. 488: 33555456,
  487. 504: 1048577
  488. }, {
  489. 0: 134219808,
  490. 1: 131072,
  491. 2: 134217728,
  492. 3: 32,
  493. 4: 131104,
  494. 5: 134350880,
  495. 6: 134350848,
  496. 7: 2048,
  497. 8: 134348800,
  498. 9: 134219776,
  499. 10: 133120,
  500. 11: 134348832,
  501. 12: 2080,
  502. 13: 0,
  503. 14: 134217760,
  504. 15: 133152,
  505. 2147483648: 2048,
  506. 2147483649: 134350880,
  507. 2147483650: 134219808,
  508. 2147483651: 134217728,
  509. 2147483652: 134348800,
  510. 2147483653: 133120,
  511. 2147483654: 133152,
  512. 2147483655: 32,
  513. 2147483656: 134217760,
  514. 2147483657: 2080,
  515. 2147483658: 131104,
  516. 2147483659: 134350848,
  517. 2147483660: 0,
  518. 2147483661: 134348832,
  519. 2147483662: 134219776,
  520. 2147483663: 131072,
  521. 16: 133152,
  522. 17: 134350848,
  523. 18: 32,
  524. 19: 2048,
  525. 20: 134219776,
  526. 21: 134217760,
  527. 22: 134348832,
  528. 23: 131072,
  529. 24: 0,
  530. 25: 131104,
  531. 26: 134348800,
  532. 27: 134219808,
  533. 28: 134350880,
  534. 29: 133120,
  535. 30: 2080,
  536. 31: 134217728,
  537. 2147483664: 131072,
  538. 2147483665: 2048,
  539. 2147483666: 134348832,
  540. 2147483667: 133152,
  541. 2147483668: 32,
  542. 2147483669: 134348800,
  543. 2147483670: 134217728,
  544. 2147483671: 134219808,
  545. 2147483672: 134350880,
  546. 2147483673: 134217760,
  547. 2147483674: 134219776,
  548. 2147483675: 0,
  549. 2147483676: 133120,
  550. 2147483677: 2080,
  551. 2147483678: 131104,
  552. 2147483679: 134350848
  553. }],
  554. p = [4160749569, 528482304, 33030144, 2064384, 129024, 8064, 504, 2147483679],
  555. n = f.DES = d.extend({
  556. _doReset: function () {
  557. for (var a = this._key.words, c = [], b = 0; 56 > b; b++) {
  558. var d = g[b] - 1;
  559. c[b] = a[d >>> 5] >>> 31 - d % 32 & 1
  560. }
  561. a = this._subKeys = [];
  562. for (d = 0; 16 > d; d++) {
  563. for (var e = a[d] = [], f = k[d], b = 0; 24 > b; b++)
  564. e[b / 6 | 0] |= c[(h[b] - 1 + f) % 28] << 31 - b % 6,
  565. e[4 + (b / 6 | 0)] |= c[28 + (h[b + 24] - 1 + f) % 28] << 31 - b % 6;
  566. e[0] = e[0] << 1 | e[0] >>> 31;
  567. for (b = 1; 7 > b; b++)
  568. e[b] >>>= 4 * (b - 1) + 3;
  569. e[7] = e[7] << 5 | e[7] >>> 27
  570. }
  571. c = this._invSubKeys = [];
  572. for (b = 0; 16 > b; b++)
  573. c[b] = a[15 - b]
  574. },
  575. encryptBlock: function (a, c) {
  576. this._doCryptBlock(a, c, this._subKeys)
  577. },
  578. decryptBlock: function (a, c) {
  579. this._doCryptBlock(a, c, this._invSubKeys)
  580. },
  581. _doCryptBlock: function (c, d, e) {
  582. this._lBlock = c[d];
  583. this._rBlock = c[d + 1];
  584. a.call(this, 4, 252645135);
  585. a.call(this, 16, 65535);
  586. b.call(this, 2, 858993459);
  587. b.call(this, 8, 16711935);
  588. a.call(this, 1, 1431655765);
  589. for (var f = 0; 16 > f; f++) {
  590. for (var g = e[f], h = this._lBlock, k = this._rBlock, n = 0, u = 0; 8 > u; u++)
  591. n |= l[u][((k ^ g[u]) & p[u]) >>> 0];
  592. this._lBlock = k;
  593. this._rBlock = h ^ n
  594. }
  595. e = this._lBlock;
  596. this._lBlock = this._rBlock;
  597. this._rBlock = e;
  598. a.call(this, 1, 1431655765);
  599. b.call(this, 8, 16711935);
  600. b.call(this, 2, 858993459);
  601. a.call(this, 16, 65535);
  602. a.call(this, 4, 252645135);
  603. c[d] = this._lBlock;
  604. c[d + 1] = this._rBlock
  605. },
  606. keySize: 2,
  607. ivSize: 2,
  608. blockSize: 2
  609. });
  610. c.DES = d._createHelper(n);
  611. f = f.TripleDES = d.extend({
  612. _doReset: function () {
  613. var a = this._key.words;
  614. this._des1 = n.createEncryptor(e.create(a.slice(0, 2)));
  615. this._des2 = n.createEncryptor(e.create(a.slice(2, 4)));
  616. this._des3 = n.createEncryptor(e.create(a.slice(4, 6)))
  617. },
  618. encryptBlock: function (a, c) {
  619. this._des1.encryptBlock(a, c);
  620. this._des2.decryptBlock(a, c);
  621. this._des3.encryptBlock(a, c)
  622. },
  623. decryptBlock: function (a, c) {
  624. this._des3.decryptBlock(a, c);
  625. this._des2.encryptBlock(a, c);
  626. this._des1.decryptBlock(a, c)
  627. },
  628. keySize: 6,
  629. ivSize: 2,
  630. blockSize: 2
  631. });
  632. c.TripleDES = d._createHelper(f)
  633. })();
  634. (function () {
  635. var a = CryptoJS,
  636. b = a.lib.WordArray;
  637. a.enc.Base64 = {
  638. stringify: function (a) {
  639. var b = a.words,
  640. e = a.sigBytes,
  641. f = this._map;
  642. a.clamp();
  643. a = [];
  644. for (var g = 0; g < e; g += 3)
  645. for (var h = (b[g >>> 2] >>> 24 - g % 4 * 8 & 255) << 16 | (b[g + 1 >>> 2] >>> 24 - (g + 1) % 4 * 8 & 255) << 8 | b[g + 2 >>> 2] >>> 24 - (g + 2) % 4 * 8 & 255, k = 0; 4 > k && g + .75 * k < e; k++)
  646. a.push(f.charAt(h >>> 6 * (3 - k) & 63));
  647. if (b = f.charAt(64))
  648. for (; a.length % 4;)
  649. a.push(b);
  650. return a.join("")
  651. },
  652. parse: function (a) {
  653. var d = a.length,
  654. e = this._map,
  655. f = e.charAt(64);
  656. f && (f = a.indexOf(f),
  657. -1 != f && (d = f));
  658. for (var f = [], g = 0, h = 0; h < d; h++)
  659. if (h % 4) {
  660. var k = e.indexOf(a.charAt(h - 1)) << h % 4 * 2,
  661. l = e.indexOf(a.charAt(h)) >>> 6 - h % 4 * 2;
  662. f[g >>> 2] |= (k | l) << 24 - g % 4 * 8;
  663. g++
  664. }
  665. return b.create(f, g)
  666. },
  667. _map: "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="
  668. }
  669. })();
  670. var dbits, canary = 0xdeadbeefcafe,
  671. j_lm = 15715070 == (canary & 16777215);
  672. function BigInteger(a, b, c) {
  673. null != a && ("number" == typeof a ? this.fromNumber(a, b, c) : null == b && "string" != typeof a ? this.fromString(a, 256) : this.fromString(a, b))
  674. }
  675. function nbi() {
  676. return new BigInteger(null)
  677. }
  678. function am1(a, b, c, d, e, f) {
  679. for (; 0 <= --f;) {
  680. var g = b * this[a++] + c[d] + e;
  681. e = Math.floor(g / 67108864);
  682. c[d++] = g & 67108863
  683. }
  684. return e
  685. }
  686. function am2(a, b, c, d, e, f) {
  687. var g = b & 32767;
  688. for (b >>= 15; 0 <= --f;) {
  689. var h = this[a] & 32767,
  690. k = this[a++] >> 15,
  691. l = b * h + k * g,
  692. h = g * h + ((l & 32767) << 15) + c[d] + (e & 1073741823);
  693. e = (h >>> 30) + (l >>> 15) + b * k + (e >>> 30);
  694. c[d++] = h & 1073741823
  695. }
  696. return e
  697. }
  698. function am3(a, b, c, d, e, f) {
  699. var g = b & 16383;
  700. for (b >>= 14; 0 <= --f;) {
  701. var h = this[a] & 16383,
  702. k = this[a++] >> 14,
  703. l = b * h + k * g,
  704. h = g * h + ((l & 16383) << 14) + c[d] + e;
  705. e = (h >> 28) + (l >> 14) + b * k;
  706. c[d++] = h & 268435455
  707. }
  708. return e
  709. }
  710. j_lm && "Microsoft Internet Explorer" == navigator.appName ? (BigInteger.prototype.am = am2,
  711. dbits = 30) : j_lm && "Netscape" != navigator.appName ? (BigInteger.prototype.am = am1,
  712. dbits = 26) : (BigInteger.prototype.am = am3,
  713. dbits = 28);
  714. BigInteger.prototype.DB = dbits;
  715. BigInteger.prototype.DM = (1 << dbits) - 1;
  716. BigInteger.prototype.DV = 1 << dbits;
  717. var BI_FP = 52;
  718. BigInteger.prototype.FV = Math.pow(2, BI_FP);
  719. BigInteger.prototype.F1 = BI_FP - dbits;
  720. BigInteger.prototype.F2 = 2 * dbits - BI_FP;
  721. var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz",
  722. BI_RC = [],
  723. rr, vv;
  724. rr = 48;
  725. for (vv = 0; 9 >= vv; ++vv)
  726. BI_RC[rr++] = vv;
  727. rr = 97;
  728. for (vv = 10; 36 > vv; ++vv)
  729. BI_RC[rr++] = vv;
  730. rr = 65;
  731. for (vv = 10; 36 > vv; ++vv)
  732. BI_RC[rr++] = vv;
  733. function int2char(a) {
  734. return BI_RM.charAt(a)
  735. }
  736. function intAt(a, b) {
  737. var c = BI_RC[a.charCodeAt(b)];
  738. return null == c ? -1 : c
  739. }
  740. function bnpCopyTo(a) {
  741. for (var b = this.t - 1; 0 <= b; --b)
  742. a[b] = this[b];
  743. a.t = this.t;
  744. a.s = this.s
  745. }
  746. function bnpFromInt(a) {
  747. this.t = 1;
  748. this.s = 0 > a ? -1 : 0;
  749. 0 < a ? this[0] = a : -1 > a ? this[0] = a + this.DV : this.t = 0
  750. }
  751. function nbv(a) {
  752. var b = nbi();
  753. b.fromInt(a);
  754. return b
  755. }
  756. function bnpFromString(a, b) {
  757. var c;
  758. if (16 == b)
  759. c = 4;
  760. else if (8 == b)
  761. c = 3;
  762. else if (256 == b)
  763. c = 8;
  764. else if (2 == b)
  765. c = 1;
  766. else if (32 == b)
  767. c = 5;
  768. else if (4 == b)
  769. c = 2;
  770. else {
  771. this.fromRadix(a, b);
  772. return
  773. }
  774. this.s = this.t = 0;
  775. for (var d = a.length, e = !1, f = 0; 0 <= --d;) {
  776. var g = 8 == c ? a[d] & 255 : intAt(a, d);
  777. 0 > g ? "-" == a.charAt(d) && (e = !0) : (e = !1,
  778. 0 == f ? this[this.t++] = g : f + c > this.DB ? (this[this.t - 1] |= (g & (1 << this.DB - f) - 1) << f,
  779. this[this.t++] = g >> this.DB - f) : this[this.t - 1] |= g << f,
  780. f += c,
  781. f >= this.DB && (f -= this.DB))
  782. }
  783. 8 == c && 0 != (a[0] & 128) && (this.s = -1,
  784. 0 < f && (this[this.t - 1] |= (1 << this.DB - f) - 1 << f));
  785. this.clamp();
  786. e && BigInteger.ZERO.subTo(this, this)
  787. }
  788. function bnpClamp() {
  789. for (var a = this.s & this.DM; 0 < this.t && this[this.t - 1] == a;)
  790. --this.t
  791. }
  792. function bnToString(a) {
  793. if (0 > this.s)
  794. return "-" + this.negate().toString(a);
  795. if (16 == a)
  796. a = 4;
  797. else if (8 == a)
  798. a = 3;
  799. else if (2 == a)
  800. a = 1;
  801. else if (32 == a)
  802. a = 5;
  803. else if (4 == a)
  804. a = 2;
  805. else
  806. return this.toRadix(a);
  807. var b = (1 << a) - 1,
  808. c, d = !1,
  809. e = "",
  810. f = this.t,
  811. g = this.DB - f * this.DB % a;
  812. if (0 < f--)
  813. for (g < this.DB && 0 < (c = this[f] >> g) && (d = !0,
  814. e = int2char(c)); 0 <= f;)
  815. g < a ? (c = (this[f] & (1 << g) - 1) << a - g,
  816. c |= this[--f] >> (g += this.DB - a)) : (c = this[f] >> (g -= a) & b,
  817. 0 >= g && (g += this.DB,
  818. --f)),
  819. 0 < c && (d = !0),
  820. d && (e += int2char(c));
  821. return d ? e : "0"
  822. }
  823. function bnNegate() {
  824. var a = nbi();
  825. BigInteger.ZERO.subTo(this, a);
  826. return a
  827. }
  828. function bnAbs() {
  829. return 0 > this.s ? this.negate() : this
  830. }
  831. function bnCompareTo(a) {
  832. var b = this.s - a.s;
  833. if (0 != b)
  834. return b;
  835. var c = this.t,
  836. b = c - a.t;
  837. if (0 != b)
  838. return 0 > this.s ? -b : b;
  839. for (; 0 <= --c;)
  840. if (0 != (b = this[c] - a[c]))
  841. return b;
  842. return 0
  843. }
  844. function nbits(a) {
  845. var b = 1,
  846. c;
  847. 0 != (c = a >>> 16) && (a = c,
  848. b += 16);
  849. 0 != (c = a >> 8) && (a = c,
  850. b += 8);
  851. 0 != (c = a >> 4) && (a = c,
  852. b += 4);
  853. 0 != (c = a >> 2) && (a = c,
  854. b += 2);
  855. 0 != a >> 1 && (b += 1);
  856. return b
  857. }
  858. function bnBitLength() {
  859. return 0 >= this.t ? 0 : this.DB * (this.t - 1) + nbits(this[this.t - 1] ^ this.s & this.DM)
  860. }
  861. function bnpDLShiftTo(a, b) {
  862. var c;
  863. for (c = this.t - 1; 0 <= c; --c)
  864. b[c + a] = this[c];
  865. for (c = a - 1; 0 <= c; --c)
  866. b[c] = 0;
  867. b.t = this.t + a;
  868. b.s = this.s
  869. }
  870. function bnpDRShiftTo(a, b) {
  871. for (var c = a; c < this.t; ++c)
  872. b[c - a] = this[c];
  873. b.t = Math.max(this.t - a, 0);
  874. b.s = this.s
  875. }
  876. function bnpLShiftTo(a, b) {
  877. var c = a % this.DB,
  878. d = this.DB - c,
  879. e = (1 << d) - 1,
  880. f = Math.floor(a / this.DB),
  881. g = this.s << c & this.DM,
  882. h;
  883. for (h = this.t - 1; 0 <= h; --h)
  884. b[h + f + 1] = this[h] >> d | g,
  885. g = (this[h] & e) << c;
  886. for (h = f - 1; 0 <= h; --h)
  887. b[h] = 0;
  888. b[f] = g;
  889. b.t = this.t + f + 1;
  890. b.s = this.s;
  891. b.clamp()
  892. }
  893. function bnpRShiftTo(a, b) {
  894. b.s = this.s;
  895. var c = Math.floor(a / this.DB);
  896. if (c >= this.t)
  897. b.t = 0;
  898. else {
  899. var d = a % this.DB,
  900. e = this.DB - d,
  901. f = (1 << d) - 1;
  902. b[0] = this[c] >> d;
  903. for (var g = c + 1; g < this.t; ++g)
  904. b[g - c - 1] |= (this[g] & f) << e,
  905. b[g - c] = this[g] >> d;
  906. 0 < d && (b[this.t - c - 1] |= (this.s & f) << e);
  907. b.t = this.t - c;
  908. b.clamp()
  909. }
  910. }
  911. function bnpSubTo(a, b) {
  912. for (var c = 0, d = 0, e = Math.min(a.t, this.t); c < e;)
  913. d += this[c] - a[c],
  914. b[c++] = d & this.DM,
  915. d >>= this.DB;
  916. if (a.t < this.t) {
  917. for (d -= a.s; c < this.t;)
  918. d += this[c],
  919. b[c++] = d & this.DM,
  920. d >>= this.DB;
  921. d += this.s
  922. } else {
  923. for (d += this.s; c < a.t;)
  924. d -= a[c],
  925. b[c++] = d & this.DM,
  926. d >>= this.DB;
  927. d -= a.s
  928. }
  929. b.s = 0 > d ? -1 : 0; -
  930. 1 > d ? b[c++] = this.DV + d : 0 < d && (b[c++] = d);
  931. b.t = c;
  932. b.clamp()
  933. }
  934. function bnpMultiplyTo(a, b) {
  935. var c = this.abs(),
  936. d = a.abs(),
  937. e = c.t;
  938. for (b.t = e + d.t; 0 <= --e;)
  939. b[e] = 0;
  940. for (e = 0; e < d.t; ++e)
  941. b[e + c.t] = c.am(0, d[e], b, e, 0, c.t);
  942. b.s = 0;
  943. b.clamp();
  944. this.s != a.s && BigInteger.ZERO.subTo(b, b)
  945. }
  946. function bnpSquareTo(a) {
  947. for (var b = this.abs(), c = a.t = 2 * b.t; 0 <= --c;)
  948. a[c] = 0;
  949. for (c = 0; c < b.t - 1; ++c) {
  950. var d = b.am(c, b[c], a, 2 * c, 0, 1);
  951. (a[c + b.t] += b.am(c + 1, 2 * b[c], a, 2 * c + 1, d, b.t - c - 1)) >= b.DV && (a[c + b.t] -= b.DV,
  952. a[c + b.t + 1] = 1)
  953. }
  954. 0 < a.t && (a[a.t - 1] += b.am(c, b[c], a, 2 * c, 0, 1));
  955. a.s = 0;
  956. a.clamp()
  957. }
  958. function bnpDivRemTo(a, b, c) {
  959. var d = a.abs();
  960. if (!(0 >= d.t)) {
  961. var e = this.abs();
  962. if (e.t < d.t)
  963. null != b && b.fromInt(0),
  964. null != c && this.copyTo(c);
  965. else {
  966. null == c && (c = nbi());
  967. var f = nbi(),
  968. g = this.s;
  969. a = a.s;
  970. var h = this.DB - nbits(d[d.t - 1]);
  971. 0 < h ? (d.lShiftTo(h, f),
  972. e.lShiftTo(h, c)) : (d.copyTo(f),
  973. e.copyTo(c));
  974. d = f.t;
  975. e = f[d - 1];
  976. if (0 != e) {
  977. var k = e * (1 << this.F1) + (1 < d ? f[d - 2] >> this.F2 : 0),
  978. l = this.FV / k,
  979. k = (1 << this.F1) / k,
  980. p = 1 << this.F2,
  981. n = c.t,
  982. q = n - d,
  983. m = null == b ? nbi() : b;
  984. f.dlShiftTo(q, m);
  985. 0 <= c.compareTo(m) && (c[c.t++] = 1,
  986. c.subTo(m, c));
  987. BigInteger.ONE.dlShiftTo(d, m);
  988. for (m.subTo(f, f); f.t < d;)
  989. f[f.t++] = 0;
  990. for (; 0 <= --q;) {
  991. var r = c[--n] == e ? this.DM : Math.floor(c[n] * l + (c[n - 1] + p) * k);
  992. if ((c[n] += f.am(0, r, c, q, 0, d)) < r)
  993. for (f.dlShiftTo(q, m),
  994. c.subTo(m, c); c[n] < --r;)
  995. c.subTo(m, c)
  996. }
  997. null != b && (c.drShiftTo(d, b),
  998. g != a && BigInteger.ZERO.subTo(b, b));
  999. c.t = d;
  1000. c.clamp();
  1001. 0 < h && c.rShiftTo(h, c);
  1002. 0 > g && BigInteger.ZERO.subTo(c, c)
  1003. }
  1004. }
  1005. }
  1006. }
  1007. function bnMod(a) {
  1008. var b = nbi();
  1009. this.abs().divRemTo(a, null, b);
  1010. 0 > this.s && 0 < b.compareTo(BigInteger.ZERO) && a.subTo(b, b);
  1011. return b
  1012. }
  1013. function Classic(a) {
  1014. this.m = a
  1015. }
  1016. function cConvert(a) {
  1017. return 0 > a.s || 0 <= a.compareTo(this.m) ? a.mod(this.m) : a
  1018. }
  1019. function cRevert(a) {
  1020. return a
  1021. }
  1022. function cReduce(a) {
  1023. a.divRemTo(this.m, null, a)
  1024. }
  1025. function cMulTo(a, b, c) {
  1026. a.multiplyTo(b, c);
  1027. this.reduce(c)
  1028. }
  1029. function cSqrTo(a, b) {
  1030. a.squareTo(b);
  1031. this.reduce(b)
  1032. }
  1033. Classic.prototype.convert = cConvert;
  1034. Classic.prototype.revert = cRevert;
  1035. Classic.prototype.reduce = cReduce;
  1036. Classic.prototype.mulTo = cMulTo;
  1037. Classic.prototype.sqrTo = cSqrTo;
  1038. function bnpInvDigit() {
  1039. if (1 > this.t)
  1040. return 0;
  1041. var a = this[0];
  1042. if (0 == (a & 1))
  1043. return 0;
  1044. var b = a & 3,
  1045. b = b * (2 - (a & 15) * b) & 15,
  1046. b = b * (2 - (a & 255) * b) & 255,
  1047. b = b * (2 - ((a & 65535) * b & 65535)) & 65535,
  1048. b = b * (2 - a * b % this.DV) % this.DV;
  1049. return 0 < b ? this.DV - b : -b
  1050. }
  1051. function Montgomery(a) {
  1052. this.m = a;
  1053. this.mp = a.invDigit();
  1054. this.mpl = this.mp & 32767;
  1055. this.mph = this.mp >> 15;
  1056. this.um = (1 << a.DB - 15) - 1;
  1057. this.mt2 = 2 * a.t
  1058. }
  1059. function montConvert(a) {
  1060. var b = nbi();
  1061. a.abs().dlShiftTo(this.m.t, b);
  1062. b.divRemTo(this.m, null, b);
  1063. 0 > a.s && 0 < b.compareTo(BigInteger.ZERO) && this.m.subTo(b, b);
  1064. return b
  1065. }
  1066. function montRevert(a) {
  1067. var b = nbi();
  1068. a.copyTo(b);
  1069. this.reduce(b);
  1070. return b
  1071. }
  1072. function montReduce(a) {
  1073. for (; a.t <= this.mt2;)
  1074. a[a.t++] = 0;
  1075. for (var b = 0; b < this.m.t; ++b) {
  1076. var c = a[b] & 32767,
  1077. d = c * this.mpl + ((c * this.mph + (a[b] >> 15) * this.mpl & this.um) << 15) & a.DM,
  1078. c = b + this.m.t;
  1079. for (a[c] += this.m.am(0, d, a, b, 0, this.m.t); a[c] >= a.DV;)
  1080. a[c] -= a.DV,
  1081. a[++c]++
  1082. }
  1083. a.clamp();
  1084. a.drShiftTo(this.m.t, a);
  1085. 0 <= a.compareTo(this.m) && a.subTo(this.m, a)
  1086. }
  1087. function montSqrTo(a, b) {
  1088. a.squareTo(b);
  1089. this.reduce(b)
  1090. }
  1091. function montMulTo(a, b, c) {
  1092. a.multiplyTo(b, c);
  1093. this.reduce(c)
  1094. }
  1095. Montgomery.prototype.convert = montConvert;
  1096. Montgomery.prototype.revert = montRevert;
  1097. Montgomery.prototype.reduce = montReduce;
  1098. Montgomery.prototype.mulTo = montMulTo;
  1099. Montgomery.prototype.sqrTo = montSqrTo;
  1100. function bnpIsEven() {
  1101. return 0 == (0 < this.t ? this[0] & 1 : this.s)
  1102. }
  1103. function bnpExp(a, b) {
  1104. if (4294967295 < a || 1 > a)
  1105. return BigInteger.ONE;
  1106. var c = nbi(),
  1107. d = nbi(),
  1108. e = b.convert(this),
  1109. f = nbits(a) - 1;
  1110. for (e.copyTo(c); 0 <= --f;)
  1111. if (b.sqrTo(c, d),
  1112. 0 < (a & 1 << f))
  1113. b.mulTo(d, e, c);
  1114. else
  1115. var g = c,
  1116. c = d,
  1117. d = g;
  1118. return b.revert(c)
  1119. }
  1120. function bnModPowInt(a, b) {
  1121. var c;
  1122. c = 256 > a || b.isEven() ? new Classic(b) : new Montgomery(b);
  1123. return this.exp(a, c)
  1124. }
  1125. BigInteger.prototype.copyTo = bnpCopyTo;
  1126. BigInteger.prototype.fromInt = bnpFromInt;
  1127. BigInteger.prototype.fromString = bnpFromString;
  1128. BigInteger.prototype.clamp = bnpClamp;
  1129. BigInteger.prototype.dlShiftTo = bnpDLShiftTo;
  1130. BigInteger.prototype.drShiftTo = bnpDRShiftTo;
  1131. BigInteger.prototype.lShiftTo = bnpLShiftTo;
  1132. BigInteger.prototype.rShiftTo = bnpRShiftTo;
  1133. BigInteger.prototype.subTo = bnpSubTo;
  1134. BigInteger.prototype.multiplyTo = bnpMultiplyTo;
  1135. BigInteger.prototype.squareTo = bnpSquareTo;
  1136. BigInteger.prototype.divRemTo = bnpDivRemTo;
  1137. BigInteger.prototype.invDigit = bnpInvDigit;
  1138. BigInteger.prototype.isEven = bnpIsEven;
  1139. BigInteger.prototype.exp = bnpExp;
  1140. BigInteger.prototype.toString = bnToString;
  1141. BigInteger.prototype.negate = bnNegate;
  1142. BigInteger.prototype.abs = bnAbs;
  1143. BigInteger.prototype.compareTo = bnCompareTo;
  1144. BigInteger.prototype.bitLength = bnBitLength;
  1145. BigInteger.prototype.mod = bnMod;
  1146. BigInteger.prototype.modPowInt = bnModPowInt;
  1147. BigInteger.ZERO = nbv(0);
  1148. BigInteger.ONE = nbv(1);
  1149. function bnClone() {
  1150. var a = nbi();
  1151. this.copyTo(a);
  1152. return a
  1153. }
  1154. function bnIntValue() {
  1155. if (0 > this.s) {
  1156. if (1 == this.t)
  1157. return this[0] - this.DV;
  1158. if (0 == this.t)
  1159. return -1
  1160. } else {
  1161. if (1 == this.t)
  1162. return this[0];
  1163. if (0 == this.t)
  1164. return 0
  1165. }
  1166. return (this[1] & (1 << 32 - this.DB) - 1) << this.DB | this[0]
  1167. }
  1168. function bnByteValue() {
  1169. return 0 == this.t ? this.s : this[0] << 24 >> 24
  1170. }
  1171. function bnShortValue() {
  1172. return 0 == this.t ? this.s : this[0] << 16 >> 16
  1173. }
  1174. function bnpChunkSize(a) {
  1175. return Math.floor(Math.LN2 * this.DB / Math.log(a))
  1176. }
  1177. function bnSigNum() {
  1178. return 0 > this.s ? -1 : 0 >= this.t || 1 == this.t && 0 >= this[0] ? 0 : 1
  1179. }
  1180. function bnpToRadix(a) {
  1181. null == a && (a = 10);
  1182. if (0 == this.signum() || 2 > a || 36 < a)
  1183. return "0";
  1184. var b = this.chunkSize(a),
  1185. b = Math.pow(a, b),
  1186. c = nbv(b),
  1187. d = nbi(),
  1188. e = nbi(),
  1189. f = "";
  1190. for (this.divRemTo(c, d, e); 0 < d.signum();)
  1191. f = (b + e.intValue()).toString(a).substr(1) + f,
  1192. d.divRemTo(c, d, e);
  1193. return e.intValue().toString(a) + f
  1194. }
  1195. function bnpFromRadix(a, b) {
  1196. this.fromInt(0);
  1197. null == b && (b = 10);
  1198. for (var c = this.chunkSize(b), d = Math.pow(b, c), e = !1, f = 0, g = 0, h = 0; h < a.length; ++h) {
  1199. var k = intAt(a, h);
  1200. 0 > k ? "-" == a.charAt(h) && 0 == this.signum() && (e = !0) : (g = b * g + k,
  1201. ++f >= c && (this.dMultiply(d),
  1202. this.dAddOffset(g, 0),
  1203. g = f = 0))
  1204. }
  1205. 0 < f && (this.dMultiply(Math.pow(b, f)),
  1206. this.dAddOffset(g, 0));
  1207. e && BigInteger.ZERO.subTo(this, this)
  1208. }
  1209. function bnpFromNumber(a, b, c) {
  1210. if ("number" == typeof b)
  1211. if (2 > a)
  1212. this.fromInt(1);
  1213. else
  1214. for (this.fromNumber(a, c),
  1215. this.testBit(a - 1) || this.bitwiseTo(BigInteger.ONE.shiftLeft(a - 1), op_or, this),
  1216. this.isEven() && this.dAddOffset(1, 0); !this.isProbablePrime(b);)
  1217. this.dAddOffset(2, 0),
  1218. this.bitLength() > a && this.subTo(BigInteger.ONE.shiftLeft(a - 1), this);
  1219. else {
  1220. c = [];
  1221. var d = a & 7;
  1222. c.length = (a >> 3) + 1;
  1223. b.nextBytes(c);
  1224. c[0] = 0 < d ? c[0] & (1 << d) - 1 : 0;
  1225. this.fromString(c, 256)
  1226. }
  1227. }
  1228. function bnToByteArray() {
  1229. var a = this.t,
  1230. b = [];
  1231. b[0] = this.s;
  1232. var c = this.DB - a * this.DB % 8,
  1233. d, e = 0;
  1234. if (0 < a--)
  1235. for (c < this.DB && (d = this[a] >> c) != (this.s & this.DM) >> c && (b[e++] = d | this.s << this.DB - c); 0 <= a;)
  1236. if (8 > c ? (d = (this[a] & (1 << c) - 1) << 8 - c,
  1237. d |= this[--a] >> (c += this.DB - 8)) : (d = this[a] >> (c -= 8) & 255,
  1238. 0 >= c && (c += this.DB,
  1239. --a)),
  1240. 0 != (d & 128) && (d |= -256),
  1241. 0 == e && (this.s & 128) != (d & 128) && ++e,
  1242. 0 < e || d != this.s)
  1243. b[e++] = d;
  1244. return b
  1245. }
  1246. function bnEquals(a) {
  1247. return 0 == this.compareTo(a)
  1248. }
  1249. function bnMin(a) {
  1250. return 0 > this.compareTo(a) ? this : a
  1251. }
  1252. function bnMax(a) {
  1253. return 0 < this.compareTo(a) ? this : a
  1254. }
  1255. function bnpBitwiseTo(a, b, c) {
  1256. var d, e, f = Math.min(a.t, this.t);
  1257. for (d = 0; d < f; ++d)
  1258. c[d] = b(this[d], a[d]);
  1259. if (a.t < this.t) {
  1260. e = a.s & this.DM;
  1261. for (d = f; d < this.t; ++d)
  1262. c[d] = b(this[d], e);
  1263. c.t = this.t
  1264. } else {
  1265. e = this.s & this.DM;
  1266. for (d = f; d < a.t; ++d)
  1267. c[d] = b(e, a[d]);
  1268. c.t = a.t
  1269. }
  1270. c.s = b(this.s, a.s);
  1271. c.clamp()
  1272. }
  1273. function op_and(a, b) {
  1274. return a & b
  1275. }
  1276. function bnAnd(a) {
  1277. var b = nbi();
  1278. this.bitwiseTo(a, op_and, b);
  1279. return b
  1280. }
  1281. function op_or(a, b) {
  1282. return a | b
  1283. }
  1284. function bnOr(a) {
  1285. var b = nbi();
  1286. this.bitwiseTo(a, op_or, b);
  1287. return b
  1288. }
  1289. function op_xor(a, b) {
  1290. return a ^ b
  1291. }
  1292. function bnXor(a) {
  1293. var b = nbi();
  1294. this.bitwiseTo(a, op_xor, b);
  1295. return b
  1296. }
  1297. function op_andnot(a, b) {
  1298. return a & ~b
  1299. }
  1300. function bnAndNot(a) {
  1301. var b = nbi();
  1302. this.bitwiseTo(a, op_andnot, b);
  1303. return b
  1304. }
  1305. function bnNot() {
  1306. for (var a = nbi(), b = 0; b < this.t; ++b)
  1307. a[b] = this.DM & ~this[b];
  1308. a.t = this.t;
  1309. a.s = ~this.s;
  1310. return a
  1311. }
  1312. function bnShiftLeft(a) {
  1313. var b = nbi();
  1314. 0 > a ? this.rShiftTo(-a, b) : this.lShiftTo(a, b);
  1315. return b
  1316. }
  1317. function bnShiftRight(a) {
  1318. var b = nbi();
  1319. 0 > a ? this.lShiftTo(-a, b) : this.rShiftTo(a, b);
  1320. return b
  1321. }
  1322. function lbit(a) {
  1323. if (0 == a)
  1324. return -1;
  1325. var b = 0;
  1326. 0 == (a & 65535) && (a >>= 16,
  1327. b += 16);
  1328. 0 == (a & 255) && (a >>= 8,
  1329. b += 8);
  1330. 0 == (a & 15) && (a >>= 4,
  1331. b += 4);
  1332. 0 == (a & 3) && (a >>= 2,
  1333. b += 2);
  1334. 0 == (a & 1) && ++b;
  1335. return b
  1336. }
  1337. function bnGetLowestSetBit() {
  1338. for (var a = 0; a < this.t; ++a)
  1339. if (0 != this[a])
  1340. return a * this.DB + lbit(this[a]);
  1341. return 0 > this.s ? this.t * this.DB : -1
  1342. }
  1343. function cbit(a) {
  1344. for (var b = 0; 0 != a;)
  1345. a &= a - 1,
  1346. ++b;
  1347. return b
  1348. }
  1349. function bnBitCount() {
  1350. for (var a = 0, b = this.s & this.DM, c = 0; c < this.t; ++c)
  1351. a += cbit(this[c] ^ b);
  1352. return a
  1353. }
  1354. function bnTestBit(a) {
  1355. var b = Math.floor(a / this.DB);
  1356. return b >= this.t ? 0 != this.s : 0 != (this[b] & 1 << a % this.DB)
  1357. }
  1358. function bnpChangeBit(a, b) {
  1359. var c = BigInteger.ONE.shiftLeft(a);
  1360. this.bitwiseTo(c, b, c);
  1361. return c
  1362. }
  1363. function bnSetBit(a) {
  1364. return this.changeBit(a, op_or)
  1365. }
  1366. function bnClearBit(a) {
  1367. return this.changeBit(a, op_andnot)
  1368. }
  1369. function bnFlipBit(a) {
  1370. return this.changeBit(a, op_xor)
  1371. }
  1372. function bnpAddTo(a, b) {
  1373. for (var c = 0, d = 0, e = Math.min(a.t, this.t); c < e;)
  1374. d += this[c] + a[c],
  1375. b[c++] = d & this.DM,
  1376. d >>= this.DB;
  1377. if (a.t < this.t) {
  1378. for (d += a.s; c < this.t;)
  1379. d += this[c],
  1380. b[c++] = d & this.DM,
  1381. d >>= this.DB;
  1382. d += this.s
  1383. } else {
  1384. for (d += this.s; c < a.t;)
  1385. d += a[c],
  1386. b[c++] = d & this.DM,
  1387. d >>= this.DB;
  1388. d += a.s
  1389. }
  1390. b.s = 0 > d ? -1 : 0;
  1391. 0 < d ? b[c++] = d : -1 > d && (b[c++] = this.DV + d);
  1392. b.t = c;
  1393. b.clamp()
  1394. }
  1395. function bnAdd(a) {
  1396. var b = nbi();
  1397. this.addTo(a, b);
  1398. return b
  1399. }
  1400. function bnSubtract(a) {
  1401. var b = nbi();
  1402. this.subTo(a, b);
  1403. return b
  1404. }
  1405. function bnMultiply(a) {
  1406. var b = nbi();
  1407. this.multiplyTo(a, b);
  1408. return b
  1409. }
  1410. function bnSquare() {
  1411. var a = nbi();
  1412. this.squareTo(a);
  1413. return a
  1414. }
  1415. function bnDivide(a) {
  1416. var b = nbi();
  1417. this.divRemTo(a, b, null);
  1418. return b
  1419. }
  1420. function bnRemainder(a) {
  1421. var b = nbi();
  1422. this.divRemTo(a, null, b);
  1423. return b
  1424. }
  1425. function bnDivideAndRemainder(a) {
  1426. var b = nbi(),
  1427. c = nbi();
  1428. this.divRemTo(a, b, c);
  1429. return [b, c]
  1430. }
  1431. function bnpDMultiply(a) {
  1432. this[this.t] = this.am(0, a - 1, this, 0, 0, this.t);
  1433. ++this.t;
  1434. this.clamp()
  1435. }
  1436. function bnpDAddOffset(a, b) {
  1437. if (0 != a) {
  1438. for (; this.t <= b;)
  1439. this[this.t++] = 0;
  1440. for (this[b] += a; this[b] >= this.DV;)
  1441. this[b] -= this.DV,
  1442. ++b >= this.t && (this[this.t++] = 0),
  1443. ++this[b]
  1444. }
  1445. }
  1446. function NullExp() {}
  1447. function nNop(a) {
  1448. return a
  1449. }
  1450. function nMulTo(a, b, c) {
  1451. a.multiplyTo(b, c)
  1452. }
  1453. function nSqrTo(a, b) {
  1454. a.squareTo(b)
  1455. }
  1456. NullExp.prototype.convert = nNop;
  1457. NullExp.prototype.revert = nNop;
  1458. NullExp.prototype.mulTo = nMulTo;
  1459. NullExp.prototype.sqrTo = nSqrTo;
  1460. function bnPow(a) {
  1461. return this.exp(a, new NullExp)
  1462. }
  1463. function bnpMultiplyLowerTo(a, b, c) {
  1464. var d = Math.min(this.t + a.t, b);
  1465. c.s = 0;
  1466. for (c.t = d; 0 < d;)
  1467. c[--d] = 0;
  1468. var e;
  1469. for (e = c.t - this.t; d < e; ++d)
  1470. c[d + this.t] = this.am(0, a[d], c, d, 0, this.t);
  1471. for (e = Math.min(a.t, b); d < e; ++d)
  1472. this.am(0, a[d], c, d, 0, b - d);
  1473. c.clamp()
  1474. }
  1475. function bnpMultiplyUpperTo(a, b, c) {
  1476. --b;
  1477. var d = c.t = this.t + a.t - b;
  1478. for (c.s = 0; 0 <= --d;)
  1479. c[d] = 0;
  1480. for (d = Math.max(b - this.t, 0); d < a.t; ++d)
  1481. c[this.t + d - b] = this.am(b - d, a[d], c, 0, 0, this.t + d - b);
  1482. c.clamp();
  1483. c.drShiftTo(1, c)
  1484. }
  1485. function Barrett(a) {
  1486. this.r2 = nbi();
  1487. this.q3 = nbi();
  1488. BigInteger.ONE.dlShiftTo(2 * a.t, this.r2);
  1489. this.mu = this.r2.divide(a);
  1490. this.m = a
  1491. }
  1492. function barrettConvert(a) {
  1493. if (0 > a.s || a.t > 2 * this.m.t)
  1494. return a.mod(this.m);
  1495. if (0 > a.compareTo(this.m))
  1496. return a;
  1497. var b = nbi();
  1498. a.copyTo(b);
  1499. this.reduce(b);
  1500. return b
  1501. }
  1502. function barrettRevert(a) {
  1503. return a
  1504. }
  1505. function barrettReduce(a) {
  1506. a.drShiftTo(this.m.t - 1, this.r2);
  1507. a.t > this.m.t + 1 && (a.t = this.m.t + 1,
  1508. a.clamp());
  1509. this.mu.multiplyUpperTo(this.r2, this.m.t + 1, this.q3);
  1510. for (this.m.multiplyLowerTo(this.q3, this.m.t + 1, this.r2); 0 > a.compareTo(this.r2);)
  1511. a.dAddOffset(1, this.m.t + 1);
  1512. for (a.subTo(this.r2, a); 0 <= a.compareTo(this.m);)
  1513. a.subTo(this.m, a)
  1514. }
  1515. function barrettSqrTo(a, b) {
  1516. a.squareTo(b);
  1517. this.reduce(b)
  1518. }
  1519. function barrettMulTo(a, b, c) {
  1520. a.multiplyTo(b, c);
  1521. this.reduce(c)
  1522. }
  1523. Barrett.prototype.convert = barrettConvert;
  1524. Barrett.prototype.revert = barrettRevert;
  1525. Barrett.prototype.reduce = barrettReduce;
  1526. Barrett.prototype.mulTo = barrettMulTo;
  1527. Barrett.prototype.sqrTo = barrettSqrTo;
  1528. function bnModPow(a, b) {
  1529. var c = a.bitLength(),
  1530. d, e = nbv(1),
  1531. f;
  1532. if (0 >= c)
  1533. return e;
  1534. d = 18 > c ? 1 : 48 > c ? 3 : 144 > c ? 4 : 768 > c ? 5 : 6;
  1535. f = 8 > c ? new Classic(b) : b.isEven() ? new Barrett(b) : new Montgomery(b);
  1536. var g = [],
  1537. h = 3,
  1538. k = d - 1,
  1539. l = (1 << d) - 1;
  1540. g[1] = f.convert(this);
  1541. if (1 < d)
  1542. for (c = nbi(),
  1543. f.sqrTo(g[1], c); h <= l;)
  1544. g[h] = nbi(),
  1545. f.mulTo(c, g[h - 2], g[h]),
  1546. h += 2;
  1547. for (var p = a.t - 1, n, q = !0, m = nbi(), c = nbits(a[p]) - 1; 0 <= p;) {
  1548. c >= k ? n = a[p] >> c - k & l : (n = (a[p] & (1 << c + 1) - 1) << k - c,
  1549. 0 < p && (n |= a[p - 1] >> this.DB + c - k));
  1550. for (h = d; 0 == (n & 1);)
  1551. n >>= 1,
  1552. --h;
  1553. 0 > (c -= h) && (c += this.DB,
  1554. --p);
  1555. if (q)
  1556. g[n].copyTo(e),
  1557. q = !1;
  1558. else {
  1559. for (; 1 < h;)
  1560. f.sqrTo(e, m),
  1561. f.sqrTo(m, e),
  1562. h -= 2;
  1563. 0 < h ? f.sqrTo(e, m) : (h = e,
  1564. e = m,
  1565. m = h);
  1566. f.mulTo(m, g[n], e)
  1567. }
  1568. for (; 0 <= p && 0 == (a[p] & 1 << c);)
  1569. f.sqrTo(e, m),
  1570. h = e,
  1571. e = m,
  1572. m = h,
  1573. 0 > --c && (c = this.DB - 1,
  1574. --p)
  1575. }
  1576. return f.revert(e)
  1577. }
  1578. function bnGCD(a) {
  1579. var b = 0 > this.s ? this.negate() : this.clone();
  1580. a = 0 > a.s ? a.negate() : a.clone();
  1581. if (0 > b.compareTo(a)) {
  1582. var c = b,
  1583. b = a;
  1584. a = c
  1585. }
  1586. var c = b.getLowestSetBit(),
  1587. d = a.getLowestSetBit();
  1588. if (0 > d)
  1589. return b;
  1590. c < d && (d = c);
  1591. 0 < d && (b.rShiftTo(d, b),
  1592. a.rShiftTo(d, a));
  1593. for (; 0 < b.signum();)
  1594. 0 < (c = b.getLowestSetBit()) && b.rShiftTo(c, b),
  1595. 0 < (c = a.getLowestSetBit()) && a.rShiftTo(c, a),
  1596. 0 <= b.compareTo(a) ? (b.subTo(a, b),
  1597. b.rShiftTo(1, b)) : (a.subTo(b, a),
  1598. a.rShiftTo(1, a));
  1599. 0 < d && a.lShiftTo(d, a);
  1600. return a
  1601. }
  1602. function bnpModInt(a) {
  1603. if (0 >= a)
  1604. return 0;
  1605. var b = this.DV % a,
  1606. c = 0 > this.s ? a - 1 : 0;
  1607. if (0 < this.t)
  1608. if (0 == b)
  1609. c = this[0] % a;
  1610. else
  1611. for (var d = this.t - 1; 0 <= d; --d)
  1612. c = (b * c + this[d]) % a;
  1613. return c
  1614. }
  1615. function bnModInverse(a) {
  1616. var b = a.isEven();
  1617. if (this.isEven() && b || 0 == a.signum())
  1618. return BigInteger.ZERO;
  1619. for (var c = a.clone(), d = this.clone(), e = nbv(1), f = nbv(0), g = nbv(0), h = nbv(1); 0 != c.signum();) {
  1620. for (; c.isEven();)
  1621. c.rShiftTo(1, c),
  1622. b ? (e.isEven() && f.isEven() || (e.addTo(this, e),
  1623. f.subTo(a, f)),
  1624. e.rShiftTo(1, e)) : f.isEven() || f.subTo(a, f),
  1625. f.rShiftTo(1, f);
  1626. for (; d.isEven();)
  1627. d.rShiftTo(1, d),
  1628. b ? (g.isEven() && h.isEven() || (g.addTo(this, g),
  1629. h.subTo(a, h)),
  1630. g.rShiftTo(1, g)) : h.isEven() || h.subTo(a, h),
  1631. h.rShiftTo(1, h);
  1632. 0 <= c.compareTo(d) ? (c.subTo(d, c),
  1633. b && e.subTo(g, e),
  1634. f.subTo(h, f)) : (d.subTo(c, d),
  1635. b && g.subTo(e, g),
  1636. h.subTo(f, h))
  1637. }
  1638. if (0 != d.compareTo(BigInteger.ONE))
  1639. return BigInteger.ZERO;
  1640. if (0 <= h.compareTo(a))
  1641. return h.subtract(a);
  1642. if (0 > h.signum())
  1643. h.addTo(a, h);
  1644. else
  1645. return h;
  1646. return 0 > h.signum() ? h.add(a) : h
  1647. }
  1648. 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],
  1649. lplim = 67108864 / lowprimes[lowprimes.length - 1];
  1650. function bnIsProbablePrime(a) {
  1651. var b, c = this.abs();
  1652. if (1 == c.t && c[0] <= lowprimes[lowprimes.length - 1]) {
  1653. for (b = 0; b < lowprimes.length; ++b)
  1654. if (c[0] == lowprimes[b])
  1655. return !0;
  1656. return !1
  1657. }
  1658. if (c.isEven())
  1659. return !1;
  1660. for (b = 1; b < lowprimes.length;) {
  1661. for (var d = lowprimes[b], e = b + 1; e < lowprimes.length && d < lplim;)
  1662. d *= lowprimes[e++];
  1663. for (d = c.modInt(d); b < e;)
  1664. if (0 == d % lowprimes[b++])
  1665. return !1
  1666. }
  1667. return c.millerRabin(a)
  1668. }
  1669. function bnpMillerRabin(a) {
  1670. var b = this.subtract(BigInteger.ONE),
  1671. c = b.getLowestSetBit();
  1672. if (0 >= c)
  1673. return !1;
  1674. var d = b.shiftRight(c);
  1675. a = a + 1 >> 1;
  1676. a > lowprimes.length && (a = lowprimes.length);
  1677. for (var e = nbi(), f = 0; f < a; ++f) {
  1678. e.fromInt(lowprimes[Math.floor(Math.random() * lowprimes.length)]);
  1679. var g = e.modPow(d, this);
  1680. if (0 != g.compareTo(BigInteger.ONE) && 0 != g.compareTo(b)) {
  1681. for (var h = 1; h++ < c && 0 != g.compareTo(b);)
  1682. if (g = g.modPowInt(2, this),
  1683. 0 == g.compareTo(BigInteger.ONE))
  1684. return !1;
  1685. if (0 != g.compareTo(b))
  1686. return !1
  1687. }
  1688. }
  1689. return !0
  1690. }
  1691. BigInteger.prototype.chunkSize = bnpChunkSize;
  1692. BigInteger.prototype.toRadix = bnpToRadix;
  1693. BigInteger.prototype.fromRadix = bnpFromRadix;
  1694. BigInteger.prototype.fromNumber = bnpFromNumber;
  1695. BigInteger.prototype.bitwiseTo = bnpBitwiseTo;
  1696. BigInteger.prototype.changeBit = bnpChangeBit;
  1697. BigInteger.prototype.addTo = bnpAddTo;
  1698. BigInteger.prototype.dMultiply = bnpDMultiply;
  1699. BigInteger.prototype.dAddOffset = bnpDAddOffset;
  1700. BigInteger.prototype.multiplyLowerTo = bnpMultiplyLowerTo;
  1701. BigInteger.prototype.multiplyUpperTo = bnpMultiplyUpperTo;
  1702. BigInteger.prototype.modInt = bnpModInt;
  1703. BigInteger.prototype.millerRabin = bnpMillerRabin;
  1704. BigInteger.prototype.clone = bnClone;
  1705. BigInteger.prototype.intValue = bnIntValue;
  1706. BigInteger.prototype.byteValue = bnByteValue;
  1707. BigInteger.prototype.shortValue = bnShortValue;
  1708. BigInteger.prototype.signum = bnSigNum;
  1709. BigInteger.prototype.toByteArray = bnToByteArray;
  1710. BigInteger.prototype.equals = bnEquals;
  1711. BigInteger.prototype.min = bnMin;
  1712. BigInteger.prototype.max = bnMax;
  1713. BigInteger.prototype.and = bnAnd;
  1714. BigInteger.prototype.or = bnOr;
  1715. BigInteger.prototype.xor = bnXor;
  1716. BigInteger.prototype.andNot = bnAndNot;
  1717. BigInteger.prototype.not = bnNot;
  1718. BigInteger.prototype.shiftLeft = bnShiftLeft;
  1719. BigInteger.prototype.shiftRight = bnShiftRight;
  1720. BigInteger.prototype.getLowestSetBit = bnGetLowestSetBit;
  1721. BigInteger.prototype.bitCount = bnBitCount;
  1722. BigInteger.prototype.testBit = bnTestBit;
  1723. BigInteger.prototype.setBit = bnSetBit;
  1724. BigInteger.prototype.clearBit = bnClearBit;
  1725. BigInteger.prototype.flipBit = bnFlipBit;
  1726. BigInteger.prototype.add = bnAdd;
  1727. BigInteger.prototype.subtract = bnSubtract;
  1728. BigInteger.prototype.multiply = bnMultiply;
  1729. BigInteger.prototype.divide = bnDivide;
  1730. BigInteger.prototype.remainder = bnRemainder;
  1731. BigInteger.prototype.divideAndRemainder = bnDivideAndRemainder;
  1732. BigInteger.prototype.modPow = bnModPow;
  1733. BigInteger.prototype.modInverse = bnModInverse;
  1734. BigInteger.prototype.pow = bnPow;
  1735. BigInteger.prototype.gcd = bnGCD;
  1736. BigInteger.prototype.isProbablePrime = bnIsProbablePrime;
  1737. BigInteger.prototype.square = bnSquare;
  1738. function Arcfour() {
  1739. this.j = this.i = 0;
  1740. this.S = []
  1741. }
  1742. function ARC4init(a) {
  1743. var b, c, d;
  1744. for (b = 0; 256 > b; ++b)
  1745. this.S[b] = b;
  1746. for (b = c = 0; 256 > b; ++b)
  1747. c = c + this.S[b] + a[b % a.length] & 255,
  1748. d = this.S[b],
  1749. this.S[b] = this.S[c],
  1750. this.S[c] = d;
  1751. this.j = this.i = 0
  1752. }
  1753. function ARC4next() {
  1754. var a;
  1755. this.i = this.i + 1 & 255;
  1756. this.j = this.j + this.S[this.i] & 255;
  1757. a = this.S[this.i];
  1758. this.S[this.i] = this.S[this.j];
  1759. this.S[this.j] = a;
  1760. return this.S[a + this.S[this.i] & 255]
  1761. }
  1762. Arcfour.prototype.init = ARC4init;
  1763. Arcfour.prototype.next = ARC4next;
  1764. function prng_newstate() {
  1765. return new Arcfour
  1766. }
  1767. var rng_psize = 256,
  1768. rng_state, rng_pool, rng_pptr;
  1769. function rng_seed_int(a) {
  1770. rng_pool[rng_pptr++] ^= a & 255;
  1771. rng_pool[rng_pptr++] ^= a >> 8 & 255;
  1772. rng_pool[rng_pptr++] ^= a >> 16 & 255;
  1773. rng_pool[rng_pptr++] ^= a >> 24 & 255;
  1774. rng_pptr >= rng_psize && (rng_pptr -= rng_psize)
  1775. }
  1776. function rng_seed_time() {
  1777. rng_seed_int((new Date).getTime())
  1778. }
  1779. if (null == rng_pool) {
  1780. rng_pool = [];
  1781. rng_pptr = 0;
  1782. var t;
  1783. if ("Netscape" == navigator.appName && "5" > navigator.appVersion && window.crypto) {
  1784. var z = window.crypto.random(32);
  1785. for (t = 0; t < z.length; ++t)
  1786. rng_pool[rng_pptr++] = z.charCodeAt(t) & 255
  1787. }
  1788. for (; rng_pptr < rng_psize;)
  1789. t = Math.floor(65536 * Math.random()),
  1790. rng_pool[rng_pptr++] = t >>> 8,
  1791. rng_pool[rng_pptr++] = t & 255;
  1792. rng_pptr = 0;
  1793. rng_seed_time()
  1794. }
  1795. function rng_get_byte() {
  1796. if (null == rng_state) {
  1797. rng_seed_time();
  1798. rng_state = prng_newstate();
  1799. rng_state.init(rng_pool);
  1800. for (rng_pptr = 0; rng_pptr < rng_pool.length; ++rng_pptr)
  1801. rng_pool[rng_pptr] = 0;
  1802. rng_pptr = 0
  1803. }
  1804. return rng_state.next()
  1805. }
  1806. function rng_get_bytes(a) {
  1807. var b;
  1808. for (b = 0; b < a.length; ++b)
  1809. a[b] = rng_get_byte()
  1810. }
  1811. function SecureRandom() {}
  1812. SecureRandom.prototype.nextBytes = rng_get_bytes;
  1813. var KJUR = {};
  1814. //"undefined" != typeof KJUR && KJUR || (KJUR = {});
  1815. "undefined" != typeof KJUR.crypto && KJUR.crypto || (KJUR.crypto = {});
  1816. KJUR.crypto.Util = new function () {
  1817. this.DIGESTINFOHEAD = {
  1818. sha1: "3021300906052b0e03021a05000414",
  1819. sha224: "302d300d06096086480165030402040500041c",
  1820. sha256: "3031300d060960864801650304020105000420",
  1821. sha384: "3041300d060960864801650304020205000430",
  1822. sha512: "3051300d060960864801650304020305000440",
  1823. md2: "3020300c06082a864886f70d020205000410",
  1824. md5: "3020300c06082a864886f70d020505000410",
  1825. ripemd160: "3021300906052b2403020105000414"
  1826. };
  1827. this.DEFAULTPROVIDER = {
  1828. md5: "cryptojs",
  1829. sha1: "cryptojs",
  1830. sha224: "cryptojs",
  1831. sha256: "cryptojs",
  1832. sha384: "cryptojs",
  1833. sha512: "cryptojs",
  1834. ripemd160: "cryptojs",
  1835. hmacmd5: "cryptojs",
  1836. hmacsha1: "cryptojs",
  1837. hmacsha224: "cryptojs",
  1838. hmacsha256: "cryptojs",
  1839. hmacsha384: "cryptojs",
  1840. hmacsha512: "cryptojs",
  1841. hmacripemd160: "cryptojs",
  1842. sm3: "cryptojs",
  1843. MD5withRSA: "cryptojs/jsrsa",
  1844. SHA1withRSA: "cryptojs/jsrsa",
  1845. SHA224withRSA: "cryptojs/jsrsa",
  1846. SHA256withRSA: "cryptojs/jsrsa",
  1847. SHA384withRSA: "cryptojs/jsrsa",
  1848. SHA512withRSA: "cryptojs/jsrsa",
  1849. RIPEMD160withRSA: "cryptojs/jsrsa",
  1850. MD5withECDSA: "cryptojs/jsrsa",
  1851. SHA1withECDSA: "cryptojs/jsrsa",
  1852. SHA224withECDSA: "cryptojs/jsrsa",
  1853. SHA256withECDSA: "cryptojs/jsrsa",
  1854. SHA384withECDSA: "cryptojs/jsrsa",
  1855. SHA512withECDSA: "cryptojs/jsrsa",
  1856. RIPEMD160withECDSA: "cryptojs/jsrsa",
  1857. SHA1withDSA: "cryptojs/jsrsa",
  1858. SHA224withDSA: "cryptojs/jsrsa",
  1859. SHA256withDSA: "cryptojs/jsrsa",
  1860. MD5withRSAandMGF1: "cryptojs/jsrsa",
  1861. SHA1withRSAandMGF1: "cryptojs/jsrsa",
  1862. SHA224withRSAandMGF1: "cryptojs/jsrsa",
  1863. SHA256withRSAandMGF1: "cryptojs/jsrsa",
  1864. SHA384withRSAandMGF1: "cryptojs/jsrsa",
  1865. SHA512withRSAandMGF1: "cryptojs/jsrsa",
  1866. RIPEMD160withRSAandMGF1: "cryptojs/jsrsa"
  1867. };
  1868. this.CRYPTOJSMESSAGEDIGESTNAME = {
  1869. md5: "CryptoJS.algo.MD5",
  1870. sha1: "CryptoJS.algo.SHA1",
  1871. sha224: "CryptoJS.algo.SHA224",
  1872. sha256: "CryptoJS.algo.SHA256",
  1873. sha384: "CryptoJS.algo.SHA384",
  1874. sha512: "CryptoJS.algo.SHA512",
  1875. ripemd160: "CryptoJS.algo.RIPEMD160",
  1876. sm3: "CryptoJS.algo.SM3"
  1877. };
  1878. this.getDigestInfoHex = function (a, b) {
  1879. if ("undefined" == typeof this.DIGESTINFOHEAD[b])
  1880. throw "alg not supported in Util.DIGESTINFOHEAD: " + b;
  1881. return this.DIGESTINFOHEAD[b] + a
  1882. };
  1883. this.getPaddedDigestInfoHex = function (a, b, c) {
  1884. var d = this.getDigestInfoHex(a, b);
  1885. a = c / 4;
  1886. if (d.length + 22 > a)
  1887. throw "key is too short for SigAlg: keylen=" + c + "," + b;
  1888. b = "00" + d;
  1889. c = "";
  1890. a = a - 4 - b.length;
  1891. for (d = 0; d < a; d += 2)
  1892. c += "ff";
  1893. return "0001" + c + b
  1894. };
  1895. this.hashString = function (a, b) {
  1896. return (new KJUR.crypto.MessageDigest({
  1897. alg: b
  1898. })).digestString(a)
  1899. };
  1900. this.hashHex = function (a, b) {
  1901. return (new KJUR.crypto.MessageDigest({
  1902. alg: b
  1903. })).digestHex(a)
  1904. };
  1905. this.sha1 = function (a) {
  1906. return (new KJUR.crypto.MessageDigest({
  1907. alg: "sha1",
  1908. prov: "cryptojs"
  1909. })).digestString(a)
  1910. };
  1911. this.sha256 = function (a) {
  1912. return (new KJUR.crypto.MessageDigest({
  1913. alg: "sha256",
  1914. prov: "cryptojs"
  1915. })).digestString(a)
  1916. };
  1917. this.sha256Hex = function (a) {
  1918. return (new KJUR.crypto.MessageDigest({
  1919. alg: "sha256",
  1920. prov: "cryptojs"
  1921. })).digestHex(a)
  1922. };
  1923. this.sha512 = function (a) {
  1924. return (new KJUR.crypto.MessageDigest({
  1925. alg: "sha512",
  1926. prov: "cryptojs"
  1927. })).digestString(a)
  1928. };
  1929. this.sha512Hex = function (a) {
  1930. return (new KJUR.crypto.MessageDigest({
  1931. alg: "sha512",
  1932. prov: "cryptojs"
  1933. })).digestHex(a)
  1934. };
  1935. this.md5 = function (a) {
  1936. return (new KJUR.crypto.MessageDigest({
  1937. alg: "md5",
  1938. prov: "cryptojs"
  1939. })).digestString(a)
  1940. };
  1941. this.ripemd160 = function (a) {
  1942. return (new KJUR.crypto.MessageDigest({
  1943. alg: "ripemd160",
  1944. prov: "cryptojs"
  1945. })).digestString(a)
  1946. };
  1947. this.getCryptoJSMDByName = function (a) {}
  1948. };
  1949. KJUR.crypto.MessageDigest = function (a) {
  1950. this.setAlgAndProvider = function (a, c) {
  1951. null != a && void 0 === c && (c = KJUR.crypto.Util.DEFAULTPROVIDER[a]);
  1952. if (-1 != ":md5:sha1:sha224:sha256:sha384:sha512:ripemd160:sm3:".indexOf(a) && "cryptojs" == c) {
  1953. try {
  1954. this.md = eval(KJUR.crypto.Util.CRYPTOJSMESSAGEDIGESTNAME[a]).create()
  1955. } catch (d) {
  1956. throw "setAlgAndProvider hash alg set fail alg=" + a + "/" + d;
  1957. }
  1958. this.updateString = function (a) {
  1959. this.md.update(a)
  1960. };
  1961. this.updateHex = function (a) {
  1962. a = CryptoJS.enc.Hex.parse(a);
  1963. this.md.update(a)
  1964. };
  1965. this.digest = function () {
  1966. return this.md.finalize().toString(CryptoJS.enc.Hex)
  1967. };
  1968. this.digestString = function (a) {
  1969. this.updateString(a);
  1970. return this.digest()
  1971. };
  1972. this.digestHex = function (a) {
  1973. this.updateHex(a);
  1974. return this.digest()
  1975. }
  1976. }
  1977. if (-1 != ":sha256:".indexOf(a) && "sjcl" == c) {
  1978. try {
  1979. this.md = new sjcl.hash.sha256
  1980. } catch (d) {
  1981. throw "setAlgAndProvider hash alg set fail alg=" + a + "/" + d;
  1982. }
  1983. this.updateString = function (a) {
  1984. this.md.update(a)
  1985. };
  1986. this.updateHex = function (a) {
  1987. a = sjcl.codec.hex.toBits(a);
  1988. this.md.update(a)
  1989. };
  1990. this.digest = function () {
  1991. var a = this.md.finalize();
  1992. return sjcl.codec.hex.fromBits(a)
  1993. };
  1994. this.digestString = function (a) {
  1995. this.updateString(a);
  1996. return this.digest()
  1997. };
  1998. this.digestHex = function (a) {
  1999. this.updateHex(a);
  2000. return this.digest()
  2001. }
  2002. }
  2003. };
  2004. this.updateString = function (a) {
  2005. throw "updateString(str) not supported for this alg/prov: " + this.algName + "/" + this.provName;
  2006. };
  2007. this.updateHex = function (a) {
  2008. throw "updateHex(hex) not supported for this alg/prov: " + this.algName + "/" + this.provName;
  2009. };
  2010. this.digest = function () {
  2011. throw "digest() not supported for this alg/prov: " + this.algName + "/" + this.provName;
  2012. };
  2013. this.digestString = function (a) {
  2014. throw "digestString(str) not supported for this alg/prov: " + this.algName + "/" + this.provName;
  2015. };
  2016. this.digestHex = function (a) {
  2017. throw "digestHex(hex) not supported for this alg/prov: " + this.algName + "/" + this.provName;
  2018. };
  2019. void 0 !== a && void 0 !== a.alg && (this.algName = a.alg,
  2020. void 0 === a.prov && (this.provName = KJUR.crypto.Util.DEFAULTPROVIDER[this.algName]),
  2021. this.setAlgAndProvider(this.algName, this.provName))
  2022. };
  2023. KJUR.crypto.Mac = function (a) {
  2024. this.setAlgAndProvider = function (a, c) {
  2025. null == a && (a = "hmacsha1");
  2026. a = a.toLowerCase();
  2027. if ("hmac" != a.substr(0, 4))
  2028. throw "setAlgAndProvider unsupported HMAC alg: " + a;
  2029. void 0 === c && (c = KJUR.crypto.Util.DEFAULTPROVIDER[a]);
  2030. this.algProv = a + "/" + c;
  2031. var d = a.substr(4);
  2032. if (-1 != ":md5:sha1:sha224:sha256:sha384:sha512:ripemd160:".indexOf(d) && "cryptojs" == c) {
  2033. try {
  2034. var e = eval(KJUR.crypto.Util.CRYPTOJSMESSAGEDIGESTNAME[d]);
  2035. this.mac = CryptoJS.algo.HMAC.create(e, this.pass)
  2036. } catch (f) {
  2037. throw "setAlgAndProvider hash alg set fail hashAlg=" + d + "/" + f;
  2038. }
  2039. this.updateString = function (a) {
  2040. this.mac.update(a)
  2041. };
  2042. this.updateHex = function (a) {
  2043. a = CryptoJS.enc.Hex.parse(a);
  2044. this.mac.update(a)
  2045. };
  2046. this.doFinal = function () {
  2047. return this.mac.finalize().toString(CryptoJS.enc.Hex)
  2048. };
  2049. this.doFinalString = function (a) {
  2050. this.updateString(a);
  2051. return this.doFinal()
  2052. };
  2053. this.doFinalHex = function (a) {
  2054. this.updateHex(a);
  2055. return this.doFinal()
  2056. }
  2057. }
  2058. };
  2059. this.updateString = function (a) {
  2060. throw "updateString(str) not supported for this alg/prov: " + this.algProv;
  2061. };
  2062. this.updateHex = function (a) {
  2063. throw "updateHex(hex) not supported for this alg/prov: " + this.algProv;
  2064. };
  2065. this.doFinal = function () {
  2066. throw "digest() not supported for this alg/prov: " + this.algProv;
  2067. };
  2068. this.doFinalString = function (a) {
  2069. throw "digestString(str) not supported for this alg/prov: " + this.algProv;
  2070. };
  2071. this.doFinalHex = function (a) {
  2072. throw "digestHex(hex) not supported for this alg/prov: " + this.algProv;
  2073. };
  2074. void 0 !== a && (void 0 !== a.pass && (this.pass = a.pass),
  2075. void 0 !== a.alg && (this.algName = a.alg,
  2076. void 0 === a.prov && (this.provName = KJUR.crypto.Util.DEFAULTPROVIDER[this.algName]),
  2077. this.setAlgAndProvider(this.algName, this.provName)))
  2078. };
  2079. KJUR.crypto.Signature = function (a) {
  2080. var b = null;
  2081. this._setAlgNames = function () {
  2082. this.algName.match(/^(.+)with(.+)$/) && (this.mdAlgName = RegExp.$1.toLowerCase(),
  2083. this.pubkeyAlgName = RegExp.$2.toLowerCase())
  2084. };
  2085. this._zeroPaddingOfSignature = function (a, b) {
  2086. for (var e = "", f = b / 4 - a.length, g = 0; g < f; g++)
  2087. e += "0";
  2088. return e + a
  2089. };
  2090. this.setAlgAndProvider = function (a, b) {
  2091. this._setAlgNames();
  2092. if ("cryptojs/jsrsa" != b)
  2093. throw "provider not supported: " + b;
  2094. if (-1 != ":md5:sha1:sha224:sha256:sha384:sha512:ripemd160:sm3:".indexOf(this.mdAlgName)) {
  2095. try {
  2096. this.md = new KJUR.crypto.MessageDigest({
  2097. alg: this.mdAlgName
  2098. })
  2099. } catch (e) {
  2100. throw "setAlgAndProvider hash alg set fail alg=" + this.mdAlgName + "/" + e;
  2101. }
  2102. this.init = function (a, c) {
  2103. var b = null;
  2104. try {
  2105. b = void 0 === c ? KEYUTIL.getKey(a) : KEYUTIL.getKey(a, c)
  2106. } catch (d) {
  2107. throw "init failed:" + d;
  2108. }
  2109. if (!0 === b.isPrivate)
  2110. this.prvKey = b,
  2111. this.state = "SIGN";
  2112. else if (!0 === b.isPublic)
  2113. this.pubKey = b,
  2114. this.state = "VERIFY";
  2115. else
  2116. throw "init failed.:" + b;
  2117. };
  2118. this.initSign = function (a) {
  2119. "string" == typeof a.ecprvhex && "string" == typeof a.eccurvename ? (this.ecprvhex = a.ecprvhex,
  2120. this.eccurvename = a.eccurvename) : this.prvKey = a;
  2121. this.state = "SIGN"
  2122. };
  2123. this.initVerifyByPublicKey = function (a) {
  2124. "string" == typeof a.ecpubhex && "string" == typeof a.eccurvename ? (this.ecpubhex = a.ecpubhex,
  2125. this.eccurvename = a.eccurvename) : a instanceof KJUR.crypto.ECDSA ? this.pubKey = a : a instanceof RSAKey && (this.pubKey = a);
  2126. this.state = "VERIFY"
  2127. };
  2128. this.initVerifyByCertificatePEM = function (a) {
  2129. var c = new X509;
  2130. c.readCertPEM(a);
  2131. this.pubKey = c.subjectPublicKeyRSA;
  2132. this.state = "VERIFY"
  2133. };
  2134. this.updateString = function (a) {
  2135. this.md.updateString(a)
  2136. };
  2137. this.updateHex = function (a) {
  2138. this.md.updateHex(a)
  2139. };
  2140. this.sign = function () {
  2141. "sm2" != this.eccurvename && (this.sHashHex = this.md.digest());
  2142. if ("undefined" != typeof this.ecprvhex && "undefined" != typeof this.eccurvename) {
  2143. if ("sm2" == this.eccurvename) {
  2144. var a = new KJUR.crypto.SM3withSM2({
  2145. curve: this.eccurvename
  2146. }),
  2147. c = a.ecparams.G,
  2148. b = c.multiply(new BigInteger(this.ecprvhex, 16)),
  2149. d = b.getX().toBigInteger().toRadix(16) + b.getY().toBigInteger().toRadix(16),
  2150. b = new SM3Digest,
  2151. c = (new SM3Digest).GetZ(c, d),
  2152. c = b.GetWords(b.GetHex(c).toString()),
  2153. d = CryptoJS.enc.Utf8.stringify(this.md.md._data),
  2154. d = CryptoJS.enc.Utf8.parse(d).toString(),
  2155. d = b.GetWords(d),
  2156. k = Array(b.GetDigestSize());
  2157. b.BlockUpdate(c, 0, c.length);
  2158. b.BlockUpdate(d, 0, d.length);
  2159. b.DoFinal(k, 0);
  2160. this.sHashHex = b.GetHex(k).toString()
  2161. } else
  2162. a = new KJUR.crypto.ECDSA({
  2163. curve: this.eccurvename
  2164. });
  2165. this.hSign = a.signHex(this.sHashHex, this.ecprvhex)
  2166. } else if ("rsaandmgf1" == this.pubkeyAlgName)
  2167. this.hSign = this.prvKey.signWithMessageHashPSS(this.sHashHex, this.mdAlgName, this.pssSaltLen);
  2168. else if ("rsa" == this.pubkeyAlgName)
  2169. this.hSign = this.prvKey.signWithMessageHash(this.sHashHex, this.mdAlgName);
  2170. else if (this.prvKey instanceof KJUR.crypto.ECDSA)
  2171. this.hSign = this.prvKey.signWithMessageHash(this.sHashHex);
  2172. else if (this.prvKey instanceof KJUR.crypto.DSA)
  2173. this.hSign = this.prvKey.signWithMessageHash(this.sHashHex);
  2174. else
  2175. throw "Signature: unsupported public key alg: " + this.pubkeyAlgName;
  2176. return this.hSign
  2177. };
  2178. this.signString = function (a) {
  2179. this.updateString(a);
  2180. this.sign()
  2181. };
  2182. this.signHex = function (a) {
  2183. this.updateHex(a);
  2184. this.sign()
  2185. };
  2186. this.verify = function (a) {
  2187. "sm2" != this.eccurvename && (this.sHashHex = this.md.digest());
  2188. if ("undefined" != typeof this.ecpubhex && "undefined" != typeof this.eccurvename) {
  2189. if ("sm2" == this.eccurvename) {
  2190. var c = new KJUR.crypto.SM3withSM2({
  2191. curve: this.eccurvename
  2192. }),
  2193. b = c.ecparams.G,
  2194. d = this.ecpubhex.substr(2, 128),
  2195. k = new SM3Digest,
  2196. b = (new SM3Digest).GetZ(b, d),
  2197. b = k.GetWords(k.GetHex(b).toString()),
  2198. d = CryptoJS.enc.Utf8.stringify(this.md.md._data),
  2199. d = CryptoJS.enc.Utf8.parse(d).toString(),
  2200. d = k.GetWords(d),
  2201. l = Array(k.GetDigestSize());
  2202. k.BlockUpdate(b, 0, b.length);
  2203. k.BlockUpdate(d, 0, d.length);
  2204. k.DoFinal(l, 0);
  2205. this.sHashHex = k.GetHex(l).toString()
  2206. } else
  2207. c = new KJUR.crypto.ECDSA({
  2208. curve: this.eccurvename
  2209. });
  2210. return c.verifyHex(this.sHashHex, a, this.ecpubhex)
  2211. }
  2212. if ("rsaandmgf1" == this.pubkeyAlgName)
  2213. return this.pubKey.verifyWithMessageHashPSS(this.sHashHex, a, this.mdAlgName, this.pssSaltLen);
  2214. if ("rsa" == this.pubkeyAlgName || this.pubKey instanceof KJUR.crypto.ECDSA || this.pubKey instanceof KJUR.crypto.DSA)
  2215. return this.pubKey.verifyWithMessageHash(this.sHashHex, a);
  2216. throw "Signature: unsupported public key alg: " + this.pubkeyAlgName;
  2217. }
  2218. }
  2219. };
  2220. this.init = function (a, b) {
  2221. throw "init(key, pass) not supported for this alg:prov=" + this.algProvName;
  2222. };
  2223. this.initVerifyByPublicKey = function (a) {
  2224. throw "initVerifyByPublicKey(rsaPubKeyy) not supported for this alg:prov=" + this.algProvName;
  2225. };
  2226. this.initVerifyByCertificatePEM = function (a) {
  2227. throw "initVerifyByCertificatePEM(certPEM) not supported for this alg:prov=" + this.algProvName;
  2228. };
  2229. this.initSign = function (a) {
  2230. throw "initSign(prvKey) not supported for this alg:prov=" + this.algProvName;
  2231. };
  2232. this.updateString = function (a) {
  2233. throw "updateString(str) not supported for this alg:prov=" + this.algProvName;
  2234. };
  2235. this.updateHex = function (a) {
  2236. throw "updateHex(hex) not supported for this alg:prov=" + this.algProvName;
  2237. };
  2238. this.sign = function () {
  2239. throw "sign() not supported for this alg:prov=" + this.algProvName;
  2240. };
  2241. this.signString = function (a) {
  2242. throw "digestString(str) not supported for this alg:prov=" + this.algProvName;
  2243. };
  2244. this.signHex = function (a) {
  2245. throw "digestHex(hex) not supported for this alg:prov=" + this.algProvName;
  2246. };
  2247. this.verify = function (a) {
  2248. throw "verify(hSigVal) not supported for this alg:prov=" + this.algProvName;
  2249. };
  2250. this.initParams = a;
  2251. if (void 0 !== a && (void 0 !== a.alg && (this.algName = a.alg,
  2252. this.provName = void 0 === a.prov ? KJUR.crypto.Util.DEFAULTPROVIDER[this.algName] : a.prov,
  2253. this.algProvName = this.algName + ":" + this.provName,
  2254. this.setAlgAndProvider(this.algName, this.provName),
  2255. this._setAlgNames()),
  2256. void 0 !== a.psssaltlen && (this.pssSaltLen = a.psssaltlen),
  2257. void 0 !== a.prvkeypem)) {
  2258. if (void 0 !== a.prvkeypas)
  2259. throw "both prvkeypem and prvkeypas parameters not supported";
  2260. try {
  2261. b = new RSAKey,
  2262. b.readPrivateKeyFromPEMString(a.prvkeypem),
  2263. this.initSign(b)
  2264. } catch (c) {
  2265. throw "fatal error to load pem private key: " + c;
  2266. }
  2267. }
  2268. };
  2269. KJUR.crypto.OID = new function () {
  2270. this.oidhex2name = {
  2271. "2a864886f70d010101": "rsaEncryption",
  2272. "2a8648ce3d0201": "ecPublicKey",
  2273. "2a8648ce380401": "dsa",
  2274. "2a8648ce3d030107": "secp256r1",
  2275. "2b8104001f": "secp192k1",
  2276. "2b81040021": "secp224r1",
  2277. "2b8104000a": "secp256k1",
  2278. "2b81040023": "secp521r1",
  2279. "2b81040022": "secp384r1",
  2280. "2a8648ce380403": "SHA1withDSA",
  2281. "608648016503040301": "SHA224withDSA",
  2282. "608648016503040302": "SHA256withDSA"
  2283. }
  2284. };
  2285. function ECFieldElementFp(a, b) {
  2286. this.x = b;
  2287. this.q = a
  2288. }
  2289. function feFpEquals(a) {
  2290. return a == this ? !0 : this.q.equals(a.q) && this.x.equals(a.x)
  2291. }
  2292. function feFpToBigInteger() {
  2293. return this.x
  2294. }
  2295. function feFpNegate() {
  2296. return new ECFieldElementFp(this.q, this.x.negate().mod(this.q))
  2297. }
  2298. function feFpAdd(a) {
  2299. return new ECFieldElementFp(this.q, this.x.add(a.toBigInteger()).mod(this.q))
  2300. }
  2301. function feFpSubtract(a) {
  2302. return new ECFieldElementFp(this.q, this.x.subtract(a.toBigInteger()).mod(this.q))
  2303. }
  2304. function feFpMultiply(a) {
  2305. return new ECFieldElementFp(this.q, this.x.multiply(a.toBigInteger()).mod(this.q))
  2306. }
  2307. function feFpSquare() {
  2308. return new ECFieldElementFp(this.q, this.x.square().mod(this.q))
  2309. }
  2310. function feFpDivide(a) {
  2311. return new ECFieldElementFp(this.q, this.x.multiply(a.toBigInteger().modInverse(this.q)).mod(this.q))
  2312. }
  2313. ECFieldElementFp.prototype.equals = feFpEquals;
  2314. ECFieldElementFp.prototype.toBigInteger = feFpToBigInteger;
  2315. ECFieldElementFp.prototype.negate = feFpNegate;
  2316. ECFieldElementFp.prototype.add = feFpAdd;
  2317. ECFieldElementFp.prototype.subtract = feFpSubtract;
  2318. ECFieldElementFp.prototype.multiply = feFpMultiply;
  2319. ECFieldElementFp.prototype.square = feFpSquare;
  2320. ECFieldElementFp.prototype.divide = feFpDivide;
  2321. function ECPointFp(a, b, c, d) {
  2322. this.curve = a;
  2323. this.x = b;
  2324. this.y = c;
  2325. this.z = null == d ? BigInteger.ONE : d;
  2326. this.zinv = null
  2327. }
  2328. function pointFpGetX() {
  2329. null == this.zinv && (this.zinv = this.z.modInverse(this.curve.q));
  2330. return this.curve.fromBigInteger(this.x.toBigInteger().multiply(this.zinv).mod(this.curve.q))
  2331. }
  2332. function pointFpGetY() {
  2333. null == this.zinv && (this.zinv = this.z.modInverse(this.curve.q));
  2334. return this.curve.fromBigInteger(this.y.toBigInteger().multiply(this.zinv).mod(this.curve.q))
  2335. }
  2336. function pointFpEquals(a) {
  2337. return a == this ? !0 : this.isInfinity() ? a.isInfinity() : a.isInfinity() ? this.isInfinity() : a.y.toBigInteger().multiply(this.z).subtract(this.y.toBigInteger().multiply(a.z)).mod(this.curve.q).equals(BigInteger.ZERO) ? a.x.toBigInteger().multiply(this.z).subtract(this.x.toBigInteger().multiply(a.z)).mod(this.curve.q).equals(BigInteger.ZERO) : !1
  2338. }
  2339. function pointFpIsInfinity() {
  2340. return null == this.x && null == this.y ? !0 : this.z.equals(BigInteger.ZERO) && !this.y.toBigInteger().equals(BigInteger.ZERO)
  2341. }
  2342. function pointFpNegate() {
  2343. return new ECPointFp(this.curve, this.x, this.y.negate(), this.z)
  2344. }
  2345. function pointFpAdd(a) {
  2346. if (this.isInfinity())
  2347. return a;
  2348. if (a.isInfinity())
  2349. return this;
  2350. var b = a.y.toBigInteger().multiply(this.z).subtract(this.y.toBigInteger().multiply(a.z)).mod(this.curve.q),
  2351. c = a.x.toBigInteger().multiply(this.z).subtract(this.x.toBigInteger().multiply(a.z)).mod(this.curve.q);
  2352. if (BigInteger.ZERO.equals(c))
  2353. return BigInteger.ZERO.equals(b) ? this.twice() : this.curve.getInfinity();
  2354. var d = new BigInteger("3"),
  2355. e = this.x.toBigInteger(),
  2356. f = this.y.toBigInteger();
  2357. a.x.toBigInteger();
  2358. a.y.toBigInteger();
  2359. var g = c.square(),
  2360. h = g.multiply(c),
  2361. e = e.multiply(g),
  2362. g = b.square().multiply(this.z),
  2363. c = g.subtract(e.shiftLeft(1)).multiply(a.z).subtract(h).multiply(c).mod(this.curve.q),
  2364. b = e.multiply(d).multiply(b).subtract(f.multiply(h)).subtract(g.multiply(b)).multiply(a.z).add(b.multiply(h)).mod(this.curve.q);
  2365. a = h.multiply(this.z).multiply(a.z).mod(this.curve.q);
  2366. return new ECPointFp(this.curve, this.curve.fromBigInteger(c), this.curve.fromBigInteger(b), a)
  2367. }
  2368. function pointFpTwice() {
  2369. if (this.isInfinity())
  2370. return this;
  2371. if (0 == this.y.toBigInteger().signum())
  2372. return this.curve.getInfinity();
  2373. var a = new BigInteger("3"),
  2374. b = this.x.toBigInteger(),
  2375. c = this.y.toBigInteger(),
  2376. d = c.multiply(this.z),
  2377. e = d.multiply(c).mod(this.curve.q),
  2378. c = this.curve.a.toBigInteger(),
  2379. f = b.square().multiply(a);
  2380. BigInteger.ZERO.equals(c) || (f = f.add(this.z.square().multiply(c)));
  2381. f = f.mod(this.curve.q);
  2382. c = f.square().subtract(b.shiftLeft(3).multiply(e)).shiftLeft(1).multiply(d).mod(this.curve.q);
  2383. a = f.multiply(a).multiply(b).subtract(e.shiftLeft(1)).shiftLeft(2).multiply(e).subtract(f.square().multiply(f)).mod(this.curve.q);
  2384. d = d.square().multiply(d).shiftLeft(3).mod(this.curve.q);
  2385. return new ECPointFp(this.curve, this.curve.fromBigInteger(c), this.curve.fromBigInteger(a), d)
  2386. }
  2387. function pointFpMultiply(a) {
  2388. if (this.isInfinity())
  2389. return this;
  2390. if (0 == a.signum())
  2391. return this.curve.getInfinity();
  2392. var b = a.multiply(new BigInteger("3")),
  2393. c = this.negate(),
  2394. d = this,
  2395. e;
  2396. for (e = b.bitLength() - 2; 0 < e; --e) {
  2397. var d = d.twice(),
  2398. f = b.testBit(e),
  2399. g = a.testBit(e);
  2400. f != g && (d = d.add(f ? this : c))
  2401. }
  2402. return d
  2403. }
  2404. function pointFpMultiplyTwo(a, b, c) {
  2405. var d;
  2406. d = a.bitLength() > c.bitLength() ? a.bitLength() - 1 : c.bitLength() - 1;
  2407. for (var e = this.curve.getInfinity(), f = this.add(b); 0 <= d;)
  2408. e = e.twice(),
  2409. a.testBit(d) ? e = c.testBit(d) ? e.add(f) : e.add(this) : c.testBit(d) && (e = e.add(b)),
  2410. --d;
  2411. return e
  2412. }
  2413. ECPointFp.prototype.getX = pointFpGetX;
  2414. ECPointFp.prototype.getY = pointFpGetY;
  2415. ECPointFp.prototype.equals = pointFpEquals;
  2416. ECPointFp.prototype.isInfinity = pointFpIsInfinity;
  2417. ECPointFp.prototype.negate = pointFpNegate;
  2418. ECPointFp.prototype.add = pointFpAdd;
  2419. ECPointFp.prototype.twice = pointFpTwice;
  2420. ECPointFp.prototype.multiply = pointFpMultiply;
  2421. ECPointFp.prototype.multiplyTwo = pointFpMultiplyTwo;
  2422. function ECCurveFp(a, b, c) {
  2423. this.q = a;
  2424. this.a = this.fromBigInteger(b);
  2425. this.b = this.fromBigInteger(c);
  2426. this.infinity = new ECPointFp(this, null, null)
  2427. }
  2428. function curveFpGetQ() {
  2429. return this.q
  2430. }
  2431. function curveFpGetA() {
  2432. return this.a
  2433. }
  2434. function curveFpGetB() {
  2435. return this.b
  2436. }
  2437. function curveFpEquals(a) {
  2438. return a == this ? !0 : this.q.equals(a.q) && this.a.equals(a.a) && this.b.equals(a.b)
  2439. }
  2440. function curveFpGetInfinity() {
  2441. return this.infinity
  2442. }
  2443. function curveFpFromBigInteger(a) {
  2444. return new ECFieldElementFp(this.q, a)
  2445. }
  2446. function curveFpDecodePointHex(a) {
  2447. switch (parseInt(a.substr(0, 2), 16)) {
  2448. case 0:
  2449. return this.infinity;
  2450. case 2:
  2451. case 3:
  2452. return null;
  2453. case 4:
  2454. case 6:
  2455. case 7:
  2456. var b = (a.length - 2) / 2,
  2457. c = a.substr(2, b);
  2458. a = a.substr(b + 2, b);
  2459. return new ECPointFp(this, this.fromBigInteger(new BigInteger(c, 16)), this.fromBigInteger(new BigInteger(a, 16)));
  2460. default:
  2461. return null
  2462. }
  2463. }
  2464. ECCurveFp.prototype.getQ = curveFpGetQ;
  2465. ECCurveFp.prototype.getA = curveFpGetA;
  2466. ECCurveFp.prototype.getB = curveFpGetB;
  2467. ECCurveFp.prototype.equals = curveFpEquals;
  2468. ECCurveFp.prototype.getInfinity = curveFpGetInfinity;
  2469. ECCurveFp.prototype.fromBigInteger = curveFpFromBigInteger;
  2470. ECCurveFp.prototype.decodePointHex = curveFpDecodePointHex;
  2471. ECFieldElementFp.prototype.getByteLength = function () {
  2472. return Math.floor((this.toBigInteger().bitLength() + 7) / 8)
  2473. };
  2474. ECPointFp.prototype.getEncoded = function (a) {
  2475. var b = function (a, c) {
  2476. var b = a.toByteArrayUnsigned();
  2477. if (c < b.length)
  2478. b = b.slice(b.length - c);
  2479. else
  2480. for (; c > b.length;)
  2481. b.unshift(0);
  2482. return b
  2483. },
  2484. c = this.getX().toBigInteger(),
  2485. d = this.getY().toBigInteger(),
  2486. c = b(c, 32);
  2487. a ? d.isEven() ? c.unshift(2) : c.unshift(3) : (c.unshift(4),
  2488. c = c.concat(b(d, 32)));
  2489. return c
  2490. };
  2491. ECPointFp.decodeFrom = function (a, b) {
  2492. var c = b.length - 1,
  2493. d = b.slice(1, 1 + c / 2),
  2494. c = b.slice(1 + c / 2, 1 + c);
  2495. d.unshift(0);
  2496. c.unshift(0);
  2497. d = new BigInteger(d);
  2498. c = new BigInteger(c);
  2499. return new ECPointFp(a, a.fromBigInteger(d), a.fromBigInteger(c))
  2500. };
  2501. ECPointFp.decodeFromHex = function (a, b) {
  2502. b.substr(0, 2);
  2503. var c = b.length - 2,
  2504. d = b.substr(2, c / 2),
  2505. c = b.substr(2 + c / 2, c / 2),
  2506. d = new BigInteger(d, 16),
  2507. c = new BigInteger(c, 16);
  2508. return new ECPointFp(a, a.fromBigInteger(d), a.fromBigInteger(c))
  2509. };
  2510. ECPointFp.prototype.add2D = function (a) {
  2511. if (this.isInfinity())
  2512. return a;
  2513. if (a.isInfinity())
  2514. return this;
  2515. if (this.x.equals(a.x))
  2516. return this.y.equals(a.y) ? this.twice() : this.curve.getInfinity();
  2517. var b = a.x.subtract(this.x),
  2518. b = a.y.subtract(this.y).divide(b);
  2519. a = b.square().subtract(this.x).subtract(a.x);
  2520. b = b.multiply(this.x.subtract(a)).subtract(this.y);
  2521. return new ECPointFp(this.curve, a, b)
  2522. };
  2523. ECPointFp.prototype.twice2D = function () {
  2524. if (this.isInfinity())
  2525. return this;
  2526. if (0 == this.y.toBigInteger().signum())
  2527. return this.curve.getInfinity();
  2528. var a = this.curve.fromBigInteger(BigInteger.valueOf(2)),
  2529. b = this.curve.fromBigInteger(BigInteger.valueOf(3)),
  2530. b = this.x.square().multiply(b).add(this.curve.a).divide(this.y.multiply(a)),
  2531. a = b.square().subtract(this.x.multiply(a)),
  2532. b = b.multiply(this.x.subtract(a)).subtract(this.y);
  2533. return new ECPointFp(this.curve, a, b)
  2534. };
  2535. ECPointFp.prototype.multiply2D = function (a) {
  2536. if (this.isInfinity())
  2537. return this;
  2538. if (0 == a.signum())
  2539. return this.curve.getInfinity();
  2540. var b = a.multiply(new BigInteger("3")),
  2541. c = this.negate(),
  2542. d = this,
  2543. e;
  2544. for (e = b.bitLength() - 2; 0 < e; --e) {
  2545. var d = d.twice(),
  2546. f = b.testBit(e),
  2547. g = a.testBit(e);
  2548. f != g && (d = d.add2D(f ? this : c))
  2549. }
  2550. return d
  2551. };
  2552. ECPointFp.prototype.isOnCurve = function () {
  2553. var a = this.getX().toBigInteger(),
  2554. b = this.getY().toBigInteger(),
  2555. c = this.curve.getA().toBigInteger(),
  2556. d = this.curve.getB().toBigInteger(),
  2557. e = this.curve.getQ(),
  2558. b = b.multiply(b).mod(e),
  2559. a = a.multiply(a).multiply(a).add(c.multiply(a)).add(d).mod(e);
  2560. return b.equals(a)
  2561. };
  2562. ECPointFp.prototype.toString = function () {
  2563. return "(" + this.getX().toBigInteger().toString() + "," + this.getY().toBigInteger().toString() + ")"
  2564. };
  2565. ECPointFp.prototype.validate = function () {
  2566. var a = this.curve.getQ();
  2567. if (this.isInfinity())
  2568. throw Error("Point is at infinity.");
  2569. var b = this.getX().toBigInteger(),
  2570. c = this.getY().toBigInteger();
  2571. if (0 > b.compareTo(BigInteger.ONE) || 0 < b.compareTo(a.subtract(BigInteger.ONE)))
  2572. throw Error("x coordinate out of bounds");
  2573. if (0 > c.compareTo(BigInteger.ONE) || 0 < c.compareTo(a.subtract(BigInteger.ONE)))
  2574. throw Error("y coordinate out of bounds");
  2575. if (!this.isOnCurve())
  2576. throw Error("Point is not on the curve.");
  2577. if (this.multiply(a).isInfinity())
  2578. throw Error("Point is not a scalar multiple of G.");
  2579. return !0
  2580. };
  2581. "undefined" != typeof KJUR && KJUR || (KJUR = {});
  2582. "undefined" != typeof KJUR.crypto && KJUR.crypto || (KJUR.crypto = {});
  2583. KJUR.crypto.ECDSA = function (a) {
  2584. var b = new SecureRandom;
  2585. this.type = "EC";
  2586. this.getBigRandom = function (a) {
  2587. return (new BigInteger(a.bitLength(), b)).mod(a.subtract(BigInteger.ONE)).add(BigInteger.ONE)
  2588. };
  2589. this.setNamedCurve = function (a) {
  2590. this.ecparams = KJUR.crypto.ECParameterDB.getByName(a);
  2591. this.pubKeyHex = this.prvKeyHex = null;
  2592. this.curveName = a
  2593. };
  2594. this.setPrivateKeyHex = function (a) {
  2595. this.isPrivate = !0;
  2596. this.prvKeyHex = a
  2597. };
  2598. this.setPublicKeyHex = function (a) {
  2599. this.isPublic = !0;
  2600. this.pubKeyHex = a
  2601. };
  2602. this.generateKeyPairHex = function () {
  2603. var a = this.getBigRandom(this.ecparams.n),
  2604. b = this.ecparams.G.multiply(a),
  2605. e = b.getX().toBigInteger(),
  2606. b = b.getY().toBigInteger(),
  2607. f = this.ecparams.keylen / 4,
  2608. a = ("0000000000" + a.toString(16)).slice(-f),
  2609. e = ("0000000000" + e.toString(16)).slice(-f),
  2610. b = ("0000000000" + b.toString(16)).slice(-f),
  2611. e = "04" + e + b;
  2612. this.setPrivateKeyHex(a);
  2613. this.setPublicKeyHex(e);
  2614. return {
  2615. ecprvhex: a,
  2616. ecpubhex: e
  2617. }
  2618. };
  2619. this.signWithMessageHash = function (a) {
  2620. return this.signHex(a, this.prvKeyHex)
  2621. };
  2622. this.signHex = function (a, b) {
  2623. var e = new BigInteger(b, 16),
  2624. f = this.ecparams.n,
  2625. g = new BigInteger(a, 16);
  2626. do
  2627. var h = this.getBigRandom(f),
  2628. k = this.ecparams.G.multiply(h).getX().toBigInteger().mod(f);
  2629. while (0 >= k.compareTo(BigInteger.ZERO));
  2630. e = h.modInverse(f).multiply(g.add(e.multiply(k))).mod(f);
  2631. return KJUR.crypto.ECDSA.biRSSigToASN1Sig(k, e)
  2632. };
  2633. this.sign = function (a, b) {
  2634. var e = this.ecparams.n,
  2635. f = BigInteger.fromByteArrayUnsigned(a);
  2636. do
  2637. var g = this.getBigRandom(e),
  2638. h = this.ecparams.G.multiply(g).getX().toBigInteger().mod(e);
  2639. while (0 >= h.compareTo(BigInteger.ZERO));
  2640. e = g.modInverse(e).multiply(f.add(b.multiply(h))).mod(e);
  2641. return this.serializeSig(h, e)
  2642. };
  2643. this.verifyWithMessageHash = function (a, b) {
  2644. return this.verifyHex(a, b, this.pubKeyHex)
  2645. };
  2646. this.verifyHex = function (a, b, e) {
  2647. var f;
  2648. f = KJUR.crypto.ECDSA.parseSigHex(b);
  2649. b = f.r;
  2650. f = f.s;
  2651. e = ECPointFp.decodeFromHex(this.ecparams.curve, e);
  2652. a = new BigInteger(a, 16);
  2653. return this.verifyRaw(a, b, f, e)
  2654. };
  2655. this.verify = function (a, b, e) {
  2656. var f;
  2657. if (Bitcoin.Util.isArray(b))
  2658. b = this.parseSig(b),
  2659. f = b.r,
  2660. b = b.s;
  2661. else if ("object" === typeof b && b.r && b.s)
  2662. f = b.r,
  2663. b = b.s;
  2664. else
  2665. throw "Invalid value for signature";
  2666. if (!(e instanceof ECPointFp))
  2667. if (Bitcoin.Util.isArray(e))
  2668. e = ECPointFp.decodeFrom(this.ecparams.curve, e);
  2669. else
  2670. throw "Invalid format for pubkey value, must be byte array or ECPointFp";
  2671. a = BigInteger.fromByteArrayUnsigned(a);
  2672. return this.verifyRaw(a, f, b, e)
  2673. };
  2674. this.verifyRaw = function (a, b, e, f) {
  2675. var g = this.ecparams.n,
  2676. h = this.ecparams.G;
  2677. if (0 > b.compareTo(BigInteger.ONE) || 0 <= b.compareTo(g) || 0 > e.compareTo(BigInteger.ONE) || 0 <= e.compareTo(g))
  2678. return !1;
  2679. e = e.modInverse(g);
  2680. a = a.multiply(e).mod(g);
  2681. e = b.multiply(e).mod(g);
  2682. return h.multiply(a).add(f.multiply(e)).getX().toBigInteger().mod(g).equals(b)
  2683. };
  2684. this.serializeSig = function (a, b) {
  2685. var e = a.toByteArraySigned(),
  2686. f = b.toByteArraySigned(),
  2687. g = [];
  2688. g.push(2);
  2689. g.push(e.length);
  2690. g = g.concat(e);
  2691. g.push(2);
  2692. g.push(f.length);
  2693. g = g.concat(f);
  2694. g.unshift(g.length);
  2695. g.unshift(48);
  2696. return g
  2697. };
  2698. this.parseSig = function (a) {
  2699. var b;
  2700. if (48 != a[0])
  2701. throw Error("Signature not a valid DERSequence");
  2702. b = 2;
  2703. if (2 != a[b])
  2704. throw Error("First element in signature must be a DERInteger");
  2705. var e = a.slice(b + 2, b + 2 + a[b + 1]);
  2706. b += 2 + a[b + 1];
  2707. if (2 != a[b])
  2708. throw Error("Second element in signature must be a DERInteger");
  2709. a = a.slice(b + 2, b + 2 + a[b + 1]);
  2710. e = BigInteger.fromByteArrayUnsigned(e);
  2711. a = BigInteger.fromByteArrayUnsigned(a);
  2712. return {
  2713. r: e,
  2714. s: a
  2715. }
  2716. };
  2717. this.parseSigCompact = function (a) {
  2718. if (65 !== a.length)
  2719. throw "Signature has the wrong length";
  2720. var b = a[0] - 27;
  2721. if (0 > b || 7 < b)
  2722. throw "Invalid signature type";
  2723. var e = this.ecparams.n,
  2724. f = BigInteger.fromByteArrayUnsigned(a.slice(1, 33)).mod(e);
  2725. a = BigInteger.fromByteArrayUnsigned(a.slice(33, 65)).mod(e);
  2726. return {
  2727. r: f,
  2728. s: a,
  2729. i: b
  2730. }
  2731. };
  2732. void 0 !== a && void 0 !== a.curve && (this.curveName = a.curve);
  2733. void 0 === this.curveName && (this.curveName = "secp256r1");
  2734. this.setNamedCurve(this.curveName);
  2735. void 0 !== a && (void 0 !== a.prv && this.setPrivateKeyHex(a.prv),
  2736. void 0 !== a.pub && this.setPublicKeyHex(a.pub))
  2737. };
  2738. KJUR.crypto.ECDSA.parseSigHex = function (a) {
  2739. var b = KJUR.crypto.ECDSA.parseSigHexInHexRS(a);
  2740. a = new BigInteger(b.r, 16);
  2741. b = new BigInteger(b.s, 16);
  2742. return {
  2743. r: a,
  2744. s: b
  2745. }
  2746. };
  2747. KJUR.crypto.ECDSA.parseSigHexInHexRS = function (a) {
  2748. if ("30" != a.substr(0, 2))
  2749. throw "signature is not a ASN.1 sequence";
  2750. var b = ASN1HEX.getPosArrayOfChildren_AtObj(a, 0);
  2751. if (2 != b.length)
  2752. throw "number of signature ASN.1 sequence elements seem wrong";
  2753. var c = b[0],
  2754. b = b[1];
  2755. if ("02" != a.substr(c, 2))
  2756. throw "1st item of sequene of signature is not ASN.1 integer";
  2757. if ("02" != a.substr(b, 2))
  2758. throw "2nd item of sequene of signature is not ASN.1 integer";
  2759. c = ASN1HEX.getHexOfV_AtObj(a, c);
  2760. a = ASN1HEX.getHexOfV_AtObj(a, b);
  2761. return {
  2762. r: c,
  2763. s: a
  2764. }
  2765. };
  2766. KJUR.crypto.ECDSA.asn1SigToConcatSig = function (a) {
  2767. var b = KJUR.crypto.ECDSA.parseSigHexInHexRS(a);
  2768. a = b.r;
  2769. b = b.s;
  2770. "00" == a.substr(0, 2) && 8 == a.length / 2 * 8 % 128 && (a = a.substr(2));
  2771. "00" == b.substr(0, 2) && 8 == b.length / 2 * 8 % 128 && (b = b.substr(2));
  2772. if (0 != a.length / 2 * 8 % 128)
  2773. throw "unknown ECDSA sig r length error";
  2774. if (0 != b.length / 2 * 8 % 128)
  2775. throw "unknown ECDSA sig s length error";
  2776. return a + b
  2777. };
  2778. KJUR.crypto.ECDSA.concatSigToASN1Sig = function (a) {
  2779. if (0 != a.length / 2 * 8 % 128)
  2780. throw "unknown ECDSA concatinated r-s sig length error";
  2781. var b = a.substr(0, a.length / 2);
  2782. a = a.substr(a.length / 2);
  2783. return KJUR.crypto.ECDSA.hexRSSigToASN1Sig(b, a)
  2784. };
  2785. KJUR.crypto.ECDSA.hexRSSigToASN1Sig = function (a, b) {
  2786. var c = new BigInteger(a, 16),
  2787. d = new BigInteger(b, 16);
  2788. return KJUR.crypto.ECDSA.biRSSigToASN1Sig(c, d)
  2789. };
  2790. KJUR.crypto.ECDSA.biRSSigToASN1Sig = function (a, b) {
  2791. var c = new KJUR.asn1.DERInteger({
  2792. bigint: a
  2793. }),
  2794. d = new KJUR.asn1.DERInteger({
  2795. bigint: b
  2796. });
  2797. return (new KJUR.asn1.DERSequence({
  2798. array: [c, d]
  2799. })).getEncodedHex()
  2800. };
  2801. (function () {
  2802. var a = CryptoJS,
  2803. b = a.lib,
  2804. c = b.WordArray,
  2805. d = b.Hasher,
  2806. e = [],
  2807. b = a.algo.SM3 = d.extend({
  2808. _doReset: function () {
  2809. this._hash = new c.init([1937774191, 1226093241, 388252375, 3666478592, 2842636476, 372324522, 3817729613, 2969243214])
  2810. },
  2811. _doProcessBlock: function (a, b) {
  2812. for (var c = this._hash.words, d = c[0], l = c[1], p = c[2], n = c[3], q = c[4], m = 0; 80 > m; m++) {
  2813. if (16 > m)
  2814. e[m] = a[b + m] | 0;
  2815. else {
  2816. var r = e[m - 3] ^ e[m - 8] ^ e[m - 14] ^ e[m - 16];
  2817. e[m] = r << 1 | r >>> 31
  2818. }
  2819. r = (d << 5 | d >>> 27) + q + e[m];
  2820. r = 20 > m ? r + ((l & p | ~l & n) + 1518500249) : 40 > m ? r + ((l ^ p ^ n) + 1859775393) : 60 > m ? r + ((l & p | l & n | p & n) - 1894007588) : r + ((l ^ p ^ n) - 899497514);
  2821. q = n;
  2822. n = p;
  2823. p = l << 30 | l >>> 2;
  2824. l = d;
  2825. d = r
  2826. }
  2827. c[0] = c[0] + d | 0;
  2828. c[1] = c[1] + l | 0;
  2829. c[2] = c[2] + p | 0;
  2830. c[3] = c[3] + n | 0;
  2831. c[4] = c[4] + q | 0
  2832. },
  2833. _doFinalize: function () {
  2834. var a = this._data,
  2835. b = a.words,
  2836. c = 8 * this._nDataBytes,
  2837. d = 8 * a.sigBytes;
  2838. b[d >>> 5] |= 128 << 24 - d % 32;
  2839. b[(d + 64 >>> 9 << 4) + 14] = Math.floor(c / 4294967296);
  2840. b[(d + 64 >>> 9 << 4) + 15] = c;
  2841. a.sigBytes = 4 * b.length;
  2842. this._process();
  2843. return this._hash
  2844. },
  2845. clone: function () {
  2846. var a = d.clone.call(this);
  2847. a._hash = this._hash.clone();
  2848. return a
  2849. }
  2850. });
  2851. a.SM3 = d._createHelper(b);
  2852. a.HmacSM3 = d._createHmacHelper(b)
  2853. })();
  2854. function SM3Digest() {
  2855. this.BYTE_LENGTH = 64;
  2856. this.xBuf = [];
  2857. this.byteCount = this.xBufOff = 0;
  2858. this.DIGEST_LENGTH = 32;
  2859. this.v0 = [1937774191, 1226093241, 388252375, 3666478592, 2842636476, 372324522, 3817729613, 2969243214];
  2860. this.v0 = [1937774191, 1226093241, 388252375, -628488704, -1452330820, 372324522, -477237683, -1325724082];
  2861. this.v = Array(8);
  2862. this.v_ = Array(8);
  2863. this.X0 = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  2864. this.X = Array(68);
  2865. this.xOff = 0;
  2866. this.T_00_15 = 2043430169;
  2867. this.T_16_63 = 2055708042;
  2868. 0 < arguments.length ? this.InitDigest(arguments[0]) : this.Init()
  2869. }
  2870. SM3Digest.prototype = {
  2871. Init: function () {
  2872. this.xBuf = Array(4);
  2873. this.Reset()
  2874. },
  2875. InitDigest: function (a) {
  2876. this.xBuf = Array(a.xBuf.length);
  2877. Array.Copy(a.xBuf, 0, this.xBuf, 0, a.xBuf.length);
  2878. this.xBufOff = a.xBufOff;
  2879. this.byteCount = a.byteCount;
  2880. Array.Copy(a.X, 0, this.X, 0, a.X.length);
  2881. this.xOff = a.xOff;
  2882. Array.Copy(a.v, 0, this.v, 0, a.v.length)
  2883. },
  2884. GetDigestSize: function () {
  2885. return this.DIGEST_LENGTH
  2886. },
  2887. Reset: function () {
  2888. this.xBufOff = this.byteCount = 0;
  2889. Array.Clear(this.xBuf, 0, this.xBuf.length);
  2890. Array.Copy(this.v0, 0, this.v, 0, this.v0.length);
  2891. this.xOff = 0;
  2892. Array.Copy(this.X0, 0, this.X, 0, this.X0.length)
  2893. },
  2894. GetByteLength: function () {
  2895. return this.BYTE_LENGTH
  2896. },
  2897. ProcessBlock: function () {
  2898. var a, b = this.X,
  2899. c = Array(64);
  2900. for (a = 16; 68 > a; a++)
  2901. b[a] = this.P1(b[a - 16] ^ b[a - 9] ^ this.ROTATE(b[a - 3], 15)) ^ this.ROTATE(b[a - 13], 7) ^ b[a - 6];
  2902. for (a = 0; 64 > a; a++)
  2903. c[a] = b[a] ^ b[a + 4];
  2904. var d = this.v,
  2905. e = this.v_;
  2906. Array.Copy(d, 0, e, 0, this.v0.length);
  2907. var f, g;
  2908. for (a = 0; 16 > a; a++)
  2909. g = this.ROTATE(e[0], 12),
  2910. f = Int32.parse(Int32.parse(g + e[4]) + this.ROTATE(this.T_00_15, a)),
  2911. f = this.ROTATE(f, 7),
  2912. g ^= f,
  2913. g = Int32.parse(Int32.parse(this.FF_00_15(e[0], e[1], e[2]) + e[3]) + g) + c[a],
  2914. f = Int32.parse(Int32.parse(this.GG_00_15(e[4], e[5], e[6]) + e[7]) + f) + b[a],
  2915. e[3] = e[2],
  2916. e[2] = this.ROTATE(e[1], 9),
  2917. e[1] = e[0],
  2918. e[0] = g,
  2919. e[7] = e[6],
  2920. e[6] = this.ROTATE(e[5], 19),
  2921. e[5] = e[4],
  2922. e[4] = this.P0(f);
  2923. for (a = 16; 64 > a; a++)
  2924. g = this.ROTATE(e[0], 12),
  2925. f = Int32.parse(Int32.parse(g + e[4]) + this.ROTATE(this.T_16_63, a)),
  2926. f = this.ROTATE(f, 7),
  2927. g ^= f,
  2928. g = Int32.parse(Int32.parse(this.FF_16_63(e[0], e[1], e[2]) + e[3]) + g) + c[a],
  2929. f = Int32.parse(Int32.parse(this.GG_16_63(e[4], e[5], e[6]) + e[7]) + f) + b[a],
  2930. e[3] = e[2],
  2931. e[2] = this.ROTATE(e[1], 9),
  2932. e[1] = e[0],
  2933. e[0] = g,
  2934. e[7] = e[6],
  2935. e[6] = this.ROTATE(e[5], 19),
  2936. e[5] = e[4],
  2937. e[4] = this.P0(f);
  2938. for (a = 0; 8 > a; a++)
  2939. d[a] ^= Int32.parse(e[a]);
  2940. this.xOff = 0;
  2941. Array.Copy(this.X0, 0, this.X, 0, this.X0.length)
  2942. },
  2943. ProcessWord: function (a, b) {
  2944. var c = a[b] << 24,
  2945. c = c | (a[++b] & 255) << 16,
  2946. c = c | (a[++b] & 255) << 8,
  2947. c = c | a[++b] & 255;
  2948. this.X[this.xOff] = c;
  2949. 16 == ++this.xOff && this.ProcessBlock()
  2950. },
  2951. ProcessLength: function (a) {
  2952. 14 < this.xOff && this.ProcessBlock();
  2953. this.X[14] = this.URShiftLong(a, 32);
  2954. this.X[15] = a & 4294967295
  2955. },
  2956. IntToBigEndian: function (a, b, c) {
  2957. b[c] = Int32.parseByte(this.URShift(a, 24));
  2958. b[++c] = Int32.parseByte(this.URShift(a, 16));
  2959. b[++c] = Int32.parseByte(this.URShift(a, 8));
  2960. b[++c] = Int32.parseByte(a)
  2961. },
  2962. DoFinal: function (a, b) {
  2963. this.Finish();
  2964. for (var c = 0; 8 > c; c++)
  2965. this.IntToBigEndian(this.v[c], a, b + 4 * c);
  2966. this.Reset();
  2967. for (var d = a.length, c = 0; c < d; c++)
  2968. a[c] &= 255;
  2969. return this.DIGEST_LENGTH
  2970. },
  2971. Update: function (a) {
  2972. this.xBuf[this.xBufOff++] = a;
  2973. this.xBufOff == this.xBuf.length && (this.ProcessWord(this.xBuf, 0),
  2974. this.xBufOff = 0);
  2975. this.byteCount++
  2976. },
  2977. BlockUpdate: function (a, b, c) {
  2978. for (; 0 != this.xBufOff && 0 < c;)
  2979. this.Update(a[b]),
  2980. b++,
  2981. c--;
  2982. for (; c > this.xBuf.length;)
  2983. this.ProcessWord(a, b),
  2984. b += this.xBuf.length,
  2985. c -= this.xBuf.length,
  2986. this.byteCount += this.xBuf.length;
  2987. for (; 0 < c;)
  2988. this.Update(a[b]),
  2989. b++,
  2990. c--
  2991. },
  2992. Finish: function () {
  2993. var a = this.byteCount << 3;
  2994. for (this.Update(128); 0 != this.xBufOff;)
  2995. this.Update(0);
  2996. this.ProcessLength(a);
  2997. this.ProcessBlock()
  2998. },
  2999. ROTATE: function (a, b) {
  3000. return a << b | this.URShift(a, 32 - b)
  3001. },
  3002. P0: function (a) {
  3003. return a ^ this.ROTATE(a, 9) ^ this.ROTATE(a, 17)
  3004. },
  3005. P1: function (a) {
  3006. return a ^ this.ROTATE(a, 15) ^ this.ROTATE(a, 23)
  3007. },
  3008. FF_00_15: function (a, b, c) {
  3009. return a ^ b ^ c
  3010. },
  3011. FF_16_63: function (a, b, c) {
  3012. return a & b | a & c | b & c
  3013. },
  3014. GG_00_15: function (a, b, c) {
  3015. return a ^ b ^ c
  3016. },
  3017. GG_16_63: function (a, b, c) {
  3018. return a & b | ~a & c
  3019. },
  3020. URShift: function (a, b) {
  3021. if (a > Int32.maxValue || a < Int32.minValue)
  3022. a = Int32.parse(a);
  3023. return 0 <= a ? a >> b : (a >> b) + (2 << ~b)
  3024. },
  3025. URShiftLong: function (a, b) {
  3026. var c;
  3027. c = new BigInteger;
  3028. c.fromInt(a);
  3029. if (0 <= c.signum())
  3030. c = c.shiftRight(b).intValue();
  3031. else {
  3032. var d = new BigInteger;
  3033. d.fromInt(2);
  3034. var e = ~b;
  3035. c = "";
  3036. if (0 > e) {
  3037. d = 64 + e;
  3038. for (e = 0; e < d; e++)
  3039. c += "0";
  3040. d = new BigInteger;
  3041. d.fromInt(a >> b);
  3042. c = new BigInteger("10" + c, 2);
  3043. c.toRadix(10);
  3044. c = c.add(d).toRadix(10)
  3045. } else
  3046. c = d.shiftLeft(~b).intValue(),
  3047. c = (a >> b) + c
  3048. }
  3049. return c
  3050. },
  3051. GetZ: function (a, b) {
  3052. var c = CryptoJS.enc.Utf8.parse("1234567812345678"),
  3053. d = 32 * c.words.length;
  3054. this.Update(d >> 8 & 255);
  3055. this.Update(d & 255);
  3056. c = this.GetWords(c.toString());
  3057. this.BlockUpdate(c, 0, c.length);
  3058. var c = this.GetWords(a.curve.a.toBigInteger().toRadix(16)),
  3059. d = this.GetWords(a.curve.b.toBigInteger().toRadix(16)),
  3060. e = this.GetWords(a.getX().toBigInteger().toRadix(16)),
  3061. f = this.GetWords(a.getY().toBigInteger().toRadix(16)),
  3062. g = this.GetWords(b.substr(0, 64)),
  3063. h = this.GetWords(b.substr(64, 64));
  3064. this.BlockUpdate(c, 0, c.length);
  3065. this.BlockUpdate(d, 0, d.length);
  3066. this.BlockUpdate(e, 0, e.length);
  3067. this.BlockUpdate(f, 0, f.length);
  3068. this.BlockUpdate(g, 0, g.length);
  3069. this.BlockUpdate(h, 0, h.length);
  3070. c = Array(this.GetDigestSize());
  3071. this.DoFinal(c, 0);
  3072. return c
  3073. },
  3074. GetWords: function (a) {
  3075. for (var b = [], c = a.length, d = 0; d < c; d += 2)
  3076. b[b.length] = parseInt(a.substr(d, 2), 16);
  3077. return b
  3078. },
  3079. GetHex: function (a) {
  3080. for (var b = [], c = 0, d = 0; d < 2 * a.length; d += 2)
  3081. b[d >>> 3] |= parseInt(a[c]) << 24 - d % 8 * 4,
  3082. c++;
  3083. return new CryptoJS.lib.WordArray.init(b, a.length)
  3084. }
  3085. };
  3086. Array.Clear = function (a, b, c) {
  3087. for (var elm in a)
  3088. a[elm] = null
  3089. };
  3090. Array.Copy = function (a, b, c, d, e) {
  3091. a = a.slice(b, b + e);
  3092. for (b = 0; b < a.length; b++)
  3093. c[d] = a[b],
  3094. d++
  3095. };
  3096. var Int32 = { //zdk
  3097. minValue: -parseInt("10000000000000000000000000000000", 2),
  3098. maxValue: 2147483647,
  3099. parse: function (a) {
  3100. if (a < this.minValue) {
  3101. a = new Number(-a);
  3102. a = a.toString(2);
  3103. a = a.substr(a.length - 31, 31);
  3104. for (var b = "", c = 0; c < a.length; c++)
  3105. var d = a.substr(c, 1),
  3106. b = b + ("0" == d ? "1" : "0");
  3107. a = parseInt(b, 2);
  3108. return a + 1
  3109. }
  3110. if (a > this.maxValue) {
  3111. a = Number(a);
  3112. a = a.toString(2);
  3113. a = a.substr(a.length - 31, 31);
  3114. b = "";
  3115. for (c = 0; c < a.length; c++)
  3116. d = a.substr(c, 1),
  3117. b += "0" == d ? "1" : "0";
  3118. a = parseInt(b, 2);
  3119. return -(a + 1)
  3120. }
  3121. return a
  3122. },
  3123. parseByte: function (a) {
  3124. if (0 > a) {
  3125. a = new Number(-a);
  3126. a = a.toString(2);
  3127. a = a.substr(a.length - 8, 8);
  3128. for (var b = "", c = 0; c < a.length; c++)
  3129. var d = a.substr(c, 1),
  3130. b = b + ("0" == d ? "1" : "0");
  3131. return parseInt(b, 2) + 1
  3132. }
  3133. return 255 < a ? (a = Number(a),
  3134. a = a.toString(2),
  3135. parseInt(a.substr(a.length - 8, 8), 2)) : a
  3136. }
  3137. };
  3138. "undefined" != typeof KJUR && KJUR || (KJUR = {});
  3139. "undefined" != typeof KJUR.crypto && KJUR.crypto || (KJUR.crypto = {});
  3140. KJUR.crypto.SM3withSM2 = function (a) {
  3141. var b = new SecureRandom;
  3142. this.type = "SM2";
  3143. this.getBigRandom = function (a) {
  3144. return (new BigInteger(a.bitLength(), b)).mod(a.subtract(BigInteger.ONE)).add(BigInteger.ONE)
  3145. };
  3146. this.setNamedCurve = function (a) {
  3147. this.ecparams = KJUR.crypto.ECParameterDB.getByName(a);
  3148. this.pubKeyHex = this.prvKeyHex = null;
  3149. this.curveName = a
  3150. };
  3151. this.setPrivateKeyHex = function (a) {
  3152. this.isPrivate = !0;
  3153. this.prvKeyHex = a
  3154. };
  3155. this.setPublicKeyHex = function (a) {
  3156. this.isPublic = !0;
  3157. this.pubKeyHex = a
  3158. };
  3159. this.generateKeyPairHex = function () {
  3160. var a = this.getBigRandom(this.ecparams.n),
  3161. b = this.ecparams.G.multiply(a),
  3162. e = b.getX().toBigInteger(),
  3163. b = b.getY().toBigInteger(),
  3164. f = this.ecparams.keylen / 4,
  3165. a = ("0000000000" + a.toString(16)).slice(-f),
  3166. e = ("0000000000" + e.toString(16)).slice(-f),
  3167. b = ("0000000000" + b.toString(16)).slice(-f),
  3168. e = "04" + e + b;
  3169. this.setPrivateKeyHex(a);
  3170. this.setPublicKeyHex(e);
  3171. return {
  3172. ecprvhex: a,
  3173. ecpubhex: e
  3174. }
  3175. };
  3176. this.signWithMessageHash = function (a) {
  3177. return this.signHex(a, this.prvKeyHex)
  3178. };
  3179. this.signHex = function (a, b) {
  3180. var e = new BigInteger(b, 16),
  3181. f = this.ecparams.n,
  3182. g = new BigInteger(a, 16),
  3183. h = null,
  3184. k = null,
  3185. l = k = null;
  3186. do {
  3187. do
  3188. k = this.generateKeyPairHex(),
  3189. h = new BigInteger(k.ecprvhex, 16),
  3190. k = ECPointFp.decodeFromHex(this.ecparams.curve, k.ecpubhex),
  3191. k = g.add(k.getX().toBigInteger()),
  3192. k = k.mod(f);
  3193. while (k.equals(BigInteger.ZERO) || k.add(h).equals(f));
  3194. var p = e.add(BigInteger.ONE),
  3195. p = p.modInverse(f),
  3196. l = k.multiply(e),
  3197. l = h.subtract(l).mod(f),
  3198. l = p.multiply(l).mod(f)
  3199. } while (l.equals(BigInteger.ZERO));
  3200. return KJUR.crypto.ECDSA.biRSSigToASN1Sig(k, l)
  3201. };
  3202. this.sign = function (a, b) {
  3203. var e = this.ecparams.n,
  3204. f = BigInteger.fromByteArrayUnsigned(a);
  3205. do
  3206. var g = this.getBigRandom(e),
  3207. h = this.ecparams.G.multiply(g).getX().toBigInteger().mod(e);
  3208. while (0 >= h.compareTo(BigInteger.ZERO));
  3209. e = g.modInverse(e).multiply(f.add(b.multiply(h))).mod(e);
  3210. return this.serializeSig(h, e)
  3211. };
  3212. this.verifyWithMessageHash = function (a, b) {
  3213. return this.verifyHex(a, b, this.pubKeyHex)
  3214. };
  3215. this.verifyHex = function (a, b, e) {
  3216. var f;
  3217. f = KJUR.crypto.ECDSA.parseSigHex(b);
  3218. b = f.r;
  3219. f = f.s;
  3220. e = ECPointFp.decodeFromHex(this.ecparams.curve, e);
  3221. a = new BigInteger(a, 16);
  3222. return this.verifyRaw(a, b, f, e)
  3223. };
  3224. this.verify = function (a, b, e) {
  3225. var f;
  3226. if (Bitcoin.Util.isArray(b))
  3227. b = this.parseSig(b),
  3228. f = b.r,
  3229. b = b.s;
  3230. else if ("object" === typeof b && b.r && b.s)
  3231. f = b.r,
  3232. b = b.s;
  3233. else
  3234. throw "Invalid value for signature";
  3235. if (!(e instanceof ECPointFp))
  3236. if (Bitcoin.Util.isArray(e))
  3237. e = ECPointFp.decodeFrom(this.ecparams.curve, e);
  3238. else
  3239. throw "Invalid format for pubkey value, must be byte array or ECPointFp";
  3240. a = BigInteger.fromByteArrayUnsigned(a);
  3241. return this.verifyRaw(a, f, b, e)
  3242. };
  3243. this.verifyRaw = function (a, b, e, f) {
  3244. var g = this.ecparams.n,
  3245. h = this.ecparams.G,
  3246. k = b.add(e).mod(g);
  3247. if (k.equals(BigInteger.ZERO))
  3248. return !1;
  3249. e = h.multiply(e);
  3250. e = e.add(f.multiply(k));
  3251. a = a.add(e.getX().toBigInteger()).mod(g);
  3252. return b.equals(a)
  3253. };
  3254. this.serializeSig = function (a, b) {
  3255. var e = a.toByteArraySigned(),
  3256. f = b.toByteArraySigned(),
  3257. g = [];
  3258. g.push(2);
  3259. g.push(e.length);
  3260. g = g.concat(e);
  3261. g.push(2);
  3262. g.push(f.length);
  3263. g = g.concat(f);
  3264. g.unshift(g.length);
  3265. g.unshift(48);
  3266. return g
  3267. };
  3268. this.parseSig = function (a) {
  3269. var b;
  3270. if (48 != a[0])
  3271. throw Error("Signature not a valid DERSequence");
  3272. b = 2;
  3273. if (2 != a[b])
  3274. throw Error("First element in signature must be a DERInteger");
  3275. var e = a.slice(b + 2, b + 2 + a[b + 1]);
  3276. b += 2 + a[b + 1];
  3277. if (2 != a[b])
  3278. throw Error("Second element in signature must be a DERInteger");
  3279. a = a.slice(b + 2, b + 2 + a[b + 1]);
  3280. e = BigInteger.fromByteArrayUnsigned(e);
  3281. a = BigInteger.fromByteArrayUnsigned(a);
  3282. return {
  3283. r: e,
  3284. s: a
  3285. }
  3286. };
  3287. this.parseSigCompact = function (a) {
  3288. if (65 !== a.length)
  3289. throw "Signature has the wrong length";
  3290. var b = a[0] - 27;
  3291. if (0 > b || 7 < b)
  3292. throw "Invalid signature type";
  3293. var e = this.ecparams.n,
  3294. f = BigInteger.fromByteArrayUnsigned(a.slice(1, 33)).mod(e);
  3295. a = BigInteger.fromByteArrayUnsigned(a.slice(33, 65)).mod(e);
  3296. return {
  3297. r: f,
  3298. s: a,
  3299. i: b
  3300. }
  3301. };
  3302. void 0 !== a && void 0 !== a.curve && (this.curveName = a.curve);
  3303. void 0 === this.curveName && (this.curveName = "sm2");
  3304. this.setNamedCurve(this.curveName);
  3305. void 0 !== a && (void 0 !== a.prv && this.setPrivateKeyHex(a.prv),
  3306. void 0 !== a.pub && this.setPublicKeyHex(a.pub))
  3307. };
  3308. "undefined" != typeof KJUR && KJUR || (KJUR = {});
  3309. "undefined" != typeof KJUR.crypto && KJUR.crypto || (KJUR.crypto = {});
  3310. KJUR.crypto.ECParameterDB = new function () {
  3311. var a = {},
  3312. b = {};
  3313. this.getByName = function (c) {
  3314. var d = c;
  3315. "undefined" != typeof b[d] && (d = b[c]);
  3316. if ("undefined" != typeof a[d])
  3317. return a[d];
  3318. throw "unregistered EC curve name: " + d;
  3319. };
  3320. this.regist = function (c, d, e, f, g, h, k, l, p, n, q, m) {
  3321. a[c] = {};
  3322. e = new BigInteger(e, 16);
  3323. f = new BigInteger(f, 16);
  3324. g = new BigInteger(g, 16);
  3325. h = new BigInteger(h, 16);
  3326. k = new BigInteger(k, 16);
  3327. e = new ECCurveFp(e, f, g);
  3328. l = e.decodePointHex("04" + l + p);
  3329. a[c].name = c;
  3330. a[c].keylen = d;
  3331. a[c].curve = e;
  3332. a[c].G = l;
  3333. a[c].n = h;
  3334. a[c].h = k;
  3335. a[c].oid = q;
  3336. a[c].info = m;
  3337. for (d = 0; d < n.length; d++)
  3338. b[n[d]] = c
  3339. }
  3340. };
  3341. KJUR.crypto.ECParameterDB.regist("secp128r1", 128, "FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFDFFFFFFFFFFFFFFFFFFFFFFFC", "E87579C11079F43DD824993C2CEE5ED3", "FFFFFFFE0000000075A30D1B9038A115", "1", "161FF7528B899B2D0C28607CA52C5B86", "CF5AC8395BAFEB13C02DA292DDED7A83", [], "", "secp128r1 : SECG curve over a 128 bit prime field");
  3342. KJUR.crypto.ECParameterDB.regist("secp160k1", 160, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFAC73", "0", "7", "0100000000000000000001B8FA16DFAB9ACA16B6B3", "1", "3B4C382CE37AA192A4019E763036F4F5DD4D7EBB", "938CF935318FDCED6BC28286531733C3F03C4FEE", [], "", "secp160k1 : SECG curve over a 160 bit prime field");
  3343. KJUR.crypto.ECParameterDB.regist("secp160r1", 160, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF7FFFFFFC", "1C97BEFC54BD7A8B65ACF89F81D4D4ADC565FA45", "0100000000000000000001F4C8F927AED3CA752257", "1", "4A96B5688EF573284664698968C38BB913CBFC82", "23A628553168947D59DCC912042351377AC5FB32", [], "", "secp160r1 : SECG curve over a 160 bit prime field");
  3344. KJUR.crypto.ECParameterDB.regist("secp192k1", 192, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFEE37", "0", "3", "FFFFFFFFFFFFFFFFFFFFFFFE26F2FC170F69466A74DEFD8D", "1", "DB4FF10EC057E9AE26B07D0280B7F4341DA5D1B1EAE06C7D", "9B2F2F6D9C5628A7844163D015BE86344082AA88D95E2F9D", []);
  3345. KJUR.crypto.ECParameterDB.regist("secp192r1", 192, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFC", "64210519E59C80E70FA7E9AB72243049FEB8DEECC146B9B1", "FFFFFFFFFFFFFFFFFFFFFFFF99DEF836146BC9B1B4D22831", "1", "188DA80EB03090F67CBF20EB43A18800F4FF0AFD82FF1012", "07192B95FFC8DA78631011ED6B24CDD573F977A11E794811", []);
  3346. KJUR.crypto.ECParameterDB.regist("secp224r1", 224, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF000000000000000000000001", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFE", "B4050A850C04B3ABF54132565044B0B7D7BFD8BA270B39432355FFB4", "FFFFFFFFFFFFFFFFFFFFFFFFFFFF16A2E0B8F03E13DD29455C5C2A3D", "1", "B70E0CBD6BB4BF7F321390B94A03C1D356C21122343280D6115C1D21", "BD376388B5F723FB4C22DFE6CD4375A05A07476444D5819985007E34", []);
  3347. KJUR.crypto.ECParameterDB.regist("secp256k1", 256, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFC2F", "0", "7", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEBAAEDCE6AF48A03BBFD25E8CD0364141", "1", "79BE667EF9DCBBAC55A06295CE870B07029BFCDB2DCE28D959F2815B16F81798", "483ADA7726A3C4655DA4FBFC0E1108A8FD17B448A68554199C47D08FFB10D4B8", []);
  3348. KJUR.crypto.ECParameterDB.regist("secp256r1", 256, "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFF", "FFFFFFFF00000001000000000000000000000000FFFFFFFFFFFFFFFFFFFFFFFC", "5AC635D8AA3A93E7B3EBBD55769886BC651D06B0CC53B0F63BCE3C3E27D2604B", "FFFFFFFF00000000FFFFFFFFFFFFFFFFBCE6FAADA7179E84F3B9CAC2FC632551", "1", "6B17D1F2E12C4247F8BCE6E563A440F277037D812DEB33A0F4A13945D898C296", "4FE342E2FE1A7F9B8EE7EB4A7C0F9E162BCE33576B315ECECBB6406837BF51F5", ["NIST P-256", "P-256", "prime256v1"]);
  3349. KJUR.crypto.ECParameterDB.regist("secp384r1", 384, "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFF", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFEFFFFFFFF0000000000000000FFFFFFFC", "B3312FA7E23EE7E4988E056BE3F82D19181D9C6EFE8141120314088F5013875AC656398D8A2ED19D2A85C8EDD3EC2AEF", "FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC7634D81F4372DDF581A0DB248B0A77AECEC196ACCC52973", "1", "AA87CA22BE8B05378EB1C71EF320AD746E1D3B628BA79B9859F741E082542A385502F25DBF55296C3A545E3872760AB7", "3617de4a96262c6f5d9e98bf9292dc29f8f41dbd289a147ce9da3113b5f0b8c00a60b1ce1d7e819d7a431d7c90ea0e5f", ["NIST P-384", "P-384"]);
  3350. KJUR.crypto.ECParameterDB.regist("secp521r1", 521, "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF", "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFC", "051953EB9618E1C9A1F929A21A0B68540EEA2DA725B99B315F3B8B489918EF109E156193951EC7E937B1652C0BD3BB1BF073573DF883D2C34F1EF451FD46B503F00", "1FFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFA51868783BF2F966B7FCC0148F709A5D03BB5C9B8899C47AEBB6FB71E91386409", "1", "C6858E06B70404E9CD9E3ECB662395B4429C648139053FB521F828AF606B4D3DBAA14B5E77EFE75928FE1DC127A2FFA8DE3348B3C1856A429BF97E7E31C2E5BD66", "011839296a789a3bc0045c8a5fb42c7d1bd998f54449579b446817afbd17273e662c97ee72995ef42640c550b9013fad0761353c7086a272c24088be94769fd16650", ["NIST P-521", "P-521"]);
  3351. KJUR.crypto.ECParameterDB.regist("sm2", 256, "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFF", "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF00000000FFFFFFFFFFFFFFFC", "28E9FA9E9D9F5E344D5A9E4BCF6509A7F39789F515AB8F92DDBCBD414D940E93", "FFFFFFFEFFFFFFFFFFFFFFFFFFFFFFFF7203DF6B21C6052B53BBF40939D54123", "1", "32C4AE2C1F1981195F9904466A39C9948FE30BBFF2660BE1715A4589334C74C7", "BC3736A2F4F6779C59BDCEE36B692153D0A9877CC62A474002DF32E52139F0A0", ["sm2", "SM2"]);
  3352. SM2Cipher.prototype = {
  3353. Reset: function () {
  3354. this.sm3keybase = new SM3Digest;
  3355. this.sm3c3 = new SM3Digest;
  3356. for (var a = this.p2.getX().toBigInteger().toRadix(16); 64 > a.length;)
  3357. a = "0" + a;
  3358. for (var a = this.GetWords(a), b = this.p2.getY().toBigInteger().toRadix(16); 64 > b.length;)
  3359. b = "0" + b;
  3360. b = this.GetWords(b);
  3361. this.sm3keybase.BlockUpdate(a, 0, a.length);
  3362. this.sm3c3.BlockUpdate(a, 0, a.length);
  3363. this.sm3keybase.BlockUpdate(b, 0, b.length);
  3364. this.ct = 1;
  3365. this.NextKey()
  3366. },
  3367. NextKey: function () {
  3368. var a = new SM3Digest(this.sm3keybase);
  3369. a.Update(this.ct >> 24 & 255);
  3370. a.Update(this.ct >> 16 & 255);
  3371. a.Update(this.ct >> 8 & 255);
  3372. a.Update(this.ct & 255);
  3373. a.DoFinal(this.key, 0);
  3374. this.keyOff = 0;
  3375. this.ct++
  3376. },
  3377. KDF: function (a) {
  3378. var b = Array(a),
  3379. c = new SM3Digest,
  3380. d = Array(32),
  3381. e = 1,
  3382. f = a / 32;
  3383. a %= 32;
  3384. for (var g = this.p2.getX().toBigInteger().toRadix(16); 64 > g.length;)
  3385. g = "0" + g;
  3386. for (var g = this.GetWords(g), h = this.p2.getY().toBigInteger().toRadix(16); 64 > h.length;)
  3387. h = "0" + h;
  3388. for (var h = this.GetWords(h), k = 0, l = 0; l < f; l++)
  3389. c.BlockUpdate(g, 0, g.length),
  3390. c.BlockUpdate(h, 0, h.length),
  3391. c.Update(e >> 24 & 255),
  3392. c.Update(e >> 16 & 255),
  3393. c.Update(e >> 8 & 255),
  3394. c.Update(e & 255),
  3395. c.DoFinal(b, k),
  3396. k += 32,
  3397. e++;
  3398. 0 != a && (c.BlockUpdate(g, 0, g.length),
  3399. c.BlockUpdate(h, 0, h.length),
  3400. c.Update(e >> 24 & 255),
  3401. c.Update(e >> 16 & 255),
  3402. c.Update(e >> 8 & 255),
  3403. c.Update(e & 255),
  3404. c.DoFinal(d, 0));
  3405. Array.Copy(d, 0, b, k, a);
  3406. for (l = 0; l < b.length; l++)
  3407. b[l] &= 255;
  3408. return b
  3409. },
  3410. InitEncipher: function (a) {
  3411. var b = null,
  3412. c = null,
  3413. c = new KJUR.crypto.ECDSA({
  3414. curve: "sm2"
  3415. }),
  3416. d = c.generateKeyPairHex(),
  3417. b = new BigInteger(d.ecprvhex, 16),
  3418. c = ECPointFp.decodeFromHex(c.ecparams.curve, d.ecpubhex);
  3419. this.p2 = a.multiply(b);
  3420. this.Reset();
  3421. return c
  3422. },
  3423. EncryptBlock: function (a) {
  3424. this.sm3c3.BlockUpdate(a, 0, a.length);
  3425. for (var b = this.KDF(a.length), c = 0; c < a.length; c++)
  3426. a[c] ^= b[c]
  3427. },
  3428. InitDecipher: function (a, b) {
  3429. this.p2 = b.multiply(a);
  3430. this.p2.getX().toBigInteger().toRadix(16);
  3431. this.p2.getY().toBigInteger().toRadix(16);
  3432. this.Reset()
  3433. },
  3434. DecryptBlock: function (a) {
  3435. for (var b = this.KDF(a.length), c = 0, d = "", c = 0; c < b.length; c++)
  3436. d += b[c].toString(16);
  3437. for (c = 0; c < a.length; c++)
  3438. a[c] ^= b[c];
  3439. this.sm3c3.BlockUpdate(a, 0, a.length)
  3440. },
  3441. Dofinal: function (a) {
  3442. for (var b = this.p2.getY().toBigInteger().toRadix(16); 64 > b.length;)
  3443. b = "0" + b;
  3444. b = this.GetWords(b);
  3445. this.sm3c3.BlockUpdate(b, 0, b.length);
  3446. this.sm3c3.DoFinal(a, 0);
  3447. this.Reset()
  3448. },
  3449. Encrypt: function (a, b) {
  3450. var c = Array(b.length);
  3451. Array.Copy(b, 0, c, 0, b.length);
  3452. var d = this.InitEncipher(a);
  3453. this.EncryptBlock(c);
  3454. var e = Array(32);
  3455. this.Dofinal(e);
  3456. for (var f = d.getX().toBigInteger().toRadix(16), d = d.getY().toBigInteger().toRadix(16); 64 > f.length;)
  3457. f = "0" + f;
  3458. for (; 64 > d.length;)
  3459. d = "0" + d;
  3460. f += d;
  3461. c = this.GetHex(c).toString();
  3462. 0 != c.length % 2 && (c = "0" + c);
  3463. e = this.GetHex(e).toString();
  3464. d = f + c + e;
  3465. this.cipherMode == SM2CipherMode.C1C3C2 && (d = f + e + c);
  3466. return d
  3467. },
  3468. GetWords: function (a) {
  3469. for (var b = [], c = a.length, d = 0; d < c; d += 2)
  3470. b[b.length] = parseInt(a.substr(d, 2), 16);
  3471. return b
  3472. },
  3473. GetHex: function (a) {
  3474. for (var b = [], c = 0, d = 0; d < 2 * a.length; d += 2)
  3475. b[d >>> 3] |= parseInt(a[c]) << 24 - d % 8 * 4,
  3476. c++;
  3477. return new CryptoJS.lib.WordArray.init(b, a.length)
  3478. },
  3479. Decrypt: function (a, b) {
  3480. var c = b.substr(0, 64),
  3481. d = b.substr(0 + c.length, 64),
  3482. e = b.substr(c.length + d.length, b.length - c.length - d.length - 64),
  3483. f = b.substr(b.length - 64);
  3484. this.cipherMode == SM2CipherMode.C1C3C2 && (f = b.substr(c.length + d.length, 64),
  3485. e = b.substr(c.length + d.length + 64));
  3486. e = this.GetWords(e);
  3487. c = this.CreatePoint(c, d);
  3488. this.InitDecipher(a, c);
  3489. this.DecryptBlock(e);
  3490. c = Array(32);
  3491. this.Dofinal(c);
  3492. return this.GetHex(c).toString() == f ? (f = this.GetHex(e),
  3493. CryptoJS.enc.Utf8.stringify(f)) : ""
  3494. },
  3495. CreatePoint: function (a, b) {
  3496. var c = new KJUR.crypto.ECDSA({
  3497. curve: "sm2"
  3498. });
  3499. return ECPointFp.decodeFromHex(c.ecparams.curve, "04" + a + b)
  3500. }
  3501. };
  3502. /*-------------下面修改----------*/
  3503. var SM2Key = function (key) {
  3504. this.setKey(key);
  3505. };
  3506. function SM2SetKey(key) {
  3507. if (key && typeof key === 'object') {
  3508. this.eccX = key.eccX;
  3509. this.eccY = key.eccY;
  3510. } else {
  3511. this.eccX = "F1342ADB38855E1F8C37D1181378DE446E52788389F7DB3DEA022A1FC4D4D856";
  3512. this.eccY = "66FC6DE253C822F1E52914D9E0B80C5D825759CE696CF039A8449F98017510B7";
  3513. }
  3514. }
  3515. /*
  3516. *加密数据
  3517. */
  3518. function SM2Encrypt(text) {
  3519. var cipherMode = SM2CipherMode.C1C3C2,
  3520. cipher = new SM2Cipher(cipherMode),
  3521. textData = CryptoJS.enc.Utf8.parse(text);
  3522. var cipher = new SM2Cipher(cipherMode);
  3523. var userKey = cipher.CreatePoint(this.eccX, this.eccY);
  3524. var msgData = cipher.GetWords(textData.toString());
  3525. return cipher.Encrypt(userKey, msgData);
  3526. }
  3527. SM2Key.prototype.setKey = SM2SetKey;
  3528. SM2Key.prototype.encrypt = SM2Encrypt;
  3529. //export default SM2Key;
  3530. global.SM2 = {
  3531. SM2CipherMode: SM2CipherMode,
  3532. SM2Cipher: SM2Cipher,
  3533. CryptoJS: CryptoJS
  3534. }
  3535. }(window));
  3536. window.SM2Utils = {};
  3537. function sm2Encrypt(data, publickey, cipherMode) {
  3538. cipherMode = cipherMode == 0 ? cipherMode : 1;
  3539. // msg = SM2.utf8tob64(msg);
  3540. var msgData = CryptoJS.enc.Utf8.parse(data);
  3541. msgData = CryptoJS.enc.Base64.stringify(msgData);
  3542. //在转utf-8
  3543. msgData = CryptoJS.enc.Utf8.parse(msgData);
  3544. var pubkeyHex = publickey;
  3545. if (pubkeyHex.length > 64 * 2) {
  3546. pubkeyHex = pubkeyHex.substr(pubkeyHex.length - 64 * 2);
  3547. }
  3548. var xHex = pubkeyHex.substr(0, 64);
  3549. var yHex = pubkeyHex.substr(64);
  3550. var cipher = new SM2Cipher(cipherMode);
  3551. var userKey = cipher.CreatePoint(xHex, yHex);
  3552. msgData = cipher.GetWords(msgData.toString());
  3553. var encryptData = cipher.Encrypt(userKey, msgData);
  3554. return '04' + encryptData;
  3555. }
  3556. /**
  3557. * 根据公钥进行加密
  3558. */
  3559. SM2Utils.encs = function (key, s, cipherMode) {
  3560. if (s == null || s.length == 0) {
  3561. return "";
  3562. }
  3563. return sm2Encrypt(s, key, cipherMode);
  3564. }