openpgp.js 759 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030603160326033603460356036603760386039604060416042604360446045604660476048604960506051605260536054605560566057605860596060606160626063606460656066606760686069607060716072607360746075607660776078607960806081608260836084608560866087608860896090609160926093609460956096609760986099610061016102610361046105610661076108610961106111611261136114611561166117611861196120612161226123612461256126612761286129613061316132613361346135613661376138613961406141614261436144614561466147614861496150615161526153615461556156615761586159616061616162616361646165616661676168616961706171617261736174617561766177617861796180618161826183618461856186618761886189619061916192619361946195619661976198619962006201620262036204620562066207620862096210621162126213621462156216621762186219622062216222622362246225622662276228622962306231623262336234623562366237623862396240624162426243624462456246624762486249625062516252625362546255625662576258625962606261626262636264626562666267626862696270627162726273627462756276627762786279628062816282628362846285628662876288628962906291629262936294629562966297629862996300630163026303630463056306630763086309631063116312631363146315631663176318631963206321632263236324632563266327632863296330633163326333633463356336633763386339634063416342634363446345634663476348634963506351635263536354635563566357635863596360636163626363636463656366636763686369637063716372637363746375637663776378637963806381638263836384638563866387638863896390639163926393639463956396639763986399640064016402640364046405640664076408640964106411641264136414641564166417641864196420642164226423642464256426642764286429643064316432643364346435643664376438643964406441644264436444644564466447644864496450645164526453645464556456645764586459646064616462646364646465646664676468646964706471647264736474647564766477647864796480648164826483648464856486648764886489649064916492649364946495649664976498649965006501650265036504650565066507650865096510651165126513651465156516651765186519652065216522652365246525652665276528652965306531653265336534653565366537653865396540654165426543654465456546654765486549655065516552655365546555655665576558655965606561656265636564656565666567656865696570657165726573657465756576657765786579658065816582658365846585658665876588658965906591659265936594659565966597659865996600660166026603660466056606660766086609661066116612661366146615661666176618661966206621662266236624662566266627662866296630663166326633663466356636663766386639664066416642664366446645664666476648664966506651665266536654665566566657665866596660666166626663666466656666666766686669667066716672667366746675667666776678667966806681668266836684668566866687668866896690669166926693669466956696669766986699670067016702670367046705670667076708670967106711671267136714671567166717671867196720672167226723672467256726672767286729673067316732673367346735673667376738673967406741674267436744674567466747674867496750675167526753675467556756675767586759676067616762676367646765676667676768676967706771677267736774677567766777677867796780678167826783678467856786678767886789679067916792679367946795679667976798679968006801680268036804680568066807680868096810681168126813681468156816681768186819682068216822682368246825682668276828682968306831683268336834683568366837683868396840684168426843684468456846684768486849685068516852685368546855685668576858685968606861686268636864686568666867686868696870687168726873687468756876687768786879688068816882688368846885688668876888688968906891689268936894689568966897689868996900690169026903690469056906690769086909691069116912691369146915691669176918691969206921692269236924692569266927692869296930693169326933693469356936693769386939694069416942694369446945694669476948694969506951695269536954695569566957695869596960696169626963696469656966696769686969697069716972697369746975697669776978697969806981698269836984698569866987698869896990699169926993699469956996699769986999700070017002700370047005700670077008700970107011701270137014701570167017701870197020702170227023702470257026702770287029703070317032703370347035703670377038703970407041704270437044704570467047704870497050705170527053705470557056705770587059706070617062706370647065706670677068706970707071707270737074707570767077707870797080708170827083708470857086708770887089709070917092709370947095709670977098709971007101710271037104710571067107710871097110711171127113711471157116711771187119712071217122712371247125712671277128712971307131713271337134713571367137713871397140714171427143714471457146714771487149715071517152715371547155715671577158715971607161716271637164716571667167716871697170717171727173717471757176717771787179718071817182718371847185718671877188718971907191719271937194719571967197719871997200720172027203720472057206720772087209721072117212721372147215721672177218721972207221722272237224722572267227722872297230723172327233723472357236723772387239724072417242724372447245724672477248724972507251725272537254725572567257725872597260726172627263726472657266726772687269727072717272727372747275727672777278727972807281728272837284728572867287728872897290729172927293729472957296729772987299730073017302730373047305730673077308730973107311731273137314731573167317731873197320732173227323732473257326732773287329733073317332733373347335733673377338733973407341734273437344734573467347734873497350735173527353735473557356735773587359736073617362736373647365736673677368736973707371737273737374737573767377737873797380738173827383738473857386738773887389739073917392739373947395739673977398739974007401740274037404740574067407740874097410741174127413741474157416741774187419742074217422742374247425742674277428742974307431743274337434743574367437743874397440744174427443744474457446744774487449745074517452745374547455745674577458745974607461746274637464746574667467746874697470747174727473747474757476747774787479748074817482748374847485748674877488748974907491749274937494749574967497749874997500750175027503750475057506750775087509751075117512751375147515751675177518751975207521752275237524752575267527752875297530753175327533753475357536753775387539754075417542754375447545754675477548754975507551755275537554755575567557755875597560756175627563756475657566756775687569757075717572757375747575757675777578757975807581758275837584758575867587758875897590759175927593759475957596759775987599760076017602760376047605760676077608760976107611761276137614761576167617761876197620762176227623762476257626762776287629763076317632763376347635763676377638763976407641764276437644764576467647764876497650765176527653765476557656765776587659766076617662766376647665766676677668766976707671767276737674767576767677767876797680768176827683768476857686768776887689769076917692769376947695769676977698769977007701770277037704770577067707770877097710771177127713771477157716771777187719772077217722772377247725772677277728772977307731773277337734773577367737773877397740774177427743774477457746774777487749775077517752775377547755775677577758775977607761776277637764776577667767776877697770777177727773777477757776777777787779778077817782778377847785778677877788778977907791779277937794779577967797779877997800780178027803780478057806780778087809781078117812781378147815781678177818781978207821782278237824782578267827782878297830783178327833783478357836783778387839784078417842784378447845784678477848784978507851785278537854785578567857785878597860786178627863786478657866786778687869787078717872787378747875787678777878787978807881788278837884788578867887788878897890789178927893789478957896789778987899790079017902790379047905790679077908790979107911791279137914791579167917791879197920792179227923792479257926792779287929793079317932793379347935793679377938793979407941794279437944794579467947794879497950795179527953795479557956795779587959796079617962796379647965796679677968796979707971797279737974797579767977797879797980798179827983798479857986798779887989799079917992799379947995799679977998799980008001800280038004800580068007800880098010801180128013801480158016801780188019802080218022802380248025802680278028802980308031803280338034803580368037803880398040804180428043804480458046804780488049805080518052805380548055805680578058805980608061806280638064806580668067806880698070807180728073807480758076807780788079808080818082808380848085808680878088808980908091809280938094809580968097809880998100810181028103810481058106810781088109811081118112811381148115811681178118811981208121812281238124812581268127812881298130813181328133813481358136813781388139814081418142814381448145814681478148814981508151815281538154815581568157815881598160816181628163816481658166816781688169817081718172817381748175817681778178817981808181818281838184818581868187818881898190819181928193819481958196819781988199820082018202820382048205820682078208820982108211821282138214821582168217821882198220822182228223822482258226822782288229823082318232823382348235823682378238823982408241824282438244824582468247824882498250825182528253825482558256825782588259826082618262826382648265826682678268826982708271827282738274827582768277827882798280828182828283828482858286828782888289829082918292829382948295829682978298829983008301830283038304830583068307830883098310831183128313831483158316831783188319832083218322832383248325832683278328832983308331833283338334833583368337833883398340834183428343834483458346834783488349835083518352835383548355835683578358835983608361836283638364836583668367836883698370837183728373837483758376837783788379838083818382838383848385838683878388838983908391839283938394839583968397839883998400840184028403840484058406840784088409841084118412841384148415841684178418841984208421842284238424842584268427842884298430843184328433843484358436843784388439844084418442844384448445844684478448844984508451845284538454845584568457845884598460846184628463846484658466846784688469847084718472847384748475847684778478847984808481848284838484848584868487848884898490849184928493849484958496849784988499850085018502850385048505850685078508850985108511851285138514851585168517851885198520852185228523852485258526852785288529853085318532853385348535853685378538853985408541854285438544854585468547854885498550855185528553855485558556855785588559856085618562856385648565856685678568856985708571857285738574857585768577857885798580858185828583858485858586858785888589859085918592859385948595859685978598859986008601860286038604860586068607860886098610861186128613861486158616861786188619862086218622862386248625862686278628862986308631863286338634863586368637863886398640864186428643864486458646864786488649865086518652865386548655865686578658865986608661866286638664866586668667866886698670867186728673867486758676867786788679868086818682868386848685868686878688868986908691869286938694869586968697869886998700870187028703870487058706870787088709871087118712871387148715871687178718871987208721872287238724872587268727872887298730873187328733873487358736873787388739874087418742874387448745874687478748874987508751875287538754875587568757875887598760876187628763876487658766876787688769877087718772877387748775877687778778877987808781878287838784878587868787878887898790879187928793879487958796879787988799880088018802880388048805880688078808880988108811881288138814881588168817881888198820882188228823882488258826882788288829883088318832883388348835883688378838883988408841884288438844884588468847884888498850885188528853885488558856885788588859886088618862886388648865886688678868886988708871887288738874887588768877887888798880888188828883888488858886888788888889889088918892889388948895889688978898889989008901890289038904890589068907890889098910891189128913891489158916891789188919892089218922892389248925892689278928892989308931893289338934893589368937893889398940894189428943894489458946894789488949895089518952895389548955895689578958895989608961896289638964896589668967896889698970897189728973897489758976897789788979898089818982898389848985898689878988898989908991899289938994899589968997899889999000900190029003900490059006900790089009901090119012901390149015901690179018901990209021902290239024902590269027902890299030903190329033903490359036903790389039904090419042904390449045904690479048904990509051905290539054905590569057905890599060906190629063906490659066906790689069907090719072907390749075907690779078907990809081908290839084908590869087908890899090909190929093909490959096909790989099910091019102910391049105910691079108910991109111911291139114911591169117911891199120912191229123912491259126912791289129913091319132913391349135913691379138913991409141914291439144914591469147914891499150915191529153915491559156915791589159916091619162916391649165916691679168916991709171917291739174917591769177917891799180918191829183918491859186918791889189919091919192919391949195919691979198919992009201920292039204920592069207920892099210921192129213921492159216921792189219922092219222922392249225922692279228922992309231923292339234923592369237923892399240924192429243924492459246924792489249925092519252925392549255925692579258925992609261926292639264926592669267926892699270927192729273927492759276927792789279928092819282928392849285928692879288928992909291929292939294929592969297929892999300930193029303930493059306930793089309931093119312931393149315931693179318931993209321932293239324932593269327932893299330933193329333933493359336933793389339934093419342934393449345934693479348934993509351935293539354935593569357935893599360936193629363936493659366936793689369937093719372937393749375937693779378937993809381938293839384938593869387938893899390939193929393939493959396939793989399940094019402940394049405940694079408940994109411941294139414941594169417941894199420942194229423942494259426942794289429943094319432943394349435943694379438943994409441944294439444944594469447944894499450945194529453945494559456945794589459946094619462946394649465946694679468946994709471947294739474947594769477947894799480948194829483948494859486948794889489949094919492949394949495949694979498949995009501950295039504950595069507950895099510951195129513951495159516951795189519952095219522952395249525952695279528952995309531953295339534953595369537953895399540954195429543954495459546954795489549955095519552955395549555955695579558955995609561956295639564956595669567956895699570957195729573957495759576957795789579958095819582958395849585958695879588958995909591959295939594959595969597959895999600960196029603960496059606960796089609961096119612961396149615961696179618961996209621962296239624962596269627962896299630963196329633963496359636963796389639964096419642964396449645964696479648964996509651965296539654965596569657965896599660966196629663966496659666966796689669967096719672967396749675967696779678967996809681968296839684968596869687968896899690969196929693969496959696969796989699970097019702970397049705970697079708970997109711971297139714971597169717971897199720972197229723972497259726972797289729973097319732973397349735973697379738973997409741974297439744974597469747974897499750975197529753975497559756975797589759976097619762976397649765976697679768976997709771977297739774977597769777977897799780978197829783978497859786978797889789979097919792979397949795979697979798979998009801980298039804980598069807980898099810981198129813981498159816981798189819982098219822982398249825982698279828982998309831983298339834983598369837983898399840984198429843984498459846984798489849985098519852985398549855985698579858985998609861986298639864986598669867986898699870987198729873987498759876987798789879988098819882988398849885988698879888988998909891989298939894989598969897989898999900990199029903990499059906990799089909991099119912991399149915991699179918991999209921992299239924992599269927992899299930993199329933993499359936993799389939994099419942994399449945994699479948994999509951995299539954995599569957995899599960996199629963996499659966996799689969997099719972997399749975997699779978997999809981998299839984998599869987998899899990999199929993999499959996999799989999100001000110002100031000410005100061000710008100091001010011100121001310014100151001610017100181001910020100211002210023100241002510026100271002810029100301003110032100331003410035100361003710038100391004010041100421004310044100451004610047100481004910050100511005210053100541005510056100571005810059100601006110062100631006410065100661006710068100691007010071100721007310074100751007610077100781007910080100811008210083100841008510086100871008810089100901009110092100931009410095100961009710098100991010010101101021010310104101051010610107101081010910110101111011210113101141011510116101171011810119101201012110122101231012410125101261012710128101291013010131101321013310134101351013610137101381013910140101411014210143101441014510146101471014810149101501015110152101531015410155101561015710158101591016010161101621016310164101651016610167101681016910170101711017210173101741017510176101771017810179101801018110182101831018410185101861018710188101891019010191101921019310194101951019610197101981019910200102011020210203102041020510206102071020810209102101021110212102131021410215102161021710218102191022010221102221022310224102251022610227102281022910230102311023210233102341023510236102371023810239102401024110242102431024410245102461024710248102491025010251102521025310254102551025610257102581025910260102611026210263102641026510266102671026810269102701027110272102731027410275102761027710278102791028010281102821028310284102851028610287102881028910290102911029210293102941029510296102971029810299103001030110302103031030410305103061030710308103091031010311103121031310314103151031610317103181031910320103211032210323103241032510326103271032810329103301033110332103331033410335103361033710338103391034010341103421034310344103451034610347103481034910350103511035210353103541035510356103571035810359103601036110362103631036410365103661036710368103691037010371103721037310374103751037610377103781037910380103811038210383103841038510386103871038810389103901039110392103931039410395103961039710398103991040010401104021040310404104051040610407104081040910410104111041210413104141041510416104171041810419104201042110422104231042410425104261042710428104291043010431104321043310434104351043610437104381043910440104411044210443104441044510446104471044810449104501045110452104531045410455104561045710458104591046010461104621046310464104651046610467104681046910470104711047210473104741047510476104771047810479104801048110482104831048410485104861048710488104891049010491104921049310494104951049610497104981049910500105011050210503105041050510506105071050810509105101051110512105131051410515105161051710518105191052010521105221052310524105251052610527105281052910530105311053210533105341053510536105371053810539105401054110542105431054410545105461054710548105491055010551105521055310554105551055610557105581055910560105611056210563105641056510566105671056810569105701057110572105731057410575105761057710578105791058010581105821058310584105851058610587105881058910590105911059210593105941059510596105971059810599106001060110602106031060410605106061060710608106091061010611106121061310614106151061610617106181061910620106211062210623106241062510626106271062810629106301063110632106331063410635106361063710638106391064010641106421064310644106451064610647106481064910650106511065210653106541065510656106571065810659106601066110662106631066410665106661066710668106691067010671106721067310674106751067610677106781067910680106811068210683106841068510686106871068810689106901069110692106931069410695106961069710698106991070010701107021070310704107051070610707107081070910710107111071210713107141071510716107171071810719107201072110722107231072410725107261072710728107291073010731107321073310734107351073610737107381073910740107411074210743107441074510746107471074810749107501075110752107531075410755107561075710758107591076010761107621076310764107651076610767107681076910770107711077210773107741077510776107771077810779107801078110782107831078410785107861078710788107891079010791107921079310794107951079610797107981079910800108011080210803108041080510806108071080810809108101081110812108131081410815108161081710818108191082010821108221082310824108251082610827108281082910830108311083210833108341083510836108371083810839108401084110842108431084410845108461084710848108491085010851108521085310854108551085610857108581085910860108611086210863108641086510866108671086810869108701087110872108731087410875108761087710878108791088010881108821088310884108851088610887108881088910890108911089210893108941089510896108971089810899109001090110902109031090410905109061090710908109091091010911109121091310914109151091610917109181091910920109211092210923109241092510926109271092810929109301093110932109331093410935109361093710938109391094010941109421094310944109451094610947109481094910950109511095210953109541095510956109571095810959109601096110962109631096410965109661096710968109691097010971109721097310974109751097610977109781097910980109811098210983109841098510986109871098810989109901099110992109931099410995109961099710998109991100011001110021100311004110051100611007110081100911010110111101211013110141101511016110171101811019110201102111022110231102411025110261102711028110291103011031110321103311034110351103611037110381103911040110411104211043110441104511046110471104811049110501105111052110531105411055110561105711058110591106011061110621106311064110651106611067110681106911070110711107211073110741107511076110771107811079110801108111082110831108411085110861108711088110891109011091110921109311094110951109611097110981109911100111011110211103111041110511106111071110811109111101111111112111131111411115111161111711118111191112011121111221112311124111251112611127111281112911130111311113211133111341113511136111371113811139111401114111142111431114411145111461114711148111491115011151111521115311154111551115611157111581115911160111611116211163111641116511166111671116811169111701117111172111731117411175111761117711178111791118011181111821118311184111851118611187111881118911190111911119211193111941119511196111971119811199112001120111202112031120411205112061120711208112091121011211112121121311214112151121611217112181121911220112211122211223112241122511226112271122811229112301123111232112331123411235112361123711238112391124011241112421124311244112451124611247112481124911250112511125211253112541125511256112571125811259112601126111262112631126411265112661126711268112691127011271112721127311274112751127611277112781127911280112811128211283112841128511286112871128811289112901129111292112931129411295112961129711298112991130011301113021130311304113051130611307113081130911310113111131211313113141131511316113171131811319113201132111322113231132411325113261132711328113291133011331113321133311334113351133611337113381133911340113411134211343113441134511346113471134811349113501135111352113531135411355113561135711358113591136011361113621136311364113651136611367113681136911370113711137211373113741137511376113771137811379113801138111382113831138411385113861138711388113891139011391113921139311394113951139611397113981139911400114011140211403114041140511406114071140811409114101141111412114131141411415114161141711418114191142011421114221142311424114251142611427114281142911430114311143211433114341143511436114371143811439114401144111442114431144411445114461144711448114491145011451114521145311454114551145611457114581145911460114611146211463114641146511466114671146811469114701147111472114731147411475114761147711478114791148011481114821148311484114851148611487114881148911490114911149211493114941149511496114971149811499115001150111502115031150411505115061150711508115091151011511115121151311514115151151611517115181151911520115211152211523115241152511526115271152811529115301153111532115331153411535115361153711538115391154011541115421154311544115451154611547115481154911550115511155211553115541155511556115571155811559115601156111562115631156411565115661156711568115691157011571115721157311574115751157611577115781157911580115811158211583115841158511586115871158811589115901159111592115931159411595115961159711598115991160011601116021160311604116051160611607116081160911610116111161211613116141161511616116171161811619116201162111622116231162411625116261162711628116291163011631116321163311634116351163611637116381163911640116411164211643116441164511646116471164811649116501165111652116531165411655116561165711658116591166011661116621166311664116651166611667116681166911670116711167211673116741167511676116771167811679116801168111682116831168411685116861168711688116891169011691116921169311694116951169611697116981169911700117011170211703117041170511706117071170811709117101171111712117131171411715117161171711718117191172011721117221172311724117251172611727117281172911730117311173211733117341173511736117371173811739117401174111742117431174411745117461174711748117491175011751117521175311754117551175611757117581175911760117611176211763117641176511766117671176811769117701177111772117731177411775117761177711778117791178011781117821178311784117851178611787117881178911790117911179211793117941179511796117971179811799118001180111802118031180411805118061180711808118091181011811118121181311814118151181611817118181181911820118211182211823118241182511826118271182811829118301183111832118331183411835118361183711838118391184011841118421184311844118451184611847118481184911850118511185211853118541185511856118571185811859118601186111862118631186411865118661186711868118691187011871118721187311874118751187611877118781187911880118811188211883118841188511886118871188811889118901189111892118931189411895118961189711898118991190011901119021190311904119051190611907119081190911910119111191211913119141191511916119171191811919119201192111922119231192411925119261192711928119291193011931119321193311934119351193611937119381193911940119411194211943119441194511946119471194811949119501195111952119531195411955119561195711958119591196011961119621196311964119651196611967119681196911970119711197211973119741197511976119771197811979119801198111982119831198411985119861198711988119891199011991119921199311994119951199611997119981199912000120011200212003120041200512006120071200812009120101201112012120131201412015120161201712018120191202012021120221202312024120251202612027120281202912030120311203212033120341203512036120371203812039120401204112042120431204412045120461204712048120491205012051120521205312054120551205612057120581205912060120611206212063120641206512066120671206812069120701207112072120731207412075120761207712078120791208012081120821208312084120851208612087120881208912090120911209212093120941209512096120971209812099121001210112102121031210412105121061210712108121091211012111121121211312114121151211612117121181211912120121211212212123121241212512126121271212812129121301213112132121331213412135121361213712138121391214012141121421214312144121451214612147121481214912150121511215212153121541215512156121571215812159121601216112162121631216412165121661216712168121691217012171121721217312174121751217612177121781217912180121811218212183121841218512186121871218812189121901219112192121931219412195121961219712198121991220012201122021220312204122051220612207122081220912210122111221212213122141221512216122171221812219122201222112222122231222412225122261222712228122291223012231122321223312234122351223612237122381223912240122411224212243122441224512246122471224812249122501225112252122531225412255122561225712258122591226012261122621226312264122651226612267122681226912270122711227212273122741227512276122771227812279122801228112282122831228412285122861228712288122891229012291122921229312294122951229612297122981229912300123011230212303123041230512306123071230812309123101231112312123131231412315123161231712318123191232012321123221232312324123251232612327123281232912330123311233212333123341233512336123371233812339123401234112342123431234412345123461234712348123491235012351123521235312354123551235612357123581235912360123611236212363123641236512366123671236812369123701237112372123731237412375123761237712378123791238012381123821238312384123851238612387123881238912390123911239212393123941239512396123971239812399124001240112402124031240412405124061240712408124091241012411124121241312414124151241612417124181241912420124211242212423124241242512426124271242812429124301243112432124331243412435124361243712438124391244012441124421244312444124451244612447124481244912450124511245212453124541245512456124571245812459124601246112462124631246412465124661246712468124691247012471124721247312474124751247612477124781247912480124811248212483124841248512486124871248812489124901249112492124931249412495124961249712498124991250012501125021250312504125051250612507125081250912510125111251212513125141251512516125171251812519125201252112522125231252412525125261252712528125291253012531125321253312534125351253612537125381253912540125411254212543125441254512546125471254812549125501255112552125531255412555125561255712558125591256012561125621256312564125651256612567125681256912570125711257212573125741257512576125771257812579125801258112582125831258412585125861258712588125891259012591125921259312594125951259612597125981259912600126011260212603126041260512606126071260812609126101261112612126131261412615126161261712618126191262012621126221262312624126251262612627126281262912630126311263212633126341263512636126371263812639126401264112642126431264412645126461264712648126491265012651126521265312654126551265612657126581265912660126611266212663126641266512666126671266812669126701267112672126731267412675126761267712678126791268012681126821268312684126851268612687126881268912690126911269212693126941269512696126971269812699127001270112702127031270412705127061270712708127091271012711127121271312714127151271612717127181271912720127211272212723127241272512726127271272812729127301273112732127331273412735127361273712738127391274012741127421274312744127451274612747127481274912750127511275212753127541275512756127571275812759127601276112762127631276412765127661276712768127691277012771127721277312774127751277612777127781277912780127811278212783127841278512786127871278812789127901279112792127931279412795127961279712798127991280012801128021280312804128051280612807128081280912810128111281212813128141281512816128171281812819128201282112822128231282412825128261282712828128291283012831128321283312834128351283612837128381283912840128411284212843128441284512846128471284812849128501285112852128531285412855128561285712858128591286012861128621286312864128651286612867128681286912870128711287212873128741287512876128771287812879128801288112882128831288412885128861288712888128891289012891128921289312894128951289612897128981289912900129011290212903129041290512906129071290812909129101291112912129131291412915129161291712918129191292012921129221292312924129251292612927129281292912930129311293212933129341293512936129371293812939129401294112942129431294412945129461294712948129491295012951129521295312954129551295612957129581295912960129611296212963129641296512966129671296812969129701297112972129731297412975129761297712978129791298012981129821298312984129851298612987129881298912990129911299212993129941299512996129971299812999130001300113002130031300413005130061300713008130091301013011130121301313014130151301613017130181301913020130211302213023130241302513026130271302813029130301303113032130331303413035130361303713038130391304013041130421304313044130451304613047130481304913050130511305213053130541305513056130571305813059130601306113062130631306413065130661306713068130691307013071130721307313074130751307613077130781307913080130811308213083130841308513086130871308813089130901309113092130931309413095130961309713098130991310013101131021310313104131051310613107131081310913110131111311213113131141311513116131171311813119131201312113122131231312413125131261312713128131291313013131131321313313134131351313613137131381313913140131411314213143131441314513146131471314813149131501315113152131531315413155131561315713158131591316013161131621316313164131651316613167131681316913170131711317213173131741317513176131771317813179131801318113182131831318413185131861318713188131891319013191131921319313194131951319613197131981319913200132011320213203132041320513206132071320813209132101321113212132131321413215132161321713218132191322013221132221322313224132251322613227132281322913230132311323213233132341323513236132371323813239132401324113242132431324413245132461324713248132491325013251132521325313254132551325613257132581325913260132611326213263132641326513266132671326813269132701327113272132731327413275132761327713278132791328013281132821328313284132851328613287132881328913290132911329213293132941329513296132971329813299133001330113302133031330413305133061330713308133091331013311133121331313314133151331613317133181331913320133211332213323133241332513326133271332813329133301333113332133331333413335133361333713338133391334013341133421334313344133451334613347133481334913350133511335213353133541335513356133571335813359133601336113362133631336413365133661336713368133691337013371133721337313374133751337613377133781337913380133811338213383133841338513386133871338813389133901339113392133931339413395133961339713398133991340013401134021340313404134051340613407134081340913410134111341213413134141341513416134171341813419134201342113422134231342413425134261342713428134291343013431134321343313434134351343613437134381343913440134411344213443134441344513446134471344813449134501345113452134531345413455134561345713458134591346013461134621346313464134651346613467134681346913470134711347213473134741347513476134771347813479134801348113482134831348413485134861348713488134891349013491134921349313494134951349613497134981349913500135011350213503135041350513506135071350813509135101351113512135131351413515135161351713518135191352013521135221352313524135251352613527135281352913530135311353213533135341353513536135371353813539135401354113542135431354413545135461354713548135491355013551135521355313554135551355613557135581355913560135611356213563135641356513566135671356813569135701357113572135731357413575135761357713578135791358013581135821358313584135851358613587135881358913590135911359213593135941359513596135971359813599136001360113602136031360413605136061360713608136091361013611136121361313614136151361613617136181361913620136211362213623136241362513626136271362813629136301363113632136331363413635136361363713638136391364013641136421364313644136451364613647136481364913650136511365213653136541365513656136571365813659136601366113662136631366413665136661366713668136691367013671136721367313674136751367613677136781367913680136811368213683136841368513686136871368813689136901369113692136931369413695136961369713698136991370013701137021370313704137051370613707137081370913710137111371213713137141371513716137171371813719137201372113722137231372413725137261372713728137291373013731137321373313734137351373613737137381373913740137411374213743137441374513746137471374813749137501375113752137531375413755137561375713758137591376013761137621376313764137651376613767137681376913770137711377213773137741377513776137771377813779137801378113782137831378413785137861378713788137891379013791137921379313794137951379613797137981379913800138011380213803138041380513806138071380813809138101381113812138131381413815138161381713818138191382013821138221382313824138251382613827138281382913830138311383213833138341383513836138371383813839138401384113842138431384413845138461384713848138491385013851138521385313854138551385613857138581385913860138611386213863138641386513866138671386813869138701387113872138731387413875138761387713878138791388013881138821388313884138851388613887138881388913890138911389213893138941389513896138971389813899139001390113902139031390413905139061390713908139091391013911139121391313914139151391613917139181391913920139211392213923139241392513926139271392813929139301393113932139331393413935139361393713938139391394013941139421394313944139451394613947139481394913950139511395213953139541395513956139571395813959139601396113962139631396413965139661396713968139691397013971139721397313974139751397613977139781397913980139811398213983139841398513986139871398813989139901399113992139931399413995139961399713998139991400014001140021400314004140051400614007140081400914010140111401214013140141401514016140171401814019140201402114022140231402414025140261402714028140291403014031140321403314034140351403614037140381403914040140411404214043140441404514046140471404814049140501405114052140531405414055140561405714058140591406014061140621406314064140651406614067140681406914070140711407214073140741407514076140771407814079140801408114082140831408414085140861408714088140891409014091140921409314094140951409614097140981409914100141011410214103141041410514106141071410814109141101411114112141131411414115141161411714118141191412014121141221412314124141251412614127141281412914130141311413214133141341413514136141371413814139141401414114142141431414414145141461414714148141491415014151141521415314154141551415614157141581415914160141611416214163141641416514166141671416814169141701417114172141731417414175141761417714178141791418014181141821418314184141851418614187141881418914190141911419214193141941419514196141971419814199142001420114202142031420414205142061420714208142091421014211142121421314214142151421614217142181421914220142211422214223142241422514226142271422814229142301423114232142331423414235142361423714238142391424014241142421424314244142451424614247142481424914250142511425214253142541425514256142571425814259142601426114262142631426414265142661426714268142691427014271142721427314274142751427614277142781427914280142811428214283142841428514286142871428814289142901429114292142931429414295142961429714298142991430014301143021430314304143051430614307143081430914310143111431214313143141431514316143171431814319143201432114322143231432414325143261432714328143291433014331143321433314334143351433614337143381433914340143411434214343143441434514346143471434814349143501435114352143531435414355143561435714358143591436014361143621436314364143651436614367143681436914370143711437214373143741437514376143771437814379143801438114382143831438414385143861438714388143891439014391143921439314394143951439614397143981439914400144011440214403144041440514406144071440814409144101441114412144131441414415144161441714418144191442014421144221442314424144251442614427144281442914430144311443214433144341443514436144371443814439144401444114442144431444414445144461444714448144491445014451144521445314454144551445614457144581445914460144611446214463144641446514466144671446814469144701447114472144731447414475144761447714478144791448014481144821448314484144851448614487144881448914490144911449214493144941449514496144971449814499145001450114502145031450414505145061450714508145091451014511145121451314514145151451614517145181451914520145211452214523145241452514526145271452814529145301453114532145331453414535145361453714538145391454014541145421454314544145451454614547145481454914550145511455214553145541455514556145571455814559145601456114562145631456414565145661456714568145691457014571145721457314574145751457614577145781457914580145811458214583145841458514586145871458814589145901459114592145931459414595145961459714598145991460014601146021460314604146051460614607146081460914610146111461214613146141461514616146171461814619146201462114622146231462414625146261462714628146291463014631146321463314634146351463614637146381463914640146411464214643146441464514646146471464814649146501465114652146531465414655146561465714658146591466014661146621466314664146651466614667146681466914670146711467214673146741467514676146771467814679146801468114682146831468414685146861468714688146891469014691146921469314694146951469614697146981469914700147011470214703147041470514706147071470814709147101471114712147131471414715147161471714718147191472014721147221472314724147251472614727147281472914730147311473214733147341473514736147371473814739147401474114742147431474414745147461474714748147491475014751147521475314754147551475614757147581475914760147611476214763147641476514766147671476814769147701477114772147731477414775147761477714778147791478014781147821478314784147851478614787147881478914790147911479214793147941479514796147971479814799148001480114802148031480414805148061480714808148091481014811148121481314814148151481614817148181481914820148211482214823148241482514826148271482814829148301483114832148331483414835148361483714838148391484014841148421484314844148451484614847148481484914850148511485214853148541485514856148571485814859148601486114862148631486414865148661486714868148691487014871148721487314874148751487614877148781487914880148811488214883148841488514886148871488814889148901489114892148931489414895148961489714898148991490014901149021490314904149051490614907149081490914910149111491214913149141491514916149171491814919149201492114922149231492414925149261492714928149291493014931149321493314934149351493614937149381493914940149411494214943149441494514946149471494814949149501495114952149531495414955149561495714958149591496014961149621496314964149651496614967149681496914970149711497214973149741497514976149771497814979149801498114982149831498414985149861498714988149891499014991149921499314994149951499614997149981499915000150011500215003150041500515006150071500815009150101501115012150131501415015150161501715018150191502015021150221502315024150251502615027150281502915030150311503215033150341503515036150371503815039150401504115042150431504415045150461504715048150491505015051150521505315054150551505615057150581505915060150611506215063150641506515066150671506815069150701507115072150731507415075150761507715078150791508015081150821508315084150851508615087150881508915090150911509215093150941509515096150971509815099151001510115102151031510415105151061510715108151091511015111151121511315114151151511615117151181511915120151211512215123151241512515126151271512815129151301513115132151331513415135151361513715138151391514015141151421514315144151451514615147151481514915150151511515215153151541515515156151571515815159151601516115162151631516415165151661516715168151691517015171151721517315174151751517615177151781517915180151811518215183151841518515186151871518815189151901519115192151931519415195151961519715198151991520015201152021520315204152051520615207152081520915210152111521215213152141521515216152171521815219152201522115222152231522415225152261522715228152291523015231152321523315234152351523615237152381523915240152411524215243152441524515246152471524815249152501525115252152531525415255152561525715258152591526015261152621526315264152651526615267152681526915270152711527215273152741527515276152771527815279152801528115282152831528415285152861528715288152891529015291152921529315294152951529615297152981529915300153011530215303153041530515306153071530815309153101531115312153131531415315153161531715318153191532015321153221532315324153251532615327153281532915330153311533215333153341533515336153371533815339153401534115342153431534415345153461534715348153491535015351153521535315354153551535615357153581535915360153611536215363153641536515366153671536815369153701537115372153731537415375153761537715378153791538015381153821538315384153851538615387153881538915390153911539215393153941539515396153971539815399154001540115402154031540415405154061540715408154091541015411154121541315414154151541615417154181541915420154211542215423154241542515426154271542815429154301543115432154331543415435154361543715438154391544015441154421544315444154451544615447154481544915450154511545215453154541545515456154571545815459154601546115462154631546415465154661546715468154691547015471154721547315474154751547615477154781547915480154811548215483154841548515486154871548815489154901549115492154931549415495154961549715498154991550015501155021550315504155051550615507155081550915510155111551215513155141551515516155171551815519155201552115522155231552415525155261552715528155291553015531155321553315534155351553615537155381553915540155411554215543155441554515546155471554815549155501555115552155531555415555155561555715558155591556015561155621556315564155651556615567155681556915570155711557215573155741557515576155771557815579155801558115582155831558415585155861558715588155891559015591155921559315594155951559615597155981559915600156011560215603156041560515606156071560815609156101561115612156131561415615156161561715618156191562015621156221562315624156251562615627156281562915630156311563215633156341563515636156371563815639156401564115642156431564415645156461564715648156491565015651156521565315654156551565615657156581565915660156611566215663156641566515666156671566815669156701567115672156731567415675156761567715678156791568015681156821568315684156851568615687156881568915690156911569215693156941569515696156971569815699157001570115702157031570415705157061570715708157091571015711157121571315714157151571615717157181571915720157211572215723157241572515726157271572815729157301573115732157331573415735157361573715738157391574015741157421574315744157451574615747157481574915750157511575215753157541575515756157571575815759157601576115762157631576415765157661576715768157691577015771157721577315774157751577615777157781577915780157811578215783157841578515786157871578815789157901579115792157931579415795157961579715798157991580015801158021580315804158051580615807158081580915810158111581215813158141581515816158171581815819158201582115822158231582415825158261582715828158291583015831158321583315834158351583615837158381583915840158411584215843158441584515846158471584815849158501585115852158531585415855158561585715858158591586015861158621586315864158651586615867158681586915870158711587215873158741587515876158771587815879158801588115882158831588415885158861588715888158891589015891158921589315894158951589615897158981589915900159011590215903159041590515906159071590815909159101591115912159131591415915159161591715918159191592015921159221592315924159251592615927159281592915930159311593215933159341593515936159371593815939159401594115942159431594415945159461594715948159491595015951159521595315954159551595615957159581595915960159611596215963159641596515966159671596815969159701597115972159731597415975159761597715978159791598015981159821598315984159851598615987159881598915990159911599215993159941599515996159971599815999160001600116002160031600416005160061600716008160091601016011160121601316014160151601616017160181601916020160211602216023160241602516026160271602816029160301603116032160331603416035160361603716038160391604016041160421604316044160451604616047160481604916050160511605216053160541605516056160571605816059160601606116062160631606416065160661606716068160691607016071160721607316074160751607616077160781607916080160811608216083160841608516086160871608816089160901609116092160931609416095160961609716098160991610016101161021610316104161051610616107161081610916110161111611216113161141611516116161171611816119161201612116122161231612416125161261612716128161291613016131161321613316134161351613616137161381613916140161411614216143161441614516146161471614816149161501615116152161531615416155161561615716158161591616016161161621616316164161651616616167161681616916170161711617216173161741617516176161771617816179161801618116182161831618416185161861618716188161891619016191161921619316194161951619616197161981619916200162011620216203162041620516206162071620816209162101621116212162131621416215162161621716218162191622016221162221622316224162251622616227162281622916230162311623216233162341623516236162371623816239162401624116242162431624416245162461624716248162491625016251162521625316254162551625616257162581625916260162611626216263162641626516266162671626816269162701627116272162731627416275162761627716278162791628016281162821628316284162851628616287162881628916290162911629216293162941629516296162971629816299163001630116302163031630416305163061630716308163091631016311163121631316314163151631616317163181631916320163211632216323163241632516326163271632816329163301633116332163331633416335163361633716338163391634016341163421634316344163451634616347163481634916350163511635216353163541635516356163571635816359163601636116362163631636416365163661636716368163691637016371163721637316374163751637616377163781637916380163811638216383163841638516386163871638816389163901639116392163931639416395163961639716398163991640016401164021640316404164051640616407164081640916410164111641216413164141641516416164171641816419164201642116422164231642416425164261642716428164291643016431164321643316434164351643616437164381643916440164411644216443164441644516446164471644816449164501645116452164531645416455164561645716458164591646016461164621646316464164651646616467164681646916470164711647216473164741647516476164771647816479164801648116482164831648416485164861648716488164891649016491164921649316494164951649616497164981649916500165011650216503165041650516506165071650816509165101651116512165131651416515165161651716518165191652016521165221652316524165251652616527165281652916530165311653216533165341653516536165371653816539165401654116542165431654416545165461654716548165491655016551165521655316554165551655616557165581655916560165611656216563165641656516566165671656816569165701657116572165731657416575165761657716578165791658016581165821658316584165851658616587165881658916590165911659216593165941659516596165971659816599166001660116602166031660416605166061660716608166091661016611166121661316614166151661616617166181661916620166211662216623166241662516626166271662816629166301663116632166331663416635166361663716638166391664016641166421664316644166451664616647166481664916650166511665216653166541665516656166571665816659166601666116662166631666416665166661666716668166691667016671166721667316674166751667616677166781667916680166811668216683166841668516686166871668816689166901669116692166931669416695166961669716698166991670016701167021670316704167051670616707167081670916710167111671216713167141671516716167171671816719167201672116722167231672416725167261672716728167291673016731167321673316734167351673616737167381673916740167411674216743167441674516746167471674816749167501675116752167531675416755167561675716758167591676016761167621676316764167651676616767167681676916770167711677216773167741677516776167771677816779167801678116782167831678416785167861678716788167891679016791167921679316794167951679616797167981679916800168011680216803168041680516806168071680816809168101681116812168131681416815168161681716818168191682016821168221682316824168251682616827168281682916830168311683216833168341683516836168371683816839168401684116842168431684416845168461684716848168491685016851168521685316854168551685616857168581685916860168611686216863168641686516866168671686816869168701687116872168731687416875168761687716878168791688016881168821688316884168851688616887168881688916890168911689216893168941689516896168971689816899169001690116902169031690416905169061690716908169091691016911169121691316914169151691616917169181691916920169211692216923169241692516926169271692816929169301693116932169331693416935169361693716938169391694016941169421694316944169451694616947169481694916950169511695216953169541695516956169571695816959169601696116962169631696416965169661696716968169691697016971169721697316974169751697616977169781697916980169811698216983169841698516986169871698816989169901699116992169931699416995169961699716998169991700017001170021700317004170051700617007170081700917010170111701217013170141701517016170171701817019170201702117022170231702417025170261702717028170291703017031170321703317034170351703617037170381703917040170411704217043170441704517046170471704817049170501705117052170531705417055170561705717058170591706017061170621706317064170651706617067170681706917070170711707217073170741707517076170771707817079170801708117082170831708417085170861708717088170891709017091170921709317094170951709617097170981709917100171011710217103171041710517106171071710817109171101711117112171131711417115171161711717118171191712017121171221712317124171251712617127171281712917130171311713217133171341713517136171371713817139171401714117142171431714417145171461714717148171491715017151171521715317154171551715617157171581715917160171611716217163171641716517166171671716817169171701717117172171731717417175171761717717178171791718017181171821718317184171851718617187171881718917190171911719217193171941719517196171971719817199172001720117202172031720417205172061720717208172091721017211172121721317214172151721617217172181721917220172211722217223172241722517226172271722817229172301723117232172331723417235172361723717238172391724017241172421724317244172451724617247172481724917250172511725217253172541725517256172571725817259172601726117262172631726417265172661726717268172691727017271172721727317274172751727617277172781727917280172811728217283172841728517286172871728817289172901729117292172931729417295172961729717298172991730017301173021730317304173051730617307173081730917310173111731217313173141731517316173171731817319173201732117322173231732417325173261732717328173291733017331173321733317334173351733617337173381733917340173411734217343173441734517346173471734817349173501735117352173531735417355173561735717358173591736017361173621736317364173651736617367173681736917370173711737217373173741737517376173771737817379173801738117382173831738417385173861738717388173891739017391173921739317394173951739617397173981739917400174011740217403174041740517406174071740817409174101741117412174131741417415174161741717418174191742017421174221742317424174251742617427174281742917430174311743217433174341743517436174371743817439174401744117442174431744417445174461744717448174491745017451174521745317454174551745617457174581745917460174611746217463174641746517466174671746817469174701747117472174731747417475174761747717478174791748017481174821748317484174851748617487174881748917490174911749217493174941749517496174971749817499175001750117502175031750417505175061750717508175091751017511175121751317514175151751617517175181751917520175211752217523175241752517526175271752817529175301753117532175331753417535175361753717538175391754017541175421754317544175451754617547175481754917550175511755217553175541755517556175571755817559175601756117562175631756417565175661756717568175691757017571175721757317574175751757617577175781757917580175811758217583175841758517586175871758817589175901759117592175931759417595175961759717598175991760017601176021760317604176051760617607176081760917610176111761217613176141761517616176171761817619176201762117622176231762417625176261762717628176291763017631176321763317634176351763617637176381763917640176411764217643176441764517646176471764817649176501765117652176531765417655176561765717658176591766017661176621766317664176651766617667176681766917670176711767217673176741767517676176771767817679176801768117682176831768417685176861768717688176891769017691176921769317694176951769617697176981769917700177011770217703177041770517706177071770817709177101771117712177131771417715177161771717718177191772017721177221772317724177251772617727177281772917730177311773217733177341773517736177371773817739177401774117742177431774417745177461774717748177491775017751177521775317754177551775617757177581775917760177611776217763177641776517766177671776817769177701777117772177731777417775177761777717778177791778017781177821778317784177851778617787177881778917790177911779217793177941779517796177971779817799178001780117802178031780417805178061780717808178091781017811178121781317814178151781617817178181781917820178211782217823178241782517826178271782817829178301783117832178331783417835178361783717838178391784017841178421784317844178451784617847178481784917850178511785217853178541785517856178571785817859178601786117862178631786417865178661786717868178691787017871178721787317874178751787617877178781787917880178811788217883178841788517886178871788817889178901789117892178931789417895178961789717898178991790017901179021790317904179051790617907179081790917910179111791217913179141791517916179171791817919179201792117922179231792417925179261792717928179291793017931179321793317934179351793617937179381793917940179411794217943179441794517946179471794817949179501795117952179531795417955179561795717958179591796017961179621796317964179651796617967179681796917970179711797217973179741797517976179771797817979179801798117982179831798417985179861798717988179891799017991179921799317994179951799617997179981799918000180011800218003180041800518006180071800818009180101801118012180131801418015180161801718018180191802018021180221802318024180251802618027180281802918030180311803218033180341803518036180371803818039180401804118042180431804418045180461804718048180491805018051180521805318054180551805618057180581805918060180611806218063180641806518066180671806818069180701807118072180731807418075180761807718078180791808018081180821808318084180851808618087180881808918090180911809218093180941809518096180971809818099181001810118102181031810418105181061810718108181091811018111181121811318114181151811618117181181811918120181211812218123181241812518126181271812818129181301813118132181331813418135181361813718138181391814018141181421814318144181451814618147181481814918150181511815218153181541815518156181571815818159181601816118162181631816418165181661816718168181691817018171181721817318174181751817618177181781817918180181811818218183181841818518186181871818818189181901819118192181931819418195181961819718198181991820018201182021820318204182051820618207182081820918210182111821218213182141821518216182171821818219182201822118222182231822418225182261822718228182291823018231182321823318234182351823618237182381823918240182411824218243182441824518246182471824818249182501825118252182531825418255182561825718258182591826018261182621826318264182651826618267182681826918270182711827218273182741827518276182771827818279182801828118282182831828418285182861828718288182891829018291182921829318294182951829618297182981829918300183011830218303183041830518306183071830818309183101831118312183131831418315183161831718318183191832018321183221832318324183251832618327183281832918330183311833218333183341833518336183371833818339183401834118342183431834418345183461834718348183491835018351183521835318354183551835618357183581835918360183611836218363183641836518366183671836818369183701837118372183731837418375183761837718378183791838018381183821838318384183851838618387183881838918390183911839218393183941839518396183971839818399184001840118402184031840418405184061840718408184091841018411184121841318414184151841618417184181841918420184211842218423184241842518426184271842818429184301843118432184331843418435184361843718438184391844018441184421844318444184451844618447184481844918450184511845218453184541845518456184571845818459184601846118462184631846418465184661846718468184691847018471184721847318474184751847618477184781847918480184811848218483184841848518486184871848818489184901849118492184931849418495184961849718498184991850018501185021850318504185051850618507185081850918510185111851218513185141851518516185171851818519185201852118522185231852418525185261852718528185291853018531185321853318534185351853618537185381853918540185411854218543185441854518546185471854818549185501855118552185531855418555185561855718558185591856018561185621856318564185651856618567185681856918570185711857218573185741857518576185771857818579185801858118582185831858418585185861858718588185891859018591185921859318594185951859618597185981859918600186011860218603186041860518606186071860818609186101861118612186131861418615186161861718618186191862018621186221862318624186251862618627186281862918630186311863218633186341863518636186371863818639186401864118642186431864418645186461864718648186491865018651186521865318654186551865618657186581865918660186611866218663186641866518666186671866818669186701867118672186731867418675186761867718678186791868018681186821868318684186851868618687186881868918690186911869218693186941869518696186971869818699187001870118702187031870418705187061870718708187091871018711187121871318714187151871618717187181871918720187211872218723187241872518726187271872818729187301873118732187331873418735187361873718738187391874018741187421874318744187451874618747187481874918750187511875218753187541875518756187571875818759187601876118762187631876418765187661876718768187691877018771187721877318774187751877618777187781877918780187811878218783187841878518786187871878818789187901879118792187931879418795187961879718798187991880018801188021880318804188051880618807188081880918810188111881218813188141881518816188171881818819188201882118822188231882418825188261882718828188291883018831188321883318834188351883618837188381883918840188411884218843188441884518846188471884818849188501885118852188531885418855188561885718858188591886018861188621886318864188651886618867188681886918870188711887218873188741887518876188771887818879188801888118882188831888418885188861888718888188891889018891188921889318894188951889618897188981889918900189011890218903189041890518906189071890818909189101891118912189131891418915189161891718918189191892018921189221892318924189251892618927189281892918930189311893218933189341893518936189371893818939189401894118942189431894418945189461894718948189491895018951189521895318954189551895618957189581895918960189611896218963189641896518966189671896818969189701897118972189731897418975189761897718978189791898018981189821898318984189851898618987189881898918990189911899218993189941899518996189971899818999190001900119002190031900419005190061900719008190091901019011190121901319014190151901619017190181901919020190211902219023190241902519026190271902819029190301903119032190331903419035190361903719038190391904019041190421904319044190451904619047190481904919050190511905219053190541905519056190571905819059190601906119062190631906419065190661906719068190691907019071190721907319074190751907619077190781907919080190811908219083190841908519086190871908819089190901909119092190931909419095190961909719098190991910019101191021910319104191051910619107191081910919110191111911219113191141911519116191171911819119191201912119122191231912419125191261912719128191291913019131191321913319134191351913619137191381913919140191411914219143191441914519146191471914819149191501915119152191531915419155191561915719158191591916019161191621916319164191651916619167191681916919170191711917219173191741917519176191771917819179191801918119182191831918419185191861918719188191891919019191191921919319194191951919619197191981919919200192011920219203192041920519206192071920819209192101921119212192131921419215192161921719218192191922019221192221922319224192251922619227192281922919230192311923219233192341923519236192371923819239192401924119242192431924419245192461924719248192491925019251192521925319254192551925619257192581925919260192611926219263192641926519266192671926819269192701927119272192731927419275192761927719278192791928019281192821928319284192851928619287192881928919290192911929219293192941929519296192971929819299193001930119302193031930419305193061930719308193091931019311193121931319314193151931619317193181931919320193211932219323193241932519326193271932819329193301933119332193331933419335193361933719338193391934019341193421934319344193451934619347193481934919350193511935219353193541935519356193571935819359193601936119362193631936419365193661936719368193691937019371193721937319374193751937619377193781937919380193811938219383193841938519386193871938819389193901939119392193931939419395193961939719398193991940019401194021940319404194051940619407194081940919410194111941219413194141941519416194171941819419194201942119422194231942419425194261942719428194291943019431194321943319434194351943619437194381943919440194411944219443194441944519446194471944819449194501945119452194531945419455194561945719458194591946019461194621946319464194651946619467194681946919470194711947219473194741947519476194771947819479194801948119482194831948419485194861948719488194891949019491194921949319494194951949619497194981949919500195011950219503195041950519506195071950819509195101951119512195131951419515195161951719518195191952019521195221952319524195251952619527195281952919530195311953219533195341953519536195371953819539195401954119542195431954419545195461954719548195491955019551195521955319554195551955619557195581955919560195611956219563195641956519566195671956819569195701957119572195731957419575195761957719578195791958019581195821958319584195851958619587195881958919590195911959219593195941959519596195971959819599196001960119602196031960419605196061960719608196091961019611196121961319614196151961619617196181961919620196211962219623196241962519626196271962819629196301963119632196331963419635196361963719638196391964019641196421964319644196451964619647196481964919650196511965219653196541965519656196571965819659196601966119662196631966419665196661966719668196691967019671196721967319674196751967619677196781967919680196811968219683196841968519686196871968819689196901969119692196931969419695196961969719698196991970019701197021970319704197051970619707197081970919710197111971219713197141971519716197171971819719197201972119722197231972419725197261972719728197291973019731197321973319734197351973619737197381973919740197411974219743197441974519746197471974819749197501975119752197531975419755197561975719758197591976019761197621976319764197651976619767197681976919770197711977219773197741977519776197771977819779197801978119782197831978419785197861978719788197891979019791197921979319794197951979619797197981979919800198011980219803198041980519806198071980819809198101981119812198131981419815198161981719818198191982019821198221982319824198251982619827198281982919830198311983219833198341983519836198371983819839198401984119842198431984419845198461984719848198491985019851198521985319854198551985619857198581985919860198611986219863198641986519866198671986819869198701987119872198731987419875198761987719878198791988019881198821988319884198851988619887198881988919890198911989219893198941989519896198971989819899199001990119902199031990419905199061990719908199091991019911199121991319914199151991619917199181991919920199211992219923199241992519926199271992819929199301993119932199331993419935199361993719938199391994019941199421994319944199451994619947199481994919950199511995219953199541995519956199571995819959199601996119962199631996419965199661996719968199691997019971199721997319974199751997619977199781997919980199811998219983199841998519986199871998819989199901999119992199931999419995199961999719998199992000020001200022000320004200052000620007200082000920010200112001220013200142001520016200172001820019200202002120022200232002420025200262002720028200292003020031200322003320034200352003620037200382003920040200412004220043200442004520046200472004820049200502005120052200532005420055200562005720058200592006020061200622006320064200652006620067200682006920070200712007220073200742007520076200772007820079200802008120082200832008420085200862008720088200892009020091200922009320094200952009620097200982009920100201012010220103201042010520106201072010820109201102011120112201132011420115201162011720118201192012020121201222012320124201252012620127201282012920130201312013220133201342013520136201372013820139201402014120142201432014420145201462014720148201492015020151201522015320154201552015620157201582015920160201612016220163201642016520166201672016820169201702017120172201732017420175201762017720178201792018020181201822018320184201852018620187201882018920190201912019220193201942019520196201972019820199202002020120202202032020420205202062020720208202092021020211202122021320214202152021620217202182021920220202212022220223202242022520226202272022820229202302023120232202332023420235202362023720238202392024020241202422024320244202452024620247202482024920250202512025220253202542025520256202572025820259202602026120262202632026420265202662026720268202692027020271202722027320274202752027620277202782027920280202812028220283202842028520286202872028820289202902029120292202932029420295202962029720298202992030020301203022030320304203052030620307203082030920310203112031220313203142031520316203172031820319203202032120322203232032420325203262032720328203292033020331203322033320334203352033620337203382033920340203412034220343203442034520346203472034820349203502035120352203532035420355203562035720358203592036020361203622036320364203652036620367203682036920370203712037220373203742037520376203772037820379203802038120382203832038420385203862038720388203892039020391203922039320394203952039620397203982039920400204012040220403204042040520406204072040820409204102041120412204132041420415204162041720418204192042020421204222042320424204252042620427204282042920430204312043220433204342043520436204372043820439204402044120442204432044420445204462044720448204492045020451204522045320454204552045620457204582045920460204612046220463204642046520466204672046820469204702047120472204732047420475204762047720478204792048020481204822048320484204852048620487204882048920490204912049220493204942049520496204972049820499205002050120502205032050420505205062050720508205092051020511205122051320514205152051620517205182051920520205212052220523205242052520526205272052820529205302053120532205332053420535205362053720538205392054020541205422054320544205452054620547205482054920550205512055220553205542055520556205572055820559205602056120562205632056420565205662056720568205692057020571205722057320574205752057620577205782057920580205812058220583205842058520586205872058820589205902059120592205932059420595205962059720598205992060020601206022060320604206052060620607206082060920610206112061220613206142061520616206172061820619206202062120622206232062420625206262062720628206292063020631206322063320634206352063620637206382063920640206412064220643206442064520646206472064820649206502065120652206532065420655206562065720658206592066020661206622066320664206652066620667206682066920670206712067220673206742067520676206772067820679206802068120682206832068420685206862068720688206892069020691206922069320694206952069620697206982069920700207012070220703207042070520706207072070820709207102071120712207132071420715207162071720718207192072020721207222072320724207252072620727207282072920730207312073220733207342073520736207372073820739207402074120742207432074420745207462074720748207492075020751207522075320754207552075620757207582075920760207612076220763207642076520766207672076820769207702077120772207732077420775207762077720778207792078020781207822078320784207852078620787207882078920790207912079220793207942079520796207972079820799208002080120802208032080420805208062080720808208092081020811208122081320814208152081620817208182081920820208212082220823208242082520826208272082820829208302083120832208332083420835208362083720838208392084020841208422084320844208452084620847208482084920850208512085220853208542085520856208572085820859208602086120862208632086420865208662086720868208692087020871208722087320874208752087620877208782087920880208812088220883208842088520886208872088820889208902089120892208932089420895208962089720898208992090020901209022090320904209052090620907209082090920910209112091220913209142091520916209172091820919209202092120922209232092420925209262092720928209292093020931209322093320934209352093620937209382093920940209412094220943209442094520946209472094820949209502095120952209532095420955209562095720958209592096020961209622096320964209652096620967209682096920970209712097220973209742097520976209772097820979209802098120982209832098420985209862098720988209892099020991209922099320994209952099620997209982099921000210012100221003210042100521006210072100821009210102101121012210132101421015210162101721018210192102021021210222102321024210252102621027210282102921030210312103221033210342103521036210372103821039210402104121042210432104421045210462104721048210492105021051210522105321054210552105621057210582105921060210612106221063210642106521066210672106821069210702107121072210732107421075210762107721078210792108021081210822108321084210852108621087210882108921090210912109221093210942109521096210972109821099211002110121102211032110421105211062110721108211092111021111211122111321114211152111621117211182111921120211212112221123211242112521126211272112821129211302113121132211332113421135211362113721138211392114021141211422114321144211452114621147211482114921150211512115221153211542115521156
  1. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.openpgp = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(_dereq_,module,exports){
  2. /*! asmCrypto Lite v1.1.0, (c) 2013 Artem S Vybornov, opensource.org/licenses/MIT */
  3. (function ( exports, global ) {
  4. function IllegalStateError () { var err = Error.apply( this, arguments ); this.message = err.message, this.stack = err.stack; }
  5. IllegalStateError.prototype = Object.create( Error.prototype, { name: { value: 'IllegalStateError' } } );
  6. function IllegalArgumentError () { var err = Error.apply( this, arguments ); this.message = err.message, this.stack = err.stack; }
  7. IllegalArgumentError.prototype = Object.create( Error.prototype, { name: { value: 'IllegalArgumentError' } } );
  8. function SecurityError () { var err = Error.apply( this, arguments ); this.message = err.message, this.stack = err.stack; }
  9. SecurityError.prototype = Object.create( Error.prototype, { name: { value: 'SecurityError' } } );
  10. var FloatArray = global.Float64Array || global.Float32Array; // make PhantomJS happy
  11. function string_to_bytes ( str, utf8 ) {
  12. utf8 = !!utf8;
  13. var len = str.length,
  14. bytes = new Uint8Array( utf8 ? 4*len : len );
  15. for ( var i = 0, j = 0; i < len; i++ ) {
  16. var c = str.charCodeAt(i);
  17. if ( utf8 && 0xd800 <= c && c <= 0xdbff ) {
  18. if ( ++i >= len ) throw new Error( "Malformed string, low surrogate expected at position " + i );
  19. c = ( (c ^ 0xd800) << 10 ) | 0x10000 | ( str.charCodeAt(i) ^ 0xdc00 );
  20. }
  21. else if ( !utf8 && c >>> 8 ) {
  22. throw new Error("Wide characters are not allowed.");
  23. }
  24. if ( !utf8 || c <= 0x7f ) {
  25. bytes[j++] = c;
  26. }
  27. else if ( c <= 0x7ff ) {
  28. bytes[j++] = 0xc0 | (c >> 6);
  29. bytes[j++] = 0x80 | (c & 0x3f);
  30. }
  31. else if ( c <= 0xffff ) {
  32. bytes[j++] = 0xe0 | (c >> 12);
  33. bytes[j++] = 0x80 | (c >> 6 & 0x3f);
  34. bytes[j++] = 0x80 | (c & 0x3f);
  35. }
  36. else {
  37. bytes[j++] = 0xf0 | (c >> 18);
  38. bytes[j++] = 0x80 | (c >> 12 & 0x3f);
  39. bytes[j++] = 0x80 | (c >> 6 & 0x3f);
  40. bytes[j++] = 0x80 | (c & 0x3f);
  41. }
  42. }
  43. return bytes.subarray(0, j);
  44. }
  45. function hex_to_bytes ( str ) {
  46. var len = str.length;
  47. if ( len & 1 ) {
  48. str = '0'+str;
  49. len++;
  50. }
  51. var bytes = new Uint8Array(len>>1);
  52. for ( var i = 0; i < len; i += 2 ) {
  53. bytes[i>>1] = parseInt( str.substr( i, 2), 16 );
  54. }
  55. return bytes;
  56. }
  57. function base64_to_bytes ( str ) {
  58. return string_to_bytes( atob( str ) );
  59. }
  60. function bytes_to_string ( bytes, utf8 ) {
  61. utf8 = !!utf8;
  62. var len = bytes.length,
  63. chars = new Array(len);
  64. for ( var i = 0, j = 0; i < len; i++ ) {
  65. var b = bytes[i];
  66. if ( !utf8 || b < 128 ) {
  67. chars[j++] = b;
  68. }
  69. else if ( b >= 192 && b < 224 && i+1 < len ) {
  70. chars[j++] = ( (b & 0x1f) << 6 ) | (bytes[++i] & 0x3f);
  71. }
  72. else if ( b >= 224 && b < 240 && i+2 < len ) {
  73. chars[j++] = ( (b & 0xf) << 12 ) | ( (bytes[++i] & 0x3f) << 6 ) | (bytes[++i] & 0x3f);
  74. }
  75. else if ( b >= 240 && b < 248 && i+3 < len ) {
  76. var c = ( (b & 7) << 18 ) | ( (bytes[++i] & 0x3f) << 12 ) | ( (bytes[++i] & 0x3f) << 6 ) | (bytes[++i] & 0x3f);
  77. if ( c <= 0xffff ) {
  78. chars[j++] = c;
  79. }
  80. else {
  81. c ^= 0x10000;
  82. chars[j++] = 0xd800 | (c >> 10);
  83. chars[j++] = 0xdc00 | (c & 0x3ff);
  84. }
  85. }
  86. else {
  87. throw new Error("Malformed UTF8 character at byte offset " + i);
  88. }
  89. }
  90. var str = '',
  91. bs = 16384;
  92. for ( var i = 0; i < j; i += bs ) {
  93. str += String.fromCharCode.apply( String, chars.slice( i, i+bs <= j ? i+bs : j ) );
  94. }
  95. return str;
  96. }
  97. function bytes_to_hex ( arr ) {
  98. var str = '';
  99. for ( var i = 0; i < arr.length; i++ ) {
  100. var h = ( arr[i] & 0xff ).toString(16);
  101. if ( h.length < 2 ) str += '0';
  102. str += h;
  103. }
  104. return str;
  105. }
  106. function bytes_to_base64 ( arr ) {
  107. return btoa( bytes_to_string(arr) );
  108. }
  109. function pow2_ceil ( a ) {
  110. a -= 1;
  111. a |= a >>> 1;
  112. a |= a >>> 2;
  113. a |= a >>> 4;
  114. a |= a >>> 8;
  115. a |= a >>> 16;
  116. a += 1;
  117. return a;
  118. }
  119. function is_number ( a ) {
  120. return ( typeof a === 'number' );
  121. }
  122. function is_string ( a ) {
  123. return ( typeof a === 'string' );
  124. }
  125. function is_buffer ( a ) {
  126. return ( a instanceof ArrayBuffer );
  127. }
  128. function is_bytes ( a ) {
  129. return ( a instanceof Uint8Array );
  130. }
  131. function is_typed_array ( a ) {
  132. return ( a instanceof Int8Array ) || ( a instanceof Uint8Array )
  133. || ( a instanceof Int16Array ) || ( a instanceof Uint16Array )
  134. || ( a instanceof Int32Array ) || ( a instanceof Uint32Array )
  135. || ( a instanceof Float32Array )
  136. || ( a instanceof Float64Array );
  137. }
  138. function _heap_init ( constructor, options ) {
  139. var heap = options.heap,
  140. size = heap ? heap.byteLength : options.heapSize || 65536;
  141. if ( size & 0xfff || size <= 0 )
  142. throw new Error("heap size must be a positive integer and a multiple of 4096");
  143. heap = heap || new constructor( new ArrayBuffer(size) );
  144. return heap;
  145. }
  146. function _heap_write ( heap, hpos, data, dpos, dlen ) {
  147. var hlen = heap.length - hpos,
  148. wlen = ( hlen < dlen ) ? hlen : dlen;
  149. heap.set( data.subarray( dpos, dpos+wlen ), hpos );
  150. return wlen;
  151. }
  152. /**
  153. * Error definitions
  154. */
  155. global.IllegalStateError = IllegalStateError;
  156. global.IllegalArgumentError = IllegalArgumentError;
  157. global.SecurityError = SecurityError;
  158. /**
  159. * @file {@link http://asmjs.org Asm.js} implementation of the {@link https://en.wikipedia.org/wiki/Advanced_Encryption_Standard Advanced Encryption Standard}.
  160. * @author Artem S Vybornov <vybornov@gmail.com>
  161. * @license MIT
  162. */
  163. var AES_asm = function () {
  164. "use strict";
  165. /**
  166. * Galois Field stuff init flag
  167. */
  168. var ginit_done = false;
  169. /**
  170. * Galois Field exponentiation and logarithm tables for 3 (the generator)
  171. */
  172. var gexp3, glog3;
  173. /**
  174. * Init Galois Field tables
  175. */
  176. function ginit () {
  177. gexp3 = [],
  178. glog3 = [];
  179. var a = 1, c, d;
  180. for ( c = 0; c < 255; c++ ) {
  181. gexp3[c] = a;
  182. // Multiply by three
  183. d = a & 0x80, a <<= 1, a &= 255;
  184. if ( d === 0x80 ) a ^= 0x1b;
  185. a ^= gexp3[c];
  186. // Set the log table value
  187. glog3[gexp3[c]] = c;
  188. }
  189. gexp3[255] = gexp3[0];
  190. glog3[0] = 0;
  191. ginit_done = true;
  192. }
  193. /**
  194. * Galois Field multiplication
  195. * @param {int} a
  196. * @param {int} b
  197. * @return {int}
  198. */
  199. function gmul ( a, b ) {
  200. var c = gexp3[ ( glog3[a] + glog3[b] ) % 255 ];
  201. if ( a === 0 || b === 0 ) c = 0;
  202. return c;
  203. }
  204. /**
  205. * Galois Field reciprocal
  206. * @param {int} a
  207. * @return {int}
  208. */
  209. function ginv ( a ) {
  210. var i = gexp3[ 255 - glog3[a] ];
  211. if ( a === 0 ) i = 0;
  212. return i;
  213. }
  214. /**
  215. * AES stuff init flag
  216. */
  217. var aes_init_done = false;
  218. /**
  219. * Encryption, Decryption, S-Box and KeyTransform tables
  220. */
  221. var aes_sbox, aes_sinv, aes_enc, aes_dec;
  222. /**
  223. * Init AES tables
  224. */
  225. function aes_init () {
  226. if ( !ginit_done ) ginit();
  227. // Calculates AES S-Box value
  228. function _s ( a ) {
  229. var c, s, x;
  230. s = x = ginv(a);
  231. for ( c = 0; c < 4; c++ ) {
  232. s = ( (s << 1) | (s >>> 7) ) & 255;
  233. x ^= s;
  234. }
  235. x ^= 99;
  236. return x;
  237. }
  238. // Tables
  239. aes_sbox = [],
  240. aes_sinv = [],
  241. aes_enc = [ [], [], [], [] ],
  242. aes_dec = [ [], [], [], [] ];
  243. for ( var i = 0; i < 256; i++ ) {
  244. var s = _s(i);
  245. // S-Box and its inverse
  246. aes_sbox[i] = s;
  247. aes_sinv[s] = i;
  248. // Ecryption and Decryption tables
  249. aes_enc[0][i] = ( gmul( 2, s ) << 24 ) | ( s << 16 ) | ( s << 8 ) | gmul( 3, s );
  250. aes_dec[0][s] = ( gmul( 14, i ) << 24 ) | ( gmul( 9, i ) << 16 ) | ( gmul( 13, i ) << 8 ) | gmul( 11, i );
  251. // Rotate tables
  252. for ( var t = 1; t < 4; t++ ) {
  253. aes_enc[t][i] = ( aes_enc[t-1][i] >>> 8 ) | ( aes_enc[t-1][i] << 24 );
  254. aes_dec[t][s] = ( aes_dec[t-1][s] >>> 8 ) | ( aes_dec[t-1][s] << 24 );
  255. }
  256. }
  257. }
  258. /**
  259. * Asm.js module constructor.
  260. *
  261. * <p>
  262. * Heap buffer layout by offset:
  263. * <pre>
  264. * 0x0000 encryption key schedule
  265. * 0x0400 decryption key schedule
  266. * 0x0800 sbox
  267. * 0x0c00 inv sbox
  268. * 0x1000 encryption tables
  269. * 0x2000 decryption tables
  270. * 0x3000 reserved (future GCM multiplication lookup table)
  271. * 0x4000 data
  272. * </pre>
  273. * Don't touch anything before <code>0x400</code>.
  274. * </p>
  275. *
  276. * @alias AES_asm
  277. * @class
  278. * @param {GlobalScope} stdlib - global scope object (e.g. <code>window</code>)
  279. * @param {Object} foreign - <i>ignored</i>
  280. * @param {ArrayBuffer} buffer - heap buffer to link with
  281. */
  282. var wrapper = function ( stdlib, foreign, buffer ) {
  283. // Init AES stuff for the first time
  284. if ( !aes_init_done ) aes_init();
  285. // Fill up AES tables
  286. var heap = new Uint32Array(buffer);
  287. heap.set( aes_sbox, 0x0800>>2 );
  288. heap.set( aes_sinv, 0x0c00>>2 );
  289. for ( var i = 0; i < 4; i++ ) {
  290. heap.set( aes_enc[i], ( 0x1000 + 0x400 * i )>>2 );
  291. heap.set( aes_dec[i], ( 0x2000 + 0x400 * i )>>2 );
  292. }
  293. /**
  294. * Calculate AES key schedules.
  295. * @instance
  296. * @memberof AES_asm
  297. * @param {int} ks - key size, 4/6/8 (for 128/192/256-bit key correspondingly)
  298. * @param {int} k0..k7 - key vector components
  299. */
  300. function set_key ( ks, k0, k1, k2, k3, k4, k5, k6, k7 ) {
  301. var ekeys = heap.subarray( 0x000, 60 ),
  302. dkeys = heap.subarray( 0x100, 0x100+60 );
  303. // Encryption key schedule
  304. ekeys.set( [ k0, k1, k2, k3, k4, k5, k6, k7 ] );
  305. for ( var i = ks, rcon = 1; i < 4*ks+28; i++ ) {
  306. var k = ekeys[i-1];
  307. if ( ( i % ks === 0 ) || ( ks === 8 && i % ks === 4 ) ) {
  308. k = aes_sbox[k>>>24]<<24 ^ aes_sbox[k>>>16&255]<<16 ^ aes_sbox[k>>>8&255]<<8 ^ aes_sbox[k&255];
  309. }
  310. if ( i % ks === 0 ) {
  311. k = (k << 8) ^ (k >>> 24) ^ (rcon << 24);
  312. rcon = (rcon << 1) ^ ( (rcon & 0x80) ? 0x1b : 0 );
  313. }
  314. ekeys[i] = ekeys[i-ks] ^ k;
  315. }
  316. // Decryption key schedule
  317. for ( var j = 0; j < i; j += 4 ) {
  318. for ( var jj = 0; jj < 4; jj++ ) {
  319. var k = ekeys[i-(4+j)+(4-jj)%4];
  320. if ( j < 4 || j >= i-4 ) {
  321. dkeys[j+jj] = k;
  322. } else {
  323. dkeys[j+jj] = aes_dec[0][aes_sbox[k>>>24]]
  324. ^ aes_dec[1][aes_sbox[k>>>16&255]]
  325. ^ aes_dec[2][aes_sbox[k>>>8&255]]
  326. ^ aes_dec[3][aes_sbox[k&255]];
  327. }
  328. }
  329. }
  330. // Set rounds number
  331. asm.set_rounds( ks + 5 );
  332. }
  333. var asm = function ( stdlib, foreign, buffer ) {
  334. "use asm";
  335. var S0 = 0, S1 = 0, S2 = 0, S3 = 0,
  336. I0 = 0, I1 = 0, I2 = 0, I3 = 0,
  337. N0 = 0, N1 = 0, N2 = 0, N3 = 0,
  338. M0 = 0, M1 = 0, M2 = 0, M3 = 0,
  339. H0 = 0, H1 = 0, H2 = 0, H3 = 0,
  340. R = 0;
  341. var HEAP = new stdlib.Uint32Array(buffer),
  342. DATA = new stdlib.Uint8Array(buffer);
  343. /**
  344. * AES core
  345. * @param {int} k - precomputed key schedule offset
  346. * @param {int} s - precomputed sbox table offset
  347. * @param {int} t - precomputed round table offset
  348. * @param {int} r - number of inner rounds to perform
  349. * @param {int} x0..x3 - 128-bit input block vector
  350. */
  351. function _core ( k, s, t, r, x0, x1, x2, x3 ) {
  352. k = k|0;
  353. s = s|0;
  354. t = t|0;
  355. r = r|0;
  356. x0 = x0|0;
  357. x1 = x1|0;
  358. x2 = x2|0;
  359. x3 = x3|0;
  360. var t1 = 0, t2 = 0, t3 = 0,
  361. y0 = 0, y1 = 0, y2 = 0, y3 = 0,
  362. i = 0;
  363. t1 = t|0x400, t2 = t|0x800, t3 = t|0xc00;
  364. // round 0
  365. x0 = x0 ^ HEAP[(k|0)>>2],
  366. x1 = x1 ^ HEAP[(k|4)>>2],
  367. x2 = x2 ^ HEAP[(k|8)>>2],
  368. x3 = x3 ^ HEAP[(k|12)>>2];
  369. // round 1..r
  370. for ( i = 16; (i|0) <= (r<<4); i = (i+16)|0 ) {
  371. y0 = HEAP[(t|x0>>22&1020)>>2] ^ HEAP[(t1|x1>>14&1020)>>2] ^ HEAP[(t2|x2>>6&1020)>>2] ^ HEAP[(t3|x3<<2&1020)>>2] ^ HEAP[(k|i|0)>>2],
  372. y1 = HEAP[(t|x1>>22&1020)>>2] ^ HEAP[(t1|x2>>14&1020)>>2] ^ HEAP[(t2|x3>>6&1020)>>2] ^ HEAP[(t3|x0<<2&1020)>>2] ^ HEAP[(k|i|4)>>2],
  373. y2 = HEAP[(t|x2>>22&1020)>>2] ^ HEAP[(t1|x3>>14&1020)>>2] ^ HEAP[(t2|x0>>6&1020)>>2] ^ HEAP[(t3|x1<<2&1020)>>2] ^ HEAP[(k|i|8)>>2],
  374. y3 = HEAP[(t|x3>>22&1020)>>2] ^ HEAP[(t1|x0>>14&1020)>>2] ^ HEAP[(t2|x1>>6&1020)>>2] ^ HEAP[(t3|x2<<2&1020)>>2] ^ HEAP[(k|i|12)>>2];
  375. x0 = y0, x1 = y1, x2 = y2, x3 = y3;
  376. }
  377. // final round
  378. S0 = HEAP[(s|x0>>22&1020)>>2]<<24 ^ HEAP[(s|x1>>14&1020)>>2]<<16 ^ HEAP[(s|x2>>6&1020)>>2]<<8 ^ HEAP[(s|x3<<2&1020)>>2] ^ HEAP[(k|i|0)>>2],
  379. S1 = HEAP[(s|x1>>22&1020)>>2]<<24 ^ HEAP[(s|x2>>14&1020)>>2]<<16 ^ HEAP[(s|x3>>6&1020)>>2]<<8 ^ HEAP[(s|x0<<2&1020)>>2] ^ HEAP[(k|i|4)>>2],
  380. S2 = HEAP[(s|x2>>22&1020)>>2]<<24 ^ HEAP[(s|x3>>14&1020)>>2]<<16 ^ HEAP[(s|x0>>6&1020)>>2]<<8 ^ HEAP[(s|x1<<2&1020)>>2] ^ HEAP[(k|i|8)>>2],
  381. S3 = HEAP[(s|x3>>22&1020)>>2]<<24 ^ HEAP[(s|x0>>14&1020)>>2]<<16 ^ HEAP[(s|x1>>6&1020)>>2]<<8 ^ HEAP[(s|x2<<2&1020)>>2] ^ HEAP[(k|i|12)>>2];
  382. }
  383. /**
  384. * ECB mode encryption
  385. * @param {int} x0..x3 - 128-bit input block vector
  386. */
  387. function _ecb_enc ( x0, x1, x2, x3 ) {
  388. x0 = x0|0;
  389. x1 = x1|0;
  390. x2 = x2|0;
  391. x3 = x3|0;
  392. _core(
  393. 0x0000, 0x0800, 0x1000,
  394. R,
  395. x0,
  396. x1,
  397. x2,
  398. x3
  399. );
  400. }
  401. /**
  402. * ECB mode decryption
  403. * @param {int} x0..x3 - 128-bit input block vector
  404. */
  405. function _ecb_dec ( x0, x1, x2, x3 ) {
  406. x0 = x0|0;
  407. x1 = x1|0;
  408. x2 = x2|0;
  409. x3 = x3|0;
  410. var t = 0;
  411. _core(
  412. 0x0400, 0x0c00, 0x2000,
  413. R,
  414. x0,
  415. x3,
  416. x2,
  417. x1
  418. );
  419. t = S1, S1 = S3, S3 = t;
  420. }
  421. /**
  422. * CBC mode encryption
  423. * @param {int} x0..x3 - 128-bit input block vector
  424. */
  425. function _cbc_enc ( x0, x1, x2, x3 ) {
  426. x0 = x0|0;
  427. x1 = x1|0;
  428. x2 = x2|0;
  429. x3 = x3|0;
  430. _core(
  431. 0x0000, 0x0800, 0x1000,
  432. R,
  433. I0 ^ x0,
  434. I1 ^ x1,
  435. I2 ^ x2,
  436. I3 ^ x3
  437. );
  438. I0 = S0,
  439. I1 = S1,
  440. I2 = S2,
  441. I3 = S3;
  442. }
  443. /**
  444. * CBC mode decryption
  445. * @param {int} x0..x3 - 128-bit input block vector
  446. */
  447. function _cbc_dec ( x0, x1, x2, x3 ) {
  448. x0 = x0|0;
  449. x1 = x1|0;
  450. x2 = x2|0;
  451. x3 = x3|0;
  452. var t = 0;
  453. _core(
  454. 0x0400, 0x0c00, 0x2000,
  455. R,
  456. x0,
  457. x3,
  458. x2,
  459. x1
  460. );
  461. t = S1, S1 = S3, S3 = t;
  462. S0 = S0 ^ I0,
  463. S1 = S1 ^ I1,
  464. S2 = S2 ^ I2,
  465. S3 = S3 ^ I3;
  466. I0 = x0,
  467. I1 = x1,
  468. I2 = x2,
  469. I3 = x3;
  470. }
  471. /**
  472. * CFB mode encryption
  473. * @param {int} x0..x3 - 128-bit input block vector
  474. */
  475. function _cfb_enc ( x0, x1, x2, x3 ) {
  476. x0 = x0|0;
  477. x1 = x1|0;
  478. x2 = x2|0;
  479. x3 = x3|0;
  480. _core(
  481. 0x0000, 0x0800, 0x1000,
  482. R,
  483. I0,
  484. I1,
  485. I2,
  486. I3
  487. );
  488. I0 = S0 = S0 ^ x0,
  489. I1 = S1 = S1 ^ x1,
  490. I2 = S2 = S2 ^ x2,
  491. I3 = S3 = S3 ^ x3;
  492. }
  493. /**
  494. * CFB mode decryption
  495. * @param {int} x0..x3 - 128-bit input block vector
  496. */
  497. function _cfb_dec ( x0, x1, x2, x3 ) {
  498. x0 = x0|0;
  499. x1 = x1|0;
  500. x2 = x2|0;
  501. x3 = x3|0;
  502. _core(
  503. 0x0000, 0x0800, 0x1000,
  504. R,
  505. I0,
  506. I1,
  507. I2,
  508. I3
  509. );
  510. S0 = S0 ^ x0,
  511. S1 = S1 ^ x1,
  512. S2 = S2 ^ x2,
  513. S3 = S3 ^ x3;
  514. I0 = x0,
  515. I1 = x1,
  516. I2 = x2,
  517. I3 = x3;
  518. }
  519. /**
  520. * OFB mode encryption / decryption
  521. * @param {int} x0..x3 - 128-bit input block vector
  522. */
  523. function _ofb ( x0, x1, x2, x3 ) {
  524. x0 = x0|0;
  525. x1 = x1|0;
  526. x2 = x2|0;
  527. x3 = x3|0;
  528. _core(
  529. 0x0000, 0x0800, 0x1000,
  530. R,
  531. I0,
  532. I1,
  533. I2,
  534. I3
  535. );
  536. I0 = S0,
  537. I1 = S1,
  538. I2 = S2,
  539. I3 = S3;
  540. S0 = S0 ^ x0,
  541. S1 = S1 ^ x1,
  542. S2 = S2 ^ x2,
  543. S3 = S3 ^ x3;
  544. }
  545. /**
  546. * CTR mode encryption / decryption
  547. * @param {int} x0..x3 - 128-bit input block vector
  548. */
  549. function _ctr ( x0, x1, x2, x3 ) {
  550. x0 = x0|0;
  551. x1 = x1|0;
  552. x2 = x2|0;
  553. x3 = x3|0;
  554. _core(
  555. 0x0000, 0x0800, 0x1000,
  556. R,
  557. N0,
  558. N1,
  559. N2,
  560. N3
  561. );
  562. N3 = ( ~M3 & N3 ) | M3 & ( N3 + 1 ),
  563. N2 = ( ~M2 & N2 ) | M2 & ( N2 + ( (N3|0) == 0 ) ),
  564. N1 = ( ~M1 & N1 ) | M1 & ( N1 + ( (N2|0) == 0 ) ),
  565. N0 = ( ~M0 & N0 ) | M0 & ( N0 + ( (N1|0) == 0 ) );
  566. S0 = S0 ^ x0,
  567. S1 = S1 ^ x1,
  568. S2 = S2 ^ x2,
  569. S3 = S3 ^ x3;
  570. }
  571. /**
  572. * GCM mode MAC calculation
  573. * @param {int} x0..x3 - 128-bit input block vector
  574. */
  575. function _gcm_mac ( x0, x1, x2, x3 ) {
  576. x0 = x0|0;
  577. x1 = x1|0;
  578. x2 = x2|0;
  579. x3 = x3|0;
  580. var y0 = 0, y1 = 0, y2 = 0, y3 = 0,
  581. z0 = 0, z1 = 0, z2 = 0, z3 = 0,
  582. i = 0, c = 0;
  583. x0 = x0 ^ I0,
  584. x1 = x1 ^ I1,
  585. x2 = x2 ^ I2,
  586. x3 = x3 ^ I3;
  587. y0 = H0|0,
  588. y1 = H1|0,
  589. y2 = H2|0,
  590. y3 = H3|0;
  591. for ( ; (i|0) < 128; i = (i + 1)|0 ) {
  592. if ( y0 >>> 31 ) {
  593. z0 = z0 ^ x0,
  594. z1 = z1 ^ x1,
  595. z2 = z2 ^ x2,
  596. z3 = z3 ^ x3;
  597. }
  598. y0 = (y0 << 1) | (y1 >>> 31),
  599. y1 = (y1 << 1) | (y2 >>> 31),
  600. y2 = (y2 << 1) | (y3 >>> 31),
  601. y3 = (y3 << 1);
  602. c = x3 & 1;
  603. x3 = (x3 >>> 1) | (x2 << 31),
  604. x2 = (x2 >>> 1) | (x1 << 31),
  605. x1 = (x1 >>> 1) | (x0 << 31),
  606. x0 = (x0 >>> 1);
  607. if ( c ) x0 = x0 ^ 0xe1000000;
  608. }
  609. I0 = z0,
  610. I1 = z1,
  611. I2 = z2,
  612. I3 = z3;
  613. }
  614. /**
  615. * Set the internal rounds number.
  616. * @instance
  617. * @memberof AES_asm
  618. * @param {int} r - number if inner AES rounds
  619. */
  620. function set_rounds ( r ) {
  621. r = r|0;
  622. R = r;
  623. }
  624. /**
  625. * Populate the internal state of the module.
  626. * @instance
  627. * @memberof AES_asm
  628. * @param {int} s0...s3 - state vector
  629. */
  630. function set_state ( s0, s1, s2, s3 ) {
  631. s0 = s0|0;
  632. s1 = s1|0;
  633. s2 = s2|0;
  634. s3 = s3|0;
  635. S0 = s0,
  636. S1 = s1,
  637. S2 = s2,
  638. S3 = s3;
  639. }
  640. /**
  641. * Populate the internal iv of the module.
  642. * @instance
  643. * @memberof AES_asm
  644. * @param {int} i0...i3 - iv vector
  645. */
  646. function set_iv ( i0, i1, i2, i3 ) {
  647. i0 = i0|0;
  648. i1 = i1|0;
  649. i2 = i2|0;
  650. i3 = i3|0;
  651. I0 = i0,
  652. I1 = i1,
  653. I2 = i2,
  654. I3 = i3;
  655. }
  656. /**
  657. * Set nonce for CTR-family modes.
  658. * @instance
  659. * @memberof AES_asm
  660. * @param {int} n0..n3 - nonce vector
  661. */
  662. function set_nonce ( n0, n1, n2, n3 ) {
  663. n0 = n0|0;
  664. n1 = n1|0;
  665. n2 = n2|0;
  666. n3 = n3|0;
  667. N0 = n0,
  668. N1 = n1,
  669. N2 = n2,
  670. N3 = n3;
  671. }
  672. /**
  673. * Set counter mask for CTR-family modes.
  674. * @instance
  675. * @memberof AES_asm
  676. * @param {int} m0...m3 - counter mask vector
  677. */
  678. function set_mask ( m0, m1, m2, m3 ) {
  679. m0 = m0|0;
  680. m1 = m1|0;
  681. m2 = m2|0;
  682. m3 = m3|0;
  683. M0 = m0,
  684. M1 = m1,
  685. M2 = m2,
  686. M3 = m3;
  687. }
  688. /**
  689. * Set counter for CTR-family modes.
  690. * @instance
  691. * @memberof AES_asm
  692. * @param {int} c0...c3 - counter vector
  693. */
  694. function set_counter ( c0, c1, c2, c3 ) {
  695. c0 = c0|0;
  696. c1 = c1|0;
  697. c2 = c2|0;
  698. c3 = c3|0;
  699. N3 = ( ~M3 & N3 ) | M3 & c3,
  700. N2 = ( ~M2 & N2 ) | M2 & c2,
  701. N1 = ( ~M1 & N1 ) | M1 & c1,
  702. N0 = ( ~M0 & N0 ) | M0 & c0;
  703. }
  704. /**
  705. * Store the internal state vector into the heap.
  706. * @instance
  707. * @memberof AES_asm
  708. * @param {int} pos - offset where to put the data
  709. * @return {int} The number of bytes have been written into the heap, always 16.
  710. */
  711. function get_state ( pos ) {
  712. pos = pos|0;
  713. if ( pos & 15 ) return -1;
  714. DATA[pos|0] = S0>>>24,
  715. DATA[pos|1] = S0>>>16&255,
  716. DATA[pos|2] = S0>>>8&255,
  717. DATA[pos|3] = S0&255,
  718. DATA[pos|4] = S1>>>24,
  719. DATA[pos|5] = S1>>>16&255,
  720. DATA[pos|6] = S1>>>8&255,
  721. DATA[pos|7] = S1&255,
  722. DATA[pos|8] = S2>>>24,
  723. DATA[pos|9] = S2>>>16&255,
  724. DATA[pos|10] = S2>>>8&255,
  725. DATA[pos|11] = S2&255,
  726. DATA[pos|12] = S3>>>24,
  727. DATA[pos|13] = S3>>>16&255,
  728. DATA[pos|14] = S3>>>8&255,
  729. DATA[pos|15] = S3&255;
  730. return 16;
  731. }
  732. /**
  733. * Store the internal iv vector into the heap.
  734. * @instance
  735. * @memberof AES_asm
  736. * @param {int} pos - offset where to put the data
  737. * @return {int} The number of bytes have been written into the heap, always 16.
  738. */
  739. function get_iv ( pos ) {
  740. pos = pos|0;
  741. if ( pos & 15 ) return -1;
  742. DATA[pos|0] = I0>>>24,
  743. DATA[pos|1] = I0>>>16&255,
  744. DATA[pos|2] = I0>>>8&255,
  745. DATA[pos|3] = I0&255,
  746. DATA[pos|4] = I1>>>24,
  747. DATA[pos|5] = I1>>>16&255,
  748. DATA[pos|6] = I1>>>8&255,
  749. DATA[pos|7] = I1&255,
  750. DATA[pos|8] = I2>>>24,
  751. DATA[pos|9] = I2>>>16&255,
  752. DATA[pos|10] = I2>>>8&255,
  753. DATA[pos|11] = I2&255,
  754. DATA[pos|12] = I3>>>24,
  755. DATA[pos|13] = I3>>>16&255,
  756. DATA[pos|14] = I3>>>8&255,
  757. DATA[pos|15] = I3&255;
  758. return 16;
  759. }
  760. /**
  761. * GCM initialization.
  762. * @instance
  763. * @memberof AES_asm
  764. */
  765. function gcm_init ( ) {
  766. _ecb_enc( 0, 0, 0, 0 );
  767. H0 = S0,
  768. H1 = S1,
  769. H2 = S2,
  770. H3 = S3;
  771. }
  772. /**
  773. * Perform ciphering operation on the supplied data.
  774. * @instance
  775. * @memberof AES_asm
  776. * @param {int} mode - block cipher mode (see {@link AES_asm} mode constants)
  777. * @param {int} pos - offset of the data being processed
  778. * @param {int} len - length of the data being processed
  779. * @return {int} Actual amount of data have been processed.
  780. */
  781. function cipher ( mode, pos, len ) {
  782. mode = mode|0;
  783. pos = pos|0;
  784. len = len|0;
  785. var ret = 0;
  786. if ( pos & 15 ) return -1;
  787. while ( (len|0) >= 16 ) {
  788. _cipher_modes[mode&7](
  789. DATA[pos|0]<<24 | DATA[pos|1]<<16 | DATA[pos|2]<<8 | DATA[pos|3],
  790. DATA[pos|4]<<24 | DATA[pos|5]<<16 | DATA[pos|6]<<8 | DATA[pos|7],
  791. DATA[pos|8]<<24 | DATA[pos|9]<<16 | DATA[pos|10]<<8 | DATA[pos|11],
  792. DATA[pos|12]<<24 | DATA[pos|13]<<16 | DATA[pos|14]<<8 | DATA[pos|15]
  793. );
  794. DATA[pos|0] = S0>>>24,
  795. DATA[pos|1] = S0>>>16&255,
  796. DATA[pos|2] = S0>>>8&255,
  797. DATA[pos|3] = S0&255,
  798. DATA[pos|4] = S1>>>24,
  799. DATA[pos|5] = S1>>>16&255,
  800. DATA[pos|6] = S1>>>8&255,
  801. DATA[pos|7] = S1&255,
  802. DATA[pos|8] = S2>>>24,
  803. DATA[pos|9] = S2>>>16&255,
  804. DATA[pos|10] = S2>>>8&255,
  805. DATA[pos|11] = S2&255,
  806. DATA[pos|12] = S3>>>24,
  807. DATA[pos|13] = S3>>>16&255,
  808. DATA[pos|14] = S3>>>8&255,
  809. DATA[pos|15] = S3&255;
  810. ret = (ret + 16)|0,
  811. pos = (pos + 16)|0,
  812. len = (len - 16)|0;
  813. }
  814. return ret|0;
  815. }
  816. /**
  817. * Calculates MAC of the supplied data.
  818. * @instance
  819. * @memberof AES_asm
  820. * @param {int} mode - block cipher mode (see {@link AES_asm} mode constants)
  821. * @param {int} pos - offset of the data being processed
  822. * @param {int} len - length of the data being processed
  823. * @return {int} Actual amount of data have been processed.
  824. */
  825. function mac ( mode, pos, len ) {
  826. mode = mode|0;
  827. pos = pos|0;
  828. len = len|0;
  829. var ret = 0;
  830. if ( pos & 15 ) return -1;
  831. while ( (len|0) >= 16 ) {
  832. _mac_modes[mode&1](
  833. DATA[pos|0]<<24 | DATA[pos|1]<<16 | DATA[pos|2]<<8 | DATA[pos|3],
  834. DATA[pos|4]<<24 | DATA[pos|5]<<16 | DATA[pos|6]<<8 | DATA[pos|7],
  835. DATA[pos|8]<<24 | DATA[pos|9]<<16 | DATA[pos|10]<<8 | DATA[pos|11],
  836. DATA[pos|12]<<24 | DATA[pos|13]<<16 | DATA[pos|14]<<8 | DATA[pos|15]
  837. );
  838. ret = (ret + 16)|0,
  839. pos = (pos + 16)|0,
  840. len = (len - 16)|0;
  841. }
  842. return ret|0;
  843. }
  844. /**
  845. * AES cipher modes table (virual methods)
  846. */
  847. var _cipher_modes = [ _ecb_enc, _ecb_dec, _cbc_enc, _cbc_dec, _cfb_enc, _cfb_dec, _ofb, _ctr ];
  848. /**
  849. * AES MAC modes table (virual methods)
  850. */
  851. var _mac_modes = [ _cbc_enc, _gcm_mac ];
  852. /**
  853. * Asm.js module exports
  854. */
  855. return {
  856. set_rounds: set_rounds,
  857. set_state: set_state,
  858. set_iv: set_iv,
  859. set_nonce: set_nonce,
  860. set_mask: set_mask,
  861. set_counter:set_counter,
  862. get_state: get_state,
  863. get_iv: get_iv,
  864. gcm_init: gcm_init,
  865. cipher: cipher,
  866. mac: mac
  867. };
  868. }( stdlib, foreign, buffer );
  869. asm.set_key = set_key;
  870. return asm;
  871. };
  872. /**
  873. * AES enciphering mode constants
  874. * @enum {int}
  875. * @const
  876. */
  877. wrapper.ENC = {
  878. ECB: 0,
  879. CBC: 2,
  880. CFB: 4,
  881. OFB: 6,
  882. CTR: 7
  883. },
  884. /**
  885. * AES deciphering mode constants
  886. * @enum {int}
  887. * @const
  888. */
  889. wrapper.DEC = {
  890. ECB: 1,
  891. CBC: 3,
  892. CFB: 5,
  893. OFB: 6,
  894. CTR: 7
  895. },
  896. /**
  897. * AES MAC mode constants
  898. * @enum {int}
  899. * @const
  900. */
  901. wrapper.MAC = {
  902. CBC: 0,
  903. GCM: 1
  904. };
  905. /**
  906. * Heap data offset
  907. * @type {int}
  908. * @const
  909. */
  910. wrapper.HEAP_DATA = 0x4000;
  911. return wrapper;
  912. }();
  913. function AES ( options ) {
  914. options = options || {};
  915. this.heap = _heap_init( Uint8Array, options ).subarray( AES_asm.HEAP_DATA );
  916. this.asm = options.asm || AES_asm( global, null, this.heap.buffer );
  917. this.mode = null;
  918. this.key = null;
  919. this.reset( options );
  920. }
  921. function AES_set_key ( key ) {
  922. if ( key !== undefined ) {
  923. if ( is_buffer(key) || is_bytes(key) ) {
  924. key = new Uint8Array(key);
  925. }
  926. else if ( is_string(key) ) {
  927. key = string_to_bytes(key);
  928. }
  929. else {
  930. throw new TypeError("unexpected key type");
  931. }
  932. var keylen = key.length;
  933. if ( keylen !== 16 && keylen !== 24 && keylen !== 32 )
  934. throw new IllegalArgumentError("illegal key size");
  935. var keyview = new DataView( key.buffer, key.byteOffset, key.byteLength );
  936. this.asm.set_key(
  937. keylen >> 2,
  938. keyview.getUint32(0),
  939. keyview.getUint32(4),
  940. keyview.getUint32(8),
  941. keyview.getUint32(12),
  942. keylen > 16 ? keyview.getUint32(16) : 0,
  943. keylen > 16 ? keyview.getUint32(20) : 0,
  944. keylen > 24 ? keyview.getUint32(24) : 0,
  945. keylen > 24 ? keyview.getUint32(28) : 0
  946. );
  947. this.key = key;
  948. }
  949. else if ( !this.key ) {
  950. throw new Error("key is required");
  951. }
  952. }
  953. function AES_set_iv ( iv ) {
  954. if ( iv !== undefined ) {
  955. if ( is_buffer(iv) || is_bytes(iv) ) {
  956. iv = new Uint8Array(iv);
  957. }
  958. else if ( is_string(iv) ) {
  959. iv = string_to_bytes(iv);
  960. }
  961. else {
  962. throw new TypeError("unexpected iv type");
  963. }
  964. if ( iv.length !== 16 )
  965. throw new IllegalArgumentError("illegal iv size");
  966. var ivview = new DataView( iv.buffer, iv.byteOffset, iv.byteLength );
  967. this.iv = iv;
  968. this.asm.set_iv( ivview.getUint32(0), ivview.getUint32(4), ivview.getUint32(8), ivview.getUint32(12) );
  969. }
  970. else {
  971. this.iv = null;
  972. this.asm.set_iv( 0, 0, 0, 0 );
  973. }
  974. }
  975. function AES_set_padding ( padding ) {
  976. if ( padding !== undefined ) {
  977. this.padding = !!padding;
  978. }
  979. else {
  980. this.padding = true;
  981. }
  982. }
  983. function AES_reset ( options ) {
  984. options = options || {};
  985. this.result = null;
  986. this.pos = 0;
  987. this.len = 0;
  988. AES_set_key.call( this, options.key );
  989. if ( this.hasOwnProperty('iv') ) AES_set_iv.call( this, options.iv );
  990. if ( this.hasOwnProperty('padding') ) AES_set_padding.call( this, options.padding );
  991. return this;
  992. }
  993. function AES_Encrypt_process ( data ) {
  994. if ( is_string(data) )
  995. data = string_to_bytes(data);
  996. if ( is_buffer(data) )
  997. data = new Uint8Array(data);
  998. if ( !is_bytes(data) )
  999. throw new TypeError("data isn't of expected type");
  1000. var asm = this.asm,
  1001. heap = this.heap,
  1002. amode = AES_asm.ENC[this.mode],
  1003. hpos = AES_asm.HEAP_DATA,
  1004. pos = this.pos,
  1005. len = this.len,
  1006. dpos = 0,
  1007. dlen = data.length || 0,
  1008. rpos = 0,
  1009. rlen = (len + dlen) & -16,
  1010. wlen = 0;
  1011. var result = new Uint8Array(rlen);
  1012. while ( dlen > 0 ) {
  1013. wlen = _heap_write( heap, pos+len, data, dpos, dlen );
  1014. len += wlen;
  1015. dpos += wlen;
  1016. dlen -= wlen;
  1017. wlen = asm.cipher( amode, hpos + pos, len );
  1018. if ( wlen ) result.set( heap.subarray( pos, pos + wlen ), rpos );
  1019. rpos += wlen;
  1020. if ( wlen < len ) {
  1021. pos += wlen;
  1022. len -= wlen;
  1023. } else {
  1024. pos = 0;
  1025. len = 0;
  1026. }
  1027. }
  1028. this.result = result;
  1029. this.pos = pos;
  1030. this.len = len;
  1031. return this;
  1032. }
  1033. function AES_Encrypt_finish ( data ) {
  1034. var presult = null,
  1035. prlen = 0;
  1036. if ( data !== undefined ) {
  1037. presult = AES_Encrypt_process.call( this, data ).result;
  1038. prlen = presult.length;
  1039. }
  1040. var asm = this.asm,
  1041. heap = this.heap,
  1042. amode = AES_asm.ENC[this.mode],
  1043. hpos = AES_asm.HEAP_DATA,
  1044. pos = this.pos,
  1045. len = this.len,
  1046. plen = 16 - len % 16,
  1047. rlen = len;
  1048. if ( this.hasOwnProperty('padding') ) {
  1049. if ( this.padding ) {
  1050. for ( var p = 0; p < plen; ++p ) heap[ pos + len + p ] = plen;
  1051. len += plen;
  1052. rlen = len;
  1053. }
  1054. else if ( len % 16 ) {
  1055. throw new IllegalArgumentError("data length must be a multiple of the block size");
  1056. }
  1057. }
  1058. else {
  1059. len += plen;
  1060. }
  1061. var result = new Uint8Array( prlen + rlen );
  1062. if ( prlen ) result.set( presult );
  1063. if ( len ) asm.cipher( amode, hpos + pos, len );
  1064. if ( rlen ) result.set( heap.subarray( pos, pos + rlen ), prlen );
  1065. this.result = result;
  1066. this.pos = 0;
  1067. this.len = 0;
  1068. return this;
  1069. }
  1070. function AES_Decrypt_process ( data ) {
  1071. if ( is_string(data) )
  1072. data = string_to_bytes(data);
  1073. if ( is_buffer(data) )
  1074. data = new Uint8Array(data);
  1075. if ( !is_bytes(data) )
  1076. throw new TypeError("data isn't of expected type");
  1077. var asm = this.asm,
  1078. heap = this.heap,
  1079. amode = AES_asm.DEC[this.mode],
  1080. hpos = AES_asm.HEAP_DATA,
  1081. pos = this.pos,
  1082. len = this.len,
  1083. dpos = 0,
  1084. dlen = data.length || 0,
  1085. rpos = 0,
  1086. rlen = (len + dlen) & -16,
  1087. plen = 0,
  1088. wlen = 0;
  1089. if ( this.hasOwnProperty('padding') && this.padding ) {
  1090. plen = len + dlen - rlen || 16;
  1091. rlen -= plen;
  1092. }
  1093. var result = new Uint8Array(rlen);
  1094. while ( dlen > 0 ) {
  1095. wlen = _heap_write( heap, pos+len, data, dpos, dlen );
  1096. len += wlen;
  1097. dpos += wlen;
  1098. dlen -= wlen;
  1099. wlen = asm.cipher( amode, hpos + pos, len - ( !dlen ? plen : 0 ) );
  1100. if ( wlen ) result.set( heap.subarray( pos, pos + wlen ), rpos );
  1101. rpos += wlen;
  1102. if ( wlen < len ) {
  1103. pos += wlen;
  1104. len -= wlen;
  1105. } else {
  1106. pos = 0;
  1107. len = 0;
  1108. }
  1109. }
  1110. this.result = result;
  1111. this.pos = pos;
  1112. this.len = len;
  1113. return this;
  1114. }
  1115. function AES_Decrypt_finish ( data ) {
  1116. var presult = null,
  1117. prlen = 0;
  1118. if ( data !== undefined ) {
  1119. presult = AES_Decrypt_process.call( this, data ).result;
  1120. prlen = presult.length;
  1121. }
  1122. var asm = this.asm,
  1123. heap = this.heap,
  1124. amode = AES_asm.DEC[this.mode],
  1125. hpos = AES_asm.HEAP_DATA,
  1126. pos = this.pos,
  1127. len = this.len,
  1128. rlen = len;
  1129. if ( len > 0 ) {
  1130. if ( len % 16 ) {
  1131. if ( this.hasOwnProperty('padding') ) {
  1132. throw new IllegalArgumentError("data length must be a multiple of the block size");
  1133. } else {
  1134. len += 16 - len % 16;
  1135. }
  1136. }
  1137. asm.cipher( amode, hpos + pos, len );
  1138. if ( this.hasOwnProperty('padding') && this.padding ) {
  1139. var pad = heap[ pos + rlen - 1 ];
  1140. if ( pad < 1 || pad > 16 || pad > rlen )
  1141. throw new SecurityError("bad padding");
  1142. var pcheck = 0;
  1143. for ( var i = pad; i > 1; i-- ) pcheck |= pad ^ heap[ pos + rlen - i ];
  1144. if ( pcheck )
  1145. throw new SecurityError("bad padding");
  1146. rlen -= pad;
  1147. }
  1148. }
  1149. var result = new Uint8Array( prlen + rlen );
  1150. if ( prlen > 0 ) {
  1151. result.set( presult );
  1152. }
  1153. if ( rlen > 0 ) {
  1154. result.set( heap.subarray( pos, pos + rlen ), prlen );
  1155. }
  1156. this.result = result;
  1157. this.pos = 0;
  1158. this.len = 0;
  1159. return this;
  1160. }
  1161. /**
  1162. * Cipher Feedback Mode (CFB)
  1163. */
  1164. function AES_CFB ( options ) {
  1165. this.iv = null;
  1166. AES.call( this, options );
  1167. this.mode = 'CFB';
  1168. }
  1169. var AES_CFB_prototype = AES_CFB.prototype;
  1170. AES_CFB_prototype.BLOCK_SIZE = 16;
  1171. AES_CFB_prototype.reset = AES_reset;
  1172. AES_CFB_prototype.encrypt = AES_Encrypt_finish;
  1173. AES_CFB_prototype.decrypt = AES_Decrypt_finish;
  1174. function AES_CFB_Encrypt ( options ) {
  1175. AES_CFB.call( this, options );
  1176. }
  1177. var AES_CFB_Encrypt_prototype = AES_CFB_Encrypt.prototype;
  1178. AES_CFB_Encrypt_prototype.BLOCK_SIZE = 16;
  1179. AES_CFB_Encrypt_prototype.reset = AES_reset;
  1180. AES_CFB_Encrypt_prototype.process = AES_Encrypt_process;
  1181. AES_CFB_Encrypt_prototype.finish = AES_Encrypt_finish;
  1182. function AES_CFB_Decrypt ( options ) {
  1183. AES_CFB.call( this, options );
  1184. }
  1185. var AES_CFB_Decrypt_prototype = AES_CFB_Decrypt.prototype;
  1186. AES_CFB_Decrypt_prototype.BLOCK_SIZE = 16;
  1187. AES_CFB_Decrypt_prototype.reset = AES_reset;
  1188. AES_CFB_Decrypt_prototype.process = AES_Decrypt_process;
  1189. AES_CFB_Decrypt_prototype.finish = AES_Decrypt_finish;
  1190. /**
  1191. * Counter Mode (CTR)
  1192. */
  1193. function AES_CTR ( options ) {
  1194. this.nonce = null,
  1195. this.counter = 0,
  1196. this.counterSize = 0;
  1197. AES.call( this, options );
  1198. this.mode = 'CTR';
  1199. }
  1200. function AES_CTR_Crypt ( options ) {
  1201. AES_CTR.call( this, options );
  1202. }
  1203. function AES_CTR_set_options ( nonce, counter, size ) {
  1204. if ( size !== undefined ) {
  1205. if ( size < 8 || size > 48 )
  1206. throw new IllegalArgumentError("illegal counter size");
  1207. this.counterSize = size;
  1208. var mask = Math.pow( 2, size ) - 1;
  1209. this.asm.set_mask( 0, 0, (mask / 0x100000000)|0, mask|0 );
  1210. }
  1211. else {
  1212. this.counterSize = size = 48;
  1213. this.asm.set_mask( 0, 0, 0xffff, 0xffffffff );
  1214. }
  1215. if ( nonce !== undefined ) {
  1216. if ( is_buffer(nonce) || is_bytes(nonce) ) {
  1217. nonce = new Uint8Array(nonce);
  1218. }
  1219. else if ( is_string(nonce) ) {
  1220. nonce = string_to_bytes(nonce);
  1221. }
  1222. else {
  1223. throw new TypeError("unexpected nonce type");
  1224. }
  1225. var len = nonce.length;
  1226. if ( !len || len > 16 )
  1227. throw new IllegalArgumentError("illegal nonce size");
  1228. this.nonce = nonce;
  1229. var view = new DataView( new ArrayBuffer(16) );
  1230. new Uint8Array(view.buffer).set(nonce);
  1231. this.asm.set_nonce( view.getUint32(0), view.getUint32(4), view.getUint32(8), view.getUint32(12) );
  1232. }
  1233. else {
  1234. throw new Error("nonce is required");
  1235. }
  1236. if ( counter !== undefined ) {
  1237. if ( !is_number(counter) )
  1238. throw new TypeError("unexpected counter type");
  1239. if ( counter < 0 || counter >= Math.pow( 2, size ) )
  1240. throw new IllegalArgumentError("illegal counter value");
  1241. this.counter = counter;
  1242. this.asm.set_counter( 0, 0, (counter / 0x100000000)|0, counter|0 );
  1243. }
  1244. else {
  1245. this.counter = counter = 0;
  1246. }
  1247. }
  1248. function AES_CTR_reset ( options ) {
  1249. options = options || {};
  1250. AES_reset.call( this, options );
  1251. AES_CTR_set_options.call( this, options.nonce, options.counter, options.counterSize );
  1252. return this;
  1253. }
  1254. var AES_CTR_prototype = AES_CTR.prototype;
  1255. AES_CTR_prototype.BLOCK_SIZE = 16;
  1256. AES_CTR_prototype.reset = AES_CTR_reset;
  1257. AES_CTR_prototype.encrypt = AES_Encrypt_finish;
  1258. AES_CTR_prototype.decrypt = AES_Encrypt_finish;
  1259. var AES_CTR_Crypt_prototype = AES_CTR_Crypt.prototype;
  1260. AES_CTR_Crypt_prototype.BLOCK_SIZE = 16;
  1261. AES_CTR_Crypt_prototype.reset = AES_CTR_reset;
  1262. AES_CTR_Crypt_prototype.process = AES_Encrypt_process;
  1263. AES_CTR_Crypt_prototype.finish = AES_Encrypt_finish;
  1264. /**
  1265. * Galois/Counter mode
  1266. */
  1267. var _AES_GCM_data_maxLength = 68719476704; // 2^36 - 2^5
  1268. function _gcm_mac_process ( data ) {
  1269. var heap = this.heap,
  1270. asm = this.asm,
  1271. dpos = 0,
  1272. dlen = data.length || 0,
  1273. wlen = 0;
  1274. while ( dlen > 0 ) {
  1275. wlen = _heap_write( heap, 0, data, dpos, dlen );
  1276. dpos += wlen;
  1277. dlen -= wlen;
  1278. while ( wlen & 15 ) heap[ wlen++ ] = 0;
  1279. asm.mac( AES_asm.MAC.GCM, AES_asm.HEAP_DATA, wlen );
  1280. }
  1281. }
  1282. function AES_GCM ( options ) {
  1283. this.nonce = null;
  1284. this.adata = null;
  1285. this.iv = null;
  1286. this.counter = 1;
  1287. this.tagSize = 16;
  1288. AES.call( this, options );
  1289. this.mode = 'GCM';
  1290. }
  1291. function AES_GCM_Encrypt ( options ) {
  1292. AES_GCM.call( this, options );
  1293. }
  1294. function AES_GCM_Decrypt ( options ) {
  1295. AES_GCM.call( this, options );
  1296. }
  1297. function AES_GCM_reset ( options ) {
  1298. options = options || {};
  1299. AES_reset.call( this, options );
  1300. var asm = this.asm,
  1301. heap = this.heap;
  1302. asm.gcm_init();
  1303. var tagSize = options.tagSize;
  1304. if ( tagSize !== undefined ) {
  1305. if ( !is_number(tagSize) )
  1306. throw new TypeError("tagSize must be a number");
  1307. if ( tagSize < 4 || tagSize > 16 )
  1308. throw new IllegalArgumentError("illegal tagSize value");
  1309. this.tagSize = tagSize;
  1310. }
  1311. else {
  1312. this.tagSize = 16;
  1313. }
  1314. var nonce = options.nonce;
  1315. if ( nonce !== undefined ) {
  1316. if ( is_bytes(nonce) || is_buffer(nonce) ) {
  1317. nonce = new Uint8Array(nonce);
  1318. }
  1319. else if ( is_string(nonce) ) {
  1320. nonce = string_to_bytes(nonce);
  1321. }
  1322. else {
  1323. throw new TypeError("unexpected nonce type");
  1324. }
  1325. this.nonce = nonce;
  1326. var noncelen = nonce.length || 0,
  1327. noncebuf = new Uint8Array(16);
  1328. if ( noncelen !== 12 ) {
  1329. _gcm_mac_process.call( this, nonce );
  1330. heap[0] = heap[1] = heap[2] = heap[3] = heap[4] = heap[5] = heap[6] = heap[7] = heap[8] = heap[9] = heap[10] = 0,
  1331. heap[11] = noncelen>>>29,
  1332. heap[12] = noncelen>>>21&255,
  1333. heap[13] = noncelen>>>13&255,
  1334. heap[14] = noncelen>>>5&255,
  1335. heap[15] = noncelen<<3&255;
  1336. asm.mac( AES_asm.MAC.GCM, AES_asm.HEAP_DATA, 16 );
  1337. asm.get_iv( AES_asm.HEAP_DATA );
  1338. asm.set_iv();
  1339. noncebuf.set( heap.subarray( 0, 16 ) );
  1340. }
  1341. else {
  1342. noncebuf.set(nonce);
  1343. noncebuf[15] = 1;
  1344. }
  1345. var nonceview = new DataView( noncebuf.buffer );
  1346. this.gamma0 = nonceview.getUint32(12);
  1347. asm.set_nonce( nonceview.getUint32(0), nonceview.getUint32(4), nonceview.getUint32(8), 0 );
  1348. asm.set_mask( 0, 0, 0, 0xffffffff );
  1349. }
  1350. else {
  1351. throw new Error("nonce is required");
  1352. }
  1353. var adata = options.adata;
  1354. if ( adata !== undefined && adata !== null ) {
  1355. if ( is_bytes(adata) || is_buffer(adata) ) {
  1356. adata = new Uint8Array(adata);
  1357. }
  1358. else if ( is_string(adata) ) {
  1359. adata = string_to_bytes(adata);
  1360. }
  1361. else {
  1362. throw new TypeError("unexpected adata type");
  1363. }
  1364. if ( adata.length > _AES_GCM_data_maxLength )
  1365. throw new IllegalArgumentError("illegal adata length");
  1366. if ( adata.length ) {
  1367. this.adata = adata;
  1368. _gcm_mac_process.call( this, adata );
  1369. }
  1370. else {
  1371. this.adata = null;
  1372. }
  1373. }
  1374. else {
  1375. this.adata = null;
  1376. }
  1377. var counter = options.counter;
  1378. if ( counter !== undefined ) {
  1379. if ( !is_number(counter) )
  1380. throw new TypeError("counter must be a number");
  1381. if ( counter < 1 || counter > 0xffffffff )
  1382. throw new RangeError("counter must be a positive 32-bit integer");
  1383. this.counter = counter;
  1384. asm.set_counter( 0, 0, 0, this.gamma0+counter|0 );
  1385. }
  1386. else {
  1387. this.counter = 1;
  1388. asm.set_counter( 0, 0, 0, this.gamma0+1|0 );
  1389. }
  1390. var iv = options.iv;
  1391. if ( iv !== undefined ) {
  1392. if ( !is_number(counter) )
  1393. throw new TypeError("counter must be a number");
  1394. this.iv = iv;
  1395. AES_set_iv.call( this, iv );
  1396. }
  1397. return this;
  1398. }
  1399. function AES_GCM_Encrypt_process ( data ) {
  1400. if ( is_string(data) )
  1401. data = string_to_bytes(data);
  1402. if ( is_buffer(data) )
  1403. data = new Uint8Array(data);
  1404. if ( !is_bytes(data) )
  1405. throw new TypeError("data isn't of expected type");
  1406. var dpos = 0,
  1407. dlen = data.length || 0,
  1408. asm = this.asm,
  1409. heap = this.heap,
  1410. counter = this.counter,
  1411. pos = this.pos,
  1412. len = this.len,
  1413. rpos = 0,
  1414. rlen = ( len + dlen ) & -16,
  1415. wlen = 0;
  1416. if ( ((counter-1)<<4) + len + dlen > _AES_GCM_data_maxLength )
  1417. throw new RangeError("counter overflow");
  1418. var result = new Uint8Array(rlen);
  1419. while ( dlen > 0 ) {
  1420. wlen = _heap_write( heap, pos+len, data, dpos, dlen );
  1421. len += wlen;
  1422. dpos += wlen;
  1423. dlen -= wlen;
  1424. wlen = asm.cipher( AES_asm.ENC.CTR, AES_asm.HEAP_DATA + pos, len );
  1425. wlen = asm.mac( AES_asm.MAC.GCM, AES_asm.HEAP_DATA + pos, wlen );
  1426. if ( wlen ) result.set( heap.subarray( pos, pos + wlen ), rpos );
  1427. counter += (wlen>>>4);
  1428. rpos += wlen;
  1429. if ( wlen < len ) {
  1430. pos += wlen;
  1431. len -= wlen;
  1432. } else {
  1433. pos = 0;
  1434. len = 0;
  1435. }
  1436. }
  1437. this.result = result;
  1438. this.counter = counter;
  1439. this.pos = pos;
  1440. this.len = len;
  1441. return this;
  1442. }
  1443. function AES_GCM_Encrypt_finish () {
  1444. var asm = this.asm,
  1445. heap = this.heap,
  1446. counter = this.counter,
  1447. tagSize = this.tagSize,
  1448. adata = this.adata,
  1449. pos = this.pos,
  1450. len = this.len;
  1451. var result = new Uint8Array( len + tagSize );
  1452. asm.cipher( AES_asm.ENC.CTR, AES_asm.HEAP_DATA + pos, (len + 15) & -16 );
  1453. if ( len ) result.set( heap.subarray( pos, pos + len ) );
  1454. for ( var i = len; i & 15; i++ ) heap[ pos + i ] = 0;
  1455. asm.mac( AES_asm.MAC.GCM, AES_asm.HEAP_DATA + pos, i );
  1456. var alen = ( adata !== null ) ? adata.length : 0,
  1457. clen = ( (counter-1) << 4) + len;
  1458. heap[0] = heap[1] = heap[2] = 0,
  1459. heap[3] = alen>>>29,
  1460. heap[4] = alen>>>21,
  1461. heap[5] = alen>>>13&255,
  1462. heap[6] = alen>>>5&255,
  1463. heap[7] = alen<<3&255,
  1464. heap[8] = heap[9] = heap[10] = 0,
  1465. heap[11] = clen>>>29,
  1466. heap[12] = clen>>>21&255,
  1467. heap[13] = clen>>>13&255,
  1468. heap[14] = clen>>>5&255,
  1469. heap[15] = clen<<3&255;
  1470. asm.mac( AES_asm.MAC.GCM, AES_asm.HEAP_DATA, 16 );
  1471. asm.get_iv( AES_asm.HEAP_DATA );
  1472. asm.set_counter( 0, 0, 0, this.gamma0 );
  1473. asm.cipher( AES_asm.ENC.CTR, AES_asm.HEAP_DATA, 16 );
  1474. result.set( heap.subarray( 0, tagSize ), len );
  1475. this.result = result;
  1476. this.counter = 1;
  1477. this.pos = 0;
  1478. this.len = 0;
  1479. return this;
  1480. }
  1481. function AES_GCM_encrypt ( data ) {
  1482. var result1 = AES_GCM_Encrypt_process.call( this, data ).result,
  1483. result2 = AES_GCM_Encrypt_finish.call(this).result;
  1484. var result = new Uint8Array( result1.length + result2.length );
  1485. if ( result1.length ) result.set( result1 );
  1486. if ( result2.length ) result.set( result2, result1.length );
  1487. this.result = result;
  1488. return this;
  1489. }
  1490. function AES_GCM_Decrypt_process ( data ) {
  1491. if ( is_string(data) )
  1492. data = string_to_bytes(data);
  1493. if ( is_buffer(data) )
  1494. data = new Uint8Array(data);
  1495. if ( !is_bytes(data) )
  1496. throw new TypeError("data isn't of expected type");
  1497. var dpos = 0,
  1498. dlen = data.length || 0,
  1499. asm = this.asm,
  1500. heap = this.heap,
  1501. counter = this.counter,
  1502. tagSize = this.tagSize,
  1503. pos = this.pos,
  1504. len = this.len,
  1505. rpos = 0,
  1506. rlen = len + dlen > tagSize ? ( len + dlen - tagSize ) & -16 : 0,
  1507. tlen = len + dlen - rlen,
  1508. wlen = 0;
  1509. if ( ((counter-1)<<4) + len + dlen > _AES_GCM_data_maxLength )
  1510. throw new RangeError("counter overflow");
  1511. var result = new Uint8Array(rlen);
  1512. while ( dlen > tlen ) {
  1513. wlen = _heap_write( heap, pos+len, data, dpos, dlen-tlen );
  1514. len += wlen;
  1515. dpos += wlen;
  1516. dlen -= wlen;
  1517. wlen = asm.mac( AES_asm.MAC.GCM, AES_asm.HEAP_DATA + pos, wlen );
  1518. wlen = asm.cipher( AES_asm.DEC.CTR, AES_asm.HEAP_DATA + pos, wlen );
  1519. if ( wlen ) result.set( heap.subarray( pos, pos+wlen ), rpos );
  1520. counter += (wlen>>>4);
  1521. rpos += wlen;
  1522. pos = 0;
  1523. len = 0;
  1524. }
  1525. if ( dlen > 0 ) {
  1526. len += _heap_write( heap, 0, data, dpos, dlen );
  1527. }
  1528. this.result = result;
  1529. this.counter = counter;
  1530. this.pos = pos;
  1531. this.len = len;
  1532. return this;
  1533. }
  1534. function AES_GCM_Decrypt_finish () {
  1535. var asm = this.asm,
  1536. heap = this.heap,
  1537. tagSize = this.tagSize,
  1538. adata = this.adata,
  1539. counter = this.counter,
  1540. pos = this.pos,
  1541. len = this.len,
  1542. rlen = len - tagSize,
  1543. wlen = 0;
  1544. if ( len < tagSize )
  1545. throw new IllegalStateError("authentication tag not found");
  1546. var result = new Uint8Array(rlen),
  1547. atag = new Uint8Array( heap.subarray( pos+rlen, pos+len ) );
  1548. for ( var i = rlen; i & 15; i++ ) heap[ pos + i ] = 0;
  1549. wlen = asm.mac( AES_asm.MAC.GCM, AES_asm.HEAP_DATA + pos, i );
  1550. wlen = asm.cipher( AES_asm.DEC.CTR, AES_asm.HEAP_DATA + pos, i );
  1551. if ( rlen ) result.set( heap.subarray( pos, pos+rlen ) );
  1552. var alen = ( adata !== null ) ? adata.length : 0,
  1553. clen = ( (counter-1) << 4) + len - tagSize;
  1554. heap[0] = heap[1] = heap[2] = 0,
  1555. heap[3] = alen>>>29,
  1556. heap[4] = alen>>>21,
  1557. heap[5] = alen>>>13&255,
  1558. heap[6] = alen>>>5&255,
  1559. heap[7] = alen<<3&255,
  1560. heap[8] = heap[9] = heap[10] = 0,
  1561. heap[11] = clen>>>29,
  1562. heap[12] = clen>>>21&255,
  1563. heap[13] = clen>>>13&255,
  1564. heap[14] = clen>>>5&255,
  1565. heap[15] = clen<<3&255;
  1566. asm.mac( AES_asm.MAC.GCM, AES_asm.HEAP_DATA, 16 );
  1567. asm.get_iv( AES_asm.HEAP_DATA );
  1568. asm.set_counter( 0, 0, 0, this.gamma0 );
  1569. asm.cipher( AES_asm.ENC.CTR, AES_asm.HEAP_DATA, 16 );
  1570. var acheck = 0;
  1571. for ( var i = 0; i < tagSize; ++i ) acheck |= atag[i] ^ heap[i];
  1572. if ( acheck )
  1573. throw new SecurityError("data integrity check failed");
  1574. this.result = result;
  1575. this.counter = 1;
  1576. this.pos = 0;
  1577. this.len = 0;
  1578. return this;
  1579. }
  1580. function AES_GCM_decrypt ( data ) {
  1581. var result1 = AES_GCM_Decrypt_process.call( this, data ).result,
  1582. result2 = AES_GCM_Decrypt_finish.call( this ).result;
  1583. var result = new Uint8Array( result1.length + result2.length );
  1584. if ( result1.length ) result.set( result1 );
  1585. if ( result2.length ) result.set( result2, result1.length );
  1586. this.result = result;
  1587. return this;
  1588. }
  1589. var AES_GCM_prototype = AES_GCM.prototype;
  1590. AES_GCM_prototype.BLOCK_SIZE = 16;
  1591. AES_GCM_prototype.reset = AES_GCM_reset;
  1592. AES_GCM_prototype.encrypt = AES_GCM_encrypt;
  1593. AES_GCM_prototype.decrypt = AES_GCM_decrypt;
  1594. var AES_GCM_Encrypt_prototype = AES_GCM_Encrypt.prototype;
  1595. AES_GCM_Encrypt_prototype.BLOCK_SIZE = 16;
  1596. AES_GCM_Encrypt_prototype.reset = AES_GCM_reset;
  1597. AES_GCM_Encrypt_prototype.process = AES_GCM_Encrypt_process;
  1598. AES_GCM_Encrypt_prototype.finish = AES_GCM_Encrypt_finish;
  1599. var AES_GCM_Decrypt_prototype = AES_GCM_Decrypt.prototype;
  1600. AES_GCM_Decrypt_prototype.BLOCK_SIZE = 16;
  1601. AES_GCM_Decrypt_prototype.reset = AES_GCM_reset;
  1602. AES_GCM_Decrypt_prototype.process = AES_GCM_Decrypt_process;
  1603. AES_GCM_Decrypt_prototype.finish = AES_GCM_Decrypt_finish;
  1604. // shared asm.js module and heap
  1605. var _AES_heap_instance = new Uint8Array(0x100000),
  1606. _AES_asm_instance = AES_asm( global, null, _AES_heap_instance.buffer );
  1607. /**
  1608. * AES-CFB exports
  1609. */
  1610. function AES_CFB_encrypt_bytes ( data, key, iv ) {
  1611. if ( data === undefined ) throw new SyntaxError("data required");
  1612. if ( key === undefined ) throw new SyntaxError("key required");
  1613. return new AES_CFB( { heap: _AES_heap_instance, asm: _AES_asm_instance, key: key, iv: iv } ).encrypt(data).result;
  1614. }
  1615. function AES_CFB_decrypt_bytes ( data, key, iv ) {
  1616. if ( data === undefined ) throw new SyntaxError("data required");
  1617. if ( key === undefined ) throw new SyntaxError("key required");
  1618. return new AES_CFB( { heap: _AES_heap_instance, asm: _AES_asm_instance, key: key, iv: iv } ).decrypt(data).result;
  1619. }
  1620. exports.AES_CFB = AES_CFB;
  1621. exports.AES_CFB.encrypt = AES_CFB_encrypt_bytes;
  1622. exports.AES_CFB.decrypt = AES_CFB_decrypt_bytes;
  1623. exports.AES_CFB.Encrypt = AES_CFB_Encrypt;
  1624. exports.AES_CFB.Decrypt = AES_CFB_Decrypt;
  1625. /**
  1626. * AES-GCM exports
  1627. */
  1628. function AES_GCM_encrypt_bytes ( data, key, nonce, adata, tagSize ) {
  1629. if ( data === undefined ) throw new SyntaxError("data required");
  1630. if ( key === undefined ) throw new SyntaxError("key required");
  1631. if ( nonce === undefined ) throw new SyntaxError("nonce required");
  1632. return new AES_GCM( { heap: _AES_heap_instance, asm: _AES_asm_instance, key: key, nonce: nonce, adata: adata, tagSize: tagSize } ).encrypt(data).result;
  1633. }
  1634. function AES_GCM_decrypt_bytes ( data, key, nonce, adata, tagSize ) {
  1635. if ( data === undefined ) throw new SyntaxError("data required");
  1636. if ( key === undefined ) throw new SyntaxError("key required");
  1637. if ( nonce === undefined ) throw new SyntaxError("nonce required");
  1638. return new AES_GCM( { heap: _AES_heap_instance, asm: _AES_asm_instance, key: key, nonce: nonce, adata: adata, tagSize: tagSize } ).decrypt(data).result;
  1639. }
  1640. exports.AES_GCM = AES_GCM;
  1641. exports.AES_GCM.encrypt = AES_GCM_encrypt_bytes;
  1642. exports.AES_GCM.decrypt = AES_GCM_decrypt_bytes;
  1643. exports.AES_GCM.Encrypt = AES_GCM_Encrypt;
  1644. exports.AES_GCM.Decrypt = AES_GCM_Decrypt;
  1645. function hash_reset () {
  1646. this.result = null;
  1647. this.pos = 0;
  1648. this.len = 0;
  1649. this.asm.reset();
  1650. return this;
  1651. }
  1652. function hash_process ( data ) {
  1653. if ( this.result !== null )
  1654. throw new IllegalStateError("state must be reset before processing new data");
  1655. if ( is_string(data) )
  1656. data = string_to_bytes(data);
  1657. if ( is_buffer(data) )
  1658. data = new Uint8Array(data);
  1659. if ( !is_bytes(data) )
  1660. throw new TypeError("data isn't of expected type");
  1661. var asm = this.asm,
  1662. heap = this.heap,
  1663. hpos = this.pos,
  1664. hlen = this.len,
  1665. dpos = 0,
  1666. dlen = data.length,
  1667. wlen = 0;
  1668. while ( dlen > 0 ) {
  1669. wlen = _heap_write( heap, hpos+hlen, data, dpos, dlen );
  1670. hlen += wlen;
  1671. dpos += wlen;
  1672. dlen -= wlen;
  1673. wlen = asm.process( hpos, hlen );
  1674. hpos += wlen;
  1675. hlen -= wlen;
  1676. if ( !hlen ) hpos = 0;
  1677. }
  1678. this.pos = hpos;
  1679. this.len = hlen;
  1680. return this;
  1681. }
  1682. function hash_finish () {
  1683. if ( this.result !== null )
  1684. throw new IllegalStateError("state must be reset before processing new data");
  1685. this.asm.finish( this.pos, this.len, 0 );
  1686. this.result = new Uint8Array(this.HASH_SIZE);
  1687. this.result.set( this.heap.subarray( 0, this.HASH_SIZE ) );
  1688. this.pos = 0;
  1689. this.len = 0;
  1690. return this;
  1691. }
  1692. function sha256_asm ( stdlib, foreign, buffer ) {
  1693. "use asm";
  1694. // SHA256 state
  1695. var H0 = 0, H1 = 0, H2 = 0, H3 = 0, H4 = 0, H5 = 0, H6 = 0, H7 = 0,
  1696. TOTAL0 = 0, TOTAL1 = 0;
  1697. // HMAC state
  1698. var I0 = 0, I1 = 0, I2 = 0, I3 = 0, I4 = 0, I5 = 0, I6 = 0, I7 = 0,
  1699. O0 = 0, O1 = 0, O2 = 0, O3 = 0, O4 = 0, O5 = 0, O6 = 0, O7 = 0;
  1700. // I/O buffer
  1701. var HEAP = new stdlib.Uint8Array(buffer);
  1702. function _core ( w0, w1, w2, w3, w4, w5, w6, w7, w8, w9, w10, w11, w12, w13, w14, w15 ) {
  1703. w0 = w0|0;
  1704. w1 = w1|0;
  1705. w2 = w2|0;
  1706. w3 = w3|0;
  1707. w4 = w4|0;
  1708. w5 = w5|0;
  1709. w6 = w6|0;
  1710. w7 = w7|0;
  1711. w8 = w8|0;
  1712. w9 = w9|0;
  1713. w10 = w10|0;
  1714. w11 = w11|0;
  1715. w12 = w12|0;
  1716. w13 = w13|0;
  1717. w14 = w14|0;
  1718. w15 = w15|0;
  1719. var a = 0, b = 0, c = 0, d = 0, e = 0, f = 0, g = 0, h = 0,
  1720. t = 0;
  1721. a = H0;
  1722. b = H1;
  1723. c = H2;
  1724. d = H3;
  1725. e = H4;
  1726. f = H5;
  1727. g = H6;
  1728. h = H7;
  1729. // 0
  1730. t = ( w0 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x428a2f98 )|0;
  1731. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1732. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1733. // 1
  1734. t = ( w1 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x71374491 )|0;
  1735. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1736. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1737. // 2
  1738. t = ( w2 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xb5c0fbcf )|0;
  1739. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1740. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1741. // 3
  1742. t = ( w3 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xe9b5dba5 )|0;
  1743. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1744. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1745. // 4
  1746. t = ( w4 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x3956c25b )|0;
  1747. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1748. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1749. // 5
  1750. t = ( w5 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x59f111f1 )|0;
  1751. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1752. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1753. // 6
  1754. t = ( w6 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x923f82a4 )|0;
  1755. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1756. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1757. // 7
  1758. t = ( w7 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xab1c5ed5 )|0;
  1759. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1760. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1761. // 8
  1762. t = ( w8 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xd807aa98 )|0;
  1763. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1764. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1765. // 9
  1766. t = ( w9 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x12835b01 )|0;
  1767. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1768. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1769. // 10
  1770. t = ( w10 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x243185be )|0;
  1771. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1772. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1773. // 11
  1774. t = ( w11 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x550c7dc3 )|0;
  1775. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1776. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1777. // 12
  1778. t = ( w12 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x72be5d74 )|0;
  1779. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1780. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1781. // 13
  1782. t = ( w13 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x80deb1fe )|0;
  1783. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1784. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1785. // 14
  1786. t = ( w14 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x9bdc06a7 )|0;
  1787. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1788. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1789. // 15
  1790. t = ( w15 + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xc19bf174 )|0;
  1791. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1792. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1793. // 16
  1794. w0 = t = ( ( w1>>>7 ^ w1>>>18 ^ w1>>>3 ^ w1<<25 ^ w1<<14 ) + ( w14>>>17 ^ w14>>>19 ^ w14>>>10 ^ w14<<15 ^ w14<<13 ) + w0 + w9 )|0;
  1795. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xe49b69c1 )|0;
  1796. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1797. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1798. // 17
  1799. w1 = t = ( ( w2>>>7 ^ w2>>>18 ^ w2>>>3 ^ w2<<25 ^ w2<<14 ) + ( w15>>>17 ^ w15>>>19 ^ w15>>>10 ^ w15<<15 ^ w15<<13 ) + w1 + w10 )|0;
  1800. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xefbe4786 )|0;
  1801. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1802. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1803. // 18
  1804. w2 = t = ( ( w3>>>7 ^ w3>>>18 ^ w3>>>3 ^ w3<<25 ^ w3<<14 ) + ( w0>>>17 ^ w0>>>19 ^ w0>>>10 ^ w0<<15 ^ w0<<13 ) + w2 + w11 )|0;
  1805. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x0fc19dc6 )|0;
  1806. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1807. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1808. // 19
  1809. w3 = t = ( ( w4>>>7 ^ w4>>>18 ^ w4>>>3 ^ w4<<25 ^ w4<<14 ) + ( w1>>>17 ^ w1>>>19 ^ w1>>>10 ^ w1<<15 ^ w1<<13 ) + w3 + w12 )|0;
  1810. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x240ca1cc )|0;
  1811. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1812. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1813. // 20
  1814. w4 = t = ( ( w5>>>7 ^ w5>>>18 ^ w5>>>3 ^ w5<<25 ^ w5<<14 ) + ( w2>>>17 ^ w2>>>19 ^ w2>>>10 ^ w2<<15 ^ w2<<13 ) + w4 + w13 )|0;
  1815. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x2de92c6f )|0;
  1816. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1817. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1818. // 21
  1819. w5 = t = ( ( w6>>>7 ^ w6>>>18 ^ w6>>>3 ^ w6<<25 ^ w6<<14 ) + ( w3>>>17 ^ w3>>>19 ^ w3>>>10 ^ w3<<15 ^ w3<<13 ) + w5 + w14 )|0;
  1820. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x4a7484aa )|0;
  1821. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1822. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1823. // 22
  1824. w6 = t = ( ( w7>>>7 ^ w7>>>18 ^ w7>>>3 ^ w7<<25 ^ w7<<14 ) + ( w4>>>17 ^ w4>>>19 ^ w4>>>10 ^ w4<<15 ^ w4<<13 ) + w6 + w15 )|0;
  1825. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x5cb0a9dc )|0;
  1826. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1827. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1828. // 23
  1829. w7 = t = ( ( w8>>>7 ^ w8>>>18 ^ w8>>>3 ^ w8<<25 ^ w8<<14 ) + ( w5>>>17 ^ w5>>>19 ^ w5>>>10 ^ w5<<15 ^ w5<<13 ) + w7 + w0 )|0;
  1830. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x76f988da )|0;
  1831. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1832. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1833. // 24
  1834. w8 = t = ( ( w9>>>7 ^ w9>>>18 ^ w9>>>3 ^ w9<<25 ^ w9<<14 ) + ( w6>>>17 ^ w6>>>19 ^ w6>>>10 ^ w6<<15 ^ w6<<13 ) + w8 + w1 )|0;
  1835. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x983e5152 )|0;
  1836. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1837. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1838. // 25
  1839. w9 = t = ( ( w10>>>7 ^ w10>>>18 ^ w10>>>3 ^ w10<<25 ^ w10<<14 ) + ( w7>>>17 ^ w7>>>19 ^ w7>>>10 ^ w7<<15 ^ w7<<13 ) + w9 + w2 )|0;
  1840. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xa831c66d )|0;
  1841. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1842. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1843. // 26
  1844. w10 = t = ( ( w11>>>7 ^ w11>>>18 ^ w11>>>3 ^ w11<<25 ^ w11<<14 ) + ( w8>>>17 ^ w8>>>19 ^ w8>>>10 ^ w8<<15 ^ w8<<13 ) + w10 + w3 )|0;
  1845. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xb00327c8 )|0;
  1846. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1847. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1848. // 27
  1849. w11 = t = ( ( w12>>>7 ^ w12>>>18 ^ w12>>>3 ^ w12<<25 ^ w12<<14 ) + ( w9>>>17 ^ w9>>>19 ^ w9>>>10 ^ w9<<15 ^ w9<<13 ) + w11 + w4 )|0;
  1850. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xbf597fc7 )|0;
  1851. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1852. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1853. // 28
  1854. w12 = t = ( ( w13>>>7 ^ w13>>>18 ^ w13>>>3 ^ w13<<25 ^ w13<<14 ) + ( w10>>>17 ^ w10>>>19 ^ w10>>>10 ^ w10<<15 ^ w10<<13 ) + w12 + w5 )|0;
  1855. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xc6e00bf3 )|0;
  1856. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1857. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1858. // 29
  1859. w13 = t = ( ( w14>>>7 ^ w14>>>18 ^ w14>>>3 ^ w14<<25 ^ w14<<14 ) + ( w11>>>17 ^ w11>>>19 ^ w11>>>10 ^ w11<<15 ^ w11<<13 ) + w13 + w6 )|0;
  1860. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xd5a79147 )|0;
  1861. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1862. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1863. // 30
  1864. w14 = t = ( ( w15>>>7 ^ w15>>>18 ^ w15>>>3 ^ w15<<25 ^ w15<<14 ) + ( w12>>>17 ^ w12>>>19 ^ w12>>>10 ^ w12<<15 ^ w12<<13 ) + w14 + w7 )|0;
  1865. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x06ca6351 )|0;
  1866. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1867. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1868. // 31
  1869. w15 = t = ( ( w0>>>7 ^ w0>>>18 ^ w0>>>3 ^ w0<<25 ^ w0<<14 ) + ( w13>>>17 ^ w13>>>19 ^ w13>>>10 ^ w13<<15 ^ w13<<13 ) + w15 + w8 )|0;
  1870. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x14292967 )|0;
  1871. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1872. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1873. // 32
  1874. w0 = t = ( ( w1>>>7 ^ w1>>>18 ^ w1>>>3 ^ w1<<25 ^ w1<<14 ) + ( w14>>>17 ^ w14>>>19 ^ w14>>>10 ^ w14<<15 ^ w14<<13 ) + w0 + w9 )|0;
  1875. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x27b70a85 )|0;
  1876. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1877. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1878. // 33
  1879. w1 = t = ( ( w2>>>7 ^ w2>>>18 ^ w2>>>3 ^ w2<<25 ^ w2<<14 ) + ( w15>>>17 ^ w15>>>19 ^ w15>>>10 ^ w15<<15 ^ w15<<13 ) + w1 + w10 )|0;
  1880. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x2e1b2138 )|0;
  1881. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1882. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1883. // 34
  1884. w2 = t = ( ( w3>>>7 ^ w3>>>18 ^ w3>>>3 ^ w3<<25 ^ w3<<14 ) + ( w0>>>17 ^ w0>>>19 ^ w0>>>10 ^ w0<<15 ^ w0<<13 ) + w2 + w11 )|0;
  1885. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x4d2c6dfc )|0;
  1886. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1887. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1888. // 35
  1889. w3 = t = ( ( w4>>>7 ^ w4>>>18 ^ w4>>>3 ^ w4<<25 ^ w4<<14 ) + ( w1>>>17 ^ w1>>>19 ^ w1>>>10 ^ w1<<15 ^ w1<<13 ) + w3 + w12 )|0;
  1890. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x53380d13 )|0;
  1891. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1892. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1893. // 36
  1894. w4 = t = ( ( w5>>>7 ^ w5>>>18 ^ w5>>>3 ^ w5<<25 ^ w5<<14 ) + ( w2>>>17 ^ w2>>>19 ^ w2>>>10 ^ w2<<15 ^ w2<<13 ) + w4 + w13 )|0;
  1895. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x650a7354 )|0;
  1896. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1897. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1898. // 37
  1899. w5 = t = ( ( w6>>>7 ^ w6>>>18 ^ w6>>>3 ^ w6<<25 ^ w6<<14 ) + ( w3>>>17 ^ w3>>>19 ^ w3>>>10 ^ w3<<15 ^ w3<<13 ) + w5 + w14 )|0;
  1900. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x766a0abb )|0;
  1901. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1902. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1903. // 38
  1904. w6 = t = ( ( w7>>>7 ^ w7>>>18 ^ w7>>>3 ^ w7<<25 ^ w7<<14 ) + ( w4>>>17 ^ w4>>>19 ^ w4>>>10 ^ w4<<15 ^ w4<<13 ) + w6 + w15 )|0;
  1905. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x81c2c92e )|0;
  1906. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1907. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1908. // 39
  1909. w7 = t = ( ( w8>>>7 ^ w8>>>18 ^ w8>>>3 ^ w8<<25 ^ w8<<14 ) + ( w5>>>17 ^ w5>>>19 ^ w5>>>10 ^ w5<<15 ^ w5<<13 ) + w7 + w0 )|0;
  1910. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x92722c85 )|0;
  1911. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1912. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1913. // 40
  1914. w8 = t = ( ( w9>>>7 ^ w9>>>18 ^ w9>>>3 ^ w9<<25 ^ w9<<14 ) + ( w6>>>17 ^ w6>>>19 ^ w6>>>10 ^ w6<<15 ^ w6<<13 ) + w8 + w1 )|0;
  1915. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xa2bfe8a1 )|0;
  1916. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1917. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1918. // 41
  1919. w9 = t = ( ( w10>>>7 ^ w10>>>18 ^ w10>>>3 ^ w10<<25 ^ w10<<14 ) + ( w7>>>17 ^ w7>>>19 ^ w7>>>10 ^ w7<<15 ^ w7<<13 ) + w9 + w2 )|0;
  1920. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xa81a664b )|0;
  1921. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1922. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1923. // 42
  1924. w10 = t = ( ( w11>>>7 ^ w11>>>18 ^ w11>>>3 ^ w11<<25 ^ w11<<14 ) + ( w8>>>17 ^ w8>>>19 ^ w8>>>10 ^ w8<<15 ^ w8<<13 ) + w10 + w3 )|0;
  1925. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xc24b8b70 )|0;
  1926. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1927. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1928. // 43
  1929. w11 = t = ( ( w12>>>7 ^ w12>>>18 ^ w12>>>3 ^ w12<<25 ^ w12<<14 ) + ( w9>>>17 ^ w9>>>19 ^ w9>>>10 ^ w9<<15 ^ w9<<13 ) + w11 + w4 )|0;
  1930. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xc76c51a3 )|0;
  1931. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1932. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1933. // 44
  1934. w12 = t = ( ( w13>>>7 ^ w13>>>18 ^ w13>>>3 ^ w13<<25 ^ w13<<14 ) + ( w10>>>17 ^ w10>>>19 ^ w10>>>10 ^ w10<<15 ^ w10<<13 ) + w12 + w5 )|0;
  1935. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xd192e819 )|0;
  1936. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1937. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1938. // 45
  1939. w13 = t = ( ( w14>>>7 ^ w14>>>18 ^ w14>>>3 ^ w14<<25 ^ w14<<14 ) + ( w11>>>17 ^ w11>>>19 ^ w11>>>10 ^ w11<<15 ^ w11<<13 ) + w13 + w6 )|0;
  1940. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xd6990624 )|0;
  1941. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1942. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1943. // 46
  1944. w14 = t = ( ( w15>>>7 ^ w15>>>18 ^ w15>>>3 ^ w15<<25 ^ w15<<14 ) + ( w12>>>17 ^ w12>>>19 ^ w12>>>10 ^ w12<<15 ^ w12<<13 ) + w14 + w7 )|0;
  1945. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xf40e3585 )|0;
  1946. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1947. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1948. // 47
  1949. w15 = t = ( ( w0>>>7 ^ w0>>>18 ^ w0>>>3 ^ w0<<25 ^ w0<<14 ) + ( w13>>>17 ^ w13>>>19 ^ w13>>>10 ^ w13<<15 ^ w13<<13 ) + w15 + w8 )|0;
  1950. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x106aa070 )|0;
  1951. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1952. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1953. // 48
  1954. w0 = t = ( ( w1>>>7 ^ w1>>>18 ^ w1>>>3 ^ w1<<25 ^ w1<<14 ) + ( w14>>>17 ^ w14>>>19 ^ w14>>>10 ^ w14<<15 ^ w14<<13 ) + w0 + w9 )|0;
  1955. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x19a4c116 )|0;
  1956. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1957. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1958. // 49
  1959. w1 = t = ( ( w2>>>7 ^ w2>>>18 ^ w2>>>3 ^ w2<<25 ^ w2<<14 ) + ( w15>>>17 ^ w15>>>19 ^ w15>>>10 ^ w15<<15 ^ w15<<13 ) + w1 + w10 )|0;
  1960. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x1e376c08 )|0;
  1961. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1962. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1963. // 50
  1964. w2 = t = ( ( w3>>>7 ^ w3>>>18 ^ w3>>>3 ^ w3<<25 ^ w3<<14 ) + ( w0>>>17 ^ w0>>>19 ^ w0>>>10 ^ w0<<15 ^ w0<<13 ) + w2 + w11 )|0;
  1965. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x2748774c )|0;
  1966. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1967. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1968. // 51
  1969. w3 = t = ( ( w4>>>7 ^ w4>>>18 ^ w4>>>3 ^ w4<<25 ^ w4<<14 ) + ( w1>>>17 ^ w1>>>19 ^ w1>>>10 ^ w1<<15 ^ w1<<13 ) + w3 + w12 )|0;
  1970. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x34b0bcb5 )|0;
  1971. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1972. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1973. // 52
  1974. w4 = t = ( ( w5>>>7 ^ w5>>>18 ^ w5>>>3 ^ w5<<25 ^ w5<<14 ) + ( w2>>>17 ^ w2>>>19 ^ w2>>>10 ^ w2<<15 ^ w2<<13 ) + w4 + w13 )|0;
  1975. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x391c0cb3 )|0;
  1976. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1977. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1978. // 53
  1979. w5 = t = ( ( w6>>>7 ^ w6>>>18 ^ w6>>>3 ^ w6<<25 ^ w6<<14 ) + ( w3>>>17 ^ w3>>>19 ^ w3>>>10 ^ w3<<15 ^ w3<<13 ) + w5 + w14 )|0;
  1980. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x4ed8aa4a )|0;
  1981. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1982. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1983. // 54
  1984. w6 = t = ( ( w7>>>7 ^ w7>>>18 ^ w7>>>3 ^ w7<<25 ^ w7<<14 ) + ( w4>>>17 ^ w4>>>19 ^ w4>>>10 ^ w4<<15 ^ w4<<13 ) + w6 + w15 )|0;
  1985. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x5b9cca4f )|0;
  1986. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1987. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1988. // 55
  1989. w7 = t = ( ( w8>>>7 ^ w8>>>18 ^ w8>>>3 ^ w8<<25 ^ w8<<14 ) + ( w5>>>17 ^ w5>>>19 ^ w5>>>10 ^ w5<<15 ^ w5<<13 ) + w7 + w0 )|0;
  1990. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x682e6ff3 )|0;
  1991. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1992. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1993. // 56
  1994. w8 = t = ( ( w9>>>7 ^ w9>>>18 ^ w9>>>3 ^ w9<<25 ^ w9<<14 ) + ( w6>>>17 ^ w6>>>19 ^ w6>>>10 ^ w6<<15 ^ w6<<13 ) + w8 + w1 )|0;
  1995. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x748f82ee )|0;
  1996. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  1997. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  1998. // 57
  1999. w9 = t = ( ( w10>>>7 ^ w10>>>18 ^ w10>>>3 ^ w10<<25 ^ w10<<14 ) + ( w7>>>17 ^ w7>>>19 ^ w7>>>10 ^ w7<<15 ^ w7<<13 ) + w9 + w2 )|0;
  2000. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x78a5636f )|0;
  2001. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  2002. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  2003. // 58
  2004. w10 = t = ( ( w11>>>7 ^ w11>>>18 ^ w11>>>3 ^ w11<<25 ^ w11<<14 ) + ( w8>>>17 ^ w8>>>19 ^ w8>>>10 ^ w8<<15 ^ w8<<13 ) + w10 + w3 )|0;
  2005. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x84c87814 )|0;
  2006. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  2007. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  2008. // 59
  2009. w11 = t = ( ( w12>>>7 ^ w12>>>18 ^ w12>>>3 ^ w12<<25 ^ w12<<14 ) + ( w9>>>17 ^ w9>>>19 ^ w9>>>10 ^ w9<<15 ^ w9<<13 ) + w11 + w4 )|0;
  2010. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x8cc70208 )|0;
  2011. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  2012. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  2013. // 60
  2014. w12 = t = ( ( w13>>>7 ^ w13>>>18 ^ w13>>>3 ^ w13<<25 ^ w13<<14 ) + ( w10>>>17 ^ w10>>>19 ^ w10>>>10 ^ w10<<15 ^ w10<<13 ) + w12 + w5 )|0;
  2015. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0x90befffa )|0;
  2016. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  2017. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  2018. // 61
  2019. w13 = t = ( ( w14>>>7 ^ w14>>>18 ^ w14>>>3 ^ w14<<25 ^ w14<<14 ) + ( w11>>>17 ^ w11>>>19 ^ w11>>>10 ^ w11<<15 ^ w11<<13 ) + w13 + w6 )|0;
  2020. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xa4506ceb )|0;
  2021. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  2022. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  2023. // 62
  2024. w14 = t = ( ( w15>>>7 ^ w15>>>18 ^ w15>>>3 ^ w15<<25 ^ w15<<14 ) + ( w12>>>17 ^ w12>>>19 ^ w12>>>10 ^ w12<<15 ^ w12<<13 ) + w14 + w7 )|0;
  2025. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xbef9a3f7 )|0;
  2026. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  2027. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  2028. // 63
  2029. w15 = t = ( ( w0>>>7 ^ w0>>>18 ^ w0>>>3 ^ w0<<25 ^ w0<<14 ) + ( w13>>>17 ^ w13>>>19 ^ w13>>>10 ^ w13<<15 ^ w13<<13 ) + w15 + w8 )|0;
  2030. t = ( t + h + ( e>>>6 ^ e>>>11 ^ e>>>25 ^ e<<26 ^ e<<21 ^ e<<7 ) + ( g ^ e & (f^g) ) + 0xc67178f2 )|0;
  2031. h = g; g = f; f = e; e = ( d + t )|0; d = c; c = b; b = a;
  2032. a = ( t + ( (b & c) ^ ( d & (b ^ c) ) ) + ( b>>>2 ^ b>>>13 ^ b>>>22 ^ b<<30 ^ b<<19 ^ b<<10 ) )|0;
  2033. H0 = ( H0 + a )|0;
  2034. H1 = ( H1 + b )|0;
  2035. H2 = ( H2 + c )|0;
  2036. H3 = ( H3 + d )|0;
  2037. H4 = ( H4 + e )|0;
  2038. H5 = ( H5 + f )|0;
  2039. H6 = ( H6 + g )|0;
  2040. H7 = ( H7 + h )|0;
  2041. }
  2042. function _core_heap ( offset ) {
  2043. offset = offset|0;
  2044. _core(
  2045. HEAP[offset|0]<<24 | HEAP[offset|1]<<16 | HEAP[offset|2]<<8 | HEAP[offset|3],
  2046. HEAP[offset|4]<<24 | HEAP[offset|5]<<16 | HEAP[offset|6]<<8 | HEAP[offset|7],
  2047. HEAP[offset|8]<<24 | HEAP[offset|9]<<16 | HEAP[offset|10]<<8 | HEAP[offset|11],
  2048. HEAP[offset|12]<<24 | HEAP[offset|13]<<16 | HEAP[offset|14]<<8 | HEAP[offset|15],
  2049. HEAP[offset|16]<<24 | HEAP[offset|17]<<16 | HEAP[offset|18]<<8 | HEAP[offset|19],
  2050. HEAP[offset|20]<<24 | HEAP[offset|21]<<16 | HEAP[offset|22]<<8 | HEAP[offset|23],
  2051. HEAP[offset|24]<<24 | HEAP[offset|25]<<16 | HEAP[offset|26]<<8 | HEAP[offset|27],
  2052. HEAP[offset|28]<<24 | HEAP[offset|29]<<16 | HEAP[offset|30]<<8 | HEAP[offset|31],
  2053. HEAP[offset|32]<<24 | HEAP[offset|33]<<16 | HEAP[offset|34]<<8 | HEAP[offset|35],
  2054. HEAP[offset|36]<<24 | HEAP[offset|37]<<16 | HEAP[offset|38]<<8 | HEAP[offset|39],
  2055. HEAP[offset|40]<<24 | HEAP[offset|41]<<16 | HEAP[offset|42]<<8 | HEAP[offset|43],
  2056. HEAP[offset|44]<<24 | HEAP[offset|45]<<16 | HEAP[offset|46]<<8 | HEAP[offset|47],
  2057. HEAP[offset|48]<<24 | HEAP[offset|49]<<16 | HEAP[offset|50]<<8 | HEAP[offset|51],
  2058. HEAP[offset|52]<<24 | HEAP[offset|53]<<16 | HEAP[offset|54]<<8 | HEAP[offset|55],
  2059. HEAP[offset|56]<<24 | HEAP[offset|57]<<16 | HEAP[offset|58]<<8 | HEAP[offset|59],
  2060. HEAP[offset|60]<<24 | HEAP[offset|61]<<16 | HEAP[offset|62]<<8 | HEAP[offset|63]
  2061. );
  2062. }
  2063. // offset — multiple of 32
  2064. function _state_to_heap ( output ) {
  2065. output = output|0;
  2066. HEAP[output|0] = H0>>>24;
  2067. HEAP[output|1] = H0>>>16&255;
  2068. HEAP[output|2] = H0>>>8&255;
  2069. HEAP[output|3] = H0&255;
  2070. HEAP[output|4] = H1>>>24;
  2071. HEAP[output|5] = H1>>>16&255;
  2072. HEAP[output|6] = H1>>>8&255;
  2073. HEAP[output|7] = H1&255;
  2074. HEAP[output|8] = H2>>>24;
  2075. HEAP[output|9] = H2>>>16&255;
  2076. HEAP[output|10] = H2>>>8&255;
  2077. HEAP[output|11] = H2&255;
  2078. HEAP[output|12] = H3>>>24;
  2079. HEAP[output|13] = H3>>>16&255;
  2080. HEAP[output|14] = H3>>>8&255;
  2081. HEAP[output|15] = H3&255;
  2082. HEAP[output|16] = H4>>>24;
  2083. HEAP[output|17] = H4>>>16&255;
  2084. HEAP[output|18] = H4>>>8&255;
  2085. HEAP[output|19] = H4&255;
  2086. HEAP[output|20] = H5>>>24;
  2087. HEAP[output|21] = H5>>>16&255;
  2088. HEAP[output|22] = H5>>>8&255;
  2089. HEAP[output|23] = H5&255;
  2090. HEAP[output|24] = H6>>>24;
  2091. HEAP[output|25] = H6>>>16&255;
  2092. HEAP[output|26] = H6>>>8&255;
  2093. HEAP[output|27] = H6&255;
  2094. HEAP[output|28] = H7>>>24;
  2095. HEAP[output|29] = H7>>>16&255;
  2096. HEAP[output|30] = H7>>>8&255;
  2097. HEAP[output|31] = H7&255;
  2098. }
  2099. function reset () {
  2100. H0 = 0x6a09e667;
  2101. H1 = 0xbb67ae85;
  2102. H2 = 0x3c6ef372;
  2103. H3 = 0xa54ff53a;
  2104. H4 = 0x510e527f;
  2105. H5 = 0x9b05688c;
  2106. H6 = 0x1f83d9ab;
  2107. H7 = 0x5be0cd19;
  2108. TOTAL0 = TOTAL1 = 0;
  2109. }
  2110. function init ( h0, h1, h2, h3, h4, h5, h6, h7, total0, total1 ) {
  2111. h0 = h0|0;
  2112. h1 = h1|0;
  2113. h2 = h2|0;
  2114. h3 = h3|0;
  2115. h4 = h4|0;
  2116. h5 = h5|0;
  2117. h6 = h6|0;
  2118. h7 = h7|0;
  2119. total0 = total0|0;
  2120. total1 = total1|0;
  2121. H0 = h0;
  2122. H1 = h1;
  2123. H2 = h2;
  2124. H3 = h3;
  2125. H4 = h4;
  2126. H5 = h5;
  2127. H6 = h6;
  2128. H7 = h7;
  2129. TOTAL0 = total0;
  2130. TOTAL1 = total1;
  2131. }
  2132. // offset — multiple of 64
  2133. function process ( offset, length ) {
  2134. offset = offset|0;
  2135. length = length|0;
  2136. var hashed = 0;
  2137. if ( offset & 63 )
  2138. return -1;
  2139. while ( (length|0) >= 64 ) {
  2140. _core_heap(offset);
  2141. offset = ( offset + 64 )|0;
  2142. length = ( length - 64 )|0;
  2143. hashed = ( hashed + 64 )|0;
  2144. }
  2145. TOTAL0 = ( TOTAL0 + hashed )|0;
  2146. if ( TOTAL0>>>0 < hashed>>>0 ) TOTAL1 = ( TOTAL1 + 1 )|0;
  2147. return hashed|0;
  2148. }
  2149. // offset — multiple of 64
  2150. // output — multiple of 32
  2151. function finish ( offset, length, output ) {
  2152. offset = offset|0;
  2153. length = length|0;
  2154. output = output|0;
  2155. var hashed = 0,
  2156. i = 0;
  2157. if ( offset & 63 )
  2158. return -1;
  2159. if ( ~output )
  2160. if ( output & 31 )
  2161. return -1;
  2162. if ( (length|0) >= 64 ) {
  2163. hashed = process( offset, length )|0;
  2164. if ( (hashed|0) == -1 )
  2165. return -1;
  2166. offset = ( offset + hashed )|0;
  2167. length = ( length - hashed )|0;
  2168. }
  2169. hashed = ( hashed + length )|0;
  2170. TOTAL0 = ( TOTAL0 + length )|0;
  2171. if ( TOTAL0>>>0 < length>>>0 ) TOTAL1 = ( TOTAL1 + 1 )|0;
  2172. HEAP[offset|length] = 0x80;
  2173. if ( (length|0) >= 56 ) {
  2174. for ( i = (length+1)|0; (i|0) < 64; i = (i+1)|0 )
  2175. HEAP[offset|i] = 0x00;
  2176. _core_heap(offset);
  2177. length = 0;
  2178. HEAP[offset|0] = 0;
  2179. }
  2180. for ( i = (length+1)|0; (i|0) < 59; i = (i+1)|0 )
  2181. HEAP[offset|i] = 0;
  2182. HEAP[offset|56] = TOTAL1>>>21&255;
  2183. HEAP[offset|57] = TOTAL1>>>13&255;
  2184. HEAP[offset|58] = TOTAL1>>>5&255;
  2185. HEAP[offset|59] = TOTAL1<<3&255 | TOTAL0>>>29;
  2186. HEAP[offset|60] = TOTAL0>>>21&255;
  2187. HEAP[offset|61] = TOTAL0>>>13&255;
  2188. HEAP[offset|62] = TOTAL0>>>5&255;
  2189. HEAP[offset|63] = TOTAL0<<3&255;
  2190. _core_heap(offset);
  2191. if ( ~output )
  2192. _state_to_heap(output);
  2193. return hashed|0;
  2194. }
  2195. function hmac_reset () {
  2196. H0 = I0;
  2197. H1 = I1;
  2198. H2 = I2;
  2199. H3 = I3;
  2200. H4 = I4;
  2201. H5 = I5;
  2202. H6 = I6;
  2203. H7 = I7;
  2204. TOTAL0 = 64;
  2205. TOTAL1 = 0;
  2206. }
  2207. function _hmac_opad () {
  2208. H0 = O0;
  2209. H1 = O1;
  2210. H2 = O2;
  2211. H3 = O3;
  2212. H4 = O4;
  2213. H5 = O5;
  2214. H6 = O6;
  2215. H7 = O7;
  2216. TOTAL0 = 64;
  2217. TOTAL1 = 0;
  2218. }
  2219. function hmac_init ( p0, p1, p2, p3, p4, p5, p6, p7, p8, p9, p10, p11, p12, p13, p14, p15 ) {
  2220. p0 = p0|0;
  2221. p1 = p1|0;
  2222. p2 = p2|0;
  2223. p3 = p3|0;
  2224. p4 = p4|0;
  2225. p5 = p5|0;
  2226. p6 = p6|0;
  2227. p7 = p7|0;
  2228. p8 = p8|0;
  2229. p9 = p9|0;
  2230. p10 = p10|0;
  2231. p11 = p11|0;
  2232. p12 = p12|0;
  2233. p13 = p13|0;
  2234. p14 = p14|0;
  2235. p15 = p15|0;
  2236. // opad
  2237. reset();
  2238. _core(
  2239. p0 ^ 0x5c5c5c5c,
  2240. p1 ^ 0x5c5c5c5c,
  2241. p2 ^ 0x5c5c5c5c,
  2242. p3 ^ 0x5c5c5c5c,
  2243. p4 ^ 0x5c5c5c5c,
  2244. p5 ^ 0x5c5c5c5c,
  2245. p6 ^ 0x5c5c5c5c,
  2246. p7 ^ 0x5c5c5c5c,
  2247. p8 ^ 0x5c5c5c5c,
  2248. p9 ^ 0x5c5c5c5c,
  2249. p10 ^ 0x5c5c5c5c,
  2250. p11 ^ 0x5c5c5c5c,
  2251. p12 ^ 0x5c5c5c5c,
  2252. p13 ^ 0x5c5c5c5c,
  2253. p14 ^ 0x5c5c5c5c,
  2254. p15 ^ 0x5c5c5c5c
  2255. );
  2256. O0 = H0;
  2257. O1 = H1;
  2258. O2 = H2;
  2259. O3 = H3;
  2260. O4 = H4;
  2261. O5 = H5;
  2262. O6 = H6;
  2263. O7 = H7;
  2264. // ipad
  2265. reset();
  2266. _core(
  2267. p0 ^ 0x36363636,
  2268. p1 ^ 0x36363636,
  2269. p2 ^ 0x36363636,
  2270. p3 ^ 0x36363636,
  2271. p4 ^ 0x36363636,
  2272. p5 ^ 0x36363636,
  2273. p6 ^ 0x36363636,
  2274. p7 ^ 0x36363636,
  2275. p8 ^ 0x36363636,
  2276. p9 ^ 0x36363636,
  2277. p10 ^ 0x36363636,
  2278. p11 ^ 0x36363636,
  2279. p12 ^ 0x36363636,
  2280. p13 ^ 0x36363636,
  2281. p14 ^ 0x36363636,
  2282. p15 ^ 0x36363636
  2283. );
  2284. I0 = H0;
  2285. I1 = H1;
  2286. I2 = H2;
  2287. I3 = H3;
  2288. I4 = H4;
  2289. I5 = H5;
  2290. I6 = H6;
  2291. I7 = H7;
  2292. TOTAL0 = 64;
  2293. TOTAL1 = 0;
  2294. }
  2295. // offset — multiple of 64
  2296. // output — multiple of 32
  2297. function hmac_finish ( offset, length, output ) {
  2298. offset = offset|0;
  2299. length = length|0;
  2300. output = output|0;
  2301. var t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0, t6 = 0, t7 = 0,
  2302. hashed = 0;
  2303. if ( offset & 63 )
  2304. return -1;
  2305. if ( ~output )
  2306. if ( output & 31 )
  2307. return -1;
  2308. hashed = finish( offset, length, -1 )|0;
  2309. t0 = H0, t1 = H1, t2 = H2, t3 = H3, t4 = H4, t5 = H5, t6 = H6, t7 = H7;
  2310. _hmac_opad();
  2311. _core( t0, t1, t2, t3, t4, t5, t6, t7, 0x80000000, 0, 0, 0, 0, 0, 0, 768 );
  2312. if ( ~output )
  2313. _state_to_heap(output);
  2314. return hashed|0;
  2315. }
  2316. // salt is assumed to be already processed
  2317. // offset — multiple of 64
  2318. // output — multiple of 32
  2319. function pbkdf2_generate_block ( offset, length, block, count, output ) {
  2320. offset = offset|0;
  2321. length = length|0;
  2322. block = block|0;
  2323. count = count|0;
  2324. output = output|0;
  2325. var h0 = 0, h1 = 0, h2 = 0, h3 = 0, h4 = 0, h5 = 0, h6 = 0, h7 = 0,
  2326. t0 = 0, t1 = 0, t2 = 0, t3 = 0, t4 = 0, t5 = 0, t6 = 0, t7 = 0;
  2327. if ( offset & 63 )
  2328. return -1;
  2329. if ( ~output )
  2330. if ( output & 31 )
  2331. return -1;
  2332. // pad block number into heap
  2333. // FIXME probable OOB write
  2334. HEAP[(offset+length)|0] = block>>>24;
  2335. HEAP[(offset+length+1)|0] = block>>>16&255;
  2336. HEAP[(offset+length+2)|0] = block>>>8&255;
  2337. HEAP[(offset+length+3)|0] = block&255;
  2338. // finish first iteration
  2339. hmac_finish( offset, (length+4)|0, -1 )|0;
  2340. h0 = t0 = H0, h1 = t1 = H1, h2 = t2 = H2, h3 = t3 = H3, h4 = t4 = H4, h5 = t5 = H5, h6 = t6 = H6, h7 = t7 = H7;
  2341. count = (count-1)|0;
  2342. // perform the rest iterations
  2343. while ( (count|0) > 0 ) {
  2344. hmac_reset();
  2345. _core( t0, t1, t2, t3, t4, t5, t6, t7, 0x80000000, 0, 0, 0, 0, 0, 0, 768 );
  2346. t0 = H0, t1 = H1, t2 = H2, t3 = H3, t4 = H4, t5 = H5, t6 = H6, t7 = H7;
  2347. _hmac_opad();
  2348. _core( t0, t1, t2, t3, t4, t5, t6, t7, 0x80000000, 0, 0, 0, 0, 0, 0, 768 );
  2349. t0 = H0, t1 = H1, t2 = H2, t3 = H3, t4 = H4, t5 = H5, t6 = H6, t7 = H7;
  2350. h0 = h0 ^ H0;
  2351. h1 = h1 ^ H1;
  2352. h2 = h2 ^ H2;
  2353. h3 = h3 ^ H3;
  2354. h4 = h4 ^ H4;
  2355. h5 = h5 ^ H5;
  2356. h6 = h6 ^ H6;
  2357. h7 = h7 ^ H7;
  2358. count = (count-1)|0;
  2359. }
  2360. H0 = h0;
  2361. H1 = h1;
  2362. H2 = h2;
  2363. H3 = h3;
  2364. H4 = h4;
  2365. H5 = h5;
  2366. H6 = h6;
  2367. H7 = h7;
  2368. if ( ~output )
  2369. _state_to_heap(output);
  2370. return 0;
  2371. }
  2372. return {
  2373. // SHA256
  2374. reset: reset,
  2375. init: init,
  2376. process: process,
  2377. finish: finish,
  2378. // HMAC-SHA256
  2379. hmac_reset: hmac_reset,
  2380. hmac_init: hmac_init,
  2381. hmac_finish: hmac_finish,
  2382. // PBKDF2-HMAC-SHA256
  2383. pbkdf2_generate_block: pbkdf2_generate_block
  2384. }
  2385. }
  2386. var _sha256_block_size = 64,
  2387. _sha256_hash_size = 32;
  2388. function sha256_constructor ( options ) {
  2389. options = options || {};
  2390. this.heap = _heap_init( Uint8Array, options );
  2391. this.asm = options.asm || sha256_asm( global, null, this.heap.buffer );
  2392. this.BLOCK_SIZE = _sha256_block_size;
  2393. this.HASH_SIZE = _sha256_hash_size;
  2394. this.reset();
  2395. }
  2396. sha256_constructor.BLOCK_SIZE = _sha256_block_size;
  2397. sha256_constructor.HASH_SIZE = _sha256_hash_size;
  2398. var sha256_prototype = sha256_constructor.prototype;
  2399. sha256_prototype.reset = hash_reset;
  2400. sha256_prototype.process = hash_process;
  2401. sha256_prototype.finish = hash_finish;
  2402. var sha256_instance = null;
  2403. function get_sha256_instance () {
  2404. if ( sha256_instance === null ) sha256_instance = new sha256_constructor( { heapSize: 0x100000 } );
  2405. return sha256_instance;
  2406. }
  2407. /**
  2408. * SHA256 exports
  2409. */
  2410. function sha256_bytes ( data ) {
  2411. if ( data === undefined ) throw new SyntaxError("data required");
  2412. return get_sha256_instance().reset().process(data).finish().result;
  2413. }
  2414. function sha256_hex ( data ) {
  2415. var result = sha256_bytes(data);
  2416. return bytes_to_hex(result);
  2417. }
  2418. function sha256_base64 ( data ) {
  2419. var result = sha256_bytes(data);
  2420. return bytes_to_base64(result);
  2421. }
  2422. sha256_constructor.bytes = sha256_bytes;
  2423. sha256_constructor.hex = sha256_hex;
  2424. sha256_constructor.base64 = sha256_base64;
  2425. exports.SHA256 = sha256_constructor;
  2426. 'function'==typeof define&&define.amd?define([],function(){return exports}):'object'==typeof module&&module.exports?module.exports=exports:global.asmCrypto=exports;
  2427. return exports;
  2428. })( {}, function(){return this}() );
  2429. },{}],2:[function(_dereq_,module,exports){
  2430. (function (process,global){
  2431. /*!
  2432. * @overview es6-promise - a tiny implementation of Promises/A+.
  2433. * @copyright Copyright (c) 2014 Yehuda Katz, Tom Dale, Stefan Penner and contributors (Conversion to ES6 API by Jake Archibald)
  2434. * @license Licensed under MIT license
  2435. * See https://raw.githubusercontent.com/jakearchibald/es6-promise/master/LICENSE
  2436. * @version 3.2.1
  2437. */
  2438. (function() {
  2439. "use strict";
  2440. function lib$es6$promise$utils$$objectOrFunction(x) {
  2441. return typeof x === 'function' || (typeof x === 'object' && x !== null);
  2442. }
  2443. function lib$es6$promise$utils$$isFunction(x) {
  2444. return typeof x === 'function';
  2445. }
  2446. function lib$es6$promise$utils$$isMaybeThenable(x) {
  2447. return typeof x === 'object' && x !== null;
  2448. }
  2449. var lib$es6$promise$utils$$_isArray;
  2450. if (!Array.isArray) {
  2451. lib$es6$promise$utils$$_isArray = function (x) {
  2452. return Object.prototype.toString.call(x) === '[object Array]';
  2453. };
  2454. } else {
  2455. lib$es6$promise$utils$$_isArray = Array.isArray;
  2456. }
  2457. var lib$es6$promise$utils$$isArray = lib$es6$promise$utils$$_isArray;
  2458. var lib$es6$promise$asap$$len = 0;
  2459. var lib$es6$promise$asap$$vertxNext;
  2460. var lib$es6$promise$asap$$customSchedulerFn;
  2461. var lib$es6$promise$asap$$asap = function asap(callback, arg) {
  2462. lib$es6$promise$asap$$queue[lib$es6$promise$asap$$len] = callback;
  2463. lib$es6$promise$asap$$queue[lib$es6$promise$asap$$len + 1] = arg;
  2464. lib$es6$promise$asap$$len += 2;
  2465. if (lib$es6$promise$asap$$len === 2) {
  2466. // If len is 2, that means that we need to schedule an async flush.
  2467. // If additional callbacks are queued before the queue is flushed, they
  2468. // will be processed by this flush that we are scheduling.
  2469. if (lib$es6$promise$asap$$customSchedulerFn) {
  2470. lib$es6$promise$asap$$customSchedulerFn(lib$es6$promise$asap$$flush);
  2471. } else {
  2472. lib$es6$promise$asap$$scheduleFlush();
  2473. }
  2474. }
  2475. }
  2476. function lib$es6$promise$asap$$setScheduler(scheduleFn) {
  2477. lib$es6$promise$asap$$customSchedulerFn = scheduleFn;
  2478. }
  2479. function lib$es6$promise$asap$$setAsap(asapFn) {
  2480. lib$es6$promise$asap$$asap = asapFn;
  2481. }
  2482. var lib$es6$promise$asap$$browserWindow = (typeof window !== 'undefined') ? window : undefined;
  2483. var lib$es6$promise$asap$$browserGlobal = lib$es6$promise$asap$$browserWindow || {};
  2484. var lib$es6$promise$asap$$BrowserMutationObserver = lib$es6$promise$asap$$browserGlobal.MutationObserver || lib$es6$promise$asap$$browserGlobal.WebKitMutationObserver;
  2485. var lib$es6$promise$asap$$isNode = typeof self === 'undefined' && typeof process !== 'undefined' && {}.toString.call(process) === '[object process]';
  2486. // test for web worker but not in IE10
  2487. var lib$es6$promise$asap$$isWorker = typeof Uint8ClampedArray !== 'undefined' &&
  2488. typeof importScripts !== 'undefined' &&
  2489. typeof MessageChannel !== 'undefined';
  2490. // node
  2491. function lib$es6$promise$asap$$useNextTick() {
  2492. // node version 0.10.x displays a deprecation warning when nextTick is used recursively
  2493. // see https://github.com/cujojs/when/issues/410 for details
  2494. return function() {
  2495. process.nextTick(lib$es6$promise$asap$$flush);
  2496. };
  2497. }
  2498. // vertx
  2499. function lib$es6$promise$asap$$useVertxTimer() {
  2500. return function() {
  2501. lib$es6$promise$asap$$vertxNext(lib$es6$promise$asap$$flush);
  2502. };
  2503. }
  2504. function lib$es6$promise$asap$$useMutationObserver() {
  2505. var iterations = 0;
  2506. var observer = new lib$es6$promise$asap$$BrowserMutationObserver(lib$es6$promise$asap$$flush);
  2507. var node = document.createTextNode('');
  2508. observer.observe(node, { characterData: true });
  2509. return function() {
  2510. node.data = (iterations = ++iterations % 2);
  2511. };
  2512. }
  2513. // web worker
  2514. function lib$es6$promise$asap$$useMessageChannel() {
  2515. var channel = new MessageChannel();
  2516. channel.port1.onmessage = lib$es6$promise$asap$$flush;
  2517. return function () {
  2518. channel.port2.postMessage(0);
  2519. };
  2520. }
  2521. function lib$es6$promise$asap$$useSetTimeout() {
  2522. return function() {
  2523. setTimeout(lib$es6$promise$asap$$flush, 1);
  2524. };
  2525. }
  2526. var lib$es6$promise$asap$$queue = new Array(1000);
  2527. function lib$es6$promise$asap$$flush() {
  2528. for (var i = 0; i < lib$es6$promise$asap$$len; i+=2) {
  2529. var callback = lib$es6$promise$asap$$queue[i];
  2530. var arg = lib$es6$promise$asap$$queue[i+1];
  2531. callback(arg);
  2532. lib$es6$promise$asap$$queue[i] = undefined;
  2533. lib$es6$promise$asap$$queue[i+1] = undefined;
  2534. }
  2535. lib$es6$promise$asap$$len = 0;
  2536. }
  2537. function lib$es6$promise$asap$$attemptVertx() {
  2538. try {
  2539. var r = _dereq_;
  2540. var vertx = r('vertx');
  2541. lib$es6$promise$asap$$vertxNext = vertx.runOnLoop || vertx.runOnContext;
  2542. return lib$es6$promise$asap$$useVertxTimer();
  2543. } catch(e) {
  2544. return lib$es6$promise$asap$$useSetTimeout();
  2545. }
  2546. }
  2547. var lib$es6$promise$asap$$scheduleFlush;
  2548. // Decide what async method to use to triggering processing of queued callbacks:
  2549. if (lib$es6$promise$asap$$isNode) {
  2550. lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useNextTick();
  2551. } else if (lib$es6$promise$asap$$BrowserMutationObserver) {
  2552. lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useMutationObserver();
  2553. } else if (lib$es6$promise$asap$$isWorker) {
  2554. lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useMessageChannel();
  2555. } else if (lib$es6$promise$asap$$browserWindow === undefined && typeof _dereq_ === 'function') {
  2556. lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$attemptVertx();
  2557. } else {
  2558. lib$es6$promise$asap$$scheduleFlush = lib$es6$promise$asap$$useSetTimeout();
  2559. }
  2560. function lib$es6$promise$then$$then(onFulfillment, onRejection) {
  2561. var parent = this;
  2562. var child = new this.constructor(lib$es6$promise$$internal$$noop);
  2563. if (child[lib$es6$promise$$internal$$PROMISE_ID] === undefined) {
  2564. lib$es6$promise$$internal$$makePromise(child);
  2565. }
  2566. var state = parent._state;
  2567. if (state) {
  2568. var callback = arguments[state - 1];
  2569. lib$es6$promise$asap$$asap(function(){
  2570. lib$es6$promise$$internal$$invokeCallback(state, child, callback, parent._result);
  2571. });
  2572. } else {
  2573. lib$es6$promise$$internal$$subscribe(parent, child, onFulfillment, onRejection);
  2574. }
  2575. return child;
  2576. }
  2577. var lib$es6$promise$then$$default = lib$es6$promise$then$$then;
  2578. function lib$es6$promise$promise$resolve$$resolve(object) {
  2579. /*jshint validthis:true */
  2580. var Constructor = this;
  2581. if (object && typeof object === 'object' && object.constructor === Constructor) {
  2582. return object;
  2583. }
  2584. var promise = new Constructor(lib$es6$promise$$internal$$noop);
  2585. lib$es6$promise$$internal$$resolve(promise, object);
  2586. return promise;
  2587. }
  2588. var lib$es6$promise$promise$resolve$$default = lib$es6$promise$promise$resolve$$resolve;
  2589. var lib$es6$promise$$internal$$PROMISE_ID = Math.random().toString(36).substring(16);
  2590. function lib$es6$promise$$internal$$noop() {}
  2591. var lib$es6$promise$$internal$$PENDING = void 0;
  2592. var lib$es6$promise$$internal$$FULFILLED = 1;
  2593. var lib$es6$promise$$internal$$REJECTED = 2;
  2594. var lib$es6$promise$$internal$$GET_THEN_ERROR = new lib$es6$promise$$internal$$ErrorObject();
  2595. function lib$es6$promise$$internal$$selfFulfillment() {
  2596. return new TypeError("You cannot resolve a promise with itself");
  2597. }
  2598. function lib$es6$promise$$internal$$cannotReturnOwn() {
  2599. return new TypeError('A promises callback cannot return that same promise.');
  2600. }
  2601. function lib$es6$promise$$internal$$getThen(promise) {
  2602. try {
  2603. return promise.then;
  2604. } catch(error) {
  2605. lib$es6$promise$$internal$$GET_THEN_ERROR.error = error;
  2606. return lib$es6$promise$$internal$$GET_THEN_ERROR;
  2607. }
  2608. }
  2609. function lib$es6$promise$$internal$$tryThen(then, value, fulfillmentHandler, rejectionHandler) {
  2610. try {
  2611. then.call(value, fulfillmentHandler, rejectionHandler);
  2612. } catch(e) {
  2613. return e;
  2614. }
  2615. }
  2616. function lib$es6$promise$$internal$$handleForeignThenable(promise, thenable, then) {
  2617. lib$es6$promise$asap$$asap(function(promise) {
  2618. var sealed = false;
  2619. var error = lib$es6$promise$$internal$$tryThen(then, thenable, function(value) {
  2620. if (sealed) { return; }
  2621. sealed = true;
  2622. if (thenable !== value) {
  2623. lib$es6$promise$$internal$$resolve(promise, value);
  2624. } else {
  2625. lib$es6$promise$$internal$$fulfill(promise, value);
  2626. }
  2627. }, function(reason) {
  2628. if (sealed) { return; }
  2629. sealed = true;
  2630. lib$es6$promise$$internal$$reject(promise, reason);
  2631. }, 'Settle: ' + (promise._label || ' unknown promise'));
  2632. if (!sealed && error) {
  2633. sealed = true;
  2634. lib$es6$promise$$internal$$reject(promise, error);
  2635. }
  2636. }, promise);
  2637. }
  2638. function lib$es6$promise$$internal$$handleOwnThenable(promise, thenable) {
  2639. if (thenable._state === lib$es6$promise$$internal$$FULFILLED) {
  2640. lib$es6$promise$$internal$$fulfill(promise, thenable._result);
  2641. } else if (thenable._state === lib$es6$promise$$internal$$REJECTED) {
  2642. lib$es6$promise$$internal$$reject(promise, thenable._result);
  2643. } else {
  2644. lib$es6$promise$$internal$$subscribe(thenable, undefined, function(value) {
  2645. lib$es6$promise$$internal$$resolve(promise, value);
  2646. }, function(reason) {
  2647. lib$es6$promise$$internal$$reject(promise, reason);
  2648. });
  2649. }
  2650. }
  2651. function lib$es6$promise$$internal$$handleMaybeThenable(promise, maybeThenable, then) {
  2652. if (maybeThenable.constructor === promise.constructor &&
  2653. then === lib$es6$promise$then$$default &&
  2654. constructor.resolve === lib$es6$promise$promise$resolve$$default) {
  2655. lib$es6$promise$$internal$$handleOwnThenable(promise, maybeThenable);
  2656. } else {
  2657. if (then === lib$es6$promise$$internal$$GET_THEN_ERROR) {
  2658. lib$es6$promise$$internal$$reject(promise, lib$es6$promise$$internal$$GET_THEN_ERROR.error);
  2659. } else if (then === undefined) {
  2660. lib$es6$promise$$internal$$fulfill(promise, maybeThenable);
  2661. } else if (lib$es6$promise$utils$$isFunction(then)) {
  2662. lib$es6$promise$$internal$$handleForeignThenable(promise, maybeThenable, then);
  2663. } else {
  2664. lib$es6$promise$$internal$$fulfill(promise, maybeThenable);
  2665. }
  2666. }
  2667. }
  2668. function lib$es6$promise$$internal$$resolve(promise, value) {
  2669. if (promise === value) {
  2670. lib$es6$promise$$internal$$reject(promise, lib$es6$promise$$internal$$selfFulfillment());
  2671. } else if (lib$es6$promise$utils$$objectOrFunction(value)) {
  2672. lib$es6$promise$$internal$$handleMaybeThenable(promise, value, lib$es6$promise$$internal$$getThen(value));
  2673. } else {
  2674. lib$es6$promise$$internal$$fulfill(promise, value);
  2675. }
  2676. }
  2677. function lib$es6$promise$$internal$$publishRejection(promise) {
  2678. if (promise._onerror) {
  2679. promise._onerror(promise._result);
  2680. }
  2681. lib$es6$promise$$internal$$publish(promise);
  2682. }
  2683. function lib$es6$promise$$internal$$fulfill(promise, value) {
  2684. if (promise._state !== lib$es6$promise$$internal$$PENDING) { return; }
  2685. promise._result = value;
  2686. promise._state = lib$es6$promise$$internal$$FULFILLED;
  2687. if (promise._subscribers.length !== 0) {
  2688. lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publish, promise);
  2689. }
  2690. }
  2691. function lib$es6$promise$$internal$$reject(promise, reason) {
  2692. if (promise._state !== lib$es6$promise$$internal$$PENDING) { return; }
  2693. promise._state = lib$es6$promise$$internal$$REJECTED;
  2694. promise._result = reason;
  2695. lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publishRejection, promise);
  2696. }
  2697. function lib$es6$promise$$internal$$subscribe(parent, child, onFulfillment, onRejection) {
  2698. var subscribers = parent._subscribers;
  2699. var length = subscribers.length;
  2700. parent._onerror = null;
  2701. subscribers[length] = child;
  2702. subscribers[length + lib$es6$promise$$internal$$FULFILLED] = onFulfillment;
  2703. subscribers[length + lib$es6$promise$$internal$$REJECTED] = onRejection;
  2704. if (length === 0 && parent._state) {
  2705. lib$es6$promise$asap$$asap(lib$es6$promise$$internal$$publish, parent);
  2706. }
  2707. }
  2708. function lib$es6$promise$$internal$$publish(promise) {
  2709. var subscribers = promise._subscribers;
  2710. var settled = promise._state;
  2711. if (subscribers.length === 0) { return; }
  2712. var child, callback, detail = promise._result;
  2713. for (var i = 0; i < subscribers.length; i += 3) {
  2714. child = subscribers[i];
  2715. callback = subscribers[i + settled];
  2716. if (child) {
  2717. lib$es6$promise$$internal$$invokeCallback(settled, child, callback, detail);
  2718. } else {
  2719. callback(detail);
  2720. }
  2721. }
  2722. promise._subscribers.length = 0;
  2723. }
  2724. function lib$es6$promise$$internal$$ErrorObject() {
  2725. this.error = null;
  2726. }
  2727. var lib$es6$promise$$internal$$TRY_CATCH_ERROR = new lib$es6$promise$$internal$$ErrorObject();
  2728. function lib$es6$promise$$internal$$tryCatch(callback, detail) {
  2729. try {
  2730. return callback(detail);
  2731. } catch(e) {
  2732. lib$es6$promise$$internal$$TRY_CATCH_ERROR.error = e;
  2733. return lib$es6$promise$$internal$$TRY_CATCH_ERROR;
  2734. }
  2735. }
  2736. function lib$es6$promise$$internal$$invokeCallback(settled, promise, callback, detail) {
  2737. var hasCallback = lib$es6$promise$utils$$isFunction(callback),
  2738. value, error, succeeded, failed;
  2739. if (hasCallback) {
  2740. value = lib$es6$promise$$internal$$tryCatch(callback, detail);
  2741. if (value === lib$es6$promise$$internal$$TRY_CATCH_ERROR) {
  2742. failed = true;
  2743. error = value.error;
  2744. value = null;
  2745. } else {
  2746. succeeded = true;
  2747. }
  2748. if (promise === value) {
  2749. lib$es6$promise$$internal$$reject(promise, lib$es6$promise$$internal$$cannotReturnOwn());
  2750. return;
  2751. }
  2752. } else {
  2753. value = detail;
  2754. succeeded = true;
  2755. }
  2756. if (promise._state !== lib$es6$promise$$internal$$PENDING) {
  2757. // noop
  2758. } else if (hasCallback && succeeded) {
  2759. lib$es6$promise$$internal$$resolve(promise, value);
  2760. } else if (failed) {
  2761. lib$es6$promise$$internal$$reject(promise, error);
  2762. } else if (settled === lib$es6$promise$$internal$$FULFILLED) {
  2763. lib$es6$promise$$internal$$fulfill(promise, value);
  2764. } else if (settled === lib$es6$promise$$internal$$REJECTED) {
  2765. lib$es6$promise$$internal$$reject(promise, value);
  2766. }
  2767. }
  2768. function lib$es6$promise$$internal$$initializePromise(promise, resolver) {
  2769. try {
  2770. resolver(function resolvePromise(value){
  2771. lib$es6$promise$$internal$$resolve(promise, value);
  2772. }, function rejectPromise(reason) {
  2773. lib$es6$promise$$internal$$reject(promise, reason);
  2774. });
  2775. } catch(e) {
  2776. lib$es6$promise$$internal$$reject(promise, e);
  2777. }
  2778. }
  2779. var lib$es6$promise$$internal$$id = 0;
  2780. function lib$es6$promise$$internal$$nextId() {
  2781. return lib$es6$promise$$internal$$id++;
  2782. }
  2783. function lib$es6$promise$$internal$$makePromise(promise) {
  2784. promise[lib$es6$promise$$internal$$PROMISE_ID] = lib$es6$promise$$internal$$id++;
  2785. promise._state = undefined;
  2786. promise._result = undefined;
  2787. promise._subscribers = [];
  2788. }
  2789. function lib$es6$promise$promise$all$$all(entries) {
  2790. return new lib$es6$promise$enumerator$$default(this, entries).promise;
  2791. }
  2792. var lib$es6$promise$promise$all$$default = lib$es6$promise$promise$all$$all;
  2793. function lib$es6$promise$promise$race$$race(entries) {
  2794. /*jshint validthis:true */
  2795. var Constructor = this;
  2796. if (!lib$es6$promise$utils$$isArray(entries)) {
  2797. return new Constructor(function(resolve, reject) {
  2798. reject(new TypeError('You must pass an array to race.'));
  2799. });
  2800. } else {
  2801. return new Constructor(function(resolve, reject) {
  2802. var length = entries.length;
  2803. for (var i = 0; i < length; i++) {
  2804. Constructor.resolve(entries[i]).then(resolve, reject);
  2805. }
  2806. });
  2807. }
  2808. }
  2809. var lib$es6$promise$promise$race$$default = lib$es6$promise$promise$race$$race;
  2810. function lib$es6$promise$promise$reject$$reject(reason) {
  2811. /*jshint validthis:true */
  2812. var Constructor = this;
  2813. var promise = new Constructor(lib$es6$promise$$internal$$noop);
  2814. lib$es6$promise$$internal$$reject(promise, reason);
  2815. return promise;
  2816. }
  2817. var lib$es6$promise$promise$reject$$default = lib$es6$promise$promise$reject$$reject;
  2818. function lib$es6$promise$promise$$needsResolver() {
  2819. throw new TypeError('You must pass a resolver function as the first argument to the promise constructor');
  2820. }
  2821. function lib$es6$promise$promise$$needsNew() {
  2822. throw new TypeError("Failed to construct 'Promise': Please use the 'new' operator, this object constructor cannot be called as a function.");
  2823. }
  2824. var lib$es6$promise$promise$$default = lib$es6$promise$promise$$Promise;
  2825. /**
  2826. Promise objects represent the eventual result of an asynchronous operation. The
  2827. primary way of interacting with a promise is through its `then` method, which
  2828. registers callbacks to receive either a promise's eventual value or the reason
  2829. why the promise cannot be fulfilled.
  2830. Terminology
  2831. -----------
  2832. - `promise` is an object or function with a `then` method whose behavior conforms to this specification.
  2833. - `thenable` is an object or function that defines a `then` method.
  2834. - `value` is any legal JavaScript value (including undefined, a thenable, or a promise).
  2835. - `exception` is a value that is thrown using the throw statement.
  2836. - `reason` is a value that indicates why a promise was rejected.
  2837. - `settled` the final resting state of a promise, fulfilled or rejected.
  2838. A promise can be in one of three states: pending, fulfilled, or rejected.
  2839. Promises that are fulfilled have a fulfillment value and are in the fulfilled
  2840. state. Promises that are rejected have a rejection reason and are in the
  2841. rejected state. A fulfillment value is never a thenable.
  2842. Promises can also be said to *resolve* a value. If this value is also a
  2843. promise, then the original promise's settled state will match the value's
  2844. settled state. So a promise that *resolves* a promise that rejects will
  2845. itself reject, and a promise that *resolves* a promise that fulfills will
  2846. itself fulfill.
  2847. Basic Usage:
  2848. ------------
  2849. ```js
  2850. var promise = new Promise(function(resolve, reject) {
  2851. // on success
  2852. resolve(value);
  2853. // on failure
  2854. reject(reason);
  2855. });
  2856. promise.then(function(value) {
  2857. // on fulfillment
  2858. }, function(reason) {
  2859. // on rejection
  2860. });
  2861. ```
  2862. Advanced Usage:
  2863. ---------------
  2864. Promises shine when abstracting away asynchronous interactions such as
  2865. `XMLHttpRequest`s.
  2866. ```js
  2867. function getJSON(url) {
  2868. return new Promise(function(resolve, reject){
  2869. var xhr = new XMLHttpRequest();
  2870. xhr.open('GET', url);
  2871. xhr.onreadystatechange = handler;
  2872. xhr.responseType = 'json';
  2873. xhr.setRequestHeader('Accept', 'application/json');
  2874. xhr.send();
  2875. function handler() {
  2876. if (this.readyState === this.DONE) {
  2877. if (this.status === 200) {
  2878. resolve(this.response);
  2879. } else {
  2880. reject(new Error('getJSON: `' + url + '` failed with status: [' + this.status + ']'));
  2881. }
  2882. }
  2883. };
  2884. });
  2885. }
  2886. getJSON('/posts.json').then(function(json) {
  2887. // on fulfillment
  2888. }, function(reason) {
  2889. // on rejection
  2890. });
  2891. ```
  2892. Unlike callbacks, promises are great composable primitives.
  2893. ```js
  2894. Promise.all([
  2895. getJSON('/posts'),
  2896. getJSON('/comments')
  2897. ]).then(function(values){
  2898. values[0] // => postsJSON
  2899. values[1] // => commentsJSON
  2900. return values;
  2901. });
  2902. ```
  2903. @class Promise
  2904. @param {function} resolver
  2905. Useful for tooling.
  2906. @constructor
  2907. */
  2908. function lib$es6$promise$promise$$Promise(resolver) {
  2909. this[lib$es6$promise$$internal$$PROMISE_ID] = lib$es6$promise$$internal$$nextId();
  2910. this._result = this._state = undefined;
  2911. this._subscribers = [];
  2912. if (lib$es6$promise$$internal$$noop !== resolver) {
  2913. typeof resolver !== 'function' && lib$es6$promise$promise$$needsResolver();
  2914. this instanceof lib$es6$promise$promise$$Promise ? lib$es6$promise$$internal$$initializePromise(this, resolver) : lib$es6$promise$promise$$needsNew();
  2915. }
  2916. }
  2917. lib$es6$promise$promise$$Promise.all = lib$es6$promise$promise$all$$default;
  2918. lib$es6$promise$promise$$Promise.race = lib$es6$promise$promise$race$$default;
  2919. lib$es6$promise$promise$$Promise.resolve = lib$es6$promise$promise$resolve$$default;
  2920. lib$es6$promise$promise$$Promise.reject = lib$es6$promise$promise$reject$$default;
  2921. lib$es6$promise$promise$$Promise._setScheduler = lib$es6$promise$asap$$setScheduler;
  2922. lib$es6$promise$promise$$Promise._setAsap = lib$es6$promise$asap$$setAsap;
  2923. lib$es6$promise$promise$$Promise._asap = lib$es6$promise$asap$$asap;
  2924. lib$es6$promise$promise$$Promise.prototype = {
  2925. constructor: lib$es6$promise$promise$$Promise,
  2926. /**
  2927. The primary way of interacting with a promise is through its `then` method,
  2928. which registers callbacks to receive either a promise's eventual value or the
  2929. reason why the promise cannot be fulfilled.
  2930. ```js
  2931. findUser().then(function(user){
  2932. // user is available
  2933. }, function(reason){
  2934. // user is unavailable, and you are given the reason why
  2935. });
  2936. ```
  2937. Chaining
  2938. --------
  2939. The return value of `then` is itself a promise. This second, 'downstream'
  2940. promise is resolved with the return value of the first promise's fulfillment
  2941. or rejection handler, or rejected if the handler throws an exception.
  2942. ```js
  2943. findUser().then(function (user) {
  2944. return user.name;
  2945. }, function (reason) {
  2946. return 'default name';
  2947. }).then(function (userName) {
  2948. // If `findUser` fulfilled, `userName` will be the user's name, otherwise it
  2949. // will be `'default name'`
  2950. });
  2951. findUser().then(function (user) {
  2952. throw new Error('Found user, but still unhappy');
  2953. }, function (reason) {
  2954. throw new Error('`findUser` rejected and we're unhappy');
  2955. }).then(function (value) {
  2956. // never reached
  2957. }, function (reason) {
  2958. // if `findUser` fulfilled, `reason` will be 'Found user, but still unhappy'.
  2959. // If `findUser` rejected, `reason` will be '`findUser` rejected and we're unhappy'.
  2960. });
  2961. ```
  2962. If the downstream promise does not specify a rejection handler, rejection reasons will be propagated further downstream.
  2963. ```js
  2964. findUser().then(function (user) {
  2965. throw new PedagogicalException('Upstream error');
  2966. }).then(function (value) {
  2967. // never reached
  2968. }).then(function (value) {
  2969. // never reached
  2970. }, function (reason) {
  2971. // The `PedgagocialException` is propagated all the way down to here
  2972. });
  2973. ```
  2974. Assimilation
  2975. ------------
  2976. Sometimes the value you want to propagate to a downstream promise can only be
  2977. retrieved asynchronously. This can be achieved by returning a promise in the
  2978. fulfillment or rejection handler. The downstream promise will then be pending
  2979. until the returned promise is settled. This is called *assimilation*.
  2980. ```js
  2981. findUser().then(function (user) {
  2982. return findCommentsByAuthor(user);
  2983. }).then(function (comments) {
  2984. // The user's comments are now available
  2985. });
  2986. ```
  2987. If the assimliated promise rejects, then the downstream promise will also reject.
  2988. ```js
  2989. findUser().then(function (user) {
  2990. return findCommentsByAuthor(user);
  2991. }).then(function (comments) {
  2992. // If `findCommentsByAuthor` fulfills, we'll have the value here
  2993. }, function (reason) {
  2994. // If `findCommentsByAuthor` rejects, we'll have the reason here
  2995. });
  2996. ```
  2997. Simple Example
  2998. --------------
  2999. Synchronous Example
  3000. ```javascript
  3001. var result;
  3002. try {
  3003. result = findResult();
  3004. // success
  3005. } catch(reason) {
  3006. // failure
  3007. }
  3008. ```
  3009. Errback Example
  3010. ```js
  3011. findResult(function(result, err){
  3012. if (err) {
  3013. // failure
  3014. } else {
  3015. // success
  3016. }
  3017. });
  3018. ```
  3019. Promise Example;
  3020. ```javascript
  3021. findResult().then(function(result){
  3022. // success
  3023. }, function(reason){
  3024. // failure
  3025. });
  3026. ```
  3027. Advanced Example
  3028. --------------
  3029. Synchronous Example
  3030. ```javascript
  3031. var author, books;
  3032. try {
  3033. author = findAuthor();
  3034. books = findBooksByAuthor(author);
  3035. // success
  3036. } catch(reason) {
  3037. // failure
  3038. }
  3039. ```
  3040. Errback Example
  3041. ```js
  3042. function foundBooks(books) {
  3043. }
  3044. function failure(reason) {
  3045. }
  3046. findAuthor(function(author, err){
  3047. if (err) {
  3048. failure(err);
  3049. // failure
  3050. } else {
  3051. try {
  3052. findBoooksByAuthor(author, function(books, err) {
  3053. if (err) {
  3054. failure(err);
  3055. } else {
  3056. try {
  3057. foundBooks(books);
  3058. } catch(reason) {
  3059. failure(reason);
  3060. }
  3061. }
  3062. });
  3063. } catch(error) {
  3064. failure(err);
  3065. }
  3066. // success
  3067. }
  3068. });
  3069. ```
  3070. Promise Example;
  3071. ```javascript
  3072. findAuthor().
  3073. then(findBooksByAuthor).
  3074. then(function(books){
  3075. // found books
  3076. }).catch(function(reason){
  3077. // something went wrong
  3078. });
  3079. ```
  3080. @method then
  3081. @param {Function} onFulfilled
  3082. @param {Function} onRejected
  3083. Useful for tooling.
  3084. @return {Promise}
  3085. */
  3086. then: lib$es6$promise$then$$default,
  3087. /**
  3088. `catch` is simply sugar for `then(undefined, onRejection)` which makes it the same
  3089. as the catch block of a try/catch statement.
  3090. ```js
  3091. function findAuthor(){
  3092. throw new Error('couldn't find that author');
  3093. }
  3094. // synchronous
  3095. try {
  3096. findAuthor();
  3097. } catch(reason) {
  3098. // something went wrong
  3099. }
  3100. // async with promises
  3101. findAuthor().catch(function(reason){
  3102. // something went wrong
  3103. });
  3104. ```
  3105. @method catch
  3106. @param {Function} onRejection
  3107. Useful for tooling.
  3108. @return {Promise}
  3109. */
  3110. 'catch': function(onRejection) {
  3111. return this.then(null, onRejection);
  3112. }
  3113. };
  3114. var lib$es6$promise$enumerator$$default = lib$es6$promise$enumerator$$Enumerator;
  3115. function lib$es6$promise$enumerator$$Enumerator(Constructor, input) {
  3116. this._instanceConstructor = Constructor;
  3117. this.promise = new Constructor(lib$es6$promise$$internal$$noop);
  3118. if (!this.promise[lib$es6$promise$$internal$$PROMISE_ID]) {
  3119. lib$es6$promise$$internal$$makePromise(this.promise);
  3120. }
  3121. if (lib$es6$promise$utils$$isArray(input)) {
  3122. this._input = input;
  3123. this.length = input.length;
  3124. this._remaining = input.length;
  3125. this._result = new Array(this.length);
  3126. if (this.length === 0) {
  3127. lib$es6$promise$$internal$$fulfill(this.promise, this._result);
  3128. } else {
  3129. this.length = this.length || 0;
  3130. this._enumerate();
  3131. if (this._remaining === 0) {
  3132. lib$es6$promise$$internal$$fulfill(this.promise, this._result);
  3133. }
  3134. }
  3135. } else {
  3136. lib$es6$promise$$internal$$reject(this.promise, lib$es6$promise$enumerator$$validationError());
  3137. }
  3138. }
  3139. function lib$es6$promise$enumerator$$validationError() {
  3140. return new Error('Array Methods must be provided an Array');
  3141. }
  3142. lib$es6$promise$enumerator$$Enumerator.prototype._enumerate = function() {
  3143. var length = this.length;
  3144. var input = this._input;
  3145. for (var i = 0; this._state === lib$es6$promise$$internal$$PENDING && i < length; i++) {
  3146. this._eachEntry(input[i], i);
  3147. }
  3148. };
  3149. lib$es6$promise$enumerator$$Enumerator.prototype._eachEntry = function(entry, i) {
  3150. var c = this._instanceConstructor;
  3151. var resolve = c.resolve;
  3152. if (resolve === lib$es6$promise$promise$resolve$$default) {
  3153. var then = lib$es6$promise$$internal$$getThen(entry);
  3154. if (then === lib$es6$promise$then$$default &&
  3155. entry._state !== lib$es6$promise$$internal$$PENDING) {
  3156. this._settledAt(entry._state, i, entry._result);
  3157. } else if (typeof then !== 'function') {
  3158. this._remaining--;
  3159. this._result[i] = entry;
  3160. } else if (c === lib$es6$promise$promise$$default) {
  3161. var promise = new c(lib$es6$promise$$internal$$noop);
  3162. lib$es6$promise$$internal$$handleMaybeThenable(promise, entry, then);
  3163. this._willSettleAt(promise, i);
  3164. } else {
  3165. this._willSettleAt(new c(function(resolve) { resolve(entry); }), i);
  3166. }
  3167. } else {
  3168. this._willSettleAt(resolve(entry), i);
  3169. }
  3170. };
  3171. lib$es6$promise$enumerator$$Enumerator.prototype._settledAt = function(state, i, value) {
  3172. var promise = this.promise;
  3173. if (promise._state === lib$es6$promise$$internal$$PENDING) {
  3174. this._remaining--;
  3175. if (state === lib$es6$promise$$internal$$REJECTED) {
  3176. lib$es6$promise$$internal$$reject(promise, value);
  3177. } else {
  3178. this._result[i] = value;
  3179. }
  3180. }
  3181. if (this._remaining === 0) {
  3182. lib$es6$promise$$internal$$fulfill(promise, this._result);
  3183. }
  3184. };
  3185. lib$es6$promise$enumerator$$Enumerator.prototype._willSettleAt = function(promise, i) {
  3186. var enumerator = this;
  3187. lib$es6$promise$$internal$$subscribe(promise, undefined, function(value) {
  3188. enumerator._settledAt(lib$es6$promise$$internal$$FULFILLED, i, value);
  3189. }, function(reason) {
  3190. enumerator._settledAt(lib$es6$promise$$internal$$REJECTED, i, reason);
  3191. });
  3192. };
  3193. function lib$es6$promise$polyfill$$polyfill() {
  3194. var local;
  3195. if (typeof global !== 'undefined') {
  3196. local = global;
  3197. } else if (typeof self !== 'undefined') {
  3198. local = self;
  3199. } else {
  3200. try {
  3201. local = Function('return this')();
  3202. } catch (e) {
  3203. throw new Error('polyfill failed because global object is unavailable in this environment');
  3204. }
  3205. }
  3206. var P = local.Promise;
  3207. if (P && Object.prototype.toString.call(P.resolve()) === '[object Promise]' && !P.cast) {
  3208. return;
  3209. }
  3210. local.Promise = lib$es6$promise$promise$$default;
  3211. }
  3212. var lib$es6$promise$polyfill$$default = lib$es6$promise$polyfill$$polyfill;
  3213. var lib$es6$promise$umd$$ES6Promise = {
  3214. 'Promise': lib$es6$promise$promise$$default,
  3215. 'polyfill': lib$es6$promise$polyfill$$default
  3216. };
  3217. /* global define:true module:true window: true */
  3218. if (typeof define === 'function' && define['amd']) {
  3219. define(function() { return lib$es6$promise$umd$$ES6Promise; });
  3220. } else if (typeof module !== 'undefined' && module['exports']) {
  3221. module['exports'] = lib$es6$promise$umd$$ES6Promise;
  3222. } else if (typeof this !== 'undefined') {
  3223. this['ES6Promise'] = lib$es6$promise$umd$$ES6Promise;
  3224. }
  3225. lib$es6$promise$polyfill$$default();
  3226. }).call(this);
  3227. }).call(this,_dereq_('_process'),typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  3228. },{"_process":3}],3:[function(_dereq_,module,exports){
  3229. // shim for using process in browser
  3230. var process = module.exports = {};
  3231. // cached from whatever global is present so that test runners that stub it
  3232. // don't break things. But we need to wrap it in a try catch in case it is
  3233. // wrapped in strict mode code which doesn't define any globals. It's inside a
  3234. // function because try/catches deoptimize in certain engines.
  3235. var cachedSetTimeout;
  3236. var cachedClearTimeout;
  3237. (function () {
  3238. try {
  3239. cachedSetTimeout = setTimeout;
  3240. } catch (e) {
  3241. cachedSetTimeout = function () {
  3242. throw new Error('setTimeout is not defined');
  3243. }
  3244. }
  3245. try {
  3246. cachedClearTimeout = clearTimeout;
  3247. } catch (e) {
  3248. cachedClearTimeout = function () {
  3249. throw new Error('clearTimeout is not defined');
  3250. }
  3251. }
  3252. } ())
  3253. var queue = [];
  3254. var draining = false;
  3255. var currentQueue;
  3256. var queueIndex = -1;
  3257. function cleanUpNextTick() {
  3258. if (!draining || !currentQueue) {
  3259. return;
  3260. }
  3261. draining = false;
  3262. if (currentQueue.length) {
  3263. queue = currentQueue.concat(queue);
  3264. } else {
  3265. queueIndex = -1;
  3266. }
  3267. if (queue.length) {
  3268. drainQueue();
  3269. }
  3270. }
  3271. function drainQueue() {
  3272. if (draining) {
  3273. return;
  3274. }
  3275. var timeout = cachedSetTimeout(cleanUpNextTick);
  3276. draining = true;
  3277. var len = queue.length;
  3278. while(len) {
  3279. currentQueue = queue;
  3280. queue = [];
  3281. while (++queueIndex < len) {
  3282. if (currentQueue) {
  3283. currentQueue[queueIndex].run();
  3284. }
  3285. }
  3286. queueIndex = -1;
  3287. len = queue.length;
  3288. }
  3289. currentQueue = null;
  3290. draining = false;
  3291. cachedClearTimeout(timeout);
  3292. }
  3293. process.nextTick = function (fun) {
  3294. var args = new Array(arguments.length - 1);
  3295. if (arguments.length > 1) {
  3296. for (var i = 1; i < arguments.length; i++) {
  3297. args[i - 1] = arguments[i];
  3298. }
  3299. }
  3300. queue.push(new Item(fun, args));
  3301. if (queue.length === 1 && !draining) {
  3302. cachedSetTimeout(drainQueue, 0);
  3303. }
  3304. };
  3305. // v8 likes predictible objects
  3306. function Item(fun, array) {
  3307. this.fun = fun;
  3308. this.array = array;
  3309. }
  3310. Item.prototype.run = function () {
  3311. this.fun.apply(null, this.array);
  3312. };
  3313. process.title = 'browser';
  3314. process.browser = true;
  3315. process.env = {};
  3316. process.argv = [];
  3317. process.version = ''; // empty string to avoid regexp issues
  3318. process.versions = {};
  3319. function noop() {}
  3320. process.on = noop;
  3321. process.addListener = noop;
  3322. process.once = noop;
  3323. process.off = noop;
  3324. process.removeListener = noop;
  3325. process.removeAllListeners = noop;
  3326. process.emit = noop;
  3327. process.binding = function (name) {
  3328. throw new Error('process.binding is not supported');
  3329. };
  3330. process.cwd = function () { return '/' };
  3331. process.chdir = function (dir) {
  3332. throw new Error('process.chdir is not supported');
  3333. };
  3334. process.umask = function() { return 0; };
  3335. },{}],4:[function(_dereq_,module,exports){
  3336. (function (global){
  3337. /*
  3338. * Rusha, a JavaScript implementation of the Secure Hash Algorithm, SHA-1,
  3339. * as defined in FIPS PUB 180-1, tuned for high performance with large inputs.
  3340. * (http://github.com/srijs/rusha)
  3341. *
  3342. * Inspired by Paul Johnstons implementation (http://pajhome.org.uk/crypt/md5).
  3343. *
  3344. * Copyright (c) 2013 Sam Rijs (http://awesam.de).
  3345. * Released under the terms of the MIT license as follows:
  3346. *
  3347. * Permission is hereby granted, free of charge, to any person obtaining a
  3348. * copy of this software and associated documentation files (the "Software"),
  3349. * to deal in the Software without restriction, including without limitation
  3350. * the rights to use, copy, modify, merge, publish, distribute, sublicense,
  3351. * and/or sell copies of the Software, and to permit persons to whom the
  3352. * Software is furnished to do so, subject to the following conditions:
  3353. *
  3354. * The above copyright notice and this permission notice shall be included in
  3355. * all copies or substantial portions of the Software.
  3356. *
  3357. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  3358. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  3359. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  3360. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  3361. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  3362. * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  3363. * IN THE SOFTWARE.
  3364. */
  3365. (function () {
  3366. var util = {
  3367. getDataType: function (data) {
  3368. if (typeof data === 'string') {
  3369. return 'string';
  3370. }
  3371. if (data instanceof Array) {
  3372. return 'array';
  3373. }
  3374. if (typeof global !== 'undefined' && global.Buffer && global.Buffer.isBuffer(data)) {
  3375. return 'buffer';
  3376. }
  3377. if (data instanceof ArrayBuffer) {
  3378. return 'arraybuffer';
  3379. }
  3380. if (data.buffer instanceof ArrayBuffer) {
  3381. return 'view';
  3382. }
  3383. if (data instanceof Blob) {
  3384. return 'blob';
  3385. }
  3386. throw new Error('Unsupported data type.');
  3387. }
  3388. };
  3389. // The Rusha object is a wrapper around the low-level RushaCore.
  3390. // It provides means of converting different inputs to the
  3391. // format accepted by RushaCore as well as other utility methods.
  3392. function Rusha(chunkSize) {
  3393. 'use strict';
  3394. // Private object structure.
  3395. var self$2 = { fill: 0 };
  3396. // Calculate the length of buffer that the sha1 routine uses
  3397. // including the padding.
  3398. var padlen = function (len) {
  3399. for (len += 9; len % 64 > 0; len += 1);
  3400. return len;
  3401. };
  3402. var padZeroes = function (bin, len) {
  3403. for (var i = len >> 2; i < bin.length; i++)
  3404. bin[i] = 0;
  3405. };
  3406. var padData = function (bin, chunkLen, msgLen) {
  3407. bin[chunkLen >> 2] |= 128 << 24 - (chunkLen % 4 << 3);
  3408. bin[((chunkLen >> 2) + 2 & ~15) + 14] = msgLen >> 29;
  3409. bin[((chunkLen >> 2) + 2 & ~15) + 15] = msgLen << 3;
  3410. };
  3411. // Convert a binary string and write it to the heap.
  3412. // A binary string is expected to only contain char codes < 256.
  3413. var convStr = function (H8, H32, start, len, off) {
  3414. var str = this, i, om = off % 4, lm = len % 4, j = len - lm;
  3415. if (j > 0) {
  3416. switch (om) {
  3417. case 0:
  3418. H8[off + 3 | 0] = str.charCodeAt(start);
  3419. case 1:
  3420. H8[off + 2 | 0] = str.charCodeAt(start + 1);
  3421. case 2:
  3422. H8[off + 1 | 0] = str.charCodeAt(start + 2);
  3423. case 3:
  3424. H8[off | 0] = str.charCodeAt(start + 3);
  3425. }
  3426. }
  3427. for (i = om; i < j; i = i + 4 | 0) {
  3428. H32[off + i >> 2] = str.charCodeAt(start + i) << 24 | str.charCodeAt(start + i + 1) << 16 | str.charCodeAt(start + i + 2) << 8 | str.charCodeAt(start + i + 3);
  3429. }
  3430. switch (lm) {
  3431. case 3:
  3432. H8[off + j + 1 | 0] = str.charCodeAt(start + j + 2);
  3433. case 2:
  3434. H8[off + j + 2 | 0] = str.charCodeAt(start + j + 1);
  3435. case 1:
  3436. H8[off + j + 3 | 0] = str.charCodeAt(start + j);
  3437. }
  3438. };
  3439. // Convert a buffer or array and write it to the heap.
  3440. // The buffer or array is expected to only contain elements < 256.
  3441. var convBuf = function (H8, H32, start, len, off) {
  3442. var buf = this, i, om = off % 4, lm = len % 4, j = len - lm;
  3443. if (j > 0) {
  3444. switch (om) {
  3445. case 0:
  3446. H8[off + 3 | 0] = buf[start];
  3447. case 1:
  3448. H8[off + 2 | 0] = buf[start + 1];
  3449. case 2:
  3450. H8[off + 1 | 0] = buf[start + 2];
  3451. case 3:
  3452. H8[off | 0] = buf[start + 3];
  3453. }
  3454. }
  3455. for (i = 4 - om; i < j; i = i += 4 | 0) {
  3456. H32[off + i >> 2] = buf[start + i] << 24 | buf[start + i + 1] << 16 | buf[start + i + 2] << 8 | buf[start + i + 3];
  3457. }
  3458. switch (lm) {
  3459. case 3:
  3460. H8[off + j + 1 | 0] = buf[start + j + 2];
  3461. case 2:
  3462. H8[off + j + 2 | 0] = buf[start + j + 1];
  3463. case 1:
  3464. H8[off + j + 3 | 0] = buf[start + j];
  3465. }
  3466. };
  3467. var convBlob = function (H8, H32, start, len, off) {
  3468. var blob = this, i, om = off % 4, lm = len % 4, j = len - lm;
  3469. var buf = new Uint8Array(reader.readAsArrayBuffer(blob.slice(start, start + len)));
  3470. if (j > 0) {
  3471. switch (om) {
  3472. case 0:
  3473. H8[off + 3 | 0] = buf[0];
  3474. case 1:
  3475. H8[off + 2 | 0] = buf[1];
  3476. case 2:
  3477. H8[off + 1 | 0] = buf[2];
  3478. case 3:
  3479. H8[off | 0] = buf[3];
  3480. }
  3481. }
  3482. for (i = 4 - om; i < j; i = i += 4 | 0) {
  3483. H32[off + i >> 2] = buf[i] << 24 | buf[i + 1] << 16 | buf[i + 2] << 8 | buf[i + 3];
  3484. }
  3485. switch (lm) {
  3486. case 3:
  3487. H8[off + j + 1 | 0] = buf[j + 2];
  3488. case 2:
  3489. H8[off + j + 2 | 0] = buf[j + 1];
  3490. case 1:
  3491. H8[off + j + 3 | 0] = buf[j];
  3492. }
  3493. };
  3494. var convFn = function (data) {
  3495. switch (util.getDataType(data)) {
  3496. case 'string':
  3497. return convStr.bind(data);
  3498. case 'array':
  3499. return convBuf.bind(data);
  3500. case 'buffer':
  3501. return convBuf.bind(data);
  3502. case 'arraybuffer':
  3503. return convBuf.bind(new Uint8Array(data));
  3504. case 'view':
  3505. return convBuf.bind(new Uint8Array(data.buffer, data.byteOffset, data.byteLength));
  3506. case 'blob':
  3507. return convBlob.bind(data);
  3508. }
  3509. };
  3510. var slice = function (data, offset) {
  3511. switch (util.getDataType(data)) {
  3512. case 'string':
  3513. return data.slice(offset);
  3514. case 'array':
  3515. return data.slice(offset);
  3516. case 'buffer':
  3517. return data.slice(offset);
  3518. case 'arraybuffer':
  3519. return data.slice(offset);
  3520. case 'view':
  3521. return data.buffer.slice(offset);
  3522. }
  3523. };
  3524. // Convert an ArrayBuffer into its hexadecimal string representation.
  3525. var hex = function (arrayBuffer) {
  3526. var i, x, hex_tab = '0123456789abcdef', res = [], binarray = new Uint8Array(arrayBuffer);
  3527. for (i = 0; i < binarray.length; i++) {
  3528. x = binarray[i];
  3529. res[i] = hex_tab.charAt(x >> 4 & 15) + hex_tab.charAt(x >> 0 & 15);
  3530. }
  3531. return res.join('');
  3532. };
  3533. var ceilHeapSize = function (v) {
  3534. // The asm.js spec says:
  3535. // The heap object's byteLength must be either
  3536. // 2^n for n in [12, 24) or 2^24 * n for n ≥ 1.
  3537. // Also, byteLengths smaller than 2^16 are deprecated.
  3538. var p;
  3539. // If v is smaller than 2^16, the smallest possible solution
  3540. // is 2^16.
  3541. if (v <= 65536)
  3542. return 65536;
  3543. // If v < 2^24, we round up to 2^n,
  3544. // otherwise we round up to 2^24 * n.
  3545. if (v < 16777216) {
  3546. for (p = 1; p < v; p = p << 1);
  3547. } else {
  3548. for (p = 16777216; p < v; p += 16777216);
  3549. }
  3550. return p;
  3551. };
  3552. // Initialize the internal data structures to a new capacity.
  3553. var init = function (size) {
  3554. if (size % 64 > 0) {
  3555. throw new Error('Chunk size must be a multiple of 128 bit');
  3556. }
  3557. self$2.maxChunkLen = size;
  3558. self$2.padMaxChunkLen = padlen(size);
  3559. // The size of the heap is the sum of:
  3560. // 1. The padded input message size
  3561. // 2. The extended space the algorithm needs (320 byte)
  3562. // 3. The 160 bit state the algoritm uses
  3563. self$2.heap = new ArrayBuffer(ceilHeapSize(self$2.padMaxChunkLen + 320 + 20));
  3564. self$2.h32 = new Int32Array(self$2.heap);
  3565. self$2.h8 = new Int8Array(self$2.heap);
  3566. self$2.core = new Rusha._core({
  3567. Int32Array: Int32Array,
  3568. DataView: DataView
  3569. }, {}, self$2.heap);
  3570. self$2.buffer = null;
  3571. };
  3572. // Iinitializethe datastructures according
  3573. // to a chunk siyze.
  3574. init(chunkSize || 64 * 1024);
  3575. var initState = function (heap, padMsgLen) {
  3576. var io = new Int32Array(heap, padMsgLen + 320, 5);
  3577. io[0] = 1732584193;
  3578. io[1] = -271733879;
  3579. io[2] = -1732584194;
  3580. io[3] = 271733878;
  3581. io[4] = -1009589776;
  3582. };
  3583. var padChunk = function (chunkLen, msgLen) {
  3584. var padChunkLen = padlen(chunkLen);
  3585. var view = new Int32Array(self$2.heap, 0, padChunkLen >> 2);
  3586. padZeroes(view, chunkLen);
  3587. padData(view, chunkLen, msgLen);
  3588. return padChunkLen;
  3589. };
  3590. // Write data to the heap.
  3591. var write = function (data, chunkOffset, chunkLen) {
  3592. convFn(data)(self$2.h8, self$2.h32, chunkOffset, chunkLen, 0);
  3593. };
  3594. // Initialize and call the RushaCore,
  3595. // assuming an input buffer of length len * 4.
  3596. var coreCall = function (data, chunkOffset, chunkLen, msgLen, finalize) {
  3597. var padChunkLen = chunkLen;
  3598. if (finalize) {
  3599. padChunkLen = padChunk(chunkLen, msgLen);
  3600. }
  3601. write(data, chunkOffset, chunkLen);
  3602. self$2.core.hash(padChunkLen, self$2.padMaxChunkLen);
  3603. };
  3604. var getRawDigest = function (heap, padMaxChunkLen) {
  3605. var io = new Int32Array(heap, padMaxChunkLen + 320, 5);
  3606. var out = new Int32Array(5);
  3607. var arr = new DataView(out.buffer);
  3608. arr.setInt32(0, io[0], false);
  3609. arr.setInt32(4, io[1], false);
  3610. arr.setInt32(8, io[2], false);
  3611. arr.setInt32(12, io[3], false);
  3612. arr.setInt32(16, io[4], false);
  3613. return out;
  3614. };
  3615. // Calculate the hash digest as an array of 5 32bit integers.
  3616. var rawDigest = this.rawDigest = function (str) {
  3617. var msgLen = str.byteLength || str.length || str.size || 0;
  3618. initState(self$2.heap, self$2.padMaxChunkLen);
  3619. var chunkOffset = 0, chunkLen = self$2.maxChunkLen, last;
  3620. for (chunkOffset = 0; msgLen > chunkOffset + chunkLen; chunkOffset += chunkLen) {
  3621. coreCall(str, chunkOffset, chunkLen, msgLen, false);
  3622. }
  3623. coreCall(str, chunkOffset, msgLen - chunkOffset, msgLen, true);
  3624. return getRawDigest(self$2.heap, self$2.padMaxChunkLen);
  3625. };
  3626. // The digest and digestFrom* interface returns the hash digest
  3627. // as a hex string.
  3628. this.digest = this.digestFromString = this.digestFromBuffer = this.digestFromArrayBuffer = function (str) {
  3629. return hex(rawDigest(str).buffer);
  3630. };
  3631. }
  3632. ;
  3633. // The low-level RushCore module provides the heart of Rusha,
  3634. // a high-speed sha1 implementation working on an Int32Array heap.
  3635. // At first glance, the implementation seems complicated, however
  3636. // with the SHA1 spec at hand, it is obvious this almost a textbook
  3637. // implementation that has a few functions hand-inlined and a few loops
  3638. // hand-unrolled.
  3639. Rusha._core = function RushaCore(stdlib, foreign, heap) {
  3640. 'use asm';
  3641. var H = new stdlib.Int32Array(heap);
  3642. function hash(k, x) {
  3643. // k in bytes
  3644. k = k | 0;
  3645. x = x | 0;
  3646. var i = 0, j = 0, y0 = 0, z0 = 0, y1 = 0, z1 = 0, y2 = 0, z2 = 0, y3 = 0, z3 = 0, y4 = 0, z4 = 0, t0 = 0, t1 = 0;
  3647. y0 = H[x + 320 >> 2] | 0;
  3648. y1 = H[x + 324 >> 2] | 0;
  3649. y2 = H[x + 328 >> 2] | 0;
  3650. y3 = H[x + 332 >> 2] | 0;
  3651. y4 = H[x + 336 >> 2] | 0;
  3652. for (i = 0; (i | 0) < (k | 0); i = i + 64 | 0) {
  3653. z0 = y0;
  3654. z1 = y1;
  3655. z2 = y2;
  3656. z3 = y3;
  3657. z4 = y4;
  3658. for (j = 0; (j | 0) < 64; j = j + 4 | 0) {
  3659. t1 = H[i + j >> 2] | 0;
  3660. t0 = ((y0 << 5 | y0 >>> 27) + (y1 & y2 | ~y1 & y3) | 0) + ((t1 + y4 | 0) + 1518500249 | 0) | 0;
  3661. y4 = y3;
  3662. y3 = y2;
  3663. y2 = y1 << 30 | y1 >>> 2;
  3664. y1 = y0;
  3665. y0 = t0;
  3666. H[k + j >> 2] = t1;
  3667. }
  3668. for (j = k + 64 | 0; (j | 0) < (k + 80 | 0); j = j + 4 | 0) {
  3669. t1 = (H[j - 12 >> 2] ^ H[j - 32 >> 2] ^ H[j - 56 >> 2] ^ H[j - 64 >> 2]) << 1 | (H[j - 12 >> 2] ^ H[j - 32 >> 2] ^ H[j - 56 >> 2] ^ H[j - 64 >> 2]) >>> 31;
  3670. t0 = ((y0 << 5 | y0 >>> 27) + (y1 & y2 | ~y1 & y3) | 0) + ((t1 + y4 | 0) + 1518500249 | 0) | 0;
  3671. y4 = y3;
  3672. y3 = y2;
  3673. y2 = y1 << 30 | y1 >>> 2;
  3674. y1 = y0;
  3675. y0 = t0;
  3676. H[j >> 2] = t1;
  3677. }
  3678. for (j = k + 80 | 0; (j | 0) < (k + 160 | 0); j = j + 4 | 0) {
  3679. t1 = (H[j - 12 >> 2] ^ H[j - 32 >> 2] ^ H[j - 56 >> 2] ^ H[j - 64 >> 2]) << 1 | (H[j - 12 >> 2] ^ H[j - 32 >> 2] ^ H[j - 56 >> 2] ^ H[j - 64 >> 2]) >>> 31;
  3680. t0 = ((y0 << 5 | y0 >>> 27) + (y1 ^ y2 ^ y3) | 0) + ((t1 + y4 | 0) + 1859775393 | 0) | 0;
  3681. y4 = y3;
  3682. y3 = y2;
  3683. y2 = y1 << 30 | y1 >>> 2;
  3684. y1 = y0;
  3685. y0 = t0;
  3686. H[j >> 2] = t1;
  3687. }
  3688. for (j = k + 160 | 0; (j | 0) < (k + 240 | 0); j = j + 4 | 0) {
  3689. t1 = (H[j - 12 >> 2] ^ H[j - 32 >> 2] ^ H[j - 56 >> 2] ^ H[j - 64 >> 2]) << 1 | (H[j - 12 >> 2] ^ H[j - 32 >> 2] ^ H[j - 56 >> 2] ^ H[j - 64 >> 2]) >>> 31;
  3690. t0 = ((y0 << 5 | y0 >>> 27) + (y1 & y2 | y1 & y3 | y2 & y3) | 0) + ((t1 + y4 | 0) - 1894007588 | 0) | 0;
  3691. y4 = y3;
  3692. y3 = y2;
  3693. y2 = y1 << 30 | y1 >>> 2;
  3694. y1 = y0;
  3695. y0 = t0;
  3696. H[j >> 2] = t1;
  3697. }
  3698. for (j = k + 240 | 0; (j | 0) < (k + 320 | 0); j = j + 4 | 0) {
  3699. t1 = (H[j - 12 >> 2] ^ H[j - 32 >> 2] ^ H[j - 56 >> 2] ^ H[j - 64 >> 2]) << 1 | (H[j - 12 >> 2] ^ H[j - 32 >> 2] ^ H[j - 56 >> 2] ^ H[j - 64 >> 2]) >>> 31;
  3700. t0 = ((y0 << 5 | y0 >>> 27) + (y1 ^ y2 ^ y3) | 0) + ((t1 + y4 | 0) - 899497514 | 0) | 0;
  3701. y4 = y3;
  3702. y3 = y2;
  3703. y2 = y1 << 30 | y1 >>> 2;
  3704. y1 = y0;
  3705. y0 = t0;
  3706. H[j >> 2] = t1;
  3707. }
  3708. y0 = y0 + z0 | 0;
  3709. y1 = y1 + z1 | 0;
  3710. y2 = y2 + z2 | 0;
  3711. y3 = y3 + z3 | 0;
  3712. y4 = y4 + z4 | 0;
  3713. }
  3714. H[x + 320 >> 2] = y0;
  3715. H[x + 324 >> 2] = y1;
  3716. H[x + 328 >> 2] = y2;
  3717. H[x + 332 >> 2] = y3;
  3718. H[x + 336 >> 2] = y4;
  3719. }
  3720. return { hash: hash };
  3721. };
  3722. // If we'e running in Node.JS, export a module.
  3723. if (typeof module !== 'undefined') {
  3724. module.exports = Rusha;
  3725. } else if (typeof window !== 'undefined') {
  3726. window.Rusha = Rusha;
  3727. }
  3728. // If we're running in a webworker, accept
  3729. // messages containing a jobid and a buffer
  3730. // or blob object, and return the hash result.
  3731. if (typeof FileReaderSync !== 'undefined') {
  3732. var reader = new FileReaderSync(), hasher = new Rusha(4 * 1024 * 1024);
  3733. self.onmessage = function onMessage(event) {
  3734. var hash, data = event.data.data;
  3735. try {
  3736. hash = hasher.digest(data);
  3737. self.postMessage({
  3738. id: event.data.id,
  3739. hash: hash
  3740. });
  3741. } catch (e) {
  3742. self.postMessage({
  3743. id: event.data.id,
  3744. error: e.name
  3745. });
  3746. }
  3747. };
  3748. }
  3749. }());
  3750. }).call(this,typeof global !== "undefined" ? global : typeof self !== "undefined" ? self : typeof window !== "undefined" ? window : {})
  3751. },{}],5:[function(_dereq_,module,exports){
  3752. // GPG4Browsers - An OpenPGP implementation in javascript
  3753. // Copyright (C) 2011 Recurity Labs GmbH
  3754. //
  3755. // This library is free software; you can redistribute it and/or
  3756. // modify it under the terms of the GNU Lesser General Public
  3757. // License as published by the Free Software Foundation; either
  3758. // version 3.0 of the License, or (at your option) any later version.
  3759. //
  3760. // This library is distributed in the hope that it will be useful,
  3761. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  3762. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  3763. // Lesser General Public License for more details.
  3764. //
  3765. // You should have received a copy of the GNU Lesser General Public
  3766. // License along with this library; if not, write to the Free Software
  3767. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  3768. /**
  3769. * @requires config
  3770. * @requires encoding/armor
  3771. * @requires enums
  3772. * @requires packet
  3773. * @module cleartext
  3774. */
  3775. 'use strict';
  3776. Object.defineProperty(exports, "__esModule", {
  3777. value: true
  3778. });
  3779. exports.CleartextMessage = CleartextMessage;
  3780. exports.readArmored = readArmored;
  3781. var _config = _dereq_('./config');
  3782. var _config2 = _interopRequireDefault(_config);
  3783. var _packet = _dereq_('./packet');
  3784. var _packet2 = _interopRequireDefault(_packet);
  3785. var _enums = _dereq_('./enums.js');
  3786. var _enums2 = _interopRequireDefault(_enums);
  3787. var _armor = _dereq_('./encoding/armor.js');
  3788. var _armor2 = _interopRequireDefault(_armor);
  3789. var _signature = _dereq_('./signature.js');
  3790. var sigModule = _interopRequireWildcard(_signature);
  3791. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  3792. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  3793. /**
  3794. * @class
  3795. * @classdesc Class that represents an OpenPGP cleartext signed message.
  3796. * See {@link http://tools.ietf.org/html/rfc4880#section-7}
  3797. * @param {String} text The cleartext of the signed message
  3798. * @param {module:signature} signature The detached signature or an empty signature if message not yet signed
  3799. */
  3800. function CleartextMessage(text, signature) {
  3801. if (!(this instanceof CleartextMessage)) {
  3802. return new CleartextMessage(text, signature);
  3803. }
  3804. // normalize EOL to canonical form <CR><LF>
  3805. this.text = text.replace(/\r/g, '').replace(/[\t ]+\n/g, "\n").replace(/\n/g, "\r\n");
  3806. if (signature && !(signature instanceof sigModule.Signature)) {
  3807. throw new Error('Invalid signature input');
  3808. }
  3809. this.signature = signature || new sigModule.Signature(new _packet2.default.List());
  3810. }
  3811. /**
  3812. * Returns the key IDs of the keys that signed the cleartext message
  3813. * @return {Array<module:type/keyid>} array of keyid objects
  3814. */
  3815. CleartextMessage.prototype.getSigningKeyIds = function () {
  3816. var keyIds = [];
  3817. var signatureList = this.signature.packets;
  3818. signatureList.forEach(function (packet) {
  3819. keyIds.push(packet.issuerKeyId);
  3820. });
  3821. return keyIds;
  3822. };
  3823. /**
  3824. * Sign the cleartext message
  3825. * @param {Array<module:key~Key>} privateKeys private keys with decrypted secret key data for signing
  3826. */
  3827. CleartextMessage.prototype.sign = function (privateKeys) {
  3828. this.signature = this.signDetached(privateKeys);
  3829. };
  3830. /**
  3831. * Sign the cleartext message
  3832. * @param {Array<module:key~Key>} privateKeys private keys with decrypted secret key data for signing
  3833. * @return {module:signature~Signature} new detached signature of message content
  3834. */
  3835. CleartextMessage.prototype.signDetached = function (privateKeys) {
  3836. var packetlist = new _packet2.default.List();
  3837. var literalDataPacket = new _packet2.default.Literal();
  3838. literalDataPacket.setText(this.text);
  3839. for (var i = 0; i < privateKeys.length; i++) {
  3840. if (privateKeys[i].isPublic()) {
  3841. throw new Error('Need private key for signing');
  3842. }
  3843. var signaturePacket = new _packet2.default.Signature();
  3844. signaturePacket.signatureType = _enums2.default.signature.text;
  3845. signaturePacket.hashAlgorithm = _config2.default.prefer_hash_algorithm;
  3846. var signingKeyPacket = privateKeys[i].getSigningKeyPacket();
  3847. signaturePacket.publicKeyAlgorithm = signingKeyPacket.algorithm;
  3848. if (!signingKeyPacket.isDecrypted) {
  3849. throw new Error('Private key is not decrypted.');
  3850. }
  3851. signaturePacket.sign(signingKeyPacket, literalDataPacket);
  3852. packetlist.push(signaturePacket);
  3853. }
  3854. return new sigModule.Signature(packetlist);
  3855. };
  3856. /**
  3857. * Verify signatures of cleartext signed message
  3858. * @param {Array<module:key~Key>} keys array of keys to verify signatures
  3859. * @return {Array<{keyid: module:type/keyid, valid: Boolean}>} list of signer's keyid and validity of signature
  3860. */
  3861. CleartextMessage.prototype.verify = function (keys) {
  3862. return this.verifyDetached(this.signature, keys);
  3863. };
  3864. /**
  3865. * Verify signatures of cleartext signed message
  3866. * @param {Array<module:key~Key>} keys array of keys to verify signatures
  3867. * @return {Array<{keyid: module:type/keyid, valid: Boolean}>} list of signer's keyid and validity of signature
  3868. */
  3869. CleartextMessage.prototype.verifyDetached = function (signature, keys) {
  3870. var result = [];
  3871. var signatureList = signature.packets;
  3872. var literalDataPacket = new _packet2.default.Literal();
  3873. // we assume that cleartext signature is generated based on UTF8 cleartext
  3874. literalDataPacket.setText(this.text);
  3875. for (var i = 0; i < signatureList.length; i++) {
  3876. var keyPacket = null;
  3877. for (var j = 0; j < keys.length; j++) {
  3878. keyPacket = keys[j].getSigningKeyPacket(signatureList[i].issuerKeyId);
  3879. if (keyPacket) {
  3880. break;
  3881. }
  3882. }
  3883. var verifiedSig = {};
  3884. if (keyPacket) {
  3885. verifiedSig.keyid = signatureList[i].issuerKeyId;
  3886. verifiedSig.valid = signatureList[i].verify(keyPacket, literalDataPacket);
  3887. } else {
  3888. verifiedSig.keyid = signatureList[i].issuerKeyId;
  3889. verifiedSig.valid = null;
  3890. }
  3891. verifiedSig.signature = new sigModule.Signature([signatureList[i]]);
  3892. result.push(verifiedSig);
  3893. }
  3894. return result;
  3895. };
  3896. /**
  3897. * Get cleartext
  3898. * @return {String} cleartext of message
  3899. */
  3900. CleartextMessage.prototype.getText = function () {
  3901. // normalize end of line to \n
  3902. return this.text.replace(/\r\n/g, "\n");
  3903. };
  3904. /**
  3905. * Returns ASCII armored text of cleartext signed message
  3906. * @return {String} ASCII armor
  3907. */
  3908. CleartextMessage.prototype.armor = function () {
  3909. var body = {
  3910. hash: _enums2.default.read(_enums2.default.hash, _config2.default.prefer_hash_algorithm).toUpperCase(),
  3911. text: this.text,
  3912. data: this.signature.packets.write()
  3913. };
  3914. return _armor2.default.encode(_enums2.default.armor.signed, body);
  3915. };
  3916. /**
  3917. * reads an OpenPGP cleartext signed message and returns a CleartextMessage object
  3918. * @param {String} armoredText text to be parsed
  3919. * @return {module:cleartext~CleartextMessage} new cleartext message object
  3920. * @static
  3921. */
  3922. function readArmored(armoredText) {
  3923. var input = _armor2.default.decode(armoredText);
  3924. if (input.type !== _enums2.default.armor.signed) {
  3925. throw new Error('No cleartext signed message.');
  3926. }
  3927. var packetlist = new _packet2.default.List();
  3928. packetlist.read(input.data);
  3929. verifyHeaders(input.headers, packetlist);
  3930. var signature = new sigModule.Signature(packetlist);
  3931. var newMessage = new CleartextMessage(input.text, signature);
  3932. return newMessage;
  3933. }
  3934. /**
  3935. * Compare hash algorithm specified in the armor header with signatures
  3936. * @private
  3937. * @param {Array<String>} headers Armor headers
  3938. * @param {module:packet/packetlist} packetlist The packetlist with signature packets
  3939. */
  3940. function verifyHeaders(headers, packetlist) {
  3941. var checkHashAlgos = function checkHashAlgos(hashAlgos) {
  3942. function check(algo) {
  3943. return packetlist[i].hashAlgorithm === algo;
  3944. }
  3945. for (var i = 0; i < packetlist.length; i++) {
  3946. if (packetlist[i].tag === _enums2.default.packet.signature && !hashAlgos.some(check)) {
  3947. return false;
  3948. }
  3949. }
  3950. return true;
  3951. };
  3952. var oneHeader = null;
  3953. var hashAlgos = [];
  3954. headers.forEach(function (header) {
  3955. oneHeader = header.match(/Hash: (.+)/); // get header value
  3956. if (oneHeader) {
  3957. oneHeader = oneHeader[1].replace(/\s/g, ''); // remove whitespace
  3958. oneHeader = oneHeader.split(',');
  3959. oneHeader = oneHeader.map(function (hash) {
  3960. hash = hash.toLowerCase();
  3961. try {
  3962. return _enums2.default.write(_enums2.default.hash, hash);
  3963. } catch (e) {
  3964. throw new Error('Unknown hash algorithm in armor header: ' + hash);
  3965. }
  3966. });
  3967. hashAlgos = hashAlgos.concat(oneHeader);
  3968. } else {
  3969. throw new Error('Only "Hash" header allowed in cleartext signed message');
  3970. }
  3971. });
  3972. if (!hashAlgos.length && !checkHashAlgos([_enums2.default.hash.md5])) {
  3973. throw new Error('If no "Hash" header in cleartext signed message, then only MD5 signatures allowed');
  3974. } else if (!checkHashAlgos(hashAlgos)) {
  3975. throw new Error('Hash algorithm mismatch in armor header and signature');
  3976. }
  3977. }
  3978. },{"./config":10,"./encoding/armor.js":33,"./enums.js":35,"./packet":47,"./signature.js":66}],6:[function(_dereq_,module,exports){
  3979. /** @license zlib.js 2012 - imaya [ https://github.com/imaya/zlib.js ] The MIT License */(function() {'use strict';var n=void 0,u=!0,aa=this;function ba(e,d){var c=e.split("."),f=aa;!(c[0]in f)&&f.execScript&&f.execScript("var "+c[0]);for(var a;c.length&&(a=c.shift());)!c.length&&d!==n?f[a]=d:f=f[a]?f[a]:f[a]={}};var C="undefined"!==typeof Uint8Array&&"undefined"!==typeof Uint16Array&&"undefined"!==typeof Uint32Array&&"undefined"!==typeof DataView;function K(e,d){this.index="number"===typeof d?d:0;this.d=0;this.buffer=e instanceof(C?Uint8Array:Array)?e:new (C?Uint8Array:Array)(32768);if(2*this.buffer.length<=this.index)throw Error("invalid index");this.buffer.length<=this.index&&ca(this)}function ca(e){var d=e.buffer,c,f=d.length,a=new (C?Uint8Array:Array)(f<<1);if(C)a.set(d);else for(c=0;c<f;++c)a[c]=d[c];return e.buffer=a}
  3980. K.prototype.a=function(e,d,c){var f=this.buffer,a=this.index,b=this.d,k=f[a],m;c&&1<d&&(e=8<d?(L[e&255]<<24|L[e>>>8&255]<<16|L[e>>>16&255]<<8|L[e>>>24&255])>>32-d:L[e]>>8-d);if(8>d+b)k=k<<d|e,b+=d;else for(m=0;m<d;++m)k=k<<1|e>>d-m-1&1,8===++b&&(b=0,f[a++]=L[k],k=0,a===f.length&&(f=ca(this)));f[a]=k;this.buffer=f;this.d=b;this.index=a};K.prototype.finish=function(){var e=this.buffer,d=this.index,c;0<this.d&&(e[d]<<=8-this.d,e[d]=L[e[d]],d++);C?c=e.subarray(0,d):(e.length=d,c=e);return c};
  3981. var ga=new (C?Uint8Array:Array)(256),M;for(M=0;256>M;++M){for(var R=M,S=R,ha=7,R=R>>>1;R;R>>>=1)S<<=1,S|=R&1,--ha;ga[M]=(S<<ha&255)>>>0}var L=ga;function ja(e){this.buffer=new (C?Uint16Array:Array)(2*e);this.length=0}ja.prototype.getParent=function(e){return 2*((e-2)/4|0)};ja.prototype.push=function(e,d){var c,f,a=this.buffer,b;c=this.length;a[this.length++]=d;for(a[this.length++]=e;0<c;)if(f=this.getParent(c),a[c]>a[f])b=a[c],a[c]=a[f],a[f]=b,b=a[c+1],a[c+1]=a[f+1],a[f+1]=b,c=f;else break;return this.length};
  3982. ja.prototype.pop=function(){var e,d,c=this.buffer,f,a,b;d=c[0];e=c[1];this.length-=2;c[0]=c[this.length];c[1]=c[this.length+1];for(b=0;;){a=2*b+2;if(a>=this.length)break;a+2<this.length&&c[a+2]>c[a]&&(a+=2);if(c[a]>c[b])f=c[b],c[b]=c[a],c[a]=f,f=c[b+1],c[b+1]=c[a+1],c[a+1]=f;else break;b=a}return{index:e,value:d,length:this.length}};function ka(e,d){this.e=ma;this.f=0;this.input=C&&e instanceof Array?new Uint8Array(e):e;this.c=0;d&&(d.lazy&&(this.f=d.lazy),"number"===typeof d.compressionType&&(this.e=d.compressionType),d.outputBuffer&&(this.b=C&&d.outputBuffer instanceof Array?new Uint8Array(d.outputBuffer):d.outputBuffer),"number"===typeof d.outputIndex&&(this.c=d.outputIndex));this.b||(this.b=new (C?Uint8Array:Array)(32768))}var ma=2,T=[],U;
  3983. for(U=0;288>U;U++)switch(u){case 143>=U:T.push([U+48,8]);break;case 255>=U:T.push([U-144+400,9]);break;case 279>=U:T.push([U-256+0,7]);break;case 287>=U:T.push([U-280+192,8]);break;default:throw"invalid literal: "+U;}
  3984. ka.prototype.h=function(){var e,d,c,f,a=this.input;switch(this.e){case 0:c=0;for(f=a.length;c<f;){d=C?a.subarray(c,c+65535):a.slice(c,c+65535);c+=d.length;var b=d,k=c===f,m=n,g=n,p=n,v=n,x=n,l=this.b,h=this.c;if(C){for(l=new Uint8Array(this.b.buffer);l.length<=h+b.length+5;)l=new Uint8Array(l.length<<1);l.set(this.b)}m=k?1:0;l[h++]=m|0;g=b.length;p=~g+65536&65535;l[h++]=g&255;l[h++]=g>>>8&255;l[h++]=p&255;l[h++]=p>>>8&255;if(C)l.set(b,h),h+=b.length,l=l.subarray(0,h);else{v=0;for(x=b.length;v<x;++v)l[h++]=
  3985. b[v];l.length=h}this.c=h;this.b=l}break;case 1:var q=new K(C?new Uint8Array(this.b.buffer):this.b,this.c);q.a(1,1,u);q.a(1,2,u);var t=na(this,a),w,da,z;w=0;for(da=t.length;w<da;w++)if(z=t[w],K.prototype.a.apply(q,T[z]),256<z)q.a(t[++w],t[++w],u),q.a(t[++w],5),q.a(t[++w],t[++w],u);else if(256===z)break;this.b=q.finish();this.c=this.b.length;break;case ma:var B=new K(C?new Uint8Array(this.b.buffer):this.b,this.c),ra,J,N,O,P,Ia=[16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15],W,sa,X,ta,ea,ia=Array(19),
  3986. ua,Q,fa,y,va;ra=ma;B.a(1,1,u);B.a(ra,2,u);J=na(this,a);W=oa(this.j,15);sa=pa(W);X=oa(this.i,7);ta=pa(X);for(N=286;257<N&&0===W[N-1];N--);for(O=30;1<O&&0===X[O-1];O--);var wa=N,xa=O,F=new (C?Uint32Array:Array)(wa+xa),r,G,s,Y,E=new (C?Uint32Array:Array)(316),D,A,H=new (C?Uint8Array:Array)(19);for(r=G=0;r<wa;r++)F[G++]=W[r];for(r=0;r<xa;r++)F[G++]=X[r];if(!C){r=0;for(Y=H.length;r<Y;++r)H[r]=0}r=D=0;for(Y=F.length;r<Y;r+=G){for(G=1;r+G<Y&&F[r+G]===F[r];++G);s=G;if(0===F[r])if(3>s)for(;0<s--;)E[D++]=0,
  3987. H[0]++;else for(;0<s;)A=138>s?s:138,A>s-3&&A<s&&(A=s-3),10>=A?(E[D++]=17,E[D++]=A-3,H[17]++):(E[D++]=18,E[D++]=A-11,H[18]++),s-=A;else if(E[D++]=F[r],H[F[r]]++,s--,3>s)for(;0<s--;)E[D++]=F[r],H[F[r]]++;else for(;0<s;)A=6>s?s:6,A>s-3&&A<s&&(A=s-3),E[D++]=16,E[D++]=A-3,H[16]++,s-=A}e=C?E.subarray(0,D):E.slice(0,D);ea=oa(H,7);for(y=0;19>y;y++)ia[y]=ea[Ia[y]];for(P=19;4<P&&0===ia[P-1];P--);ua=pa(ea);B.a(N-257,5,u);B.a(O-1,5,u);B.a(P-4,4,u);for(y=0;y<P;y++)B.a(ia[y],3,u);y=0;for(va=e.length;y<va;y++)if(Q=
  3988. e[y],B.a(ua[Q],ea[Q],u),16<=Q){y++;switch(Q){case 16:fa=2;break;case 17:fa=3;break;case 18:fa=7;break;default:throw"invalid code: "+Q;}B.a(e[y],fa,u)}var ya=[sa,W],za=[ta,X],I,Aa,Z,la,Ba,Ca,Da,Ea;Ba=ya[0];Ca=ya[1];Da=za[0];Ea=za[1];I=0;for(Aa=J.length;I<Aa;++I)if(Z=J[I],B.a(Ba[Z],Ca[Z],u),256<Z)B.a(J[++I],J[++I],u),la=J[++I],B.a(Da[la],Ea[la],u),B.a(J[++I],J[++I],u);else if(256===Z)break;this.b=B.finish();this.c=this.b.length;break;default:throw"invalid compression type";}return this.b};
  3989. function qa(e,d){this.length=e;this.g=d}
  3990. var Fa=function(){function e(a){switch(u){case 3===a:return[257,a-3,0];case 4===a:return[258,a-4,0];case 5===a:return[259,a-5,0];case 6===a:return[260,a-6,0];case 7===a:return[261,a-7,0];case 8===a:return[262,a-8,0];case 9===a:return[263,a-9,0];case 10===a:return[264,a-10,0];case 12>=a:return[265,a-11,1];case 14>=a:return[266,a-13,1];case 16>=a:return[267,a-15,1];case 18>=a:return[268,a-17,1];case 22>=a:return[269,a-19,2];case 26>=a:return[270,a-23,2];case 30>=a:return[271,a-27,2];case 34>=a:return[272,
  3991. a-31,2];case 42>=a:return[273,a-35,3];case 50>=a:return[274,a-43,3];case 58>=a:return[275,a-51,3];case 66>=a:return[276,a-59,3];case 82>=a:return[277,a-67,4];case 98>=a:return[278,a-83,4];case 114>=a:return[279,a-99,4];case 130>=a:return[280,a-115,4];case 162>=a:return[281,a-131,5];case 194>=a:return[282,a-163,5];case 226>=a:return[283,a-195,5];case 257>=a:return[284,a-227,5];case 258===a:return[285,a-258,0];default:throw"invalid length: "+a;}}var d=[],c,f;for(c=3;258>=c;c++)f=e(c),d[c]=f[2]<<24|
  3992. f[1]<<16|f[0];return d}(),Ga=C?new Uint32Array(Fa):Fa;
  3993. function na(e,d){function c(a,c){var b=a.g,d=[],f=0,e;e=Ga[a.length];d[f++]=e&65535;d[f++]=e>>16&255;d[f++]=e>>24;var g;switch(u){case 1===b:g=[0,b-1,0];break;case 2===b:g=[1,b-2,0];break;case 3===b:g=[2,b-3,0];break;case 4===b:g=[3,b-4,0];break;case 6>=b:g=[4,b-5,1];break;case 8>=b:g=[5,b-7,1];break;case 12>=b:g=[6,b-9,2];break;case 16>=b:g=[7,b-13,2];break;case 24>=b:g=[8,b-17,3];break;case 32>=b:g=[9,b-25,3];break;case 48>=b:g=[10,b-33,4];break;case 64>=b:g=[11,b-49,4];break;case 96>=b:g=[12,b-
  3994. 65,5];break;case 128>=b:g=[13,b-97,5];break;case 192>=b:g=[14,b-129,6];break;case 256>=b:g=[15,b-193,6];break;case 384>=b:g=[16,b-257,7];break;case 512>=b:g=[17,b-385,7];break;case 768>=b:g=[18,b-513,8];break;case 1024>=b:g=[19,b-769,8];break;case 1536>=b:g=[20,b-1025,9];break;case 2048>=b:g=[21,b-1537,9];break;case 3072>=b:g=[22,b-2049,10];break;case 4096>=b:g=[23,b-3073,10];break;case 6144>=b:g=[24,b-4097,11];break;case 8192>=b:g=[25,b-6145,11];break;case 12288>=b:g=[26,b-8193,12];break;case 16384>=
  3995. b:g=[27,b-12289,12];break;case 24576>=b:g=[28,b-16385,13];break;case 32768>=b:g=[29,b-24577,13];break;default:throw"invalid distance";}e=g;d[f++]=e[0];d[f++]=e[1];d[f++]=e[2];var k,m;k=0;for(m=d.length;k<m;++k)l[h++]=d[k];t[d[0]]++;w[d[3]]++;q=a.length+c-1;x=null}var f,a,b,k,m,g={},p,v,x,l=C?new Uint16Array(2*d.length):[],h=0,q=0,t=new (C?Uint32Array:Array)(286),w=new (C?Uint32Array:Array)(30),da=e.f,z;if(!C){for(b=0;285>=b;)t[b++]=0;for(b=0;29>=b;)w[b++]=0}t[256]=1;f=0;for(a=d.length;f<a;++f){b=
  3996. m=0;for(k=3;b<k&&f+b!==a;++b)m=m<<8|d[f+b];g[m]===n&&(g[m]=[]);p=g[m];if(!(0<q--)){for(;0<p.length&&32768<f-p[0];)p.shift();if(f+3>=a){x&&c(x,-1);b=0;for(k=a-f;b<k;++b)z=d[f+b],l[h++]=z,++t[z];break}0<p.length?(v=Ha(d,f,p),x?x.length<v.length?(z=d[f-1],l[h++]=z,++t[z],c(v,0)):c(x,-1):v.length<da?x=v:c(v,0)):x?c(x,-1):(z=d[f],l[h++]=z,++t[z])}p.push(f)}l[h++]=256;t[256]++;e.j=t;e.i=w;return C?l.subarray(0,h):l}
  3997. function Ha(e,d,c){var f,a,b=0,k,m,g,p,v=e.length;m=0;p=c.length;a:for(;m<p;m++){f=c[p-m-1];k=3;if(3<b){for(g=b;3<g;g--)if(e[f+g-1]!==e[d+g-1])continue a;k=b}for(;258>k&&d+k<v&&e[f+k]===e[d+k];)++k;k>b&&(a=f,b=k);if(258===k)break}return new qa(b,d-a)}
  3998. function oa(e,d){var c=e.length,f=new ja(572),a=new (C?Uint8Array:Array)(c),b,k,m,g,p;if(!C)for(g=0;g<c;g++)a[g]=0;for(g=0;g<c;++g)0<e[g]&&f.push(g,e[g]);b=Array(f.length/2);k=new (C?Uint32Array:Array)(f.length/2);if(1===b.length)return a[f.pop().index]=1,a;g=0;for(p=f.length/2;g<p;++g)b[g]=f.pop(),k[g]=b[g].value;m=Ja(k,k.length,d);g=0;for(p=b.length;g<p;++g)a[b[g].index]=m[g];return a}
  3999. function Ja(e,d,c){function f(a){var b=g[a][p[a]];b===d?(f(a+1),f(a+1)):--k[b];++p[a]}var a=new (C?Uint16Array:Array)(c),b=new (C?Uint8Array:Array)(c),k=new (C?Uint8Array:Array)(d),m=Array(c),g=Array(c),p=Array(c),v=(1<<c)-d,x=1<<c-1,l,h,q,t,w;a[c-1]=d;for(h=0;h<c;++h)v<x?b[h]=0:(b[h]=1,v-=x),v<<=1,a[c-2-h]=(a[c-1-h]/2|0)+d;a[0]=b[0];m[0]=Array(a[0]);g[0]=Array(a[0]);for(h=1;h<c;++h)a[h]>2*a[h-1]+b[h]&&(a[h]=2*a[h-1]+b[h]),m[h]=Array(a[h]),g[h]=Array(a[h]);for(l=0;l<d;++l)k[l]=c;for(q=0;q<a[c-1];++q)m[c-
  4000. 1][q]=e[q],g[c-1][q]=q;for(l=0;l<c;++l)p[l]=0;1===b[c-1]&&(--k[0],++p[c-1]);for(h=c-2;0<=h;--h){t=l=0;w=p[h+1];for(q=0;q<a[h];q++)t=m[h+1][w]+m[h+1][w+1],t>e[l]?(m[h][q]=t,g[h][q]=d,w+=2):(m[h][q]=e[l],g[h][q]=l,++l);p[h]=0;1===b[h]&&f(h)}return k}
  4001. function pa(e){var d=new (C?Uint16Array:Array)(e.length),c=[],f=[],a=0,b,k,m,g;b=0;for(k=e.length;b<k;b++)c[e[b]]=(c[e[b]]|0)+1;b=1;for(k=16;b<=k;b++)f[b]=a,a+=c[b]|0,a<<=1;b=0;for(k=e.length;b<k;b++){a=f[e[b]];f[e[b]]+=1;m=d[b]=0;for(g=e[b];m<g;m++)d[b]=d[b]<<1|a&1,a>>>=1}return d};ba("Zlib.RawDeflate",ka);ba("Zlib.RawDeflate.prototype.compress",ka.prototype.h);var Ka={NONE:0,FIXED:1,DYNAMIC:ma},V,La,$,Ma;if(Object.keys)V=Object.keys(Ka);else for(La in V=[],$=0,Ka)V[$++]=La;$=0;for(Ma=V.length;$<Ma;++$)La=V[$],ba("Zlib.RawDeflate.CompressionType."+La,Ka[La]);}).call(this);
  4002. },{}],7:[function(_dereq_,module,exports){
  4003. /** @license zlib.js 2012 - imaya [ https://github.com/imaya/zlib.js ] The MIT License */(function() {'use strict';var l=this;function p(b,e){var a=b.split("."),c=l;!(a[0]in c)&&c.execScript&&c.execScript("var "+a[0]);for(var d;a.length&&(d=a.shift());)!a.length&&void 0!==e?c[d]=e:c=c[d]?c[d]:c[d]={}};var q="undefined"!==typeof Uint8Array&&"undefined"!==typeof Uint16Array&&"undefined"!==typeof Uint32Array&&"undefined"!==typeof DataView;function t(b){var e=b.length,a=0,c=Number.POSITIVE_INFINITY,d,f,g,h,k,m,r,n,s,J;for(n=0;n<e;++n)b[n]>a&&(a=b[n]),b[n]<c&&(c=b[n]);d=1<<a;f=new (q?Uint32Array:Array)(d);g=1;h=0;for(k=2;g<=a;){for(n=0;n<e;++n)if(b[n]===g){m=0;r=h;for(s=0;s<g;++s)m=m<<1|r&1,r>>=1;J=g<<16|n;for(s=m;s<d;s+=k)f[s]=J;++h}++g;h<<=1;k<<=1}return[f,a,c]};function u(b,e){this.g=[];this.h=32768;this.c=this.f=this.d=this.k=0;this.input=q?new Uint8Array(b):b;this.l=!1;this.i=v;this.q=!1;if(e||!(e={}))e.index&&(this.d=e.index),e.bufferSize&&(this.h=e.bufferSize),e.bufferType&&(this.i=e.bufferType),e.resize&&(this.q=e.resize);switch(this.i){case w:this.a=32768;this.b=new (q?Uint8Array:Array)(32768+this.h+258);break;case v:this.a=0;this.b=new (q?Uint8Array:Array)(this.h);this.e=this.v;this.m=this.s;this.j=this.t;break;default:throw Error("invalid inflate mode");
  4004. }}var w=0,v=1;
  4005. u.prototype.u=function(){for(;!this.l;){var b=x(this,3);b&1&&(this.l=!0);b>>>=1;switch(b){case 0:var e=this.input,a=this.d,c=this.b,d=this.a,f=e.length,g=void 0,h=void 0,k=c.length,m=void 0;this.c=this.f=0;if(a+1>=f)throw Error("invalid uncompressed block header: LEN");g=e[a++]|e[a++]<<8;if(a+1>=f)throw Error("invalid uncompressed block header: NLEN");h=e[a++]|e[a++]<<8;if(g===~h)throw Error("invalid uncompressed block header: length verify");if(a+g>e.length)throw Error("input buffer is broken");switch(this.i){case w:for(;d+
  4006. g>c.length;){m=k-d;g-=m;if(q)c.set(e.subarray(a,a+m),d),d+=m,a+=m;else for(;m--;)c[d++]=e[a++];this.a=d;c=this.e();d=this.a}break;case v:for(;d+g>c.length;)c=this.e({o:2});break;default:throw Error("invalid inflate mode");}if(q)c.set(e.subarray(a,a+g),d),d+=g,a+=g;else for(;g--;)c[d++]=e[a++];this.d=a;this.a=d;this.b=c;break;case 1:this.j(y,z);break;case 2:A(this);break;default:throw Error("unknown BTYPE: "+b);}}return this.m()};
  4007. var B=[16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15],C=q?new Uint16Array(B):B,D=[3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258,258,258],E=q?new Uint16Array(D):D,F=[0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0],G=q?new Uint8Array(F):F,H=[1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577],I=q?new Uint16Array(H):H,K=[0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,10,11,11,12,12,13,
  4008. 13],L=q?new Uint8Array(K):K,M=new (q?Uint8Array:Array)(288),N,O;N=0;for(O=M.length;N<O;++N)M[N]=143>=N?8:255>=N?9:279>=N?7:8;var y=t(M),P=new (q?Uint8Array:Array)(30),Q,R;Q=0;for(R=P.length;Q<R;++Q)P[Q]=5;var z=t(P);function x(b,e){for(var a=b.f,c=b.c,d=b.input,f=b.d,g=d.length,h;c<e;){if(f>=g)throw Error("input buffer is broken");a|=d[f++]<<c;c+=8}h=a&(1<<e)-1;b.f=a>>>e;b.c=c-e;b.d=f;return h}
  4009. function S(b,e){for(var a=b.f,c=b.c,d=b.input,f=b.d,g=d.length,h=e[0],k=e[1],m,r;c<k&&!(f>=g);)a|=d[f++]<<c,c+=8;m=h[a&(1<<k)-1];r=m>>>16;b.f=a>>r;b.c=c-r;b.d=f;return m&65535}
  4010. function A(b){function e(a,b,c){var e,d=this.p,f,g;for(g=0;g<a;)switch(e=S(this,b),e){case 16:for(f=3+x(this,2);f--;)c[g++]=d;break;case 17:for(f=3+x(this,3);f--;)c[g++]=0;d=0;break;case 18:for(f=11+x(this,7);f--;)c[g++]=0;d=0;break;default:d=c[g++]=e}this.p=d;return c}var a=x(b,5)+257,c=x(b,5)+1,d=x(b,4)+4,f=new (q?Uint8Array:Array)(C.length),g,h,k,m;for(m=0;m<d;++m)f[C[m]]=x(b,3);if(!q){m=d;for(d=f.length;m<d;++m)f[C[m]]=0}g=t(f);h=new (q?Uint8Array:Array)(a);k=new (q?Uint8Array:Array)(c);b.p=0;
  4011. b.j(t(e.call(b,a,g,h)),t(e.call(b,c,g,k)))}u.prototype.j=function(b,e){var a=this.b,c=this.a;this.n=b;for(var d=a.length-258,f,g,h,k;256!==(f=S(this,b));)if(256>f)c>=d&&(this.a=c,a=this.e(),c=this.a),a[c++]=f;else{g=f-257;k=E[g];0<G[g]&&(k+=x(this,G[g]));f=S(this,e);h=I[f];0<L[f]&&(h+=x(this,L[f]));c>=d&&(this.a=c,a=this.e(),c=this.a);for(;k--;)a[c]=a[c++-h]}for(;8<=this.c;)this.c-=8,this.d--;this.a=c};
  4012. u.prototype.t=function(b,e){var a=this.b,c=this.a;this.n=b;for(var d=a.length,f,g,h,k;256!==(f=S(this,b));)if(256>f)c>=d&&(a=this.e(),d=a.length),a[c++]=f;else{g=f-257;k=E[g];0<G[g]&&(k+=x(this,G[g]));f=S(this,e);h=I[f];0<L[f]&&(h+=x(this,L[f]));c+k>d&&(a=this.e(),d=a.length);for(;k--;)a[c]=a[c++-h]}for(;8<=this.c;)this.c-=8,this.d--;this.a=c};
  4013. u.prototype.e=function(){var b=new (q?Uint8Array:Array)(this.a-32768),e=this.a-32768,a,c,d=this.b;if(q)b.set(d.subarray(32768,b.length));else{a=0;for(c=b.length;a<c;++a)b[a]=d[a+32768]}this.g.push(b);this.k+=b.length;if(q)d.set(d.subarray(e,e+32768));else for(a=0;32768>a;++a)d[a]=d[e+a];this.a=32768;return d};
  4014. u.prototype.v=function(b){var e,a=this.input.length/this.d+1|0,c,d,f,g=this.input,h=this.b;b&&("number"===typeof b.o&&(a=b.o),"number"===typeof b.r&&(a+=b.r));2>a?(c=(g.length-this.d)/this.n[2],f=258*(c/2)|0,d=f<h.length?h.length+f:h.length<<1):d=h.length*a;q?(e=new Uint8Array(d),e.set(h)):e=h;return this.b=e};
  4015. u.prototype.m=function(){var b=0,e=this.b,a=this.g,c,d=new (q?Uint8Array:Array)(this.k+(this.a-32768)),f,g,h,k;if(0===a.length)return q?this.b.subarray(32768,this.a):this.b.slice(32768,this.a);f=0;for(g=a.length;f<g;++f){c=a[f];h=0;for(k=c.length;h<k;++h)d[b++]=c[h]}f=32768;for(g=this.a;f<g;++f)d[b++]=e[f];this.g=[];return this.buffer=d};
  4016. u.prototype.s=function(){var b,e=this.a;q?this.q?(b=new Uint8Array(e),b.set(this.b.subarray(0,e))):b=this.b.subarray(0,e):(this.b.length>e&&(this.b.length=e),b=this.b);return this.buffer=b};p("Zlib.RawInflate",u);p("Zlib.RawInflate.prototype.decompress",u.prototype.u);var T={ADAPTIVE:v,BLOCK:w},U,V,W,X;if(Object.keys)U=Object.keys(T);else for(V in U=[],W=0,T)U[W++]=V;W=0;for(X=U.length;W<X;++W)V=U[W],p("Zlib.RawInflate.BufferType."+V,T[V]);}).call(this);
  4017. },{}],8:[function(_dereq_,module,exports){
  4018. /** @license zlib.js 2012 - imaya [ https://github.com/imaya/zlib.js ] The MIT License */(function() {'use strict';function l(d){throw d;}var v=void 0,x=!0,aa=this;function D(d,a){var c=d.split("."),e=aa;!(c[0]in e)&&e.execScript&&e.execScript("var "+c[0]);for(var b;c.length&&(b=c.shift());)!c.length&&a!==v?e[b]=a:e=e[b]?e[b]:e[b]={}};var F="undefined"!==typeof Uint8Array&&"undefined"!==typeof Uint16Array&&"undefined"!==typeof Uint32Array&&"undefined"!==typeof DataView;function H(d,a){this.index="number"===typeof a?a:0;this.i=0;this.buffer=d instanceof(F?Uint8Array:Array)?d:new (F?Uint8Array:Array)(32768);2*this.buffer.length<=this.index&&l(Error("invalid index"));this.buffer.length<=this.index&&this.f()}H.prototype.f=function(){var d=this.buffer,a,c=d.length,e=new (F?Uint8Array:Array)(c<<1);if(F)e.set(d);else for(a=0;a<c;++a)e[a]=d[a];return this.buffer=e};
  4019. H.prototype.d=function(d,a,c){var e=this.buffer,b=this.index,f=this.i,g=e[b],h;c&&1<a&&(d=8<a?(N[d&255]<<24|N[d>>>8&255]<<16|N[d>>>16&255]<<8|N[d>>>24&255])>>32-a:N[d]>>8-a);if(8>a+f)g=g<<a|d,f+=a;else for(h=0;h<a;++h)g=g<<1|d>>a-h-1&1,8===++f&&(f=0,e[b++]=N[g],g=0,b===e.length&&(e=this.f()));e[b]=g;this.buffer=e;this.i=f;this.index=b};H.prototype.finish=function(){var d=this.buffer,a=this.index,c;0<this.i&&(d[a]<<=8-this.i,d[a]=N[d[a]],a++);F?c=d.subarray(0,a):(d.length=a,c=d);return c};
  4020. var fa=new (F?Uint8Array:Array)(256),O;for(O=0;256>O;++O){for(var P=O,Q=P,ga=7,P=P>>>1;P;P>>>=1)Q<<=1,Q|=P&1,--ga;fa[O]=(Q<<ga&255)>>>0}var N=fa;function ha(d){this.buffer=new (F?Uint16Array:Array)(2*d);this.length=0}ha.prototype.getParent=function(d){return 2*((d-2)/4|0)};ha.prototype.push=function(d,a){var c,e,b=this.buffer,f;c=this.length;b[this.length++]=a;for(b[this.length++]=d;0<c;)if(e=this.getParent(c),b[c]>b[e])f=b[c],b[c]=b[e],b[e]=f,f=b[c+1],b[c+1]=b[e+1],b[e+1]=f,c=e;else break;return this.length};
  4021. ha.prototype.pop=function(){var d,a,c=this.buffer,e,b,f;a=c[0];d=c[1];this.length-=2;c[0]=c[this.length];c[1]=c[this.length+1];for(f=0;;){b=2*f+2;if(b>=this.length)break;b+2<this.length&&c[b+2]>c[b]&&(b+=2);if(c[b]>c[f])e=c[f],c[f]=c[b],c[b]=e,e=c[f+1],c[f+1]=c[b+1],c[b+1]=e;else break;f=b}return{index:d,value:a,length:this.length}};function R(d){var a=d.length,c=0,e=Number.POSITIVE_INFINITY,b,f,g,h,k,n,q,r,p,m;for(r=0;r<a;++r)d[r]>c&&(c=d[r]),d[r]<e&&(e=d[r]);b=1<<c;f=new (F?Uint32Array:Array)(b);g=1;h=0;for(k=2;g<=c;){for(r=0;r<a;++r)if(d[r]===g){n=0;q=h;for(p=0;p<g;++p)n=n<<1|q&1,q>>=1;m=g<<16|r;for(p=n;p<b;p+=k)f[p]=m;++h}++g;h<<=1;k<<=1}return[f,c,e]};function ia(d,a){this.h=ma;this.w=0;this.input=F&&d instanceof Array?new Uint8Array(d):d;this.b=0;a&&(a.lazy&&(this.w=a.lazy),"number"===typeof a.compressionType&&(this.h=a.compressionType),a.outputBuffer&&(this.a=F&&a.outputBuffer instanceof Array?new Uint8Array(a.outputBuffer):a.outputBuffer),"number"===typeof a.outputIndex&&(this.b=a.outputIndex));this.a||(this.a=new (F?Uint8Array:Array)(32768))}var ma=2,na={NONE:0,r:1,k:ma,O:3},oa=[],S;
  4022. for(S=0;288>S;S++)switch(x){case 143>=S:oa.push([S+48,8]);break;case 255>=S:oa.push([S-144+400,9]);break;case 279>=S:oa.push([S-256+0,7]);break;case 287>=S:oa.push([S-280+192,8]);break;default:l("invalid literal: "+S)}
  4023. ia.prototype.j=function(){var d,a,c,e,b=this.input;switch(this.h){case 0:c=0;for(e=b.length;c<e;){a=F?b.subarray(c,c+65535):b.slice(c,c+65535);c+=a.length;var f=a,g=c===e,h=v,k=v,n=v,q=v,r=v,p=this.a,m=this.b;if(F){for(p=new Uint8Array(this.a.buffer);p.length<=m+f.length+5;)p=new Uint8Array(p.length<<1);p.set(this.a)}h=g?1:0;p[m++]=h|0;k=f.length;n=~k+65536&65535;p[m++]=k&255;p[m++]=k>>>8&255;p[m++]=n&255;p[m++]=n>>>8&255;if(F)p.set(f,m),m+=f.length,p=p.subarray(0,m);else{q=0;for(r=f.length;q<r;++q)p[m++]=
  4024. f[q];p.length=m}this.b=m;this.a=p}break;case 1:var s=new H(F?new Uint8Array(this.a.buffer):this.a,this.b);s.d(1,1,x);s.d(1,2,x);var w=pa(this,b),y,ja,A;y=0;for(ja=w.length;y<ja;y++)if(A=w[y],H.prototype.d.apply(s,oa[A]),256<A)s.d(w[++y],w[++y],x),s.d(w[++y],5),s.d(w[++y],w[++y],x);else if(256===A)break;this.a=s.finish();this.b=this.a.length;break;case ma:var C=new H(F?new Uint8Array(this.a.buffer):this.a,this.b),Ea,M,U,V,W,gb=[16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15],ba,Fa,ca,Ga,ka,ra=Array(19),
  4025. Ha,X,la,z,Ia;Ea=ma;C.d(1,1,x);C.d(Ea,2,x);M=pa(this,b);ba=qa(this.M,15);Fa=sa(ba);ca=qa(this.L,7);Ga=sa(ca);for(U=286;257<U&&0===ba[U-1];U--);for(V=30;1<V&&0===ca[V-1];V--);var Ja=U,Ka=V,I=new (F?Uint32Array:Array)(Ja+Ka),t,J,u,da,G=new (F?Uint32Array:Array)(316),E,B,K=new (F?Uint8Array:Array)(19);for(t=J=0;t<Ja;t++)I[J++]=ba[t];for(t=0;t<Ka;t++)I[J++]=ca[t];if(!F){t=0;for(da=K.length;t<da;++t)K[t]=0}t=E=0;for(da=I.length;t<da;t+=J){for(J=1;t+J<da&&I[t+J]===I[t];++J);u=J;if(0===I[t])if(3>u)for(;0<
  4026. u--;)G[E++]=0,K[0]++;else for(;0<u;)B=138>u?u:138,B>u-3&&B<u&&(B=u-3),10>=B?(G[E++]=17,G[E++]=B-3,K[17]++):(G[E++]=18,G[E++]=B-11,K[18]++),u-=B;else if(G[E++]=I[t],K[I[t]]++,u--,3>u)for(;0<u--;)G[E++]=I[t],K[I[t]]++;else for(;0<u;)B=6>u?u:6,B>u-3&&B<u&&(B=u-3),G[E++]=16,G[E++]=B-3,K[16]++,u-=B}d=F?G.subarray(0,E):G.slice(0,E);ka=qa(K,7);for(z=0;19>z;z++)ra[z]=ka[gb[z]];for(W=19;4<W&&0===ra[W-1];W--);Ha=sa(ka);C.d(U-257,5,x);C.d(V-1,5,x);C.d(W-4,4,x);for(z=0;z<W;z++)C.d(ra[z],3,x);z=0;for(Ia=d.length;z<
  4027. Ia;z++)if(X=d[z],C.d(Ha[X],ka[X],x),16<=X){z++;switch(X){case 16:la=2;break;case 17:la=3;break;case 18:la=7;break;default:l("invalid code: "+X)}C.d(d[z],la,x)}var La=[Fa,ba],Ma=[Ga,ca],L,Na,ea,ua,Oa,Pa,Qa,Ra;Oa=La[0];Pa=La[1];Qa=Ma[0];Ra=Ma[1];L=0;for(Na=M.length;L<Na;++L)if(ea=M[L],C.d(Oa[ea],Pa[ea],x),256<ea)C.d(M[++L],M[++L],x),ua=M[++L],C.d(Qa[ua],Ra[ua],x),C.d(M[++L],M[++L],x);else if(256===ea)break;this.a=C.finish();this.b=this.a.length;break;default:l("invalid compression type")}return this.a};
  4028. function ta(d,a){this.length=d;this.H=a}
  4029. var va=function(){function d(b){switch(x){case 3===b:return[257,b-3,0];case 4===b:return[258,b-4,0];case 5===b:return[259,b-5,0];case 6===b:return[260,b-6,0];case 7===b:return[261,b-7,0];case 8===b:return[262,b-8,0];case 9===b:return[263,b-9,0];case 10===b:return[264,b-10,0];case 12>=b:return[265,b-11,1];case 14>=b:return[266,b-13,1];case 16>=b:return[267,b-15,1];case 18>=b:return[268,b-17,1];case 22>=b:return[269,b-19,2];case 26>=b:return[270,b-23,2];case 30>=b:return[271,b-27,2];case 34>=b:return[272,
  4030. b-31,2];case 42>=b:return[273,b-35,3];case 50>=b:return[274,b-43,3];case 58>=b:return[275,b-51,3];case 66>=b:return[276,b-59,3];case 82>=b:return[277,b-67,4];case 98>=b:return[278,b-83,4];case 114>=b:return[279,b-99,4];case 130>=b:return[280,b-115,4];case 162>=b:return[281,b-131,5];case 194>=b:return[282,b-163,5];case 226>=b:return[283,b-195,5];case 257>=b:return[284,b-227,5];case 258===b:return[285,b-258,0];default:l("invalid length: "+b)}}var a=[],c,e;for(c=3;258>=c;c++)e=d(c),a[c]=e[2]<<24|e[1]<<
  4031. 16|e[0];return a}(),wa=F?new Uint32Array(va):va;
  4032. function pa(d,a){function c(b,c){var a=b.H,d=[],e=0,f;f=wa[b.length];d[e++]=f&65535;d[e++]=f>>16&255;d[e++]=f>>24;var g;switch(x){case 1===a:g=[0,a-1,0];break;case 2===a:g=[1,a-2,0];break;case 3===a:g=[2,a-3,0];break;case 4===a:g=[3,a-4,0];break;case 6>=a:g=[4,a-5,1];break;case 8>=a:g=[5,a-7,1];break;case 12>=a:g=[6,a-9,2];break;case 16>=a:g=[7,a-13,2];break;case 24>=a:g=[8,a-17,3];break;case 32>=a:g=[9,a-25,3];break;case 48>=a:g=[10,a-33,4];break;case 64>=a:g=[11,a-49,4];break;case 96>=a:g=[12,a-
  4033. 65,5];break;case 128>=a:g=[13,a-97,5];break;case 192>=a:g=[14,a-129,6];break;case 256>=a:g=[15,a-193,6];break;case 384>=a:g=[16,a-257,7];break;case 512>=a:g=[17,a-385,7];break;case 768>=a:g=[18,a-513,8];break;case 1024>=a:g=[19,a-769,8];break;case 1536>=a:g=[20,a-1025,9];break;case 2048>=a:g=[21,a-1537,9];break;case 3072>=a:g=[22,a-2049,10];break;case 4096>=a:g=[23,a-3073,10];break;case 6144>=a:g=[24,a-4097,11];break;case 8192>=a:g=[25,a-6145,11];break;case 12288>=a:g=[26,a-8193,12];break;case 16384>=
  4034. a:g=[27,a-12289,12];break;case 24576>=a:g=[28,a-16385,13];break;case 32768>=a:g=[29,a-24577,13];break;default:l("invalid distance")}f=g;d[e++]=f[0];d[e++]=f[1];d[e++]=f[2];var h,k;h=0;for(k=d.length;h<k;++h)p[m++]=d[h];w[d[0]]++;y[d[3]]++;s=b.length+c-1;r=null}var e,b,f,g,h,k={},n,q,r,p=F?new Uint16Array(2*a.length):[],m=0,s=0,w=new (F?Uint32Array:Array)(286),y=new (F?Uint32Array:Array)(30),ja=d.w,A;if(!F){for(f=0;285>=f;)w[f++]=0;for(f=0;29>=f;)y[f++]=0}w[256]=1;e=0;for(b=a.length;e<b;++e){f=h=0;
  4035. for(g=3;f<g&&e+f!==b;++f)h=h<<8|a[e+f];k[h]===v&&(k[h]=[]);n=k[h];if(!(0<s--)){for(;0<n.length&&32768<e-n[0];)n.shift();if(e+3>=b){r&&c(r,-1);f=0;for(g=b-e;f<g;++f)A=a[e+f],p[m++]=A,++w[A];break}0<n.length?(q=xa(a,e,n),r?r.length<q.length?(A=a[e-1],p[m++]=A,++w[A],c(q,0)):c(r,-1):q.length<ja?r=q:c(q,0)):r?c(r,-1):(A=a[e],p[m++]=A,++w[A])}n.push(e)}p[m++]=256;w[256]++;d.M=w;d.L=y;return F?p.subarray(0,m):p}
  4036. function xa(d,a,c){var e,b,f=0,g,h,k,n,q=d.length;h=0;n=c.length;a:for(;h<n;h++){e=c[n-h-1];g=3;if(3<f){for(k=f;3<k;k--)if(d[e+k-1]!==d[a+k-1])continue a;g=f}for(;258>g&&a+g<q&&d[e+g]===d[a+g];)++g;g>f&&(b=e,f=g);if(258===g)break}return new ta(f,a-b)}
  4037. function qa(d,a){var c=d.length,e=new ha(572),b=new (F?Uint8Array:Array)(c),f,g,h,k,n;if(!F)for(k=0;k<c;k++)b[k]=0;for(k=0;k<c;++k)0<d[k]&&e.push(k,d[k]);f=Array(e.length/2);g=new (F?Uint32Array:Array)(e.length/2);if(1===f.length)return b[e.pop().index]=1,b;k=0;for(n=e.length/2;k<n;++k)f[k]=e.pop(),g[k]=f[k].value;h=ya(g,g.length,a);k=0;for(n=f.length;k<n;++k)b[f[k].index]=h[k];return b}
  4038. function ya(d,a,c){function e(b){var c=k[b][n[b]];c===a?(e(b+1),e(b+1)):--g[c];++n[b]}var b=new (F?Uint16Array:Array)(c),f=new (F?Uint8Array:Array)(c),g=new (F?Uint8Array:Array)(a),h=Array(c),k=Array(c),n=Array(c),q=(1<<c)-a,r=1<<c-1,p,m,s,w,y;b[c-1]=a;for(m=0;m<c;++m)q<r?f[m]=0:(f[m]=1,q-=r),q<<=1,b[c-2-m]=(b[c-1-m]/2|0)+a;b[0]=f[0];h[0]=Array(b[0]);k[0]=Array(b[0]);for(m=1;m<c;++m)b[m]>2*b[m-1]+f[m]&&(b[m]=2*b[m-1]+f[m]),h[m]=Array(b[m]),k[m]=Array(b[m]);for(p=0;p<a;++p)g[p]=c;for(s=0;s<b[c-1];++s)h[c-
  4039. 1][s]=d[s],k[c-1][s]=s;for(p=0;p<c;++p)n[p]=0;1===f[c-1]&&(--g[0],++n[c-1]);for(m=c-2;0<=m;--m){w=p=0;y=n[m+1];for(s=0;s<b[m];s++)w=h[m+1][y]+h[m+1][y+1],w>d[p]?(h[m][s]=w,k[m][s]=a,y+=2):(h[m][s]=d[p],k[m][s]=p,++p);n[m]=0;1===f[m]&&e(m)}return g}
  4040. function sa(d){var a=new (F?Uint16Array:Array)(d.length),c=[],e=[],b=0,f,g,h,k;f=0;for(g=d.length;f<g;f++)c[d[f]]=(c[d[f]]|0)+1;f=1;for(g=16;f<=g;f++)e[f]=b,b+=c[f]|0,b<<=1;f=0;for(g=d.length;f<g;f++){b=e[d[f]];e[d[f]]+=1;h=a[f]=0;for(k=d[f];h<k;h++)a[f]=a[f]<<1|b&1,b>>>=1}return a};function T(d,a){this.l=[];this.m=32768;this.e=this.g=this.c=this.q=0;this.input=F?new Uint8Array(d):d;this.s=!1;this.n=za;this.C=!1;if(a||!(a={}))a.index&&(this.c=a.index),a.bufferSize&&(this.m=a.bufferSize),a.bufferType&&(this.n=a.bufferType),a.resize&&(this.C=a.resize);switch(this.n){case Aa:this.b=32768;this.a=new (F?Uint8Array:Array)(32768+this.m+258);break;case za:this.b=0;this.a=new (F?Uint8Array:Array)(this.m);this.f=this.K;this.t=this.I;this.o=this.J;break;default:l(Error("invalid inflate mode"))}}
  4041. var Aa=0,za=1,Ba={F:Aa,D:za};
  4042. T.prototype.p=function(){for(;!this.s;){var d=Y(this,3);d&1&&(this.s=x);d>>>=1;switch(d){case 0:var a=this.input,c=this.c,e=this.a,b=this.b,f=a.length,g=v,h=v,k=e.length,n=v;this.e=this.g=0;c+1>=f&&l(Error("invalid uncompressed block header: LEN"));g=a[c++]|a[c++]<<8;c+1>=f&&l(Error("invalid uncompressed block header: NLEN"));h=a[c++]|a[c++]<<8;g===~h&&l(Error("invalid uncompressed block header: length verify"));c+g>a.length&&l(Error("input buffer is broken"));switch(this.n){case Aa:for(;b+g>e.length;){n=
  4043. k-b;g-=n;if(F)e.set(a.subarray(c,c+n),b),b+=n,c+=n;else for(;n--;)e[b++]=a[c++];this.b=b;e=this.f();b=this.b}break;case za:for(;b+g>e.length;)e=this.f({v:2});break;default:l(Error("invalid inflate mode"))}if(F)e.set(a.subarray(c,c+g),b),b+=g,c+=g;else for(;g--;)e[b++]=a[c++];this.c=c;this.b=b;this.a=e;break;case 1:this.o(Ca,Da);break;case 2:Sa(this);break;default:l(Error("unknown BTYPE: "+d))}}return this.t()};
  4044. var Ta=[16,17,18,0,8,7,9,6,10,5,11,4,12,3,13,2,14,1,15],Ua=F?new Uint16Array(Ta):Ta,Va=[3,4,5,6,7,8,9,10,11,13,15,17,19,23,27,31,35,43,51,59,67,83,99,115,131,163,195,227,258,258,258],Wa=F?new Uint16Array(Va):Va,Xa=[0,0,0,0,0,0,0,0,1,1,1,1,2,2,2,2,3,3,3,3,4,4,4,4,5,5,5,5,0,0,0],Ya=F?new Uint8Array(Xa):Xa,Za=[1,2,3,4,5,7,9,13,17,25,33,49,65,97,129,193,257,385,513,769,1025,1537,2049,3073,4097,6145,8193,12289,16385,24577],$a=F?new Uint16Array(Za):Za,ab=[0,0,0,0,1,1,2,2,3,3,4,4,5,5,6,6,7,7,8,8,9,9,10,
  4045. 10,11,11,12,12,13,13],bb=F?new Uint8Array(ab):ab,cb=new (F?Uint8Array:Array)(288),Z,db;Z=0;for(db=cb.length;Z<db;++Z)cb[Z]=143>=Z?8:255>=Z?9:279>=Z?7:8;var Ca=R(cb),eb=new (F?Uint8Array:Array)(30),fb,hb;fb=0;for(hb=eb.length;fb<hb;++fb)eb[fb]=5;var Da=R(eb);function Y(d,a){for(var c=d.g,e=d.e,b=d.input,f=d.c,g=b.length,h;e<a;)f>=g&&l(Error("input buffer is broken")),c|=b[f++]<<e,e+=8;h=c&(1<<a)-1;d.g=c>>>a;d.e=e-a;d.c=f;return h}
  4046. function ib(d,a){for(var c=d.g,e=d.e,b=d.input,f=d.c,g=b.length,h=a[0],k=a[1],n,q;e<k&&!(f>=g);)c|=b[f++]<<e,e+=8;n=h[c&(1<<k)-1];q=n>>>16;d.g=c>>q;d.e=e-q;d.c=f;return n&65535}
  4047. function Sa(d){function a(a,b,c){var d,e=this.z,f,g;for(g=0;g<a;)switch(d=ib(this,b),d){case 16:for(f=3+Y(this,2);f--;)c[g++]=e;break;case 17:for(f=3+Y(this,3);f--;)c[g++]=0;e=0;break;case 18:for(f=11+Y(this,7);f--;)c[g++]=0;e=0;break;default:e=c[g++]=d}this.z=e;return c}var c=Y(d,5)+257,e=Y(d,5)+1,b=Y(d,4)+4,f=new (F?Uint8Array:Array)(Ua.length),g,h,k,n;for(n=0;n<b;++n)f[Ua[n]]=Y(d,3);if(!F){n=b;for(b=f.length;n<b;++n)f[Ua[n]]=0}g=R(f);h=new (F?Uint8Array:Array)(c);k=new (F?Uint8Array:Array)(e);
  4048. d.z=0;d.o(R(a.call(d,c,g,h)),R(a.call(d,e,g,k)))}T.prototype.o=function(d,a){var c=this.a,e=this.b;this.u=d;for(var b=c.length-258,f,g,h,k;256!==(f=ib(this,d));)if(256>f)e>=b&&(this.b=e,c=this.f(),e=this.b),c[e++]=f;else{g=f-257;k=Wa[g];0<Ya[g]&&(k+=Y(this,Ya[g]));f=ib(this,a);h=$a[f];0<bb[f]&&(h+=Y(this,bb[f]));e>=b&&(this.b=e,c=this.f(),e=this.b);for(;k--;)c[e]=c[e++-h]}for(;8<=this.e;)this.e-=8,this.c--;this.b=e};
  4049. T.prototype.J=function(d,a){var c=this.a,e=this.b;this.u=d;for(var b=c.length,f,g,h,k;256!==(f=ib(this,d));)if(256>f)e>=b&&(c=this.f(),b=c.length),c[e++]=f;else{g=f-257;k=Wa[g];0<Ya[g]&&(k+=Y(this,Ya[g]));f=ib(this,a);h=$a[f];0<bb[f]&&(h+=Y(this,bb[f]));e+k>b&&(c=this.f(),b=c.length);for(;k--;)c[e]=c[e++-h]}for(;8<=this.e;)this.e-=8,this.c--;this.b=e};
  4050. T.prototype.f=function(){var d=new (F?Uint8Array:Array)(this.b-32768),a=this.b-32768,c,e,b=this.a;if(F)d.set(b.subarray(32768,d.length));else{c=0;for(e=d.length;c<e;++c)d[c]=b[c+32768]}this.l.push(d);this.q+=d.length;if(F)b.set(b.subarray(a,a+32768));else for(c=0;32768>c;++c)b[c]=b[a+c];this.b=32768;return b};
  4051. T.prototype.K=function(d){var a,c=this.input.length/this.c+1|0,e,b,f,g=this.input,h=this.a;d&&("number"===typeof d.v&&(c=d.v),"number"===typeof d.G&&(c+=d.G));2>c?(e=(g.length-this.c)/this.u[2],f=258*(e/2)|0,b=f<h.length?h.length+f:h.length<<1):b=h.length*c;F?(a=new Uint8Array(b),a.set(h)):a=h;return this.a=a};
  4052. T.prototype.t=function(){var d=0,a=this.a,c=this.l,e,b=new (F?Uint8Array:Array)(this.q+(this.b-32768)),f,g,h,k;if(0===c.length)return F?this.a.subarray(32768,this.b):this.a.slice(32768,this.b);f=0;for(g=c.length;f<g;++f){e=c[f];h=0;for(k=e.length;h<k;++h)b[d++]=e[h]}f=32768;for(g=this.b;f<g;++f)b[d++]=a[f];this.l=[];return this.buffer=b};
  4053. T.prototype.I=function(){var d,a=this.b;F?this.C?(d=new Uint8Array(a),d.set(this.a.subarray(0,a))):d=this.a.subarray(0,a):(this.a.length>a&&(this.a.length=a),d=this.a);return this.buffer=d};function jb(d){if("string"===typeof d){var a=d.split(""),c,e;c=0;for(e=a.length;c<e;c++)a[c]=(a[c].charCodeAt(0)&255)>>>0;d=a}for(var b=1,f=0,g=d.length,h,k=0;0<g;){h=1024<g?1024:g;g-=h;do b+=d[k++],f+=b;while(--h);b%=65521;f%=65521}return(f<<16|b)>>>0};function kb(d,a){var c,e;this.input=d;this.c=0;if(a||!(a={}))a.index&&(this.c=a.index),a.verify&&(this.N=a.verify);c=d[this.c++];e=d[this.c++];switch(c&15){case lb:this.method=lb;break;default:l(Error("unsupported compression method"))}0!==((c<<8)+e)%31&&l(Error("invalid fcheck flag:"+((c<<8)+e)%31));e&32&&l(Error("fdict flag is not supported"));this.B=new T(d,{index:this.c,bufferSize:a.bufferSize,bufferType:a.bufferType,resize:a.resize})}
  4054. kb.prototype.p=function(){var d=this.input,a,c;a=this.B.p();this.c=this.B.c;this.N&&(c=(d[this.c++]<<24|d[this.c++]<<16|d[this.c++]<<8|d[this.c++])>>>0,c!==jb(a)&&l(Error("invalid adler-32 checksum")));return a};var lb=8;function mb(d,a){this.input=d;this.a=new (F?Uint8Array:Array)(32768);this.h=$.k;var c={},e;if((a||!(a={}))&&"number"===typeof a.compressionType)this.h=a.compressionType;for(e in a)c[e]=a[e];c.outputBuffer=this.a;this.A=new ia(this.input,c)}var $=na;
  4055. mb.prototype.j=function(){var d,a,c,e,b,f,g,h=0;g=this.a;d=lb;switch(d){case lb:a=Math.LOG2E*Math.log(32768)-8;break;default:l(Error("invalid compression method"))}c=a<<4|d;g[h++]=c;switch(d){case lb:switch(this.h){case $.NONE:b=0;break;case $.r:b=1;break;case $.k:b=2;break;default:l(Error("unsupported compression type"))}break;default:l(Error("invalid compression method"))}e=b<<6|0;g[h++]=e|31-(256*c+e)%31;f=jb(this.input);this.A.b=h;g=this.A.j();h=g.length;F&&(g=new Uint8Array(g.buffer),g.length<=
  4056. h+4&&(this.a=new Uint8Array(g.length+4),this.a.set(g),g=this.a),g=g.subarray(0,h+4));g[h++]=f>>24&255;g[h++]=f>>16&255;g[h++]=f>>8&255;g[h++]=f&255;return g};function nb(d,a){var c,e,b,f;if(Object.keys)c=Object.keys(a);else for(e in c=[],b=0,a)c[b++]=e;b=0;for(f=c.length;b<f;++b)e=c[b],D(d+"."+e,a[e])};D("Zlib.Inflate",kb);D("Zlib.Inflate.prototype.decompress",kb.prototype.p);nb("Zlib.Inflate.BufferType",{ADAPTIVE:Ba.D,BLOCK:Ba.F});D("Zlib.Deflate",mb);D("Zlib.Deflate.compress",function(d,a){return(new mb(d,a)).j()});D("Zlib.Deflate.prototype.compress",mb.prototype.j);nb("Zlib.Deflate.CompressionType",{NONE:$.NONE,FIXED:$.r,DYNAMIC:$.k});}).call(this);
  4057. },{}],9:[function(_dereq_,module,exports){
  4058. // GPG4Browsers - An OpenPGP implementation in javascript
  4059. // Copyright (C) 2011 Recurity Labs GmbH
  4060. //
  4061. // This library is free software; you can redistribute it and/or
  4062. // modify it under the terms of the GNU Lesser General Public
  4063. // License as published by the Free Software Foundation; either
  4064. // version 3.0 of the License, or (at your option) any later version.
  4065. //
  4066. // This library is distributed in the hope that it will be useful,
  4067. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  4068. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  4069. // Lesser General Public License for more details.
  4070. //
  4071. // You should have received a copy of the GNU Lesser General Public
  4072. // License along with this library; if not, write to the Free Software
  4073. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  4074. /**
  4075. * This object contains configuration values.
  4076. * @requires enums
  4077. * @property {Integer} prefer_hash_algorithm
  4078. * @property {Integer} encryption_cipher
  4079. * @property {Integer} compression
  4080. * @property {Boolean} show_version
  4081. * @property {Boolean} show_comment
  4082. * @property {Boolean} integrity_protect
  4083. * @property {String} keyserver
  4084. * @property {Boolean} debug If enabled, debug messages will be printed
  4085. * @module config/config
  4086. */
  4087. 'use strict';
  4088. Object.defineProperty(exports, "__esModule", {
  4089. value: true
  4090. });
  4091. var _enums = _dereq_('../enums.js');
  4092. var _enums2 = _interopRequireDefault(_enums);
  4093. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  4094. exports.default = {
  4095. prefer_hash_algorithm: _enums2.default.hash.sha256,
  4096. encryption_cipher: _enums2.default.symmetric.aes256,
  4097. compression: _enums2.default.compression.zip,
  4098. aead_protect: false, // use Authenticated Encryption with Additional Data (AEAD) protection for symmetric encryption
  4099. integrity_protect: true, // use integrity protection for symmetric encryption
  4100. ignore_mdc_error: false, // fail on decrypt if message is not integrity protected
  4101. rsa_blinding: true,
  4102. use_native: true, // use native node.js crypto and Web Crypto apis (if available)
  4103. zero_copy: false, // use transferable objects between the Web Worker and main thread
  4104. debug: false,
  4105. show_version: true,
  4106. show_comment: true,
  4107. versionstring: "OpenPGP.js v2.5.3",
  4108. commentstring: "http://openpgpjs.org",
  4109. keyserver: "https://keyserver.ubuntu.com",
  4110. node_store: './openpgp.store'
  4111. };
  4112. },{"../enums.js":35}],10:[function(_dereq_,module,exports){
  4113. /**
  4114. * @see module:config/config
  4115. * @module config
  4116. */
  4117. 'use strict';
  4118. Object.defineProperty(exports, "__esModule", {
  4119. value: true
  4120. });
  4121. var _config = _dereq_('./config.js');
  4122. Object.defineProperty(exports, 'default', {
  4123. enumerable: true,
  4124. get: function get() {
  4125. return _interopRequireDefault(_config).default;
  4126. }
  4127. });
  4128. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  4129. },{"./config.js":9}],11:[function(_dereq_,module,exports){
  4130. // Modified by ProtonTech AG
  4131. // Modified by Recurity Labs GmbH
  4132. // modified version of http://www.hanewin.net/encrypt/PGdecode.js:
  4133. /* OpenPGP encryption using RSA/AES
  4134. * Copyright 2005-2006 Herbert Hanewinkel, www.haneWIN.de
  4135. * version 2.0, check www.haneWIN.de for the latest version
  4136. * This software is provided as-is, without express or implied warranty.
  4137. * Permission to use, copy, modify, distribute or sell this software, with or
  4138. * without fee, for any purpose and by any individual or organization, is hereby
  4139. * granted, provided that the above copyright notice and this paragraph appear
  4140. * in all copies. Distribution as a part of an application or binary must
  4141. * include the above copyright notice in the documentation and/or other
  4142. * materials provided with the application or distribution.
  4143. */
  4144. /**
  4145. * @requires crypto/cipher
  4146. * @module crypto/cfb
  4147. */
  4148. 'use strict';
  4149. Object.defineProperty(exports, "__esModule", {
  4150. value: true
  4151. });
  4152. var _cipher = _dereq_('./cipher');
  4153. var _cipher2 = _interopRequireDefault(_cipher);
  4154. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  4155. exports.default = {
  4156. /**
  4157. * This function encrypts a given with the specified prefixrandom
  4158. * using the specified blockcipher to encrypt a message
  4159. * @param {Uint8Array} prefixrandom random bytes of block_size length
  4160. * to be used in prefixing the data
  4161. * @param {String} cipherfn the algorithm cipher class to encrypt
  4162. * data in one block_size encryption, {@link module:crypto/cipher}.
  4163. * @param {Uint8Array} plaintext data to be encrypted
  4164. * @param {Uint8Array} key key to be used to encrypt the plaintext.
  4165. * This will be passed to the cipherfn
  4166. * @param {Boolean} resync a boolean value specifying if a resync of the
  4167. * IV should be used or not. The encrypteddatapacket uses the
  4168. * "old" style with a resync. Encryption within an
  4169. * encryptedintegrityprotecteddata packet is not resyncing the IV.
  4170. * @return {Uint8Array} encrypted data
  4171. */
  4172. encrypt: function encrypt(prefixrandom, cipherfn, plaintext, key, resync) {
  4173. cipherfn = new _cipher2.default[cipherfn](key);
  4174. var block_size = cipherfn.blockSize;
  4175. var FR = new Uint8Array(block_size);
  4176. var FRE = new Uint8Array(block_size);
  4177. var new_prefix = new Uint8Array(prefixrandom.length + 2);
  4178. new_prefix.set(prefixrandom);
  4179. new_prefix[prefixrandom.length] = prefixrandom[block_size - 2];
  4180. new_prefix[prefixrandom.length + 1] = prefixrandom[block_size - 1];
  4181. prefixrandom = new_prefix;
  4182. var ciphertext = new Uint8Array(plaintext.length + 2 + block_size * 2);
  4183. var i, n, begin;
  4184. var offset = resync ? 0 : 2;
  4185. // 1. The feedback register (FR) is set to the IV, which is all zeros.
  4186. for (i = 0; i < block_size; i++) {
  4187. FR[i] = 0;
  4188. }
  4189. // 2. FR is encrypted to produce FRE (FR Encrypted). This is the
  4190. // encryption of an all-zero value.
  4191. FRE = cipherfn.encrypt(FR);
  4192. // 3. FRE is xored with the first BS octets of random data prefixed to
  4193. // the plaintext to produce C[1] through C[BS], the first BS octets
  4194. // of ciphertext.
  4195. for (i = 0; i < block_size; i++) {
  4196. ciphertext[i] = FRE[i] ^ prefixrandom[i];
  4197. }
  4198. // 4. FR is loaded with C[1] through C[BS].
  4199. FR.set(ciphertext.subarray(0, block_size));
  4200. // 5. FR is encrypted to produce FRE, the encryption of the first BS
  4201. // octets of ciphertext.
  4202. FRE = cipherfn.encrypt(FR);
  4203. // 6. The left two octets of FRE get xored with the next two octets of
  4204. // data that were prefixed to the plaintext. This produces C[BS+1]
  4205. // and C[BS+2], the next two octets of ciphertext.
  4206. ciphertext[block_size] = FRE[0] ^ prefixrandom[block_size];
  4207. ciphertext[block_size + 1] = FRE[1] ^ prefixrandom[block_size + 1];
  4208. if (resync) {
  4209. // 7. (The resync step) FR is loaded with C[3] through C[BS+2].
  4210. FR.set(ciphertext.subarray(2, block_size + 2));
  4211. } else {
  4212. FR.set(ciphertext.subarray(0, block_size));
  4213. }
  4214. // 8. FR is encrypted to produce FRE.
  4215. FRE = cipherfn.encrypt(FR);
  4216. // 9. FRE is xored with the first BS octets of the given plaintext, now
  4217. // that we have finished encrypting the BS+2 octets of prefixed
  4218. // data. This produces C[BS+3] through C[BS+(BS+2)], the next BS
  4219. // octets of ciphertext.
  4220. for (i = 0; i < block_size; i++) {
  4221. ciphertext[block_size + 2 + i] = FRE[i + offset] ^ plaintext[i];
  4222. }
  4223. for (n = block_size; n < plaintext.length + offset; n += block_size) {
  4224. // 10. FR is loaded with C[BS+3] to C[BS + (BS+2)] (which is C11-C18 for
  4225. // an 8-octet block).
  4226. begin = n + 2 - offset;
  4227. FR.set(ciphertext.subarray(begin, begin + block_size));
  4228. // 11. FR is encrypted to produce FRE.
  4229. FRE = cipherfn.encrypt(FR);
  4230. // 12. FRE is xored with the next BS octets of plaintext, to produce
  4231. // the next BS octets of ciphertext. These are loaded into FR, and
  4232. // the process is repeated until the plaintext is used up.
  4233. for (i = 0; i < block_size; i++) {
  4234. ciphertext[block_size + begin + i] = FRE[i] ^ plaintext[n + i - offset];
  4235. }
  4236. }
  4237. ciphertext = ciphertext.subarray(0, plaintext.length + 2 + block_size);
  4238. return ciphertext;
  4239. },
  4240. /**
  4241. * Decrypts the prefixed data for the Modification Detection Code (MDC) computation
  4242. * @param {String} cipherfn.encrypt Cipher function to use,
  4243. * @see module:crypto/cipher.
  4244. * @param {Uint8Array} key Uint8Array representation of key to be used to check the mdc
  4245. * This will be passed to the cipherfn
  4246. * @param {Uint8Array} ciphertext The encrypted data
  4247. * @return {Uint8Array} plaintext Data of D(ciphertext) with blocksize length +2
  4248. */
  4249. mdc: function mdc(cipherfn, key, ciphertext) {
  4250. cipherfn = new _cipher2.default[cipherfn](key);
  4251. var block_size = cipherfn.blockSize;
  4252. var iblock = new Uint8Array(block_size);
  4253. var ablock = new Uint8Array(block_size);
  4254. var i;
  4255. // initialisation vector
  4256. for (i = 0; i < block_size; i++) {
  4257. iblock[i] = 0;
  4258. }
  4259. iblock = cipherfn.encrypt(iblock);
  4260. for (i = 0; i < block_size; i++) {
  4261. ablock[i] = ciphertext[i];
  4262. iblock[i] ^= ablock[i];
  4263. }
  4264. ablock = cipherfn.encrypt(ablock);
  4265. var result = new Uint8Array(iblock.length + 2);
  4266. result.set(iblock);
  4267. result[iblock.length] = ablock[0] ^ ciphertext[block_size];
  4268. result[iblock.length + 1] = ablock[1] ^ ciphertext[block_size + 1];
  4269. return result;
  4270. },
  4271. /**
  4272. * This function decrypts a given plaintext using the specified
  4273. * blockcipher to decrypt a message
  4274. * @param {String} cipherfn the algorithm cipher class to decrypt
  4275. * data in one block_size encryption, {@link module:crypto/cipher}.
  4276. * @param {Uint8Array} key Uint8Array representation of key to be used to decrypt the ciphertext.
  4277. * This will be passed to the cipherfn
  4278. * @param {Uint8Array} ciphertext to be decrypted
  4279. * @param {Boolean} resync a boolean value specifying if a resync of the
  4280. * IV should be used or not. The encrypteddatapacket uses the
  4281. * "old" style with a resync. Decryption within an
  4282. * encryptedintegrityprotecteddata packet is not resyncing the IV.
  4283. * @return {Uint8Array} the plaintext data
  4284. */
  4285. decrypt: function decrypt(cipherfn, key, ciphertext, resync) {
  4286. cipherfn = new _cipher2.default[cipherfn](key);
  4287. var block_size = cipherfn.blockSize;
  4288. var iblock = new Uint8Array(block_size);
  4289. var ablock = new Uint8Array(block_size);
  4290. var i, j, n;
  4291. var text = new Uint8Array(ciphertext.length - block_size);
  4292. // initialisation vector
  4293. for (i = 0; i < block_size; i++) {
  4294. iblock[i] = 0;
  4295. }
  4296. iblock = cipherfn.encrypt(iblock);
  4297. for (i = 0; i < block_size; i++) {
  4298. ablock[i] = ciphertext[i];
  4299. iblock[i] ^= ablock[i];
  4300. }
  4301. ablock = cipherfn.encrypt(ablock);
  4302. // test check octets
  4303. if (iblock[block_size - 2] !== (ablock[0] ^ ciphertext[block_size]) || iblock[block_size - 1] !== (ablock[1] ^ ciphertext[block_size + 1])) {
  4304. throw new Error('CFB decrypt: invalid key');
  4305. }
  4306. /* RFC4880: Tag 18 and Resync:
  4307. * [...] Unlike the Symmetrically Encrypted Data Packet, no
  4308. * special CFB resynchronization is done after encrypting this prefix
  4309. * data. See "OpenPGP CFB Mode" below for more details.
  4310. */
  4311. j = 0;
  4312. if (resync) {
  4313. for (i = 0; i < block_size; i++) {
  4314. iblock[i] = ciphertext[i + 2];
  4315. }
  4316. for (n = block_size + 2; n < ciphertext.length; n += block_size) {
  4317. ablock = cipherfn.encrypt(iblock);
  4318. for (i = 0; i < block_size && i + n < ciphertext.length; i++) {
  4319. iblock[i] = ciphertext[n + i];
  4320. if (j < text.length) {
  4321. text[j] = ablock[i] ^ iblock[i];
  4322. j++;
  4323. }
  4324. }
  4325. }
  4326. } else {
  4327. for (i = 0; i < block_size; i++) {
  4328. iblock[i] = ciphertext[i];
  4329. }
  4330. for (n = block_size; n < ciphertext.length; n += block_size) {
  4331. ablock = cipherfn.encrypt(iblock);
  4332. for (i = 0; i < block_size && i + n < ciphertext.length; i++) {
  4333. iblock[i] = ciphertext[n + i];
  4334. if (j < text.length) {
  4335. text[j] = ablock[i] ^ iblock[i];
  4336. j++;
  4337. }
  4338. }
  4339. }
  4340. }
  4341. n = resync ? 0 : 2;
  4342. text = text.subarray(n, ciphertext.length - block_size - 2 + n);
  4343. return text;
  4344. },
  4345. normalEncrypt: function normalEncrypt(cipherfn, key, plaintext, iv) {
  4346. cipherfn = new _cipher2.default[cipherfn](key);
  4347. var block_size = cipherfn.blockSize;
  4348. var blocki = new Uint8Array(block_size);
  4349. var blockc = new Uint8Array(block_size);
  4350. var pos = 0;
  4351. var cyphertext = new Uint8Array(plaintext.length);
  4352. var i,
  4353. j = 0;
  4354. if (iv === null) {
  4355. for (i = 0; i < block_size; i++) {
  4356. blockc[i] = 0;
  4357. }
  4358. } else {
  4359. for (i = 0; i < block_size; i++) {
  4360. blockc[i] = iv[i];
  4361. }
  4362. }
  4363. while (plaintext.length > block_size * pos) {
  4364. var encblock = cipherfn.encrypt(blockc);
  4365. blocki = plaintext.subarray(pos * block_size, pos * block_size + block_size);
  4366. for (i = 0; i < blocki.length; i++) {
  4367. blockc[i] = blocki[i] ^ encblock[i];
  4368. cyphertext[j++] = blockc[i];
  4369. }
  4370. pos++;
  4371. }
  4372. return cyphertext;
  4373. },
  4374. normalDecrypt: function normalDecrypt(cipherfn, key, ciphertext, iv) {
  4375. cipherfn = new _cipher2.default[cipherfn](key);
  4376. var block_size = cipherfn.blockSize;
  4377. var blockp;
  4378. var pos = 0;
  4379. var plaintext = new Uint8Array(ciphertext.length);
  4380. var offset = 0;
  4381. var i,
  4382. j = 0;
  4383. if (iv === null) {
  4384. blockp = new Uint8Array(block_size);
  4385. for (i = 0; i < block_size; i++) {
  4386. blockp[i] = 0;
  4387. }
  4388. } else {
  4389. blockp = iv.subarray(0, block_size);
  4390. }
  4391. while (ciphertext.length > block_size * pos) {
  4392. var decblock = cipherfn.encrypt(blockp);
  4393. blockp = ciphertext.subarray(pos * block_size + offset, pos * block_size + block_size + offset);
  4394. for (i = 0; i < blockp.length; i++) {
  4395. plaintext[j++] = blockp[i] ^ decblock[i];
  4396. }
  4397. pos++;
  4398. }
  4399. return plaintext;
  4400. }
  4401. };
  4402. },{"./cipher":16}],12:[function(_dereq_,module,exports){
  4403. /* Rijndael (AES) Encryption
  4404. * Copyright 2005 Herbert Hanewinkel, www.haneWIN.de
  4405. * version 1.1, check www.haneWIN.de for the latest version
  4406. * This software is provided as-is, without express or implied warranty.
  4407. * Permission to use, copy, modify, distribute or sell this software, with or
  4408. * without fee, for any purpose and by any individual or organization, is hereby
  4409. * granted, provided that the above copyright notice and this paragraph appear
  4410. * in all copies. Distribution as a part of an application or binary must
  4411. * include the above copyright notice in the documentation and/or other
  4412. * materials provided with the application or distribution.
  4413. */
  4414. /**
  4415. * @module crypto/cipher/aes
  4416. */
  4417. 'use strict';
  4418. // The round constants used in subkey expansion
  4419. Object.defineProperty(exports, "__esModule", {
  4420. value: true
  4421. });
  4422. var Rcon = new Uint8Array([0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36, 0x6c, 0xd8, 0xab, 0x4d, 0x9a, 0x2f, 0x5e, 0xbc, 0x63, 0xc6, 0x97, 0x35, 0x6a, 0xd4, 0xb3, 0x7d, 0xfa, 0xef, 0xc5, 0x91]);
  4423. // Precomputed lookup table for the SBox
  4424. var S = new Uint8Array([99, 124, 119, 123, 242, 107, 111, 197, 48, 1, 103, 43, 254, 215, 171, 118, 202, 130, 201, 125, 250, 89, 71, 240, 173, 212, 162, 175, 156, 164, 114, 192, 183, 253, 147, 38, 54, 63, 247, 204, 52, 165, 229, 241, 113, 216, 49, 21, 4, 199, 35, 195, 24, 150, 5, 154, 7, 18, 128, 226, 235, 39, 178, 117, 9, 131, 44, 26, 27, 110, 90, 160, 82, 59, 214, 179, 41, 227, 47, 132, 83, 209, 0, 237, 32, 252, 177, 91, 106, 203, 190, 57, 74, 76, 88, 207, 208, 239, 170, 251, 67, 77, 51, 133, 69, 249, 2, 127, 80, 60, 159, 168, 81, 163, 64, 143, 146, 157, 56, 245, 188, 182, 218, 33, 16, 255, 243, 210, 205, 12, 19, 236, 95, 151, 68, 23, 196, 167, 126, 61, 100, 93, 25, 115, 96, 129, 79, 220, 34, 42, 144, 136, 70, 238, 184, 20, 222, 94, 11, 219, 224, 50, 58, 10, 73, 6, 36, 92, 194, 211, 172, 98, 145, 149, 228, 121, 231, 200, 55, 109, 141, 213, 78, 169, 108, 86, 244, 234, 101, 122, 174, 8, 186, 120, 37, 46, 28, 166, 180, 198, 232, 221, 116, 31, 75, 189, 139, 138, 112, 62, 181, 102, 72, 3, 246, 14, 97, 53, 87, 185, 134, 193, 29, 158, 225, 248, 152, 17, 105, 217, 142, 148, 155, 30, 135, 233, 206, 85, 40, 223, 140, 161, 137, 13, 191, 230, 66, 104, 65, 153, 45, 15, 176, 84, 187, 22]);
  4425. var T1 = new Uint32Array([0xa56363c6, 0x847c7cf8, 0x997777ee, 0x8d7b7bf6, 0x0df2f2ff, 0xbd6b6bd6, 0xb16f6fde, 0x54c5c591, 0x50303060, 0x03010102, 0xa96767ce, 0x7d2b2b56, 0x19fefee7, 0x62d7d7b5, 0xe6abab4d, 0x9a7676ec, 0x45caca8f, 0x9d82821f, 0x40c9c989, 0x877d7dfa, 0x15fafaef, 0xeb5959b2, 0xc947478e, 0x0bf0f0fb, 0xecadad41, 0x67d4d4b3, 0xfda2a25f, 0xeaafaf45, 0xbf9c9c23, 0xf7a4a453, 0x967272e4, 0x5bc0c09b, 0xc2b7b775, 0x1cfdfde1, 0xae93933d, 0x6a26264c, 0x5a36366c, 0x413f3f7e, 0x02f7f7f5, 0x4fcccc83, 0x5c343468, 0xf4a5a551, 0x34e5e5d1, 0x08f1f1f9, 0x937171e2, 0x73d8d8ab, 0x53313162, 0x3f15152a, 0x0c040408, 0x52c7c795, 0x65232346, 0x5ec3c39d, 0x28181830, 0xa1969637, 0x0f05050a, 0xb59a9a2f, 0x0907070e, 0x36121224, 0x9b80801b, 0x3de2e2df, 0x26ebebcd, 0x6927274e, 0xcdb2b27f, 0x9f7575ea, 0x1b090912, 0x9e83831d, 0x742c2c58, 0x2e1a1a34, 0x2d1b1b36, 0xb26e6edc, 0xee5a5ab4, 0xfba0a05b, 0xf65252a4, 0x4d3b3b76, 0x61d6d6b7, 0xceb3b37d, 0x7b292952, 0x3ee3e3dd, 0x712f2f5e, 0x97848413, 0xf55353a6, 0x68d1d1b9, 0x00000000, 0x2cededc1, 0x60202040, 0x1ffcfce3, 0xc8b1b179, 0xed5b5bb6, 0xbe6a6ad4, 0x46cbcb8d, 0xd9bebe67, 0x4b393972, 0xde4a4a94, 0xd44c4c98, 0xe85858b0, 0x4acfcf85, 0x6bd0d0bb, 0x2aefefc5, 0xe5aaaa4f, 0x16fbfbed, 0xc5434386, 0xd74d4d9a, 0x55333366, 0x94858511, 0xcf45458a, 0x10f9f9e9, 0x06020204, 0x817f7ffe, 0xf05050a0, 0x443c3c78, 0xba9f9f25, 0xe3a8a84b, 0xf35151a2, 0xfea3a35d, 0xc0404080, 0x8a8f8f05, 0xad92923f, 0xbc9d9d21, 0x48383870, 0x04f5f5f1, 0xdfbcbc63, 0xc1b6b677, 0x75dadaaf, 0x63212142, 0x30101020, 0x1affffe5, 0x0ef3f3fd, 0x6dd2d2bf, 0x4ccdcd81, 0x140c0c18, 0x35131326, 0x2fececc3, 0xe15f5fbe, 0xa2979735, 0xcc444488, 0x3917172e, 0x57c4c493, 0xf2a7a755, 0x827e7efc, 0x473d3d7a, 0xac6464c8, 0xe75d5dba, 0x2b191932, 0x957373e6, 0xa06060c0, 0x98818119, 0xd14f4f9e, 0x7fdcdca3, 0x66222244, 0x7e2a2a54, 0xab90903b, 0x8388880b, 0xca46468c, 0x29eeeec7, 0xd3b8b86b, 0x3c141428, 0x79dedea7, 0xe25e5ebc, 0x1d0b0b16, 0x76dbdbad, 0x3be0e0db, 0x56323264, 0x4e3a3a74, 0x1e0a0a14, 0xdb494992, 0x0a06060c, 0x6c242448, 0xe45c5cb8, 0x5dc2c29f, 0x6ed3d3bd, 0xefacac43, 0xa66262c4, 0xa8919139, 0xa4959531, 0x37e4e4d3, 0x8b7979f2, 0x32e7e7d5, 0x43c8c88b, 0x5937376e, 0xb76d6dda, 0x8c8d8d01, 0x64d5d5b1, 0xd24e4e9c, 0xe0a9a949, 0xb46c6cd8, 0xfa5656ac, 0x07f4f4f3, 0x25eaeacf, 0xaf6565ca, 0x8e7a7af4, 0xe9aeae47, 0x18080810, 0xd5baba6f, 0x887878f0, 0x6f25254a, 0x722e2e5c, 0x241c1c38, 0xf1a6a657, 0xc7b4b473, 0x51c6c697, 0x23e8e8cb, 0x7cdddda1, 0x9c7474e8, 0x211f1f3e, 0xdd4b4b96, 0xdcbdbd61, 0x868b8b0d, 0x858a8a0f, 0x907070e0, 0x423e3e7c, 0xc4b5b571, 0xaa6666cc, 0xd8484890, 0x05030306, 0x01f6f6f7, 0x120e0e1c, 0xa36161c2, 0x5f35356a, 0xf95757ae, 0xd0b9b969, 0x91868617, 0x58c1c199, 0x271d1d3a, 0xb99e9e27, 0x38e1e1d9, 0x13f8f8eb, 0xb398982b, 0x33111122, 0xbb6969d2, 0x70d9d9a9, 0x898e8e07, 0xa7949433, 0xb69b9b2d, 0x221e1e3c, 0x92878715, 0x20e9e9c9, 0x49cece87, 0xff5555aa, 0x78282850, 0x7adfdfa5, 0x8f8c8c03, 0xf8a1a159, 0x80898909, 0x170d0d1a, 0xdabfbf65, 0x31e6e6d7, 0xc6424284, 0xb86868d0, 0xc3414182, 0xb0999929, 0x772d2d5a, 0x110f0f1e, 0xcbb0b07b, 0xfc5454a8, 0xd6bbbb6d, 0x3a16162c]);
  4426. var T2 = new Uint32Array([0x6363c6a5, 0x7c7cf884, 0x7777ee99, 0x7b7bf68d, 0xf2f2ff0d, 0x6b6bd6bd, 0x6f6fdeb1, 0xc5c59154, 0x30306050, 0x01010203, 0x6767cea9, 0x2b2b567d, 0xfefee719, 0xd7d7b562, 0xabab4de6, 0x7676ec9a, 0xcaca8f45, 0x82821f9d, 0xc9c98940, 0x7d7dfa87, 0xfafaef15, 0x5959b2eb, 0x47478ec9, 0xf0f0fb0b, 0xadad41ec, 0xd4d4b367, 0xa2a25ffd, 0xafaf45ea, 0x9c9c23bf, 0xa4a453f7, 0x7272e496, 0xc0c09b5b, 0xb7b775c2, 0xfdfde11c, 0x93933dae, 0x26264c6a, 0x36366c5a, 0x3f3f7e41, 0xf7f7f502, 0xcccc834f, 0x3434685c, 0xa5a551f4, 0xe5e5d134, 0xf1f1f908, 0x7171e293, 0xd8d8ab73, 0x31316253, 0x15152a3f, 0x0404080c, 0xc7c79552, 0x23234665, 0xc3c39d5e, 0x18183028, 0x969637a1, 0x05050a0f, 0x9a9a2fb5, 0x07070e09, 0x12122436, 0x80801b9b, 0xe2e2df3d, 0xebebcd26, 0x27274e69, 0xb2b27fcd, 0x7575ea9f, 0x0909121b, 0x83831d9e, 0x2c2c5874, 0x1a1a342e, 0x1b1b362d, 0x6e6edcb2, 0x5a5ab4ee, 0xa0a05bfb, 0x5252a4f6, 0x3b3b764d, 0xd6d6b761, 0xb3b37dce, 0x2929527b, 0xe3e3dd3e, 0x2f2f5e71, 0x84841397, 0x5353a6f5, 0xd1d1b968, 0x00000000, 0xededc12c, 0x20204060, 0xfcfce31f, 0xb1b179c8, 0x5b5bb6ed, 0x6a6ad4be, 0xcbcb8d46, 0xbebe67d9, 0x3939724b, 0x4a4a94de, 0x4c4c98d4, 0x5858b0e8, 0xcfcf854a, 0xd0d0bb6b, 0xefefc52a, 0xaaaa4fe5, 0xfbfbed16, 0x434386c5, 0x4d4d9ad7, 0x33336655, 0x85851194, 0x45458acf, 0xf9f9e910, 0x02020406, 0x7f7ffe81, 0x5050a0f0, 0x3c3c7844, 0x9f9f25ba, 0xa8a84be3, 0x5151a2f3, 0xa3a35dfe, 0x404080c0, 0x8f8f058a, 0x92923fad, 0x9d9d21bc, 0x38387048, 0xf5f5f104, 0xbcbc63df, 0xb6b677c1, 0xdadaaf75, 0x21214263, 0x10102030, 0xffffe51a, 0xf3f3fd0e, 0xd2d2bf6d, 0xcdcd814c, 0x0c0c1814, 0x13132635, 0xececc32f, 0x5f5fbee1, 0x979735a2, 0x444488cc, 0x17172e39, 0xc4c49357, 0xa7a755f2, 0x7e7efc82, 0x3d3d7a47, 0x6464c8ac, 0x5d5dbae7, 0x1919322b, 0x7373e695, 0x6060c0a0, 0x81811998, 0x4f4f9ed1, 0xdcdca37f, 0x22224466, 0x2a2a547e, 0x90903bab, 0x88880b83, 0x46468cca, 0xeeeec729, 0xb8b86bd3, 0x1414283c, 0xdedea779, 0x5e5ebce2, 0x0b0b161d, 0xdbdbad76, 0xe0e0db3b, 0x32326456, 0x3a3a744e, 0x0a0a141e, 0x494992db, 0x06060c0a, 0x2424486c, 0x5c5cb8e4, 0xc2c29f5d, 0xd3d3bd6e, 0xacac43ef, 0x6262c4a6, 0x919139a8, 0x959531a4, 0xe4e4d337, 0x7979f28b, 0xe7e7d532, 0xc8c88b43, 0x37376e59, 0x6d6ddab7, 0x8d8d018c, 0xd5d5b164, 0x4e4e9cd2, 0xa9a949e0, 0x6c6cd8b4, 0x5656acfa, 0xf4f4f307, 0xeaeacf25, 0x6565caaf, 0x7a7af48e, 0xaeae47e9, 0x08081018, 0xbaba6fd5, 0x7878f088, 0x25254a6f, 0x2e2e5c72, 0x1c1c3824, 0xa6a657f1, 0xb4b473c7, 0xc6c69751, 0xe8e8cb23, 0xdddda17c, 0x7474e89c, 0x1f1f3e21, 0x4b4b96dd, 0xbdbd61dc, 0x8b8b0d86, 0x8a8a0f85, 0x7070e090, 0x3e3e7c42, 0xb5b571c4, 0x6666ccaa, 0x484890d8, 0x03030605, 0xf6f6f701, 0x0e0e1c12, 0x6161c2a3, 0x35356a5f, 0x5757aef9, 0xb9b969d0, 0x86861791, 0xc1c19958, 0x1d1d3a27, 0x9e9e27b9, 0xe1e1d938, 0xf8f8eb13, 0x98982bb3, 0x11112233, 0x6969d2bb, 0xd9d9a970, 0x8e8e0789, 0x949433a7, 0x9b9b2db6, 0x1e1e3c22, 0x87871592, 0xe9e9c920, 0xcece8749, 0x5555aaff, 0x28285078, 0xdfdfa57a, 0x8c8c038f, 0xa1a159f8, 0x89890980, 0x0d0d1a17, 0xbfbf65da, 0xe6e6d731, 0x424284c6, 0x6868d0b8, 0x414182c3, 0x999929b0, 0x2d2d5a77, 0x0f0f1e11, 0xb0b07bcb, 0x5454a8fc, 0xbbbb6dd6, 0x16162c3a]);
  4427. var T3 = new Uint32Array([0x63c6a563, 0x7cf8847c, 0x77ee9977, 0x7bf68d7b, 0xf2ff0df2, 0x6bd6bd6b, 0x6fdeb16f, 0xc59154c5, 0x30605030, 0x01020301, 0x67cea967, 0x2b567d2b, 0xfee719fe, 0xd7b562d7, 0xab4de6ab, 0x76ec9a76, 0xca8f45ca, 0x821f9d82, 0xc98940c9, 0x7dfa877d, 0xfaef15fa, 0x59b2eb59, 0x478ec947, 0xf0fb0bf0, 0xad41ecad, 0xd4b367d4, 0xa25ffda2, 0xaf45eaaf, 0x9c23bf9c, 0xa453f7a4, 0x72e49672, 0xc09b5bc0, 0xb775c2b7, 0xfde11cfd, 0x933dae93, 0x264c6a26, 0x366c5a36, 0x3f7e413f, 0xf7f502f7, 0xcc834fcc, 0x34685c34, 0xa551f4a5, 0xe5d134e5, 0xf1f908f1, 0x71e29371, 0xd8ab73d8, 0x31625331, 0x152a3f15, 0x04080c04, 0xc79552c7, 0x23466523, 0xc39d5ec3, 0x18302818, 0x9637a196, 0x050a0f05, 0x9a2fb59a, 0x070e0907, 0x12243612, 0x801b9b80, 0xe2df3de2, 0xebcd26eb, 0x274e6927, 0xb27fcdb2, 0x75ea9f75, 0x09121b09, 0x831d9e83, 0x2c58742c, 0x1a342e1a, 0x1b362d1b, 0x6edcb26e, 0x5ab4ee5a, 0xa05bfba0, 0x52a4f652, 0x3b764d3b, 0xd6b761d6, 0xb37dceb3, 0x29527b29, 0xe3dd3ee3, 0x2f5e712f, 0x84139784, 0x53a6f553, 0xd1b968d1, 0x00000000, 0xedc12ced, 0x20406020, 0xfce31ffc, 0xb179c8b1, 0x5bb6ed5b, 0x6ad4be6a, 0xcb8d46cb, 0xbe67d9be, 0x39724b39, 0x4a94de4a, 0x4c98d44c, 0x58b0e858, 0xcf854acf, 0xd0bb6bd0, 0xefc52aef, 0xaa4fe5aa, 0xfbed16fb, 0x4386c543, 0x4d9ad74d, 0x33665533, 0x85119485, 0x458acf45, 0xf9e910f9, 0x02040602, 0x7ffe817f, 0x50a0f050, 0x3c78443c, 0x9f25ba9f, 0xa84be3a8, 0x51a2f351, 0xa35dfea3, 0x4080c040, 0x8f058a8f, 0x923fad92, 0x9d21bc9d, 0x38704838, 0xf5f104f5, 0xbc63dfbc, 0xb677c1b6, 0xdaaf75da, 0x21426321, 0x10203010, 0xffe51aff, 0xf3fd0ef3, 0xd2bf6dd2, 0xcd814ccd, 0x0c18140c, 0x13263513, 0xecc32fec, 0x5fbee15f, 0x9735a297, 0x4488cc44, 0x172e3917, 0xc49357c4, 0xa755f2a7, 0x7efc827e, 0x3d7a473d, 0x64c8ac64, 0x5dbae75d, 0x19322b19, 0x73e69573, 0x60c0a060, 0x81199881, 0x4f9ed14f, 0xdca37fdc, 0x22446622, 0x2a547e2a, 0x903bab90, 0x880b8388, 0x468cca46, 0xeec729ee, 0xb86bd3b8, 0x14283c14, 0xdea779de, 0x5ebce25e, 0x0b161d0b, 0xdbad76db, 0xe0db3be0, 0x32645632, 0x3a744e3a, 0x0a141e0a, 0x4992db49, 0x060c0a06, 0x24486c24, 0x5cb8e45c, 0xc29f5dc2, 0xd3bd6ed3, 0xac43efac, 0x62c4a662, 0x9139a891, 0x9531a495, 0xe4d337e4, 0x79f28b79, 0xe7d532e7, 0xc88b43c8, 0x376e5937, 0x6ddab76d, 0x8d018c8d, 0xd5b164d5, 0x4e9cd24e, 0xa949e0a9, 0x6cd8b46c, 0x56acfa56, 0xf4f307f4, 0xeacf25ea, 0x65caaf65, 0x7af48e7a, 0xae47e9ae, 0x08101808, 0xba6fd5ba, 0x78f08878, 0x254a6f25, 0x2e5c722e, 0x1c38241c, 0xa657f1a6, 0xb473c7b4, 0xc69751c6, 0xe8cb23e8, 0xdda17cdd, 0x74e89c74, 0x1f3e211f, 0x4b96dd4b, 0xbd61dcbd, 0x8b0d868b, 0x8a0f858a, 0x70e09070, 0x3e7c423e, 0xb571c4b5, 0x66ccaa66, 0x4890d848, 0x03060503, 0xf6f701f6, 0x0e1c120e, 0x61c2a361, 0x356a5f35, 0x57aef957, 0xb969d0b9, 0x86179186, 0xc19958c1, 0x1d3a271d, 0x9e27b99e, 0xe1d938e1, 0xf8eb13f8, 0x982bb398, 0x11223311, 0x69d2bb69, 0xd9a970d9, 0x8e07898e, 0x9433a794, 0x9b2db69b, 0x1e3c221e, 0x87159287, 0xe9c920e9, 0xce8749ce, 0x55aaff55, 0x28507828, 0xdfa57adf, 0x8c038f8c, 0xa159f8a1, 0x89098089, 0x0d1a170d, 0xbf65dabf, 0xe6d731e6, 0x4284c642, 0x68d0b868, 0x4182c341, 0x9929b099, 0x2d5a772d, 0x0f1e110f, 0xb07bcbb0, 0x54a8fc54, 0xbb6dd6bb, 0x162c3a16]);
  4428. var T4 = new Uint32Array([0xc6a56363, 0xf8847c7c, 0xee997777, 0xf68d7b7b, 0xff0df2f2, 0xd6bd6b6b, 0xdeb16f6f, 0x9154c5c5, 0x60503030, 0x02030101, 0xcea96767, 0x567d2b2b, 0xe719fefe, 0xb562d7d7, 0x4de6abab, 0xec9a7676, 0x8f45caca, 0x1f9d8282, 0x8940c9c9, 0xfa877d7d, 0xef15fafa, 0xb2eb5959, 0x8ec94747, 0xfb0bf0f0, 0x41ecadad, 0xb367d4d4, 0x5ffda2a2, 0x45eaafaf, 0x23bf9c9c, 0x53f7a4a4, 0xe4967272, 0x9b5bc0c0, 0x75c2b7b7, 0xe11cfdfd, 0x3dae9393, 0x4c6a2626, 0x6c5a3636, 0x7e413f3f, 0xf502f7f7, 0x834fcccc, 0x685c3434, 0x51f4a5a5, 0xd134e5e5, 0xf908f1f1, 0xe2937171, 0xab73d8d8, 0x62533131, 0x2a3f1515, 0x080c0404, 0x9552c7c7, 0x46652323, 0x9d5ec3c3, 0x30281818, 0x37a19696, 0x0a0f0505, 0x2fb59a9a, 0x0e090707, 0x24361212, 0x1b9b8080, 0xdf3de2e2, 0xcd26ebeb, 0x4e692727, 0x7fcdb2b2, 0xea9f7575, 0x121b0909, 0x1d9e8383, 0x58742c2c, 0x342e1a1a, 0x362d1b1b, 0xdcb26e6e, 0xb4ee5a5a, 0x5bfba0a0, 0xa4f65252, 0x764d3b3b, 0xb761d6d6, 0x7dceb3b3, 0x527b2929, 0xdd3ee3e3, 0x5e712f2f, 0x13978484, 0xa6f55353, 0xb968d1d1, 0x00000000, 0xc12ceded, 0x40602020, 0xe31ffcfc, 0x79c8b1b1, 0xb6ed5b5b, 0xd4be6a6a, 0x8d46cbcb, 0x67d9bebe, 0x724b3939, 0x94de4a4a, 0x98d44c4c, 0xb0e85858, 0x854acfcf, 0xbb6bd0d0, 0xc52aefef, 0x4fe5aaaa, 0xed16fbfb, 0x86c54343, 0x9ad74d4d, 0x66553333, 0x11948585, 0x8acf4545, 0xe910f9f9, 0x04060202, 0xfe817f7f, 0xa0f05050, 0x78443c3c, 0x25ba9f9f, 0x4be3a8a8, 0xa2f35151, 0x5dfea3a3, 0x80c04040, 0x058a8f8f, 0x3fad9292, 0x21bc9d9d, 0x70483838, 0xf104f5f5, 0x63dfbcbc, 0x77c1b6b6, 0xaf75dada, 0x42632121, 0x20301010, 0xe51affff, 0xfd0ef3f3, 0xbf6dd2d2, 0x814ccdcd, 0x18140c0c, 0x26351313, 0xc32fecec, 0xbee15f5f, 0x35a29797, 0x88cc4444, 0x2e391717, 0x9357c4c4, 0x55f2a7a7, 0xfc827e7e, 0x7a473d3d, 0xc8ac6464, 0xbae75d5d, 0x322b1919, 0xe6957373, 0xc0a06060, 0x19988181, 0x9ed14f4f, 0xa37fdcdc, 0x44662222, 0x547e2a2a, 0x3bab9090, 0x0b838888, 0x8cca4646, 0xc729eeee, 0x6bd3b8b8, 0x283c1414, 0xa779dede, 0xbce25e5e, 0x161d0b0b, 0xad76dbdb, 0xdb3be0e0, 0x64563232, 0x744e3a3a, 0x141e0a0a, 0x92db4949, 0x0c0a0606, 0x486c2424, 0xb8e45c5c, 0x9f5dc2c2, 0xbd6ed3d3, 0x43efacac, 0xc4a66262, 0x39a89191, 0x31a49595, 0xd337e4e4, 0xf28b7979, 0xd532e7e7, 0x8b43c8c8, 0x6e593737, 0xdab76d6d, 0x018c8d8d, 0xb164d5d5, 0x9cd24e4e, 0x49e0a9a9, 0xd8b46c6c, 0xacfa5656, 0xf307f4f4, 0xcf25eaea, 0xcaaf6565, 0xf48e7a7a, 0x47e9aeae, 0x10180808, 0x6fd5baba, 0xf0887878, 0x4a6f2525, 0x5c722e2e, 0x38241c1c, 0x57f1a6a6, 0x73c7b4b4, 0x9751c6c6, 0xcb23e8e8, 0xa17cdddd, 0xe89c7474, 0x3e211f1f, 0x96dd4b4b, 0x61dcbdbd, 0x0d868b8b, 0x0f858a8a, 0xe0907070, 0x7c423e3e, 0x71c4b5b5, 0xccaa6666, 0x90d84848, 0x06050303, 0xf701f6f6, 0x1c120e0e, 0xc2a36161, 0x6a5f3535, 0xaef95757, 0x69d0b9b9, 0x17918686, 0x9958c1c1, 0x3a271d1d, 0x27b99e9e, 0xd938e1e1, 0xeb13f8f8, 0x2bb39898, 0x22331111, 0xd2bb6969, 0xa970d9d9, 0x07898e8e, 0x33a79494, 0x2db69b9b, 0x3c221e1e, 0x15928787, 0xc920e9e9, 0x8749cece, 0xaaff5555, 0x50782828, 0xa57adfdf, 0x038f8c8c, 0x59f8a1a1, 0x09808989, 0x1a170d0d, 0x65dabfbf, 0xd731e6e6, 0x84c64242, 0xd0b86868, 0x82c34141, 0x29b09999, 0x5a772d2d, 0x1e110f0f, 0x7bcbb0b0, 0xa8fc5454, 0x6dd6bbbb, 0x2c3a1616]);
  4429. function B0(x) {
  4430. return x & 255;
  4431. }
  4432. function B1(x) {
  4433. return x >> 8 & 255;
  4434. }
  4435. function B2(x) {
  4436. return x >> 16 & 255;
  4437. }
  4438. function B3(x) {
  4439. return x >> 24 & 255;
  4440. }
  4441. function F1(x0, x1, x2, x3) {
  4442. return B1(T1[x0 & 255]) | B1(T1[x1 >> 8 & 255]) << 8 | B1(T1[x2 >> 16 & 255]) << 16 | B1(T1[x3 >>> 24]) << 24;
  4443. }
  4444. function packBytes(octets) {
  4445. var i, j;
  4446. var len = octets.length;
  4447. var b = new Array(len / 4);
  4448. if (!octets || len % 4) {
  4449. return;
  4450. }
  4451. for (i = 0, j = 0; j < len; j += 4) {
  4452. b[i++] = octets[j] | octets[j + 1] << 8 | octets[j + 2] << 16 | octets[j + 3] << 24;
  4453. }
  4454. return b;
  4455. }
  4456. function unpackBytes(packed) {
  4457. var j;
  4458. var i = 0,
  4459. l = packed.length;
  4460. var r = new Array(l * 4);
  4461. for (j = 0; j < l; j++) {
  4462. r[i++] = B0(packed[j]);
  4463. r[i++] = B1(packed[j]);
  4464. r[i++] = B2(packed[j]);
  4465. r[i++] = B3(packed[j]);
  4466. }
  4467. return r;
  4468. }
  4469. // ------------------------------------------------
  4470. var maxkc = 8;
  4471. var maxrk = 14;
  4472. function keyExpansion(key) {
  4473. var kc, i, j, r, t;
  4474. var rounds;
  4475. var keySched = new Array(maxrk + 1);
  4476. var keylen = key.length;
  4477. var k = new Array(maxkc);
  4478. var tk = new Array(maxkc);
  4479. var rconpointer = 0;
  4480. if (keylen === 16) {
  4481. rounds = 10;
  4482. kc = 4;
  4483. } else if (keylen === 24) {
  4484. rounds = 12;
  4485. kc = 6;
  4486. } else if (keylen === 32) {
  4487. rounds = 14;
  4488. kc = 8;
  4489. } else {
  4490. throw new Error('Invalid key-length for AES key:' + keylen);
  4491. }
  4492. for (i = 0; i < maxrk + 1; i++) {
  4493. keySched[i] = new Uint32Array(4);
  4494. }
  4495. for (i = 0, j = 0; j < keylen; j++, i += 4) {
  4496. k[j] = key[i] | key[i + 1] << 8 | key[i + 2] << 16 | key[i + 3] << 24;
  4497. }
  4498. for (j = kc - 1; j >= 0; j--) {
  4499. tk[j] = k[j];
  4500. }
  4501. r = 0;
  4502. t = 0;
  4503. for (j = 0; j < kc && r < rounds + 1;) {
  4504. for (; j < kc && t < 4; j++, t++) {
  4505. keySched[r][t] = tk[j];
  4506. }
  4507. if (t === 4) {
  4508. r++;
  4509. t = 0;
  4510. }
  4511. }
  4512. while (r < rounds + 1) {
  4513. var temp = tk[kc - 1];
  4514. tk[0] ^= S[B1(temp)] | S[B2(temp)] << 8 | S[B3(temp)] << 16 | S[B0(temp)] << 24;
  4515. tk[0] ^= Rcon[rconpointer++];
  4516. if (kc !== 8) {
  4517. for (j = 1; j < kc; j++) {
  4518. tk[j] ^= tk[j - 1];
  4519. }
  4520. } else {
  4521. for (j = 1; j < kc / 2; j++) {
  4522. tk[j] ^= tk[j - 1];
  4523. }
  4524. temp = tk[kc / 2 - 1];
  4525. tk[kc / 2] ^= S[B0(temp)] | S[B1(temp)] << 8 | S[B2(temp)] << 16 | S[B3(temp)] << 24;
  4526. for (j = kc / 2 + 1; j < kc; j++) {
  4527. tk[j] ^= tk[j - 1];
  4528. }
  4529. }
  4530. for (j = 0; j < kc && r < rounds + 1;) {
  4531. for (; j < kc && t < 4; j++, t++) {
  4532. keySched[r][t] = tk[j];
  4533. }
  4534. if (t === 4) {
  4535. r++;
  4536. t = 0;
  4537. }
  4538. }
  4539. }
  4540. return {
  4541. rounds: rounds,
  4542. rk: keySched
  4543. };
  4544. }
  4545. function AESencrypt(block, ctx, t) {
  4546. var r, rounds, b;
  4547. b = packBytes(block);
  4548. rounds = ctx.rounds;
  4549. for (r = 0; r < rounds - 1; r++) {
  4550. t[0] = b[0] ^ ctx.rk[r][0];
  4551. t[1] = b[1] ^ ctx.rk[r][1];
  4552. t[2] = b[2] ^ ctx.rk[r][2];
  4553. t[3] = b[3] ^ ctx.rk[r][3];
  4554. b[0] = T1[t[0] & 255] ^ T2[t[1] >> 8 & 255] ^ T3[t[2] >> 16 & 255] ^ T4[t[3] >>> 24];
  4555. b[1] = T1[t[1] & 255] ^ T2[t[2] >> 8 & 255] ^ T3[t[3] >> 16 & 255] ^ T4[t[0] >>> 24];
  4556. b[2] = T1[t[2] & 255] ^ T2[t[3] >> 8 & 255] ^ T3[t[0] >> 16 & 255] ^ T4[t[1] >>> 24];
  4557. b[3] = T1[t[3] & 255] ^ T2[t[0] >> 8 & 255] ^ T3[t[1] >> 16 & 255] ^ T4[t[2] >>> 24];
  4558. }
  4559. // last round is special
  4560. r = rounds - 1;
  4561. t[0] = b[0] ^ ctx.rk[r][0];
  4562. t[1] = b[1] ^ ctx.rk[r][1];
  4563. t[2] = b[2] ^ ctx.rk[r][2];
  4564. t[3] = b[3] ^ ctx.rk[r][3];
  4565. b[0] = F1(t[0], t[1], t[2], t[3]) ^ ctx.rk[rounds][0];
  4566. b[1] = F1(t[1], t[2], t[3], t[0]) ^ ctx.rk[rounds][1];
  4567. b[2] = F1(t[2], t[3], t[0], t[1]) ^ ctx.rk[rounds][2];
  4568. b[3] = F1(t[3], t[0], t[1], t[2]) ^ ctx.rk[rounds][3];
  4569. return unpackBytes(b);
  4570. }
  4571. function makeClass(length) {
  4572. var c = function c(key) {
  4573. this.key = keyExpansion(key);
  4574. this._temp = new Uint32Array(this.blockSize / 4);
  4575. this.encrypt = function (block) {
  4576. return AESencrypt(block, this.key, this._temp);
  4577. };
  4578. };
  4579. c.blockSize = c.prototype.blockSize = 16;
  4580. c.keySize = c.prototype.keySize = length / 8;
  4581. return c;
  4582. }
  4583. exports.default = {
  4584. 128: makeClass(128),
  4585. 192: makeClass(192),
  4586. 256: makeClass(256)
  4587. };
  4588. },{}],13:[function(_dereq_,module,exports){
  4589. /* Modified by Recurity Labs GmbH
  4590. *
  4591. * Originally written by nklein software (nklein.com)
  4592. */
  4593. /**
  4594. * @module crypto/cipher/blowfish
  4595. */
  4596. 'use strict';
  4597. /*
  4598. * Javascript implementation based on Bruce Schneier's reference implementation.
  4599. *
  4600. *
  4601. * The constructor doesn't do much of anything. It's just here
  4602. * so we can start defining properties and methods and such.
  4603. */
  4604. Object.defineProperty(exports, "__esModule", {
  4605. value: true
  4606. });
  4607. exports.default = BF;
  4608. function Blowfish() {}
  4609. /*
  4610. * Declare the block size so that protocols know what size
  4611. * Initialization Vector (IV) they will need.
  4612. */
  4613. Blowfish.prototype.BLOCKSIZE = 8;
  4614. /*
  4615. * These are the default SBOXES.
  4616. */
  4617. Blowfish.prototype.SBOXES = [[0xd1310ba6, 0x98dfb5ac, 0x2ffd72db, 0xd01adfb7, 0xb8e1afed, 0x6a267e96, 0xba7c9045, 0xf12c7f99, 0x24a19947, 0xb3916cf7, 0x0801f2e2, 0x858efc16, 0x636920d8, 0x71574e69, 0xa458fea3, 0xf4933d7e, 0x0d95748f, 0x728eb658, 0x718bcd58, 0x82154aee, 0x7b54a41d, 0xc25a59b5, 0x9c30d539, 0x2af26013, 0xc5d1b023, 0x286085f0, 0xca417918, 0xb8db38ef, 0x8e79dcb0, 0x603a180e, 0x6c9e0e8b, 0xb01e8a3e, 0xd71577c1, 0xbd314b27, 0x78af2fda, 0x55605c60, 0xe65525f3, 0xaa55ab94, 0x57489862, 0x63e81440, 0x55ca396a, 0x2aab10b6, 0xb4cc5c34, 0x1141e8ce, 0xa15486af, 0x7c72e993, 0xb3ee1411, 0x636fbc2a, 0x2ba9c55d, 0x741831f6, 0xce5c3e16, 0x9b87931e, 0xafd6ba33, 0x6c24cf5c, 0x7a325381, 0x28958677, 0x3b8f4898, 0x6b4bb9af, 0xc4bfe81b, 0x66282193, 0x61d809cc, 0xfb21a991, 0x487cac60, 0x5dec8032, 0xef845d5d, 0xe98575b1, 0xdc262302, 0xeb651b88, 0x23893e81, 0xd396acc5, 0x0f6d6ff3, 0x83f44239, 0x2e0b4482, 0xa4842004, 0x69c8f04a, 0x9e1f9b5e, 0x21c66842, 0xf6e96c9a, 0x670c9c61, 0xabd388f0, 0x6a51a0d2, 0xd8542f68, 0x960fa728, 0xab5133a3, 0x6eef0b6c, 0x137a3be4, 0xba3bf050, 0x7efb2a98, 0xa1f1651d, 0x39af0176, 0x66ca593e, 0x82430e88, 0x8cee8619, 0x456f9fb4, 0x7d84a5c3, 0x3b8b5ebe, 0xe06f75d8, 0x85c12073, 0x401a449f, 0x56c16aa6, 0x4ed3aa62, 0x363f7706, 0x1bfedf72, 0x429b023d, 0x37d0d724, 0xd00a1248, 0xdb0fead3, 0x49f1c09b, 0x075372c9, 0x80991b7b, 0x25d479d8, 0xf6e8def7, 0xe3fe501a, 0xb6794c3b, 0x976ce0bd, 0x04c006ba, 0xc1a94fb6, 0x409f60c4, 0x5e5c9ec2, 0x196a2463, 0x68fb6faf, 0x3e6c53b5, 0x1339b2eb, 0x3b52ec6f, 0x6dfc511f, 0x9b30952c, 0xcc814544, 0xaf5ebd09, 0xbee3d004, 0xde334afd, 0x660f2807, 0x192e4bb3, 0xc0cba857, 0x45c8740f, 0xd20b5f39, 0xb9d3fbdb, 0x5579c0bd, 0x1a60320a, 0xd6a100c6, 0x402c7279, 0x679f25fe, 0xfb1fa3cc, 0x8ea5e9f8, 0xdb3222f8, 0x3c7516df, 0xfd616b15, 0x2f501ec8, 0xad0552ab, 0x323db5fa, 0xfd238760, 0x53317b48, 0x3e00df82, 0x9e5c57bb, 0xca6f8ca0, 0x1a87562e, 0xdf1769db, 0xd542a8f6, 0x287effc3, 0xac6732c6, 0x8c4f5573, 0x695b27b0, 0xbbca58c8, 0xe1ffa35d, 0xb8f011a0, 0x10fa3d98, 0xfd2183b8, 0x4afcb56c, 0x2dd1d35b, 0x9a53e479, 0xb6f84565, 0xd28e49bc, 0x4bfb9790, 0xe1ddf2da, 0xa4cb7e33, 0x62fb1341, 0xcee4c6e8, 0xef20cada, 0x36774c01, 0xd07e9efe, 0x2bf11fb4, 0x95dbda4d, 0xae909198, 0xeaad8e71, 0x6b93d5a0, 0xd08ed1d0, 0xafc725e0, 0x8e3c5b2f, 0x8e7594b7, 0x8ff6e2fb, 0xf2122b64, 0x8888b812, 0x900df01c, 0x4fad5ea0, 0x688fc31c, 0xd1cff191, 0xb3a8c1ad, 0x2f2f2218, 0xbe0e1777, 0xea752dfe, 0x8b021fa1, 0xe5a0cc0f, 0xb56f74e8, 0x18acf3d6, 0xce89e299, 0xb4a84fe0, 0xfd13e0b7, 0x7cc43b81, 0xd2ada8d9, 0x165fa266, 0x80957705, 0x93cc7314, 0x211a1477, 0xe6ad2065, 0x77b5fa86, 0xc75442f5, 0xfb9d35cf, 0xebcdaf0c, 0x7b3e89a0, 0xd6411bd3, 0xae1e7e49, 0x00250e2d, 0x2071b35e, 0x226800bb, 0x57b8e0af, 0x2464369b, 0xf009b91e, 0x5563911d, 0x59dfa6aa, 0x78c14389, 0xd95a537f, 0x207d5ba2, 0x02e5b9c5, 0x83260376, 0x6295cfa9, 0x11c81968, 0x4e734a41, 0xb3472dca, 0x7b14a94a, 0x1b510052, 0x9a532915, 0xd60f573f, 0xbc9bc6e4, 0x2b60a476, 0x81e67400, 0x08ba6fb5, 0x571be91f, 0xf296ec6b, 0x2a0dd915, 0xb6636521, 0xe7b9f9b6, 0xff34052e, 0xc5855664, 0x53b02d5d, 0xa99f8fa1, 0x08ba4799, 0x6e85076a], [0x4b7a70e9, 0xb5b32944, 0xdb75092e, 0xc4192623, 0xad6ea6b0, 0x49a7df7d, 0x9cee60b8, 0x8fedb266, 0xecaa8c71, 0x699a17ff, 0x5664526c, 0xc2b19ee1, 0x193602a5, 0x75094c29, 0xa0591340, 0xe4183a3e, 0x3f54989a, 0x5b429d65, 0x6b8fe4d6, 0x99f73fd6, 0xa1d29c07, 0xefe830f5, 0x4d2d38e6, 0xf0255dc1, 0x4cdd2086, 0x8470eb26, 0x6382e9c6, 0x021ecc5e, 0x09686b3f, 0x3ebaefc9, 0x3c971814, 0x6b6a70a1, 0x687f3584, 0x52a0e286, 0xb79c5305, 0xaa500737, 0x3e07841c, 0x7fdeae5c, 0x8e7d44ec, 0x5716f2b8, 0xb03ada37, 0xf0500c0d, 0xf01c1f04, 0x0200b3ff, 0xae0cf51a, 0x3cb574b2, 0x25837a58, 0xdc0921bd, 0xd19113f9, 0x7ca92ff6, 0x94324773, 0x22f54701, 0x3ae5e581, 0x37c2dadc, 0xc8b57634, 0x9af3dda7, 0xa9446146, 0x0fd0030e, 0xecc8c73e, 0xa4751e41, 0xe238cd99, 0x3bea0e2f, 0x3280bba1, 0x183eb331, 0x4e548b38, 0x4f6db908, 0x6f420d03, 0xf60a04bf, 0x2cb81290, 0x24977c79, 0x5679b072, 0xbcaf89af, 0xde9a771f, 0xd9930810, 0xb38bae12, 0xdccf3f2e, 0x5512721f, 0x2e6b7124, 0x501adde6, 0x9f84cd87, 0x7a584718, 0x7408da17, 0xbc9f9abc, 0xe94b7d8c, 0xec7aec3a, 0xdb851dfa, 0x63094366, 0xc464c3d2, 0xef1c1847, 0x3215d908, 0xdd433b37, 0x24c2ba16, 0x12a14d43, 0x2a65c451, 0x50940002, 0x133ae4dd, 0x71dff89e, 0x10314e55, 0x81ac77d6, 0x5f11199b, 0x043556f1, 0xd7a3c76b, 0x3c11183b, 0x5924a509, 0xf28fe6ed, 0x97f1fbfa, 0x9ebabf2c, 0x1e153c6e, 0x86e34570, 0xeae96fb1, 0x860e5e0a, 0x5a3e2ab3, 0x771fe71c, 0x4e3d06fa, 0x2965dcb9, 0x99e71d0f, 0x803e89d6, 0x5266c825, 0x2e4cc978, 0x9c10b36a, 0xc6150eba, 0x94e2ea78, 0xa5fc3c53, 0x1e0a2df4, 0xf2f74ea7, 0x361d2b3d, 0x1939260f, 0x19c27960, 0x5223a708, 0xf71312b6, 0xebadfe6e, 0xeac31f66, 0xe3bc4595, 0xa67bc883, 0xb17f37d1, 0x018cff28, 0xc332ddef, 0xbe6c5aa5, 0x65582185, 0x68ab9802, 0xeecea50f, 0xdb2f953b, 0x2aef7dad, 0x5b6e2f84, 0x1521b628, 0x29076170, 0xecdd4775, 0x619f1510, 0x13cca830, 0xeb61bd96, 0x0334fe1e, 0xaa0363cf, 0xb5735c90, 0x4c70a239, 0xd59e9e0b, 0xcbaade14, 0xeecc86bc, 0x60622ca7, 0x9cab5cab, 0xb2f3846e, 0x648b1eaf, 0x19bdf0ca, 0xa02369b9, 0x655abb50, 0x40685a32, 0x3c2ab4b3, 0x319ee9d5, 0xc021b8f7, 0x9b540b19, 0x875fa099, 0x95f7997e, 0x623d7da8, 0xf837889a, 0x97e32d77, 0x11ed935f, 0x16681281, 0x0e358829, 0xc7e61fd6, 0x96dedfa1, 0x7858ba99, 0x57f584a5, 0x1b227263, 0x9b83c3ff, 0x1ac24696, 0xcdb30aeb, 0x532e3054, 0x8fd948e4, 0x6dbc3128, 0x58ebf2ef, 0x34c6ffea, 0xfe28ed61, 0xee7c3c73, 0x5d4a14d9, 0xe864b7e3, 0x42105d14, 0x203e13e0, 0x45eee2b6, 0xa3aaabea, 0xdb6c4f15, 0xfacb4fd0, 0xc742f442, 0xef6abbb5, 0x654f3b1d, 0x41cd2105, 0xd81e799e, 0x86854dc7, 0xe44b476a, 0x3d816250, 0xcf62a1f2, 0x5b8d2646, 0xfc8883a0, 0xc1c7b6a3, 0x7f1524c3, 0x69cb7492, 0x47848a0b, 0x5692b285, 0x095bbf00, 0xad19489d, 0x1462b174, 0x23820e00, 0x58428d2a, 0x0c55f5ea, 0x1dadf43e, 0x233f7061, 0x3372f092, 0x8d937e41, 0xd65fecf1, 0x6c223bdb, 0x7cde3759, 0xcbee7460, 0x4085f2a7, 0xce77326e, 0xa6078084, 0x19f8509e, 0xe8efd855, 0x61d99735, 0xa969a7aa, 0xc50c06c2, 0x5a04abfc, 0x800bcadc, 0x9e447a2e, 0xc3453484, 0xfdd56705, 0x0e1e9ec9, 0xdb73dbd3, 0x105588cd, 0x675fda79, 0xe3674340, 0xc5c43465, 0x713e38d8, 0x3d28f89e, 0xf16dff20, 0x153e21e7, 0x8fb03d4a, 0xe6e39f2b, 0xdb83adf7], [0xe93d5a68, 0x948140f7, 0xf64c261c, 0x94692934, 0x411520f7, 0x7602d4f7, 0xbcf46b2e, 0xd4a20068, 0xd4082471, 0x3320f46a, 0x43b7d4b7, 0x500061af, 0x1e39f62e, 0x97244546, 0x14214f74, 0xbf8b8840, 0x4d95fc1d, 0x96b591af, 0x70f4ddd3, 0x66a02f45, 0xbfbc09ec, 0x03bd9785, 0x7fac6dd0, 0x31cb8504, 0x96eb27b3, 0x55fd3941, 0xda2547e6, 0xabca0a9a, 0x28507825, 0x530429f4, 0x0a2c86da, 0xe9b66dfb, 0x68dc1462, 0xd7486900, 0x680ec0a4, 0x27a18dee, 0x4f3ffea2, 0xe887ad8c, 0xb58ce006, 0x7af4d6b6, 0xaace1e7c, 0xd3375fec, 0xce78a399, 0x406b2a42, 0x20fe9e35, 0xd9f385b9, 0xee39d7ab, 0x3b124e8b, 0x1dc9faf7, 0x4b6d1856, 0x26a36631, 0xeae397b2, 0x3a6efa74, 0xdd5b4332, 0x6841e7f7, 0xca7820fb, 0xfb0af54e, 0xd8feb397, 0x454056ac, 0xba489527, 0x55533a3a, 0x20838d87, 0xfe6ba9b7, 0xd096954b, 0x55a867bc, 0xa1159a58, 0xcca92963, 0x99e1db33, 0xa62a4a56, 0x3f3125f9, 0x5ef47e1c, 0x9029317c, 0xfdf8e802, 0x04272f70, 0x80bb155c, 0x05282ce3, 0x95c11548, 0xe4c66d22, 0x48c1133f, 0xc70f86dc, 0x07f9c9ee, 0x41041f0f, 0x404779a4, 0x5d886e17, 0x325f51eb, 0xd59bc0d1, 0xf2bcc18f, 0x41113564, 0x257b7834, 0x602a9c60, 0xdff8e8a3, 0x1f636c1b, 0x0e12b4c2, 0x02e1329e, 0xaf664fd1, 0xcad18115, 0x6b2395e0, 0x333e92e1, 0x3b240b62, 0xeebeb922, 0x85b2a20e, 0xe6ba0d99, 0xde720c8c, 0x2da2f728, 0xd0127845, 0x95b794fd, 0x647d0862, 0xe7ccf5f0, 0x5449a36f, 0x877d48fa, 0xc39dfd27, 0xf33e8d1e, 0x0a476341, 0x992eff74, 0x3a6f6eab, 0xf4f8fd37, 0xa812dc60, 0xa1ebddf8, 0x991be14c, 0xdb6e6b0d, 0xc67b5510, 0x6d672c37, 0x2765d43b, 0xdcd0e804, 0xf1290dc7, 0xcc00ffa3, 0xb5390f92, 0x690fed0b, 0x667b9ffb, 0xcedb7d9c, 0xa091cf0b, 0xd9155ea3, 0xbb132f88, 0x515bad24, 0x7b9479bf, 0x763bd6eb, 0x37392eb3, 0xcc115979, 0x8026e297, 0xf42e312d, 0x6842ada7, 0xc66a2b3b, 0x12754ccc, 0x782ef11c, 0x6a124237, 0xb79251e7, 0x06a1bbe6, 0x4bfb6350, 0x1a6b1018, 0x11caedfa, 0x3d25bdd8, 0xe2e1c3c9, 0x44421659, 0x0a121386, 0xd90cec6e, 0xd5abea2a, 0x64af674e, 0xda86a85f, 0xbebfe988, 0x64e4c3fe, 0x9dbc8057, 0xf0f7c086, 0x60787bf8, 0x6003604d, 0xd1fd8346, 0xf6381fb0, 0x7745ae04, 0xd736fccc, 0x83426b33, 0xf01eab71, 0xb0804187, 0x3c005e5f, 0x77a057be, 0xbde8ae24, 0x55464299, 0xbf582e61, 0x4e58f48f, 0xf2ddfda2, 0xf474ef38, 0x8789bdc2, 0x5366f9c3, 0xc8b38e74, 0xb475f255, 0x46fcd9b9, 0x7aeb2661, 0x8b1ddf84, 0x846a0e79, 0x915f95e2, 0x466e598e, 0x20b45770, 0x8cd55591, 0xc902de4c, 0xb90bace1, 0xbb8205d0, 0x11a86248, 0x7574a99e, 0xb77f19b6, 0xe0a9dc09, 0x662d09a1, 0xc4324633, 0xe85a1f02, 0x09f0be8c, 0x4a99a025, 0x1d6efe10, 0x1ab93d1d, 0x0ba5a4df, 0xa186f20f, 0x2868f169, 0xdcb7da83, 0x573906fe, 0xa1e2ce9b, 0x4fcd7f52, 0x50115e01, 0xa70683fa, 0xa002b5c4, 0x0de6d027, 0x9af88c27, 0x773f8641, 0xc3604c06, 0x61a806b5, 0xf0177a28, 0xc0f586e0, 0x006058aa, 0x30dc7d62, 0x11e69ed7, 0x2338ea63, 0x53c2dd94, 0xc2c21634, 0xbbcbee56, 0x90bcb6de, 0xebfc7da1, 0xce591d76, 0x6f05e409, 0x4b7c0188, 0x39720a3d, 0x7c927c24, 0x86e3725f, 0x724d9db9, 0x1ac15bb4, 0xd39eb8fc, 0xed545578, 0x08fca5b5, 0xd83d7cd3, 0x4dad0fc4, 0x1e50ef5e, 0xb161e6f8, 0xa28514d9, 0x6c51133c, 0x6fd5c7e7, 0x56e14ec4, 0x362abfce, 0xddc6c837, 0xd79a3234, 0x92638212, 0x670efa8e, 0x406000e0], [0x3a39ce37, 0xd3faf5cf, 0xabc27737, 0x5ac52d1b, 0x5cb0679e, 0x4fa33742, 0xd3822740, 0x99bc9bbe, 0xd5118e9d, 0xbf0f7315, 0xd62d1c7e, 0xc700c47b, 0xb78c1b6b, 0x21a19045, 0xb26eb1be, 0x6a366eb4, 0x5748ab2f, 0xbc946e79, 0xc6a376d2, 0x6549c2c8, 0x530ff8ee, 0x468dde7d, 0xd5730a1d, 0x4cd04dc6, 0x2939bbdb, 0xa9ba4650, 0xac9526e8, 0xbe5ee304, 0xa1fad5f0, 0x6a2d519a, 0x63ef8ce2, 0x9a86ee22, 0xc089c2b8, 0x43242ef6, 0xa51e03aa, 0x9cf2d0a4, 0x83c061ba, 0x9be96a4d, 0x8fe51550, 0xba645bd6, 0x2826a2f9, 0xa73a3ae1, 0x4ba99586, 0xef5562e9, 0xc72fefd3, 0xf752f7da, 0x3f046f69, 0x77fa0a59, 0x80e4a915, 0x87b08601, 0x9b09e6ad, 0x3b3ee593, 0xe990fd5a, 0x9e34d797, 0x2cf0b7d9, 0x022b8b51, 0x96d5ac3a, 0x017da67d, 0xd1cf3ed6, 0x7c7d2d28, 0x1f9f25cf, 0xadf2b89b, 0x5ad6b472, 0x5a88f54c, 0xe029ac71, 0xe019a5e6, 0x47b0acfd, 0xed93fa9b, 0xe8d3c48d, 0x283b57cc, 0xf8d56629, 0x79132e28, 0x785f0191, 0xed756055, 0xf7960e44, 0xe3d35e8c, 0x15056dd4, 0x88f46dba, 0x03a16125, 0x0564f0bd, 0xc3eb9e15, 0x3c9057a2, 0x97271aec, 0xa93a072a, 0x1b3f6d9b, 0x1e6321f5, 0xf59c66fb, 0x26dcf319, 0x7533d928, 0xb155fdf5, 0x03563482, 0x8aba3cbb, 0x28517711, 0xc20ad9f8, 0xabcc5167, 0xccad925f, 0x4de81751, 0x3830dc8e, 0x379d5862, 0x9320f991, 0xea7a90c2, 0xfb3e7bce, 0x5121ce64, 0x774fbe32, 0xa8b6e37e, 0xc3293d46, 0x48de5369, 0x6413e680, 0xa2ae0810, 0xdd6db224, 0x69852dfd, 0x09072166, 0xb39a460a, 0x6445c0dd, 0x586cdecf, 0x1c20c8ae, 0x5bbef7dd, 0x1b588d40, 0xccd2017f, 0x6bb4e3bb, 0xdda26a7e, 0x3a59ff45, 0x3e350a44, 0xbcb4cdd5, 0x72eacea8, 0xfa6484bb, 0x8d6612ae, 0xbf3c6f47, 0xd29be463, 0x542f5d9e, 0xaec2771b, 0xf64e6370, 0x740e0d8d, 0xe75b1357, 0xf8721671, 0xaf537d5d, 0x4040cb08, 0x4eb4e2cc, 0x34d2466a, 0x0115af84, 0xe1b00428, 0x95983a1d, 0x06b89fb4, 0xce6ea048, 0x6f3f3b82, 0x3520ab82, 0x011a1d4b, 0x277227f8, 0x611560b1, 0xe7933fdc, 0xbb3a792b, 0x344525bd, 0xa08839e1, 0x51ce794b, 0x2f32c9b7, 0xa01fbac9, 0xe01cc87e, 0xbcc7d1f6, 0xcf0111c3, 0xa1e8aac7, 0x1a908749, 0xd44fbd9a, 0xd0dadecb, 0xd50ada38, 0x0339c32a, 0xc6913667, 0x8df9317c, 0xe0b12b4f, 0xf79e59b7, 0x43f5bb3a, 0xf2d519ff, 0x27d9459c, 0xbf97222c, 0x15e6fc2a, 0x0f91fc71, 0x9b941525, 0xfae59361, 0xceb69ceb, 0xc2a86459, 0x12baa8d1, 0xb6c1075e, 0xe3056a0c, 0x10d25065, 0xcb03a442, 0xe0ec6e0e, 0x1698db3b, 0x4c98a0be, 0x3278e964, 0x9f1f9532, 0xe0d392df, 0xd3a0342b, 0x8971f21e, 0x1b0a7441, 0x4ba3348c, 0xc5be7120, 0xc37632d8, 0xdf359f8d, 0x9b992f2e, 0xe60b6f47, 0x0fe3f11d, 0xe54cda54, 0x1edad891, 0xce6279cf, 0xcd3e7e6f, 0x1618b166, 0xfd2c1d05, 0x848fd2c5, 0xf6fb2299, 0xf523f357, 0xa6327623, 0x93a83531, 0x56cccd02, 0xacf08162, 0x5a75ebb5, 0x6e163697, 0x88d273cc, 0xde966292, 0x81b949d0, 0x4c50901b, 0x71c65614, 0xe6c6c7bd, 0x327a140a, 0x45e1d006, 0xc3f27b9a, 0xc9aa53fd, 0x62a80f00, 0xbb25bfe2, 0x35bdd2f6, 0x71126905, 0xb2040222, 0xb6cbcf7c, 0xcd769c2b, 0x53113ec0, 0x1640e3d3, 0x38abbd60, 0x2547adf0, 0xba38209c, 0xf746ce76, 0x77afa1c5, 0x20756060, 0x85cbfe4e, 0x8ae88dd8, 0x7aaaf9b0, 0x4cf9aa7e, 0x1948c25c, 0x02fb8a8c, 0x01c36ae4, 0xd6ebe1f9, 0x90d4f869, 0xa65cdea0, 0x3f09252d, 0xc208e69f, 0xb74e6132, 0xce77e25b, 0x578fdfe3, 0x3ac372e6]];
  4618. //*
  4619. //* This is the default PARRAY
  4620. //*
  4621. Blowfish.prototype.PARRAY = [0x243f6a88, 0x85a308d3, 0x13198a2e, 0x03707344, 0xa4093822, 0x299f31d0, 0x082efa98, 0xec4e6c89, 0x452821e6, 0x38d01377, 0xbe5466cf, 0x34e90c6c, 0xc0ac29b7, 0xc97c50dd, 0x3f84d5b5, 0xb5470917, 0x9216d5d9, 0x8979fb1b];
  4622. //*
  4623. //* This is the number of rounds the cipher will go
  4624. //*
  4625. Blowfish.prototype.NN = 16;
  4626. //*
  4627. //* This function is needed to get rid of problems
  4628. //* with the high-bit getting set. If we don't do
  4629. //* this, then sometimes ( aa & 0x00FFFFFFFF ) is not
  4630. //* equal to ( bb & 0x00FFFFFFFF ) even when they
  4631. //* agree bit-for-bit for the first 32 bits.
  4632. //*
  4633. Blowfish.prototype._clean = function (xx) {
  4634. if (xx < 0) {
  4635. var yy = xx & 0x7FFFFFFF;
  4636. xx = yy + 0x80000000;
  4637. }
  4638. return xx;
  4639. };
  4640. //*
  4641. //* This is the mixing function that uses the sboxes
  4642. //*
  4643. Blowfish.prototype._F = function (xx) {
  4644. var aa;
  4645. var bb;
  4646. var cc;
  4647. var dd;
  4648. var yy;
  4649. dd = xx & 0x00FF;
  4650. xx >>>= 8;
  4651. cc = xx & 0x00FF;
  4652. xx >>>= 8;
  4653. bb = xx & 0x00FF;
  4654. xx >>>= 8;
  4655. aa = xx & 0x00FF;
  4656. yy = this.sboxes[0][aa] + this.sboxes[1][bb];
  4657. yy = yy ^ this.sboxes[2][cc];
  4658. yy = yy + this.sboxes[3][dd];
  4659. return yy;
  4660. };
  4661. //*
  4662. //* This method takes an array with two values, left and right
  4663. //* and does NN rounds of Blowfish on them.
  4664. //*
  4665. Blowfish.prototype._encrypt_block = function (vals) {
  4666. var dataL = vals[0];
  4667. var dataR = vals[1];
  4668. var ii;
  4669. for (ii = 0; ii < this.NN; ++ii) {
  4670. dataL = dataL ^ this.parray[ii];
  4671. dataR = this._F(dataL) ^ dataR;
  4672. var tmp = dataL;
  4673. dataL = dataR;
  4674. dataR = tmp;
  4675. }
  4676. dataL = dataL ^ this.parray[this.NN + 0];
  4677. dataR = dataR ^ this.parray[this.NN + 1];
  4678. vals[0] = this._clean(dataR);
  4679. vals[1] = this._clean(dataL);
  4680. };
  4681. //*
  4682. //* This method takes a vector of numbers and turns them
  4683. //* into long words so that they can be processed by the
  4684. //* real algorithm.
  4685. //*
  4686. //* Maybe I should make the real algorithm above take a vector
  4687. //* instead. That will involve more looping, but it won't require
  4688. //* the F() method to deconstruct the vector.
  4689. //*
  4690. Blowfish.prototype.encrypt_block = function (vector) {
  4691. var ii;
  4692. var vals = [0, 0];
  4693. var off = this.BLOCKSIZE / 2;
  4694. for (ii = 0; ii < this.BLOCKSIZE / 2; ++ii) {
  4695. vals[0] = vals[0] << 8 | vector[ii + 0] & 0x00FF;
  4696. vals[1] = vals[1] << 8 | vector[ii + off] & 0x00FF;
  4697. }
  4698. this._encrypt_block(vals);
  4699. var ret = [];
  4700. for (ii = 0; ii < this.BLOCKSIZE / 2; ++ii) {
  4701. ret[ii + 0] = vals[0] >>> 24 - 8 * ii & 0x00FF;
  4702. ret[ii + off] = vals[1] >>> 24 - 8 * ii & 0x00FF;
  4703. // vals[ 0 ] = ( vals[ 0 ] >>> 8 );
  4704. // vals[ 1 ] = ( vals[ 1 ] >>> 8 );
  4705. }
  4706. return ret;
  4707. };
  4708. //*
  4709. //* This method takes an array with two values, left and right
  4710. //* and undoes NN rounds of Blowfish on them.
  4711. //*
  4712. Blowfish.prototype._decrypt_block = function (vals) {
  4713. var dataL = vals[0];
  4714. var dataR = vals[1];
  4715. var ii;
  4716. for (ii = this.NN + 1; ii > 1; --ii) {
  4717. dataL = dataL ^ this.parray[ii];
  4718. dataR = this._F(dataL) ^ dataR;
  4719. var tmp = dataL;
  4720. dataL = dataR;
  4721. dataR = tmp;
  4722. }
  4723. dataL = dataL ^ this.parray[1];
  4724. dataR = dataR ^ this.parray[0];
  4725. vals[0] = this._clean(dataR);
  4726. vals[1] = this._clean(dataL);
  4727. };
  4728. //*
  4729. //* This method takes a key array and initializes the
  4730. //* sboxes and parray for this encryption.
  4731. //*
  4732. Blowfish.prototype.init = function (key) {
  4733. var ii;
  4734. var jj = 0;
  4735. this.parray = [];
  4736. for (ii = 0; ii < this.NN + 2; ++ii) {
  4737. var data = 0x00000000;
  4738. var kk;
  4739. for (kk = 0; kk < 4; ++kk) {
  4740. data = data << 8 | key[jj] & 0x00FF;
  4741. if (++jj >= key.length) {
  4742. jj = 0;
  4743. }
  4744. }
  4745. this.parray[ii] = this.PARRAY[ii] ^ data;
  4746. }
  4747. this.sboxes = [];
  4748. for (ii = 0; ii < 4; ++ii) {
  4749. this.sboxes[ii] = [];
  4750. for (jj = 0; jj < 256; ++jj) {
  4751. this.sboxes[ii][jj] = this.SBOXES[ii][jj];
  4752. }
  4753. }
  4754. var vals = [0x00000000, 0x00000000];
  4755. for (ii = 0; ii < this.NN + 2; ii += 2) {
  4756. this._encrypt_block(vals);
  4757. this.parray[ii + 0] = vals[0];
  4758. this.parray[ii + 1] = vals[1];
  4759. }
  4760. for (ii = 0; ii < 4; ++ii) {
  4761. for (jj = 0; jj < 256; jj += 2) {
  4762. this._encrypt_block(vals);
  4763. this.sboxes[ii][jj + 0] = vals[0];
  4764. this.sboxes[ii][jj + 1] = vals[1];
  4765. }
  4766. }
  4767. };
  4768. // added by Recurity Labs
  4769. function BF(key) {
  4770. this.bf = new Blowfish();
  4771. this.bf.init(key);
  4772. this.encrypt = function (block) {
  4773. return this.bf.encrypt_block(block);
  4774. };
  4775. }
  4776. BF.keySize = BF.prototype.keySize = 16;
  4777. BF.blockSize = BF.prototype.blockSize = 16;
  4778. },{}],14:[function(_dereq_,module,exports){
  4779. // Use of this source code is governed by a BSD-style
  4780. // license that can be found in the LICENSE file.
  4781. // Copyright 2010 pjacobs@xeekr.com . All rights reserved.
  4782. // Modified by Recurity Labs GmbH
  4783. // fixed/modified by Herbert Hanewinkel, www.haneWIN.de
  4784. // check www.haneWIN.de for the latest version
  4785. // cast5.js is a Javascript implementation of CAST-128, as defined in RFC 2144.
  4786. // CAST-128 is a common OpenPGP cipher.
  4787. // CAST5 constructor
  4788. /** @module crypto/cipher/cast5 */
  4789. 'use strict';
  4790. Object.defineProperty(exports, "__esModule", {
  4791. value: true
  4792. });
  4793. exports.default = Cast5;
  4794. function OpenpgpSymencCast5() {
  4795. this.BlockSize = 8;
  4796. this.KeySize = 16;
  4797. this.setKey = function (key) {
  4798. this.masking = new Array(16);
  4799. this.rotate = new Array(16);
  4800. this.reset();
  4801. if (key.length === this.KeySize) {
  4802. this.keySchedule(key);
  4803. } else {
  4804. throw new Error('CAST-128: keys must be 16 bytes');
  4805. }
  4806. return true;
  4807. };
  4808. this.reset = function () {
  4809. for (var i = 0; i < 16; i++) {
  4810. this.masking[i] = 0;
  4811. this.rotate[i] = 0;
  4812. }
  4813. };
  4814. this.getBlockSize = function () {
  4815. return this.BlockSize;
  4816. };
  4817. this.encrypt = function (src) {
  4818. var dst = new Array(src.length);
  4819. for (var i = 0; i < src.length; i += 8) {
  4820. var l = src[i] << 24 | src[i + 1] << 16 | src[i + 2] << 8 | src[i + 3];
  4821. var r = src[i + 4] << 24 | src[i + 5] << 16 | src[i + 6] << 8 | src[i + 7];
  4822. var t;
  4823. t = r;
  4824. r = l ^ f1(r, this.masking[0], this.rotate[0]);
  4825. l = t;
  4826. t = r;
  4827. r = l ^ f2(r, this.masking[1], this.rotate[1]);
  4828. l = t;
  4829. t = r;
  4830. r = l ^ f3(r, this.masking[2], this.rotate[2]);
  4831. l = t;
  4832. t = r;
  4833. r = l ^ f1(r, this.masking[3], this.rotate[3]);
  4834. l = t;
  4835. t = r;
  4836. r = l ^ f2(r, this.masking[4], this.rotate[4]);
  4837. l = t;
  4838. t = r;
  4839. r = l ^ f3(r, this.masking[5], this.rotate[5]);
  4840. l = t;
  4841. t = r;
  4842. r = l ^ f1(r, this.masking[6], this.rotate[6]);
  4843. l = t;
  4844. t = r;
  4845. r = l ^ f2(r, this.masking[7], this.rotate[7]);
  4846. l = t;
  4847. t = r;
  4848. r = l ^ f3(r, this.masking[8], this.rotate[8]);
  4849. l = t;
  4850. t = r;
  4851. r = l ^ f1(r, this.masking[9], this.rotate[9]);
  4852. l = t;
  4853. t = r;
  4854. r = l ^ f2(r, this.masking[10], this.rotate[10]);
  4855. l = t;
  4856. t = r;
  4857. r = l ^ f3(r, this.masking[11], this.rotate[11]);
  4858. l = t;
  4859. t = r;
  4860. r = l ^ f1(r, this.masking[12], this.rotate[12]);
  4861. l = t;
  4862. t = r;
  4863. r = l ^ f2(r, this.masking[13], this.rotate[13]);
  4864. l = t;
  4865. t = r;
  4866. r = l ^ f3(r, this.masking[14], this.rotate[14]);
  4867. l = t;
  4868. t = r;
  4869. r = l ^ f1(r, this.masking[15], this.rotate[15]);
  4870. l = t;
  4871. dst[i] = r >>> 24 & 255;
  4872. dst[i + 1] = r >>> 16 & 255;
  4873. dst[i + 2] = r >>> 8 & 255;
  4874. dst[i + 3] = r & 255;
  4875. dst[i + 4] = l >>> 24 & 255;
  4876. dst[i + 5] = l >>> 16 & 255;
  4877. dst[i + 6] = l >>> 8 & 255;
  4878. dst[i + 7] = l & 255;
  4879. }
  4880. return dst;
  4881. };
  4882. this.decrypt = function (src) {
  4883. var dst = new Array(src.length);
  4884. for (var i = 0; i < src.length; i += 8) {
  4885. var l = src[i] << 24 | src[i + 1] << 16 | src[i + 2] << 8 | src[i + 3];
  4886. var r = src[i + 4] << 24 | src[i + 5] << 16 | src[i + 6] << 8 | src[i + 7];
  4887. var t;
  4888. t = r;
  4889. r = l ^ f1(r, this.masking[15], this.rotate[15]);
  4890. l = t;
  4891. t = r;
  4892. r = l ^ f3(r, this.masking[14], this.rotate[14]);
  4893. l = t;
  4894. t = r;
  4895. r = l ^ f2(r, this.masking[13], this.rotate[13]);
  4896. l = t;
  4897. t = r;
  4898. r = l ^ f1(r, this.masking[12], this.rotate[12]);
  4899. l = t;
  4900. t = r;
  4901. r = l ^ f3(r, this.masking[11], this.rotate[11]);
  4902. l = t;
  4903. t = r;
  4904. r = l ^ f2(r, this.masking[10], this.rotate[10]);
  4905. l = t;
  4906. t = r;
  4907. r = l ^ f1(r, this.masking[9], this.rotate[9]);
  4908. l = t;
  4909. t = r;
  4910. r = l ^ f3(r, this.masking[8], this.rotate[8]);
  4911. l = t;
  4912. t = r;
  4913. r = l ^ f2(r, this.masking[7], this.rotate[7]);
  4914. l = t;
  4915. t = r;
  4916. r = l ^ f1(r, this.masking[6], this.rotate[6]);
  4917. l = t;
  4918. t = r;
  4919. r = l ^ f3(r, this.masking[5], this.rotate[5]);
  4920. l = t;
  4921. t = r;
  4922. r = l ^ f2(r, this.masking[4], this.rotate[4]);
  4923. l = t;
  4924. t = r;
  4925. r = l ^ f1(r, this.masking[3], this.rotate[3]);
  4926. l = t;
  4927. t = r;
  4928. r = l ^ f3(r, this.masking[2], this.rotate[2]);
  4929. l = t;
  4930. t = r;
  4931. r = l ^ f2(r, this.masking[1], this.rotate[1]);
  4932. l = t;
  4933. t = r;
  4934. r = l ^ f1(r, this.masking[0], this.rotate[0]);
  4935. l = t;
  4936. dst[i] = r >>> 24 & 255;
  4937. dst[i + 1] = r >>> 16 & 255;
  4938. dst[i + 2] = r >>> 8 & 255;
  4939. dst[i + 3] = r & 255;
  4940. dst[i + 4] = l >>> 24 & 255;
  4941. dst[i + 5] = l >> 16 & 255;
  4942. dst[i + 6] = l >> 8 & 255;
  4943. dst[i + 7] = l & 255;
  4944. }
  4945. return dst;
  4946. };
  4947. var scheduleA = new Array(4);
  4948. scheduleA[0] = new Array(4);
  4949. scheduleA[0][0] = new Array(4, 0, 0xd, 0xf, 0xc, 0xe, 0x8);
  4950. scheduleA[0][1] = new Array(5, 2, 16 + 0, 16 + 2, 16 + 1, 16 + 3, 0xa);
  4951. scheduleA[0][2] = new Array(6, 3, 16 + 7, 16 + 6, 16 + 5, 16 + 4, 9);
  4952. scheduleA[0][3] = new Array(7, 1, 16 + 0xa, 16 + 9, 16 + 0xb, 16 + 8, 0xb);
  4953. scheduleA[1] = new Array(4);
  4954. scheduleA[1][0] = new Array(0, 6, 16 + 5, 16 + 7, 16 + 4, 16 + 6, 16 + 0);
  4955. scheduleA[1][1] = new Array(1, 4, 0, 2, 1, 3, 16 + 2);
  4956. scheduleA[1][2] = new Array(2, 5, 7, 6, 5, 4, 16 + 1);
  4957. scheduleA[1][3] = new Array(3, 7, 0xa, 9, 0xb, 8, 16 + 3);
  4958. scheduleA[2] = new Array(4);
  4959. scheduleA[2][0] = new Array(4, 0, 0xd, 0xf, 0xc, 0xe, 8);
  4960. scheduleA[2][1] = new Array(5, 2, 16 + 0, 16 + 2, 16 + 1, 16 + 3, 0xa);
  4961. scheduleA[2][2] = new Array(6, 3, 16 + 7, 16 + 6, 16 + 5, 16 + 4, 9);
  4962. scheduleA[2][3] = new Array(7, 1, 16 + 0xa, 16 + 9, 16 + 0xb, 16 + 8, 0xb);
  4963. scheduleA[3] = new Array(4);
  4964. scheduleA[3][0] = new Array(0, 6, 16 + 5, 16 + 7, 16 + 4, 16 + 6, 16 + 0);
  4965. scheduleA[3][1] = new Array(1, 4, 0, 2, 1, 3, 16 + 2);
  4966. scheduleA[3][2] = new Array(2, 5, 7, 6, 5, 4, 16 + 1);
  4967. scheduleA[3][3] = new Array(3, 7, 0xa, 9, 0xb, 8, 16 + 3);
  4968. var scheduleB = new Array(4);
  4969. scheduleB[0] = new Array(4);
  4970. scheduleB[0][0] = new Array(16 + 8, 16 + 9, 16 + 7, 16 + 6, 16 + 2);
  4971. scheduleB[0][1] = new Array(16 + 0xa, 16 + 0xb, 16 + 5, 16 + 4, 16 + 6);
  4972. scheduleB[0][2] = new Array(16 + 0xc, 16 + 0xd, 16 + 3, 16 + 2, 16 + 9);
  4973. scheduleB[0][3] = new Array(16 + 0xe, 16 + 0xf, 16 + 1, 16 + 0, 16 + 0xc);
  4974. scheduleB[1] = new Array(4);
  4975. scheduleB[1][0] = new Array(3, 2, 0xc, 0xd, 8);
  4976. scheduleB[1][1] = new Array(1, 0, 0xe, 0xf, 0xd);
  4977. scheduleB[1][2] = new Array(7, 6, 8, 9, 3);
  4978. scheduleB[1][3] = new Array(5, 4, 0xa, 0xb, 7);
  4979. scheduleB[2] = new Array(4);
  4980. scheduleB[2][0] = new Array(16 + 3, 16 + 2, 16 + 0xc, 16 + 0xd, 16 + 9);
  4981. scheduleB[2][1] = new Array(16 + 1, 16 + 0, 16 + 0xe, 16 + 0xf, 16 + 0xc);
  4982. scheduleB[2][2] = new Array(16 + 7, 16 + 6, 16 + 8, 16 + 9, 16 + 2);
  4983. scheduleB[2][3] = new Array(16 + 5, 16 + 4, 16 + 0xa, 16 + 0xb, 16 + 6);
  4984. scheduleB[3] = new Array(4);
  4985. scheduleB[3][0] = new Array(8, 9, 7, 6, 3);
  4986. scheduleB[3][1] = new Array(0xa, 0xb, 5, 4, 7);
  4987. scheduleB[3][2] = new Array(0xc, 0xd, 3, 2, 8);
  4988. scheduleB[3][3] = new Array(0xe, 0xf, 1, 0, 0xd);
  4989. // changed 'in' to 'inn' (in javascript 'in' is a reserved word)
  4990. this.keySchedule = function (inn) {
  4991. var t = new Array(8);
  4992. var k = new Array(32);
  4993. var i, j;
  4994. for (i = 0; i < 4; i++) {
  4995. j = i * 4;
  4996. t[i] = inn[j] << 24 | inn[j + 1] << 16 | inn[j + 2] << 8 | inn[j + 3];
  4997. }
  4998. var x = [6, 7, 4, 5];
  4999. var ki = 0;
  5000. var w;
  5001. for (var half = 0; half < 2; half++) {
  5002. for (var round = 0; round < 4; round++) {
  5003. for (j = 0; j < 4; j++) {
  5004. var a = scheduleA[round][j];
  5005. w = t[a[1]];
  5006. w ^= sBox[4][t[a[2] >>> 2] >>> 24 - 8 * (a[2] & 3) & 0xff];
  5007. w ^= sBox[5][t[a[3] >>> 2] >>> 24 - 8 * (a[3] & 3) & 0xff];
  5008. w ^= sBox[6][t[a[4] >>> 2] >>> 24 - 8 * (a[4] & 3) & 0xff];
  5009. w ^= sBox[7][t[a[5] >>> 2] >>> 24 - 8 * (a[5] & 3) & 0xff];
  5010. w ^= sBox[x[j]][t[a[6] >>> 2] >>> 24 - 8 * (a[6] & 3) & 0xff];
  5011. t[a[0]] = w;
  5012. }
  5013. for (j = 0; j < 4; j++) {
  5014. var b = scheduleB[round][j];
  5015. w = sBox[4][t[b[0] >>> 2] >>> 24 - 8 * (b[0] & 3) & 0xff];
  5016. w ^= sBox[5][t[b[1] >>> 2] >>> 24 - 8 * (b[1] & 3) & 0xff];
  5017. w ^= sBox[6][t[b[2] >>> 2] >>> 24 - 8 * (b[2] & 3) & 0xff];
  5018. w ^= sBox[7][t[b[3] >>> 2] >>> 24 - 8 * (b[3] & 3) & 0xff];
  5019. w ^= sBox[4 + j][t[b[4] >>> 2] >>> 24 - 8 * (b[4] & 3) & 0xff];
  5020. k[ki] = w;
  5021. ki++;
  5022. }
  5023. }
  5024. }
  5025. for (i = 0; i < 16; i++) {
  5026. this.masking[i] = k[i];
  5027. this.rotate[i] = k[16 + i] & 0x1f;
  5028. }
  5029. };
  5030. // These are the three 'f' functions. See RFC 2144, section 2.2.
  5031. function f1(d, m, r) {
  5032. var t = m + d;
  5033. var I = t << r | t >>> 32 - r;
  5034. return (sBox[0][I >>> 24] ^ sBox[1][I >>> 16 & 255]) - sBox[2][I >>> 8 & 255] + sBox[3][I & 255];
  5035. }
  5036. function f2(d, m, r) {
  5037. var t = m ^ d;
  5038. var I = t << r | t >>> 32 - r;
  5039. return sBox[0][I >>> 24] - sBox[1][I >>> 16 & 255] + sBox[2][I >>> 8 & 255] ^ sBox[3][I & 255];
  5040. }
  5041. function f3(d, m, r) {
  5042. var t = m - d;
  5043. var I = t << r | t >>> 32 - r;
  5044. return (sBox[0][I >>> 24] + sBox[1][I >>> 16 & 255] ^ sBox[2][I >>> 8 & 255]) - sBox[3][I & 255];
  5045. }
  5046. var sBox = new Array(8);
  5047. sBox[0] = new Array(0x30fb40d4, 0x9fa0ff0b, 0x6beccd2f, 0x3f258c7a, 0x1e213f2f, 0x9c004dd3, 0x6003e540, 0xcf9fc949, 0xbfd4af27, 0x88bbbdb5, 0xe2034090, 0x98d09675, 0x6e63a0e0, 0x15c361d2, 0xc2e7661d, 0x22d4ff8e, 0x28683b6f, 0xc07fd059, 0xff2379c8, 0x775f50e2, 0x43c340d3, 0xdf2f8656, 0x887ca41a, 0xa2d2bd2d, 0xa1c9e0d6, 0x346c4819, 0x61b76d87, 0x22540f2f, 0x2abe32e1, 0xaa54166b, 0x22568e3a, 0xa2d341d0, 0x66db40c8, 0xa784392f, 0x004dff2f, 0x2db9d2de, 0x97943fac, 0x4a97c1d8, 0x527644b7, 0xb5f437a7, 0xb82cbaef, 0xd751d159, 0x6ff7f0ed, 0x5a097a1f, 0x827b68d0, 0x90ecf52e, 0x22b0c054, 0xbc8e5935, 0x4b6d2f7f, 0x50bb64a2, 0xd2664910, 0xbee5812d, 0xb7332290, 0xe93b159f, 0xb48ee411, 0x4bff345d, 0xfd45c240, 0xad31973f, 0xc4f6d02e, 0x55fc8165, 0xd5b1caad, 0xa1ac2dae, 0xa2d4b76d, 0xc19b0c50, 0x882240f2, 0x0c6e4f38, 0xa4e4bfd7, 0x4f5ba272, 0x564c1d2f, 0xc59c5319, 0xb949e354, 0xb04669fe, 0xb1b6ab8a, 0xc71358dd, 0x6385c545, 0x110f935d, 0x57538ad5, 0x6a390493, 0xe63d37e0, 0x2a54f6b3, 0x3a787d5f, 0x6276a0b5, 0x19a6fcdf, 0x7a42206a, 0x29f9d4d5, 0xf61b1891, 0xbb72275e, 0xaa508167, 0x38901091, 0xc6b505eb, 0x84c7cb8c, 0x2ad75a0f, 0x874a1427, 0xa2d1936b, 0x2ad286af, 0xaa56d291, 0xd7894360, 0x425c750d, 0x93b39e26, 0x187184c9, 0x6c00b32d, 0x73e2bb14, 0xa0bebc3c, 0x54623779, 0x64459eab, 0x3f328b82, 0x7718cf82, 0x59a2cea6, 0x04ee002e, 0x89fe78e6, 0x3fab0950, 0x325ff6c2, 0x81383f05, 0x6963c5c8, 0x76cb5ad6, 0xd49974c9, 0xca180dcf, 0x380782d5, 0xc7fa5cf6, 0x8ac31511, 0x35e79e13, 0x47da91d0, 0xf40f9086, 0xa7e2419e, 0x31366241, 0x051ef495, 0xaa573b04, 0x4a805d8d, 0x548300d0, 0x00322a3c, 0xbf64cddf, 0xba57a68e, 0x75c6372b, 0x50afd341, 0xa7c13275, 0x915a0bf5, 0x6b54bfab, 0x2b0b1426, 0xab4cc9d7, 0x449ccd82, 0xf7fbf265, 0xab85c5f3, 0x1b55db94, 0xaad4e324, 0xcfa4bd3f, 0x2deaa3e2, 0x9e204d02, 0xc8bd25ac, 0xeadf55b3, 0xd5bd9e98, 0xe31231b2, 0x2ad5ad6c, 0x954329de, 0xadbe4528, 0xd8710f69, 0xaa51c90f, 0xaa786bf6, 0x22513f1e, 0xaa51a79b, 0x2ad344cc, 0x7b5a41f0, 0xd37cfbad, 0x1b069505, 0x41ece491, 0xb4c332e6, 0x032268d4, 0xc9600acc, 0xce387e6d, 0xbf6bb16c, 0x6a70fb78, 0x0d03d9c9, 0xd4df39de, 0xe01063da, 0x4736f464, 0x5ad328d8, 0xb347cc96, 0x75bb0fc3, 0x98511bfb, 0x4ffbcc35, 0xb58bcf6a, 0xe11f0abc, 0xbfc5fe4a, 0xa70aec10, 0xac39570a, 0x3f04442f, 0x6188b153, 0xe0397a2e, 0x5727cb79, 0x9ceb418f, 0x1cacd68d, 0x2ad37c96, 0x0175cb9d, 0xc69dff09, 0xc75b65f0, 0xd9db40d8, 0xec0e7779, 0x4744ead4, 0xb11c3274, 0xdd24cb9e, 0x7e1c54bd, 0xf01144f9, 0xd2240eb1, 0x9675b3fd, 0xa3ac3755, 0xd47c27af, 0x51c85f4d, 0x56907596, 0xa5bb15e6, 0x580304f0, 0xca042cf1, 0x011a37ea, 0x8dbfaadb, 0x35ba3e4a, 0x3526ffa0, 0xc37b4d09, 0xbc306ed9, 0x98a52666, 0x5648f725, 0xff5e569d, 0x0ced63d0, 0x7c63b2cf, 0x700b45e1, 0xd5ea50f1, 0x85a92872, 0xaf1fbda7, 0xd4234870, 0xa7870bf3, 0x2d3b4d79, 0x42e04198, 0x0cd0ede7, 0x26470db8, 0xf881814c, 0x474d6ad7, 0x7c0c5e5c, 0xd1231959, 0x381b7298, 0xf5d2f4db, 0xab838653, 0x6e2f1e23, 0x83719c9e, 0xbd91e046, 0x9a56456e, 0xdc39200c, 0x20c8c571, 0x962bda1c, 0xe1e696ff, 0xb141ab08, 0x7cca89b9, 0x1a69e783, 0x02cc4843, 0xa2f7c579, 0x429ef47d, 0x427b169c, 0x5ac9f049, 0xdd8f0f00, 0x5c8165bf);
  5048. sBox[1] = new Array(0x1f201094, 0xef0ba75b, 0x69e3cf7e, 0x393f4380, 0xfe61cf7a, 0xeec5207a, 0x55889c94, 0x72fc0651, 0xada7ef79, 0x4e1d7235, 0xd55a63ce, 0xde0436ba, 0x99c430ef, 0x5f0c0794, 0x18dcdb7d, 0xa1d6eff3, 0xa0b52f7b, 0x59e83605, 0xee15b094, 0xe9ffd909, 0xdc440086, 0xef944459, 0xba83ccb3, 0xe0c3cdfb, 0xd1da4181, 0x3b092ab1, 0xf997f1c1, 0xa5e6cf7b, 0x01420ddb, 0xe4e7ef5b, 0x25a1ff41, 0xe180f806, 0x1fc41080, 0x179bee7a, 0xd37ac6a9, 0xfe5830a4, 0x98de8b7f, 0x77e83f4e, 0x79929269, 0x24fa9f7b, 0xe113c85b, 0xacc40083, 0xd7503525, 0xf7ea615f, 0x62143154, 0x0d554b63, 0x5d681121, 0xc866c359, 0x3d63cf73, 0xcee234c0, 0xd4d87e87, 0x5c672b21, 0x071f6181, 0x39f7627f, 0x361e3084, 0xe4eb573b, 0x602f64a4, 0xd63acd9c, 0x1bbc4635, 0x9e81032d, 0x2701f50c, 0x99847ab4, 0xa0e3df79, 0xba6cf38c, 0x10843094, 0x2537a95e, 0xf46f6ffe, 0xa1ff3b1f, 0x208cfb6a, 0x8f458c74, 0xd9e0a227, 0x4ec73a34, 0xfc884f69, 0x3e4de8df, 0xef0e0088, 0x3559648d, 0x8a45388c, 0x1d804366, 0x721d9bfd, 0xa58684bb, 0xe8256333, 0x844e8212, 0x128d8098, 0xfed33fb4, 0xce280ae1, 0x27e19ba5, 0xd5a6c252, 0xe49754bd, 0xc5d655dd, 0xeb667064, 0x77840b4d, 0xa1b6a801, 0x84db26a9, 0xe0b56714, 0x21f043b7, 0xe5d05860, 0x54f03084, 0x066ff472, 0xa31aa153, 0xdadc4755, 0xb5625dbf, 0x68561be6, 0x83ca6b94, 0x2d6ed23b, 0xeccf01db, 0xa6d3d0ba, 0xb6803d5c, 0xaf77a709, 0x33b4a34c, 0x397bc8d6, 0x5ee22b95, 0x5f0e5304, 0x81ed6f61, 0x20e74364, 0xb45e1378, 0xde18639b, 0x881ca122, 0xb96726d1, 0x8049a7e8, 0x22b7da7b, 0x5e552d25, 0x5272d237, 0x79d2951c, 0xc60d894c, 0x488cb402, 0x1ba4fe5b, 0xa4b09f6b, 0x1ca815cf, 0xa20c3005, 0x8871df63, 0xb9de2fcb, 0x0cc6c9e9, 0x0beeff53, 0xe3214517, 0xb4542835, 0x9f63293c, 0xee41e729, 0x6e1d2d7c, 0x50045286, 0x1e6685f3, 0xf33401c6, 0x30a22c95, 0x31a70850, 0x60930f13, 0x73f98417, 0xa1269859, 0xec645c44, 0x52c877a9, 0xcdff33a6, 0xa02b1741, 0x7cbad9a2, 0x2180036f, 0x50d99c08, 0xcb3f4861, 0xc26bd765, 0x64a3f6ab, 0x80342676, 0x25a75e7b, 0xe4e6d1fc, 0x20c710e6, 0xcdf0b680, 0x17844d3b, 0x31eef84d, 0x7e0824e4, 0x2ccb49eb, 0x846a3bae, 0x8ff77888, 0xee5d60f6, 0x7af75673, 0x2fdd5cdb, 0xa11631c1, 0x30f66f43, 0xb3faec54, 0x157fd7fa, 0xef8579cc, 0xd152de58, 0xdb2ffd5e, 0x8f32ce19, 0x306af97a, 0x02f03ef8, 0x99319ad5, 0xc242fa0f, 0xa7e3ebb0, 0xc68e4906, 0xb8da230c, 0x80823028, 0xdcdef3c8, 0xd35fb171, 0x088a1bc8, 0xbec0c560, 0x61a3c9e8, 0xbca8f54d, 0xc72feffa, 0x22822e99, 0x82c570b4, 0xd8d94e89, 0x8b1c34bc, 0x301e16e6, 0x273be979, 0xb0ffeaa6, 0x61d9b8c6, 0x00b24869, 0xb7ffce3f, 0x08dc283b, 0x43daf65a, 0xf7e19798, 0x7619b72f, 0x8f1c9ba4, 0xdc8637a0, 0x16a7d3b1, 0x9fc393b7, 0xa7136eeb, 0xc6bcc63e, 0x1a513742, 0xef6828bc, 0x520365d6, 0x2d6a77ab, 0x3527ed4b, 0x821fd216, 0x095c6e2e, 0xdb92f2fb, 0x5eea29cb, 0x145892f5, 0x91584f7f, 0x5483697b, 0x2667a8cc, 0x85196048, 0x8c4bacea, 0x833860d4, 0x0d23e0f9, 0x6c387e8a, 0x0ae6d249, 0xb284600c, 0xd835731d, 0xdcb1c647, 0xac4c56ea, 0x3ebd81b3, 0x230eabb0, 0x6438bc87, 0xf0b5b1fa, 0x8f5ea2b3, 0xfc184642, 0x0a036b7a, 0x4fb089bd, 0x649da589, 0xa345415e, 0x5c038323, 0x3e5d3bb9, 0x43d79572, 0x7e6dd07c, 0x06dfdf1e, 0x6c6cc4ef, 0x7160a539, 0x73bfbe70, 0x83877605, 0x4523ecf1);
  5049. sBox[2] = new Array(0x8defc240, 0x25fa5d9f, 0xeb903dbf, 0xe810c907, 0x47607fff, 0x369fe44b, 0x8c1fc644, 0xaececa90, 0xbeb1f9bf, 0xeefbcaea, 0xe8cf1950, 0x51df07ae, 0x920e8806, 0xf0ad0548, 0xe13c8d83, 0x927010d5, 0x11107d9f, 0x07647db9, 0xb2e3e4d4, 0x3d4f285e, 0xb9afa820, 0xfade82e0, 0xa067268b, 0x8272792e, 0x553fb2c0, 0x489ae22b, 0xd4ef9794, 0x125e3fbc, 0x21fffcee, 0x825b1bfd, 0x9255c5ed, 0x1257a240, 0x4e1a8302, 0xbae07fff, 0x528246e7, 0x8e57140e, 0x3373f7bf, 0x8c9f8188, 0xa6fc4ee8, 0xc982b5a5, 0xa8c01db7, 0x579fc264, 0x67094f31, 0xf2bd3f5f, 0x40fff7c1, 0x1fb78dfc, 0x8e6bd2c1, 0x437be59b, 0x99b03dbf, 0xb5dbc64b, 0x638dc0e6, 0x55819d99, 0xa197c81c, 0x4a012d6e, 0xc5884a28, 0xccc36f71, 0xb843c213, 0x6c0743f1, 0x8309893c, 0x0feddd5f, 0x2f7fe850, 0xd7c07f7e, 0x02507fbf, 0x5afb9a04, 0xa747d2d0, 0x1651192e, 0xaf70bf3e, 0x58c31380, 0x5f98302e, 0x727cc3c4, 0x0a0fb402, 0x0f7fef82, 0x8c96fdad, 0x5d2c2aae, 0x8ee99a49, 0x50da88b8, 0x8427f4a0, 0x1eac5790, 0x796fb449, 0x8252dc15, 0xefbd7d9b, 0xa672597d, 0xada840d8, 0x45f54504, 0xfa5d7403, 0xe83ec305, 0x4f91751a, 0x925669c2, 0x23efe941, 0xa903f12e, 0x60270df2, 0x0276e4b6, 0x94fd6574, 0x927985b2, 0x8276dbcb, 0x02778176, 0xf8af918d, 0x4e48f79e, 0x8f616ddf, 0xe29d840e, 0x842f7d83, 0x340ce5c8, 0x96bbb682, 0x93b4b148, 0xef303cab, 0x984faf28, 0x779faf9b, 0x92dc560d, 0x224d1e20, 0x8437aa88, 0x7d29dc96, 0x2756d3dc, 0x8b907cee, 0xb51fd240, 0xe7c07ce3, 0xe566b4a1, 0xc3e9615e, 0x3cf8209d, 0x6094d1e3, 0xcd9ca341, 0x5c76460e, 0x00ea983b, 0xd4d67881, 0xfd47572c, 0xf76cedd9, 0xbda8229c, 0x127dadaa, 0x438a074e, 0x1f97c090, 0x081bdb8a, 0x93a07ebe, 0xb938ca15, 0x97b03cff, 0x3dc2c0f8, 0x8d1ab2ec, 0x64380e51, 0x68cc7bfb, 0xd90f2788, 0x12490181, 0x5de5ffd4, 0xdd7ef86a, 0x76a2e214, 0xb9a40368, 0x925d958f, 0x4b39fffa, 0xba39aee9, 0xa4ffd30b, 0xfaf7933b, 0x6d498623, 0x193cbcfa, 0x27627545, 0x825cf47a, 0x61bd8ba0, 0xd11e42d1, 0xcead04f4, 0x127ea392, 0x10428db7, 0x8272a972, 0x9270c4a8, 0x127de50b, 0x285ba1c8, 0x3c62f44f, 0x35c0eaa5, 0xe805d231, 0x428929fb, 0xb4fcdf82, 0x4fb66a53, 0x0e7dc15b, 0x1f081fab, 0x108618ae, 0xfcfd086d, 0xf9ff2889, 0x694bcc11, 0x236a5cae, 0x12deca4d, 0x2c3f8cc5, 0xd2d02dfe, 0xf8ef5896, 0xe4cf52da, 0x95155b67, 0x494a488c, 0xb9b6a80c, 0x5c8f82bc, 0x89d36b45, 0x3a609437, 0xec00c9a9, 0x44715253, 0x0a874b49, 0xd773bc40, 0x7c34671c, 0x02717ef6, 0x4feb5536, 0xa2d02fff, 0xd2bf60c4, 0xd43f03c0, 0x50b4ef6d, 0x07478cd1, 0x006e1888, 0xa2e53f55, 0xb9e6d4bc, 0xa2048016, 0x97573833, 0xd7207d67, 0xde0f8f3d, 0x72f87b33, 0xabcc4f33, 0x7688c55d, 0x7b00a6b0, 0x947b0001, 0x570075d2, 0xf9bb88f8, 0x8942019e, 0x4264a5ff, 0x856302e0, 0x72dbd92b, 0xee971b69, 0x6ea22fde, 0x5f08ae2b, 0xaf7a616d, 0xe5c98767, 0xcf1febd2, 0x61efc8c2, 0xf1ac2571, 0xcc8239c2, 0x67214cb8, 0xb1e583d1, 0xb7dc3e62, 0x7f10bdce, 0xf90a5c38, 0x0ff0443d, 0x606e6dc6, 0x60543a49, 0x5727c148, 0x2be98a1d, 0x8ab41738, 0x20e1be24, 0xaf96da0f, 0x68458425, 0x99833be5, 0x600d457d, 0x282f9350, 0x8334b362, 0xd91d1120, 0x2b6d8da0, 0x642b1e31, 0x9c305a00, 0x52bce688, 0x1b03588a, 0xf7baefd5, 0x4142ed9c, 0xa4315c11, 0x83323ec5, 0xdfef4636, 0xa133c501, 0xe9d3531c, 0xee353783);
  5050. sBox[3] = new Array(0x9db30420, 0x1fb6e9de, 0xa7be7bef, 0xd273a298, 0x4a4f7bdb, 0x64ad8c57, 0x85510443, 0xfa020ed1, 0x7e287aff, 0xe60fb663, 0x095f35a1, 0x79ebf120, 0xfd059d43, 0x6497b7b1, 0xf3641f63, 0x241e4adf, 0x28147f5f, 0x4fa2b8cd, 0xc9430040, 0x0cc32220, 0xfdd30b30, 0xc0a5374f, 0x1d2d00d9, 0x24147b15, 0xee4d111a, 0x0fca5167, 0x71ff904c, 0x2d195ffe, 0x1a05645f, 0x0c13fefe, 0x081b08ca, 0x05170121, 0x80530100, 0xe83e5efe, 0xac9af4f8, 0x7fe72701, 0xd2b8ee5f, 0x06df4261, 0xbb9e9b8a, 0x7293ea25, 0xce84ffdf, 0xf5718801, 0x3dd64b04, 0xa26f263b, 0x7ed48400, 0x547eebe6, 0x446d4ca0, 0x6cf3d6f5, 0x2649abdf, 0xaea0c7f5, 0x36338cc1, 0x503f7e93, 0xd3772061, 0x11b638e1, 0x72500e03, 0xf80eb2bb, 0xabe0502e, 0xec8d77de, 0x57971e81, 0xe14f6746, 0xc9335400, 0x6920318f, 0x081dbb99, 0xffc304a5, 0x4d351805, 0x7f3d5ce3, 0xa6c866c6, 0x5d5bcca9, 0xdaec6fea, 0x9f926f91, 0x9f46222f, 0x3991467d, 0xa5bf6d8e, 0x1143c44f, 0x43958302, 0xd0214eeb, 0x022083b8, 0x3fb6180c, 0x18f8931e, 0x281658e6, 0x26486e3e, 0x8bd78a70, 0x7477e4c1, 0xb506e07c, 0xf32d0a25, 0x79098b02, 0xe4eabb81, 0x28123b23, 0x69dead38, 0x1574ca16, 0xdf871b62, 0x211c40b7, 0xa51a9ef9, 0x0014377b, 0x041e8ac8, 0x09114003, 0xbd59e4d2, 0xe3d156d5, 0x4fe876d5, 0x2f91a340, 0x557be8de, 0x00eae4a7, 0x0ce5c2ec, 0x4db4bba6, 0xe756bdff, 0xdd3369ac, 0xec17b035, 0x06572327, 0x99afc8b0, 0x56c8c391, 0x6b65811c, 0x5e146119, 0x6e85cb75, 0xbe07c002, 0xc2325577, 0x893ff4ec, 0x5bbfc92d, 0xd0ec3b25, 0xb7801ab7, 0x8d6d3b24, 0x20c763ef, 0xc366a5fc, 0x9c382880, 0x0ace3205, 0xaac9548a, 0xeca1d7c7, 0x041afa32, 0x1d16625a, 0x6701902c, 0x9b757a54, 0x31d477f7, 0x9126b031, 0x36cc6fdb, 0xc70b8b46, 0xd9e66a48, 0x56e55a79, 0x026a4ceb, 0x52437eff, 0x2f8f76b4, 0x0df980a5, 0x8674cde3, 0xedda04eb, 0x17a9be04, 0x2c18f4df, 0xb7747f9d, 0xab2af7b4, 0xefc34d20, 0x2e096b7c, 0x1741a254, 0xe5b6a035, 0x213d42f6, 0x2c1c7c26, 0x61c2f50f, 0x6552daf9, 0xd2c231f8, 0x25130f69, 0xd8167fa2, 0x0418f2c8, 0x001a96a6, 0x0d1526ab, 0x63315c21, 0x5e0a72ec, 0x49bafefd, 0x187908d9, 0x8d0dbd86, 0x311170a7, 0x3e9b640c, 0xcc3e10d7, 0xd5cad3b6, 0x0caec388, 0xf73001e1, 0x6c728aff, 0x71eae2a1, 0x1f9af36e, 0xcfcbd12f, 0xc1de8417, 0xac07be6b, 0xcb44a1d8, 0x8b9b0f56, 0x013988c3, 0xb1c52fca, 0xb4be31cd, 0xd8782806, 0x12a3a4e2, 0x6f7de532, 0x58fd7eb6, 0xd01ee900, 0x24adffc2, 0xf4990fc5, 0x9711aac5, 0x001d7b95, 0x82e5e7d2, 0x109873f6, 0x00613096, 0xc32d9521, 0xada121ff, 0x29908415, 0x7fbb977f, 0xaf9eb3db, 0x29c9ed2a, 0x5ce2a465, 0xa730f32c, 0xd0aa3fe8, 0x8a5cc091, 0xd49e2ce7, 0x0ce454a9, 0xd60acd86, 0x015f1919, 0x77079103, 0xdea03af6, 0x78a8565e, 0xdee356df, 0x21f05cbe, 0x8b75e387, 0xb3c50651, 0xb8a5c3ef, 0xd8eeb6d2, 0xe523be77, 0xc2154529, 0x2f69efdf, 0xafe67afb, 0xf470c4b2, 0xf3e0eb5b, 0xd6cc9876, 0x39e4460c, 0x1fda8538, 0x1987832f, 0xca007367, 0xa99144f8, 0x296b299e, 0x492fc295, 0x9266beab, 0xb5676e69, 0x9bd3ddda, 0xdf7e052f, 0xdb25701c, 0x1b5e51ee, 0xf65324e6, 0x6afce36c, 0x0316cc04, 0x8644213e, 0xb7dc59d0, 0x7965291f, 0xccd6fd43, 0x41823979, 0x932bcdf6, 0xb657c34d, 0x4edfd282, 0x7ae5290c, 0x3cb9536b, 0x851e20fe, 0x9833557e, 0x13ecf0b0, 0xd3ffb372, 0x3f85c5c1, 0x0aef7ed2);
  5051. sBox[4] = new Array(0x7ec90c04, 0x2c6e74b9, 0x9b0e66df, 0xa6337911, 0xb86a7fff, 0x1dd358f5, 0x44dd9d44, 0x1731167f, 0x08fbf1fa, 0xe7f511cc, 0xd2051b00, 0x735aba00, 0x2ab722d8, 0x386381cb, 0xacf6243a, 0x69befd7a, 0xe6a2e77f, 0xf0c720cd, 0xc4494816, 0xccf5c180, 0x38851640, 0x15b0a848, 0xe68b18cb, 0x4caadeff, 0x5f480a01, 0x0412b2aa, 0x259814fc, 0x41d0efe2, 0x4e40b48d, 0x248eb6fb, 0x8dba1cfe, 0x41a99b02, 0x1a550a04, 0xba8f65cb, 0x7251f4e7, 0x95a51725, 0xc106ecd7, 0x97a5980a, 0xc539b9aa, 0x4d79fe6a, 0xf2f3f763, 0x68af8040, 0xed0c9e56, 0x11b4958b, 0xe1eb5a88, 0x8709e6b0, 0xd7e07156, 0x4e29fea7, 0x6366e52d, 0x02d1c000, 0xc4ac8e05, 0x9377f571, 0x0c05372a, 0x578535f2, 0x2261be02, 0xd642a0c9, 0xdf13a280, 0x74b55bd2, 0x682199c0, 0xd421e5ec, 0x53fb3ce8, 0xc8adedb3, 0x28a87fc9, 0x3d959981, 0x5c1ff900, 0xfe38d399, 0x0c4eff0b, 0x062407ea, 0xaa2f4fb1, 0x4fb96976, 0x90c79505, 0xb0a8a774, 0xef55a1ff, 0xe59ca2c2, 0xa6b62d27, 0xe66a4263, 0xdf65001f, 0x0ec50966, 0xdfdd55bc, 0x29de0655, 0x911e739a, 0x17af8975, 0x32c7911c, 0x89f89468, 0x0d01e980, 0x524755f4, 0x03b63cc9, 0x0cc844b2, 0xbcf3f0aa, 0x87ac36e9, 0xe53a7426, 0x01b3d82b, 0x1a9e7449, 0x64ee2d7e, 0xcddbb1da, 0x01c94910, 0xb868bf80, 0x0d26f3fd, 0x9342ede7, 0x04a5c284, 0x636737b6, 0x50f5b616, 0xf24766e3, 0x8eca36c1, 0x136e05db, 0xfef18391, 0xfb887a37, 0xd6e7f7d4, 0xc7fb7dc9, 0x3063fcdf, 0xb6f589de, 0xec2941da, 0x26e46695, 0xb7566419, 0xf654efc5, 0xd08d58b7, 0x48925401, 0xc1bacb7f, 0xe5ff550f, 0xb6083049, 0x5bb5d0e8, 0x87d72e5a, 0xab6a6ee1, 0x223a66ce, 0xc62bf3cd, 0x9e0885f9, 0x68cb3e47, 0x086c010f, 0xa21de820, 0xd18b69de, 0xf3f65777, 0xfa02c3f6, 0x407edac3, 0xcbb3d550, 0x1793084d, 0xb0d70eba, 0x0ab378d5, 0xd951fb0c, 0xded7da56, 0x4124bbe4, 0x94ca0b56, 0x0f5755d1, 0xe0e1e56e, 0x6184b5be, 0x580a249f, 0x94f74bc0, 0xe327888e, 0x9f7b5561, 0xc3dc0280, 0x05687715, 0x646c6bd7, 0x44904db3, 0x66b4f0a3, 0xc0f1648a, 0x697ed5af, 0x49e92ff6, 0x309e374f, 0x2cb6356a, 0x85808573, 0x4991f840, 0x76f0ae02, 0x083be84d, 0x28421c9a, 0x44489406, 0x736e4cb8, 0xc1092910, 0x8bc95fc6, 0x7d869cf4, 0x134f616f, 0x2e77118d, 0xb31b2be1, 0xaa90b472, 0x3ca5d717, 0x7d161bba, 0x9cad9010, 0xaf462ba2, 0x9fe459d2, 0x45d34559, 0xd9f2da13, 0xdbc65487, 0xf3e4f94e, 0x176d486f, 0x097c13ea, 0x631da5c7, 0x445f7382, 0x175683f4, 0xcdc66a97, 0x70be0288, 0xb3cdcf72, 0x6e5dd2f3, 0x20936079, 0x459b80a5, 0xbe60e2db, 0xa9c23101, 0xeba5315c, 0x224e42f2, 0x1c5c1572, 0xf6721b2c, 0x1ad2fff3, 0x8c25404e, 0x324ed72f, 0x4067b7fd, 0x0523138e, 0x5ca3bc78, 0xdc0fd66e, 0x75922283, 0x784d6b17, 0x58ebb16e, 0x44094f85, 0x3f481d87, 0xfcfeae7b, 0x77b5ff76, 0x8c2302bf, 0xaaf47556, 0x5f46b02a, 0x2b092801, 0x3d38f5f7, 0x0ca81f36, 0x52af4a8a, 0x66d5e7c0, 0xdf3b0874, 0x95055110, 0x1b5ad7a8, 0xf61ed5ad, 0x6cf6e479, 0x20758184, 0xd0cefa65, 0x88f7be58, 0x4a046826, 0x0ff6f8f3, 0xa09c7f70, 0x5346aba0, 0x5ce96c28, 0xe176eda3, 0x6bac307f, 0x376829d2, 0x85360fa9, 0x17e3fe2a, 0x24b79767, 0xf5a96b20, 0xd6cd2595, 0x68ff1ebf, 0x7555442c, 0xf19f06be, 0xf9e0659a, 0xeeb9491d, 0x34010718, 0xbb30cab8, 0xe822fe15, 0x88570983, 0x750e6249, 0xda627e55, 0x5e76ffa8, 0xb1534546, 0x6d47de08, 0xefe9e7d4);
  5052. sBox[5] = new Array(0xf6fa8f9d, 0x2cac6ce1, 0x4ca34867, 0xe2337f7c, 0x95db08e7, 0x016843b4, 0xeced5cbc, 0x325553ac, 0xbf9f0960, 0xdfa1e2ed, 0x83f0579d, 0x63ed86b9, 0x1ab6a6b8, 0xde5ebe39, 0xf38ff732, 0x8989b138, 0x33f14961, 0xc01937bd, 0xf506c6da, 0xe4625e7e, 0xa308ea99, 0x4e23e33c, 0x79cbd7cc, 0x48a14367, 0xa3149619, 0xfec94bd5, 0xa114174a, 0xeaa01866, 0xa084db2d, 0x09a8486f, 0xa888614a, 0x2900af98, 0x01665991, 0xe1992863, 0xc8f30c60, 0x2e78ef3c, 0xd0d51932, 0xcf0fec14, 0xf7ca07d2, 0xd0a82072, 0xfd41197e, 0x9305a6b0, 0xe86be3da, 0x74bed3cd, 0x372da53c, 0x4c7f4448, 0xdab5d440, 0x6dba0ec3, 0x083919a7, 0x9fbaeed9, 0x49dbcfb0, 0x4e670c53, 0x5c3d9c01, 0x64bdb941, 0x2c0e636a, 0xba7dd9cd, 0xea6f7388, 0xe70bc762, 0x35f29adb, 0x5c4cdd8d, 0xf0d48d8c, 0xb88153e2, 0x08a19866, 0x1ae2eac8, 0x284caf89, 0xaa928223, 0x9334be53, 0x3b3a21bf, 0x16434be3, 0x9aea3906, 0xefe8c36e, 0xf890cdd9, 0x80226dae, 0xc340a4a3, 0xdf7e9c09, 0xa694a807, 0x5b7c5ecc, 0x221db3a6, 0x9a69a02f, 0x68818a54, 0xceb2296f, 0x53c0843a, 0xfe893655, 0x25bfe68a, 0xb4628abc, 0xcf222ebf, 0x25ac6f48, 0xa9a99387, 0x53bddb65, 0xe76ffbe7, 0xe967fd78, 0x0ba93563, 0x8e342bc1, 0xe8a11be9, 0x4980740d, 0xc8087dfc, 0x8de4bf99, 0xa11101a0, 0x7fd37975, 0xda5a26c0, 0xe81f994f, 0x9528cd89, 0xfd339fed, 0xb87834bf, 0x5f04456d, 0x22258698, 0xc9c4c83b, 0x2dc156be, 0x4f628daa, 0x57f55ec5, 0xe2220abe, 0xd2916ebf, 0x4ec75b95, 0x24f2c3c0, 0x42d15d99, 0xcd0d7fa0, 0x7b6e27ff, 0xa8dc8af0, 0x7345c106, 0xf41e232f, 0x35162386, 0xe6ea8926, 0x3333b094, 0x157ec6f2, 0x372b74af, 0x692573e4, 0xe9a9d848, 0xf3160289, 0x3a62ef1d, 0xa787e238, 0xf3a5f676, 0x74364853, 0x20951063, 0x4576698d, 0xb6fad407, 0x592af950, 0x36f73523, 0x4cfb6e87, 0x7da4cec0, 0x6c152daa, 0xcb0396a8, 0xc50dfe5d, 0xfcd707ab, 0x0921c42f, 0x89dff0bb, 0x5fe2be78, 0x448f4f33, 0x754613c9, 0x2b05d08d, 0x48b9d585, 0xdc049441, 0xc8098f9b, 0x7dede786, 0xc39a3373, 0x42410005, 0x6a091751, 0x0ef3c8a6, 0x890072d6, 0x28207682, 0xa9a9f7be, 0xbf32679d, 0xd45b5b75, 0xb353fd00, 0xcbb0e358, 0x830f220a, 0x1f8fb214, 0xd372cf08, 0xcc3c4a13, 0x8cf63166, 0x061c87be, 0x88c98f88, 0x6062e397, 0x47cf8e7a, 0xb6c85283, 0x3cc2acfb, 0x3fc06976, 0x4e8f0252, 0x64d8314d, 0xda3870e3, 0x1e665459, 0xc10908f0, 0x513021a5, 0x6c5b68b7, 0x822f8aa0, 0x3007cd3e, 0x74719eef, 0xdc872681, 0x073340d4, 0x7e432fd9, 0x0c5ec241, 0x8809286c, 0xf592d891, 0x08a930f6, 0x957ef305, 0xb7fbffbd, 0xc266e96f, 0x6fe4ac98, 0xb173ecc0, 0xbc60b42a, 0x953498da, 0xfba1ae12, 0x2d4bd736, 0x0f25faab, 0xa4f3fceb, 0xe2969123, 0x257f0c3d, 0x9348af49, 0x361400bc, 0xe8816f4a, 0x3814f200, 0xa3f94043, 0x9c7a54c2, 0xbc704f57, 0xda41e7f9, 0xc25ad33a, 0x54f4a084, 0xb17f5505, 0x59357cbe, 0xedbd15c8, 0x7f97c5ab, 0xba5ac7b5, 0xb6f6deaf, 0x3a479c3a, 0x5302da25, 0x653d7e6a, 0x54268d49, 0x51a477ea, 0x5017d55b, 0xd7d25d88, 0x44136c76, 0x0404a8c8, 0xb8e5a121, 0xb81a928a, 0x60ed5869, 0x97c55b96, 0xeaec991b, 0x29935913, 0x01fdb7f1, 0x088e8dfa, 0x9ab6f6f5, 0x3b4cbf9f, 0x4a5de3ab, 0xe6051d35, 0xa0e1d855, 0xd36b4cf1, 0xf544edeb, 0xb0e93524, 0xbebb8fbd, 0xa2d762cf, 0x49c92f54, 0x38b5f331, 0x7128a454, 0x48392905, 0xa65b1db8, 0x851c97bd, 0xd675cf2f);
  5053. sBox[6] = new Array(0x85e04019, 0x332bf567, 0x662dbfff, 0xcfc65693, 0x2a8d7f6f, 0xab9bc912, 0xde6008a1, 0x2028da1f, 0x0227bce7, 0x4d642916, 0x18fac300, 0x50f18b82, 0x2cb2cb11, 0xb232e75c, 0x4b3695f2, 0xb28707de, 0xa05fbcf6, 0xcd4181e9, 0xe150210c, 0xe24ef1bd, 0xb168c381, 0xfde4e789, 0x5c79b0d8, 0x1e8bfd43, 0x4d495001, 0x38be4341, 0x913cee1d, 0x92a79c3f, 0x089766be, 0xbaeeadf4, 0x1286becf, 0xb6eacb19, 0x2660c200, 0x7565bde4, 0x64241f7a, 0x8248dca9, 0xc3b3ad66, 0x28136086, 0x0bd8dfa8, 0x356d1cf2, 0x107789be, 0xb3b2e9ce, 0x0502aa8f, 0x0bc0351e, 0x166bf52a, 0xeb12ff82, 0xe3486911, 0xd34d7516, 0x4e7b3aff, 0x5f43671b, 0x9cf6e037, 0x4981ac83, 0x334266ce, 0x8c9341b7, 0xd0d854c0, 0xcb3a6c88, 0x47bc2829, 0x4725ba37, 0xa66ad22b, 0x7ad61f1e, 0x0c5cbafa, 0x4437f107, 0xb6e79962, 0x42d2d816, 0x0a961288, 0xe1a5c06e, 0x13749e67, 0x72fc081a, 0xb1d139f7, 0xf9583745, 0xcf19df58, 0xbec3f756, 0xc06eba30, 0x07211b24, 0x45c28829, 0xc95e317f, 0xbc8ec511, 0x38bc46e9, 0xc6e6fa14, 0xbae8584a, 0xad4ebc46, 0x468f508b, 0x7829435f, 0xf124183b, 0x821dba9f, 0xaff60ff4, 0xea2c4e6d, 0x16e39264, 0x92544a8b, 0x009b4fc3, 0xaba68ced, 0x9ac96f78, 0x06a5b79a, 0xb2856e6e, 0x1aec3ca9, 0xbe838688, 0x0e0804e9, 0x55f1be56, 0xe7e5363b, 0xb3a1f25d, 0xf7debb85, 0x61fe033c, 0x16746233, 0x3c034c28, 0xda6d0c74, 0x79aac56c, 0x3ce4e1ad, 0x51f0c802, 0x98f8f35a, 0x1626a49f, 0xeed82b29, 0x1d382fe3, 0x0c4fb99a, 0xbb325778, 0x3ec6d97b, 0x6e77a6a9, 0xcb658b5c, 0xd45230c7, 0x2bd1408b, 0x60c03eb7, 0xb9068d78, 0xa33754f4, 0xf430c87d, 0xc8a71302, 0xb96d8c32, 0xebd4e7be, 0xbe8b9d2d, 0x7979fb06, 0xe7225308, 0x8b75cf77, 0x11ef8da4, 0xe083c858, 0x8d6b786f, 0x5a6317a6, 0xfa5cf7a0, 0x5dda0033, 0xf28ebfb0, 0xf5b9c310, 0xa0eac280, 0x08b9767a, 0xa3d9d2b0, 0x79d34217, 0x021a718d, 0x9ac6336a, 0x2711fd60, 0x438050e3, 0x069908a8, 0x3d7fedc4, 0x826d2bef, 0x4eeb8476, 0x488dcf25, 0x36c9d566, 0x28e74e41, 0xc2610aca, 0x3d49a9cf, 0xbae3b9df, 0xb65f8de6, 0x92aeaf64, 0x3ac7d5e6, 0x9ea80509, 0xf22b017d, 0xa4173f70, 0xdd1e16c3, 0x15e0d7f9, 0x50b1b887, 0x2b9f4fd5, 0x625aba82, 0x6a017962, 0x2ec01b9c, 0x15488aa9, 0xd716e740, 0x40055a2c, 0x93d29a22, 0xe32dbf9a, 0x058745b9, 0x3453dc1e, 0xd699296e, 0x496cff6f, 0x1c9f4986, 0xdfe2ed07, 0xb87242d1, 0x19de7eae, 0x053e561a, 0x15ad6f8c, 0x66626c1c, 0x7154c24c, 0xea082b2a, 0x93eb2939, 0x17dcb0f0, 0x58d4f2ae, 0x9ea294fb, 0x52cf564c, 0x9883fe66, 0x2ec40581, 0x763953c3, 0x01d6692e, 0xd3a0c108, 0xa1e7160e, 0xe4f2dfa6, 0x693ed285, 0x74904698, 0x4c2b0edd, 0x4f757656, 0x5d393378, 0xa132234f, 0x3d321c5d, 0xc3f5e194, 0x4b269301, 0xc79f022f, 0x3c997e7e, 0x5e4f9504, 0x3ffafbbd, 0x76f7ad0e, 0x296693f4, 0x3d1fce6f, 0xc61e45be, 0xd3b5ab34, 0xf72bf9b7, 0x1b0434c0, 0x4e72b567, 0x5592a33d, 0xb5229301, 0xcfd2a87f, 0x60aeb767, 0x1814386b, 0x30bcc33d, 0x38a0c07d, 0xfd1606f2, 0xc363519b, 0x589dd390, 0x5479f8e6, 0x1cb8d647, 0x97fd61a9, 0xea7759f4, 0x2d57539d, 0x569a58cf, 0xe84e63ad, 0x462e1b78, 0x6580f87e, 0xf3817914, 0x91da55f4, 0x40a230f3, 0xd1988f35, 0xb6e318d2, 0x3ffa50bc, 0x3d40f021, 0xc3c0bdae, 0x4958c24c, 0x518f36b2, 0x84b1d370, 0x0fedce83, 0x878ddada, 0xf2a279c7, 0x94e01be8, 0x90716f4b, 0x954b8aa3);
  5054. sBox[7] = new Array(0xe216300d, 0xbbddfffc, 0xa7ebdabd, 0x35648095, 0x7789f8b7, 0xe6c1121b, 0x0e241600, 0x052ce8b5, 0x11a9cfb0, 0xe5952f11, 0xece7990a, 0x9386d174, 0x2a42931c, 0x76e38111, 0xb12def3a, 0x37ddddfc, 0xde9adeb1, 0x0a0cc32c, 0xbe197029, 0x84a00940, 0xbb243a0f, 0xb4d137cf, 0xb44e79f0, 0x049eedfd, 0x0b15a15d, 0x480d3168, 0x8bbbde5a, 0x669ded42, 0xc7ece831, 0x3f8f95e7, 0x72df191b, 0x7580330d, 0x94074251, 0x5c7dcdfa, 0xabbe6d63, 0xaa402164, 0xb301d40a, 0x02e7d1ca, 0x53571dae, 0x7a3182a2, 0x12a8ddec, 0xfdaa335d, 0x176f43e8, 0x71fb46d4, 0x38129022, 0xce949ad4, 0xb84769ad, 0x965bd862, 0x82f3d055, 0x66fb9767, 0x15b80b4e, 0x1d5b47a0, 0x4cfde06f, 0xc28ec4b8, 0x57e8726e, 0x647a78fc, 0x99865d44, 0x608bd593, 0x6c200e03, 0x39dc5ff6, 0x5d0b00a3, 0xae63aff2, 0x7e8bd632, 0x70108c0c, 0xbbd35049, 0x2998df04, 0x980cf42a, 0x9b6df491, 0x9e7edd53, 0x06918548, 0x58cb7e07, 0x3b74ef2e, 0x522fffb1, 0xd24708cc, 0x1c7e27cd, 0xa4eb215b, 0x3cf1d2e2, 0x19b47a38, 0x424f7618, 0x35856039, 0x9d17dee7, 0x27eb35e6, 0xc9aff67b, 0x36baf5b8, 0x09c467cd, 0xc18910b1, 0xe11dbf7b, 0x06cd1af8, 0x7170c608, 0x2d5e3354, 0xd4de495a, 0x64c6d006, 0xbcc0c62c, 0x3dd00db3, 0x708f8f34, 0x77d51b42, 0x264f620f, 0x24b8d2bf, 0x15c1b79e, 0x46a52564, 0xf8d7e54e, 0x3e378160, 0x7895cda5, 0x859c15a5, 0xe6459788, 0xc37bc75f, 0xdb07ba0c, 0x0676a3ab, 0x7f229b1e, 0x31842e7b, 0x24259fd7, 0xf8bef472, 0x835ffcb8, 0x6df4c1f2, 0x96f5b195, 0xfd0af0fc, 0xb0fe134c, 0xe2506d3d, 0x4f9b12ea, 0xf215f225, 0xa223736f, 0x9fb4c428, 0x25d04979, 0x34c713f8, 0xc4618187, 0xea7a6e98, 0x7cd16efc, 0x1436876c, 0xf1544107, 0xbedeee14, 0x56e9af27, 0xa04aa441, 0x3cf7c899, 0x92ecbae6, 0xdd67016d, 0x151682eb, 0xa842eedf, 0xfdba60b4, 0xf1907b75, 0x20e3030f, 0x24d8c29e, 0xe139673b, 0xefa63fb8, 0x71873054, 0xb6f2cf3b, 0x9f326442, 0xcb15a4cc, 0xb01a4504, 0xf1e47d8d, 0x844a1be5, 0xbae7dfdc, 0x42cbda70, 0xcd7dae0a, 0x57e85b7a, 0xd53f5af6, 0x20cf4d8c, 0xcea4d428, 0x79d130a4, 0x3486ebfb, 0x33d3cddc, 0x77853b53, 0x37effcb5, 0xc5068778, 0xe580b3e6, 0x4e68b8f4, 0xc5c8b37e, 0x0d809ea2, 0x398feb7c, 0x132a4f94, 0x43b7950e, 0x2fee7d1c, 0x223613bd, 0xdd06caa2, 0x37df932b, 0xc4248289, 0xacf3ebc3, 0x5715f6b7, 0xef3478dd, 0xf267616f, 0xc148cbe4, 0x9052815e, 0x5e410fab, 0xb48a2465, 0x2eda7fa4, 0xe87b40e4, 0xe98ea084, 0x5889e9e1, 0xefd390fc, 0xdd07d35b, 0xdb485694, 0x38d7e5b2, 0x57720101, 0x730edebc, 0x5b643113, 0x94917e4f, 0x503c2fba, 0x646f1282, 0x7523d24a, 0xe0779695, 0xf9c17a8f, 0x7a5b2121, 0xd187b896, 0x29263a4d, 0xba510cdf, 0x81f47c9f, 0xad1163ed, 0xea7b5965, 0x1a00726e, 0x11403092, 0x00da6d77, 0x4a0cdd61, 0xad1f4603, 0x605bdfb0, 0x9eedc364, 0x22ebe6a8, 0xcee7d28a, 0xa0e736a0, 0x5564a6b9, 0x10853209, 0xc7eb8f37, 0x2de705ca, 0x8951570f, 0xdf09822b, 0xbd691a6c, 0xaa12e4f2, 0x87451c0f, 0xe0f6a27a, 0x3ada4819, 0x4cf1764f, 0x0d771c2b, 0x67cdb156, 0x350d8384, 0x5938fa0f, 0x42399ef3, 0x36997b07, 0x0e84093d, 0x4aa93e61, 0x8360d87b, 0x1fa98b0c, 0x1149382c, 0xe97625a5, 0x0614d1b7, 0x0e25244b, 0x0c768347, 0x589e8d82, 0x0d2059d1, 0xa466bb1e, 0xf8da0a82, 0x04f19130, 0xba6e4ec0, 0x99265164, 0x1ee7230d, 0x50b2ad80, 0xeaee6801, 0x8db2a283, 0xea8bf59e);
  5055. }
  5056. function Cast5(key) {
  5057. this.cast5 = new OpenpgpSymencCast5();
  5058. this.cast5.setKey(key);
  5059. this.encrypt = function (block) {
  5060. return this.cast5.encrypt(block);
  5061. };
  5062. }
  5063. Cast5.blockSize = Cast5.prototype.blockSize = 8;
  5064. Cast5.keySize = Cast5.prototype.keySize = 16;
  5065. },{}],15:[function(_dereq_,module,exports){
  5066. //Paul Tero, July 2001
  5067. //http://www.tero.co.uk/des/
  5068. //
  5069. //Optimised for performance with large blocks by Michael Hayworth, November 2001
  5070. //http://www.netdealing.com
  5071. //
  5072. // Modified by Recurity Labs GmbH
  5073. //THIS SOFTWARE IS PROVIDED "AS IS" AND
  5074. //ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  5075. //IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  5076. //ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  5077. //FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  5078. //DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  5079. //OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  5080. //HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  5081. //LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  5082. //OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  5083. //SUCH DAMAGE.
  5084. //des
  5085. //this takes the key, the message, and whether to encrypt or decrypt
  5086. /**
  5087. * @module crypto/cipher/des
  5088. */
  5089. 'use strict';
  5090. Object.defineProperty(exports, "__esModule", {
  5091. value: true
  5092. });
  5093. function des(keys, message, encrypt, mode, iv, padding) {
  5094. //declaring this locally speeds things up a bit
  5095. var spfunction1 = new Array(0x1010400, 0, 0x10000, 0x1010404, 0x1010004, 0x10404, 0x4, 0x10000, 0x400, 0x1010400, 0x1010404, 0x400, 0x1000404, 0x1010004, 0x1000000, 0x4, 0x404, 0x1000400, 0x1000400, 0x10400, 0x10400, 0x1010000, 0x1010000, 0x1000404, 0x10004, 0x1000004, 0x1000004, 0x10004, 0, 0x404, 0x10404, 0x1000000, 0x10000, 0x1010404, 0x4, 0x1010000, 0x1010400, 0x1000000, 0x1000000, 0x400, 0x1010004, 0x10000, 0x10400, 0x1000004, 0x400, 0x4, 0x1000404, 0x10404, 0x1010404, 0x10004, 0x1010000, 0x1000404, 0x1000004, 0x404, 0x10404, 0x1010400, 0x404, 0x1000400, 0x1000400, 0, 0x10004, 0x10400, 0, 0x1010004);
  5096. var spfunction2 = new Array(-0x7fef7fe0, -0x7fff8000, 0x8000, 0x108020, 0x100000, 0x20, -0x7fefffe0, -0x7fff7fe0, -0x7fffffe0, -0x7fef7fe0, -0x7fef8000, -0x80000000, -0x7fff8000, 0x100000, 0x20, -0x7fefffe0, 0x108000, 0x100020, -0x7fff7fe0, 0, -0x80000000, 0x8000, 0x108020, -0x7ff00000, 0x100020, -0x7fffffe0, 0, 0x108000, 0x8020, -0x7fef8000, -0x7ff00000, 0x8020, 0, 0x108020, -0x7fefffe0, 0x100000, -0x7fff7fe0, -0x7ff00000, -0x7fef8000, 0x8000, -0x7ff00000, -0x7fff8000, 0x20, -0x7fef7fe0, 0x108020, 0x20, 0x8000, -0x80000000, 0x8020, -0x7fef8000, 0x100000, -0x7fffffe0, 0x100020, -0x7fff7fe0, -0x7fffffe0, 0x100020, 0x108000, 0, -0x7fff8000, 0x8020, -0x80000000, -0x7fefffe0, -0x7fef7fe0, 0x108000);
  5097. var spfunction3 = new Array(0x208, 0x8020200, 0, 0x8020008, 0x8000200, 0, 0x20208, 0x8000200, 0x20008, 0x8000008, 0x8000008, 0x20000, 0x8020208, 0x20008, 0x8020000, 0x208, 0x8000000, 0x8, 0x8020200, 0x200, 0x20200, 0x8020000, 0x8020008, 0x20208, 0x8000208, 0x20200, 0x20000, 0x8000208, 0x8, 0x8020208, 0x200, 0x8000000, 0x8020200, 0x8000000, 0x20008, 0x208, 0x20000, 0x8020200, 0x8000200, 0, 0x200, 0x20008, 0x8020208, 0x8000200, 0x8000008, 0x200, 0, 0x8020008, 0x8000208, 0x20000, 0x8000000, 0x8020208, 0x8, 0x20208, 0x20200, 0x8000008, 0x8020000, 0x8000208, 0x208, 0x8020000, 0x20208, 0x8, 0x8020008, 0x20200);
  5098. var spfunction4 = new Array(0x802001, 0x2081, 0x2081, 0x80, 0x802080, 0x800081, 0x800001, 0x2001, 0, 0x802000, 0x802000, 0x802081, 0x81, 0, 0x800080, 0x800001, 0x1, 0x2000, 0x800000, 0x802001, 0x80, 0x800000, 0x2001, 0x2080, 0x800081, 0x1, 0x2080, 0x800080, 0x2000, 0x802080, 0x802081, 0x81, 0x800080, 0x800001, 0x802000, 0x802081, 0x81, 0, 0, 0x802000, 0x2080, 0x800080, 0x800081, 0x1, 0x802001, 0x2081, 0x2081, 0x80, 0x802081, 0x81, 0x1, 0x2000, 0x800001, 0x2001, 0x802080, 0x800081, 0x2001, 0x2080, 0x800000, 0x802001, 0x80, 0x800000, 0x2000, 0x802080);
  5099. var spfunction5 = new Array(0x100, 0x2080100, 0x2080000, 0x42000100, 0x80000, 0x100, 0x40000000, 0x2080000, 0x40080100, 0x80000, 0x2000100, 0x40080100, 0x42000100, 0x42080000, 0x80100, 0x40000000, 0x2000000, 0x40080000, 0x40080000, 0, 0x40000100, 0x42080100, 0x42080100, 0x2000100, 0x42080000, 0x40000100, 0, 0x42000000, 0x2080100, 0x2000000, 0x42000000, 0x80100, 0x80000, 0x42000100, 0x100, 0x2000000, 0x40000000, 0x2080000, 0x42000100, 0x40080100, 0x2000100, 0x40000000, 0x42080000, 0x2080100, 0x40080100, 0x100, 0x2000000, 0x42080000, 0x42080100, 0x80100, 0x42000000, 0x42080100, 0x2080000, 0, 0x40080000, 0x42000000, 0x80100, 0x2000100, 0x40000100, 0x80000, 0, 0x40080000, 0x2080100, 0x40000100);
  5100. var spfunction6 = new Array(0x20000010, 0x20400000, 0x4000, 0x20404010, 0x20400000, 0x10, 0x20404010, 0x400000, 0x20004000, 0x404010, 0x400000, 0x20000010, 0x400010, 0x20004000, 0x20000000, 0x4010, 0, 0x400010, 0x20004010, 0x4000, 0x404000, 0x20004010, 0x10, 0x20400010, 0x20400010, 0, 0x404010, 0x20404000, 0x4010, 0x404000, 0x20404000, 0x20000000, 0x20004000, 0x10, 0x20400010, 0x404000, 0x20404010, 0x400000, 0x4010, 0x20000010, 0x400000, 0x20004000, 0x20000000, 0x4010, 0x20000010, 0x20404010, 0x404000, 0x20400000, 0x404010, 0x20404000, 0, 0x20400010, 0x10, 0x4000, 0x20400000, 0x404010, 0x4000, 0x400010, 0x20004010, 0, 0x20404000, 0x20000000, 0x400010, 0x20004010);
  5101. var spfunction7 = new Array(0x200000, 0x4200002, 0x4000802, 0, 0x800, 0x4000802, 0x200802, 0x4200800, 0x4200802, 0x200000, 0, 0x4000002, 0x2, 0x4000000, 0x4200002, 0x802, 0x4000800, 0x200802, 0x200002, 0x4000800, 0x4000002, 0x4200000, 0x4200800, 0x200002, 0x4200000, 0x800, 0x802, 0x4200802, 0x200800, 0x2, 0x4000000, 0x200800, 0x4000000, 0x200800, 0x200000, 0x4000802, 0x4000802, 0x4200002, 0x4200002, 0x2, 0x200002, 0x4000000, 0x4000800, 0x200000, 0x4200800, 0x802, 0x200802, 0x4200800, 0x802, 0x4000002, 0x4200802, 0x4200000, 0x200800, 0, 0x2, 0x4200802, 0, 0x200802, 0x4200000, 0x800, 0x4000002, 0x4000800, 0x800, 0x200002);
  5102. var spfunction8 = new Array(0x10001040, 0x1000, 0x40000, 0x10041040, 0x10000000, 0x10001040, 0x40, 0x10000000, 0x40040, 0x10040000, 0x10041040, 0x41000, 0x10041000, 0x41040, 0x1000, 0x40, 0x10040000, 0x10000040, 0x10001000, 0x1040, 0x41000, 0x40040, 0x10040040, 0x10041000, 0x1040, 0, 0, 0x10040040, 0x10000040, 0x10001000, 0x41040, 0x40000, 0x41040, 0x40000, 0x10041000, 0x1000, 0x40, 0x10040040, 0x1000, 0x41040, 0x10001000, 0x40, 0x10000040, 0x10040000, 0x10040040, 0x10000000, 0x40000, 0x10001040, 0, 0x10041040, 0x40040, 0x10000040, 0x10040000, 0x10001000, 0x10001040, 0, 0x10041040, 0x41000, 0x41000, 0x1040, 0x1040, 0x40040, 0x10000000, 0x10041000);
  5103. //create the 16 or 48 subkeys we will need
  5104. var m = 0,
  5105. i,
  5106. j,
  5107. temp,
  5108. right1,
  5109. right2,
  5110. left,
  5111. right,
  5112. looping;
  5113. var cbcleft, cbcleft2, cbcright, cbcright2;
  5114. var endloop, loopinc;
  5115. var len = message.length;
  5116. //set up the loops for single and triple des
  5117. var iterations = keys.length === 32 ? 3 : 9; //single or triple des
  5118. if (iterations === 3) {
  5119. looping = encrypt ? new Array(0, 32, 2) : new Array(30, -2, -2);
  5120. } else {
  5121. looping = encrypt ? new Array(0, 32, 2, 62, 30, -2, 64, 96, 2) : new Array(94, 62, -2, 32, 64, 2, 30, -2, -2);
  5122. }
  5123. //pad the message depending on the padding parameter
  5124. //only add padding if encrypting - note that you need to use the same padding option for both encrypt and decrypt
  5125. if (encrypt) {
  5126. message = des_addPadding(message, padding);
  5127. len = message.length;
  5128. }
  5129. //store the result here
  5130. var result = new Uint8Array(len);
  5131. var k = 0;
  5132. if (mode === 1) {
  5133. //CBC mode
  5134. cbcleft = iv[m++] << 24 | iv[m++] << 16 | iv[m++] << 8 | iv[m++];
  5135. cbcright = iv[m++] << 24 | iv[m++] << 16 | iv[m++] << 8 | iv[m++];
  5136. m = 0;
  5137. }
  5138. //loop through each 64 bit chunk of the message
  5139. while (m < len) {
  5140. left = message[m++] << 24 | message[m++] << 16 | message[m++] << 8 | message[m++];
  5141. right = message[m++] << 24 | message[m++] << 16 | message[m++] << 8 | message[m++];
  5142. //for Cipher Block Chaining mode, xor the message with the previous result
  5143. if (mode === 1) {
  5144. if (encrypt) {
  5145. left ^= cbcleft;
  5146. right ^= cbcright;
  5147. } else {
  5148. cbcleft2 = cbcleft;
  5149. cbcright2 = cbcright;
  5150. cbcleft = left;
  5151. cbcright = right;
  5152. }
  5153. }
  5154. //first each 64 but chunk of the message must be permuted according to IP
  5155. temp = (left >>> 4 ^ right) & 0x0f0f0f0f;
  5156. right ^= temp;
  5157. left ^= temp << 4;
  5158. temp = (left >>> 16 ^ right) & 0x0000ffff;
  5159. right ^= temp;
  5160. left ^= temp << 16;
  5161. temp = (right >>> 2 ^ left) & 0x33333333;
  5162. left ^= temp;
  5163. right ^= temp << 2;
  5164. temp = (right >>> 8 ^ left) & 0x00ff00ff;
  5165. left ^= temp;
  5166. right ^= temp << 8;
  5167. temp = (left >>> 1 ^ right) & 0x55555555;
  5168. right ^= temp;
  5169. left ^= temp << 1;
  5170. left = left << 1 | left >>> 31;
  5171. right = right << 1 | right >>> 31;
  5172. //do this either 1 or 3 times for each chunk of the message
  5173. for (j = 0; j < iterations; j += 3) {
  5174. endloop = looping[j + 1];
  5175. loopinc = looping[j + 2];
  5176. //now go through and perform the encryption or decryption
  5177. for (i = looping[j]; i !== endloop; i += loopinc) {
  5178. //for efficiency
  5179. right1 = right ^ keys[i];
  5180. right2 = (right >>> 4 | right << 28) ^ keys[i + 1];
  5181. //the result is attained by passing these bytes through the S selection functions
  5182. temp = left;
  5183. left = right;
  5184. right = temp ^ (spfunction2[right1 >>> 24 & 0x3f] | spfunction4[right1 >>> 16 & 0x3f] | spfunction6[right1 >>> 8 & 0x3f] | spfunction8[right1 & 0x3f] | spfunction1[right2 >>> 24 & 0x3f] | spfunction3[right2 >>> 16 & 0x3f] | spfunction5[right2 >>> 8 & 0x3f] | spfunction7[right2 & 0x3f]);
  5185. }
  5186. temp = left;
  5187. left = right;
  5188. right = temp; //unreverse left and right
  5189. } //for either 1 or 3 iterations
  5190. //move then each one bit to the right
  5191. left = left >>> 1 | left << 31;
  5192. right = right >>> 1 | right << 31;
  5193. //now perform IP-1, which is IP in the opposite direction
  5194. temp = (left >>> 1 ^ right) & 0x55555555;
  5195. right ^= temp;
  5196. left ^= temp << 1;
  5197. temp = (right >>> 8 ^ left) & 0x00ff00ff;
  5198. left ^= temp;
  5199. right ^= temp << 8;
  5200. temp = (right >>> 2 ^ left) & 0x33333333;
  5201. left ^= temp;
  5202. right ^= temp << 2;
  5203. temp = (left >>> 16 ^ right) & 0x0000ffff;
  5204. right ^= temp;
  5205. left ^= temp << 16;
  5206. temp = (left >>> 4 ^ right) & 0x0f0f0f0f;
  5207. right ^= temp;
  5208. left ^= temp << 4;
  5209. //for Cipher Block Chaining mode, xor the message with the previous result
  5210. if (mode === 1) {
  5211. if (encrypt) {
  5212. cbcleft = left;
  5213. cbcright = right;
  5214. } else {
  5215. left ^= cbcleft2;
  5216. right ^= cbcright2;
  5217. }
  5218. }
  5219. result[k++] = left >>> 24;
  5220. result[k++] = left >>> 16 & 0xff;
  5221. result[k++] = left >>> 8 & 0xff;
  5222. result[k++] = left & 0xff;
  5223. result[k++] = right >>> 24;
  5224. result[k++] = right >>> 16 & 0xff;
  5225. result[k++] = right >>> 8 & 0xff;
  5226. result[k++] = right & 0xff;
  5227. } //for every 8 characters, or 64 bits in the message
  5228. //only remove padding if decrypting - note that you need to use the same padding option for both encrypt and decrypt
  5229. if (!encrypt) {
  5230. result = des_removePadding(result, padding);
  5231. }
  5232. return result;
  5233. } //end of des
  5234. //des_createKeys
  5235. //this takes as input a 64 bit key (even though only 56 bits are used)
  5236. //as an array of 2 integers, and returns 16 48 bit keys
  5237. function des_createKeys(key) {
  5238. //declaring this locally speeds things up a bit
  5239. var pc2bytes0 = new Array(0, 0x4, 0x20000000, 0x20000004, 0x10000, 0x10004, 0x20010000, 0x20010004, 0x200, 0x204, 0x20000200, 0x20000204, 0x10200, 0x10204, 0x20010200, 0x20010204);
  5240. var pc2bytes1 = new Array(0, 0x1, 0x100000, 0x100001, 0x4000000, 0x4000001, 0x4100000, 0x4100001, 0x100, 0x101, 0x100100, 0x100101, 0x4000100, 0x4000101, 0x4100100, 0x4100101);
  5241. var pc2bytes2 = new Array(0, 0x8, 0x800, 0x808, 0x1000000, 0x1000008, 0x1000800, 0x1000808, 0, 0x8, 0x800, 0x808, 0x1000000, 0x1000008, 0x1000800, 0x1000808);
  5242. var pc2bytes3 = new Array(0, 0x200000, 0x8000000, 0x8200000, 0x2000, 0x202000, 0x8002000, 0x8202000, 0x20000, 0x220000, 0x8020000, 0x8220000, 0x22000, 0x222000, 0x8022000, 0x8222000);
  5243. var pc2bytes4 = new Array(0, 0x40000, 0x10, 0x40010, 0, 0x40000, 0x10, 0x40010, 0x1000, 0x41000, 0x1010, 0x41010, 0x1000, 0x41000, 0x1010, 0x41010);
  5244. var pc2bytes5 = new Array(0, 0x400, 0x20, 0x420, 0, 0x400, 0x20, 0x420, 0x2000000, 0x2000400, 0x2000020, 0x2000420, 0x2000000, 0x2000400, 0x2000020, 0x2000420);
  5245. var pc2bytes6 = new Array(0, 0x10000000, 0x80000, 0x10080000, 0x2, 0x10000002, 0x80002, 0x10080002, 0, 0x10000000, 0x80000, 0x10080000, 0x2, 0x10000002, 0x80002, 0x10080002);
  5246. var pc2bytes7 = new Array(0, 0x10000, 0x800, 0x10800, 0x20000000, 0x20010000, 0x20000800, 0x20010800, 0x20000, 0x30000, 0x20800, 0x30800, 0x20020000, 0x20030000, 0x20020800, 0x20030800);
  5247. var pc2bytes8 = new Array(0, 0x40000, 0, 0x40000, 0x2, 0x40002, 0x2, 0x40002, 0x2000000, 0x2040000, 0x2000000, 0x2040000, 0x2000002, 0x2040002, 0x2000002, 0x2040002);
  5248. var pc2bytes9 = new Array(0, 0x10000000, 0x8, 0x10000008, 0, 0x10000000, 0x8, 0x10000008, 0x400, 0x10000400, 0x408, 0x10000408, 0x400, 0x10000400, 0x408, 0x10000408);
  5249. var pc2bytes10 = new Array(0, 0x20, 0, 0x20, 0x100000, 0x100020, 0x100000, 0x100020, 0x2000, 0x2020, 0x2000, 0x2020, 0x102000, 0x102020, 0x102000, 0x102020);
  5250. var pc2bytes11 = new Array(0, 0x1000000, 0x200, 0x1000200, 0x200000, 0x1200000, 0x200200, 0x1200200, 0x4000000, 0x5000000, 0x4000200, 0x5000200, 0x4200000, 0x5200000, 0x4200200, 0x5200200);
  5251. var pc2bytes12 = new Array(0, 0x1000, 0x8000000, 0x8001000, 0x80000, 0x81000, 0x8080000, 0x8081000, 0x10, 0x1010, 0x8000010, 0x8001010, 0x80010, 0x81010, 0x8080010, 0x8081010);
  5252. var pc2bytes13 = new Array(0, 0x4, 0x100, 0x104, 0, 0x4, 0x100, 0x104, 0x1, 0x5, 0x101, 0x105, 0x1, 0x5, 0x101, 0x105);
  5253. //how many iterations (1 for des, 3 for triple des)
  5254. var iterations = key.length > 8 ? 3 : 1; //changed by Paul 16/6/2007 to use Triple DES for 9+ byte keys
  5255. //stores the return keys
  5256. var keys = new Array(32 * iterations);
  5257. //now define the left shifts which need to be done
  5258. var shifts = new Array(0, 0, 1, 1, 1, 1, 1, 1, 0, 1, 1, 1, 1, 1, 1, 0);
  5259. //other variables
  5260. var lefttemp,
  5261. righttemp,
  5262. m = 0,
  5263. n = 0,
  5264. temp;
  5265. for (var j = 0; j < iterations; j++) {
  5266. //either 1 or 3 iterations
  5267. var left = key[m++] << 24 | key[m++] << 16 | key[m++] << 8 | key[m++];
  5268. var right = key[m++] << 24 | key[m++] << 16 | key[m++] << 8 | key[m++];
  5269. temp = (left >>> 4 ^ right) & 0x0f0f0f0f;
  5270. right ^= temp;
  5271. left ^= temp << 4;
  5272. temp = (right >>> -16 ^ left) & 0x0000ffff;
  5273. left ^= temp;
  5274. right ^= temp << -16;
  5275. temp = (left >>> 2 ^ right) & 0x33333333;
  5276. right ^= temp;
  5277. left ^= temp << 2;
  5278. temp = (right >>> -16 ^ left) & 0x0000ffff;
  5279. left ^= temp;
  5280. right ^= temp << -16;
  5281. temp = (left >>> 1 ^ right) & 0x55555555;
  5282. right ^= temp;
  5283. left ^= temp << 1;
  5284. temp = (right >>> 8 ^ left) & 0x00ff00ff;
  5285. left ^= temp;
  5286. right ^= temp << 8;
  5287. temp = (left >>> 1 ^ right) & 0x55555555;
  5288. right ^= temp;
  5289. left ^= temp << 1;
  5290. //the right side needs to be shifted and to get the last four bits of the left side
  5291. temp = left << 8 | right >>> 20 & 0x000000f0;
  5292. //left needs to be put upside down
  5293. left = right << 24 | right << 8 & 0xff0000 | right >>> 8 & 0xff00 | right >>> 24 & 0xf0;
  5294. right = temp;
  5295. //now go through and perform these shifts on the left and right keys
  5296. for (var i = 0; i < shifts.length; i++) {
  5297. //shift the keys either one or two bits to the left
  5298. if (shifts[i]) {
  5299. left = left << 2 | left >>> 26;
  5300. right = right << 2 | right >>> 26;
  5301. } else {
  5302. left = left << 1 | left >>> 27;
  5303. right = right << 1 | right >>> 27;
  5304. }
  5305. left &= -0xf;
  5306. right &= -0xf;
  5307. //now apply PC-2, in such a way that E is easier when encrypting or decrypting
  5308. //this conversion will look like PC-2 except only the last 6 bits of each byte are used
  5309. //rather than 48 consecutive bits and the order of lines will be according to
  5310. //how the S selection functions will be applied: S2, S4, S6, S8, S1, S3, S5, S7
  5311. lefttemp = pc2bytes0[left >>> 28] | pc2bytes1[left >>> 24 & 0xf] | pc2bytes2[left >>> 20 & 0xf] | pc2bytes3[left >>> 16 & 0xf] | pc2bytes4[left >>> 12 & 0xf] | pc2bytes5[left >>> 8 & 0xf] | pc2bytes6[left >>> 4 & 0xf];
  5312. righttemp = pc2bytes7[right >>> 28] | pc2bytes8[right >>> 24 & 0xf] | pc2bytes9[right >>> 20 & 0xf] | pc2bytes10[right >>> 16 & 0xf] | pc2bytes11[right >>> 12 & 0xf] | pc2bytes12[right >>> 8 & 0xf] | pc2bytes13[right >>> 4 & 0xf];
  5313. temp = (righttemp >>> 16 ^ lefttemp) & 0x0000ffff;
  5314. keys[n++] = lefttemp ^ temp;
  5315. keys[n++] = righttemp ^ temp << 16;
  5316. }
  5317. } //for each iterations
  5318. //return the keys we've created
  5319. return keys;
  5320. } //end of des_createKeys
  5321. function des_addPadding(message, padding) {
  5322. var padLength = 8 - message.length % 8;
  5323. var pad;
  5324. if (padding === 2 && padLength < 8) {
  5325. //pad the message with spaces
  5326. pad = " ".charCodeAt(0);
  5327. } else if (padding === 1) {
  5328. //PKCS7 padding
  5329. pad = padLength;
  5330. } else if (!padding && padLength < 8) {
  5331. //pad the message out with null bytes
  5332. pad = 0;
  5333. } else if (padLength === 8) {
  5334. return message;
  5335. } else {
  5336. throw new Error('des: invalid padding');
  5337. }
  5338. var paddedMessage = new Uint8Array(message.length + padLength);
  5339. for (var i = 0; i < message.length; i++) {
  5340. paddedMessage[i] = message[i];
  5341. }
  5342. for (var j = 0; j < padLength; j++) {
  5343. paddedMessage[message.length + j] = pad;
  5344. }
  5345. return paddedMessage;
  5346. }
  5347. function des_removePadding(message, padding) {
  5348. var padLength = null;
  5349. var pad;
  5350. if (padding === 2) {
  5351. // space padded
  5352. pad = " ".charCodeAt(0);
  5353. } else if (padding === 1) {
  5354. // PKCS7
  5355. padLength = message[message.length - 1];
  5356. } else if (!padding) {
  5357. // null padding
  5358. pad = 0;
  5359. } else {
  5360. throw new Error('des: invalid padding');
  5361. }
  5362. if (!padLength) {
  5363. padLength = 1;
  5364. while (message[message.length - padLength] === pad) {
  5365. padLength++;
  5366. }
  5367. padLength--;
  5368. }
  5369. return message.subarray(0, message.length - padLength);
  5370. }
  5371. // added by Recurity Labs
  5372. function Des(key) {
  5373. this.key = [];
  5374. for (var i = 0; i < 3; i++) {
  5375. this.key.push(new Uint8Array(key.subarray(i * 8, i * 8 + 8)));
  5376. }
  5377. this.encrypt = function (block) {
  5378. return des(des_createKeys(this.key[2]), des(des_createKeys(this.key[1]), des(des_createKeys(this.key[0]), block, true, 0, null, null), false, 0, null, null), true, 0, null, null);
  5379. };
  5380. }
  5381. Des.keySize = Des.prototype.keySize = 24;
  5382. Des.blockSize = Des.prototype.blockSize = 8;
  5383. // This is "original" DES - Des is actually Triple DES.
  5384. // This is only exported so we can unit test.
  5385. function OriginalDes(key) {
  5386. this.key = key;
  5387. this.encrypt = function (block, padding) {
  5388. var keys = des_createKeys(this.key);
  5389. return des(keys, block, true, 0, null, padding);
  5390. };
  5391. this.decrypt = function (block, padding) {
  5392. var keys = des_createKeys(this.key);
  5393. return des(keys, block, false, 0, null, padding);
  5394. };
  5395. }
  5396. exports.default = {
  5397. /** @static */
  5398. des: Des,
  5399. /** @static */
  5400. originalDes: OriginalDes
  5401. };
  5402. },{}],16:[function(_dereq_,module,exports){
  5403. /**
  5404. * @requires crypto/cipher/aes
  5405. * @requires crypto/cipher/blowfish
  5406. * @requires crypto/cipher/cast5
  5407. * @requires crypto/cipher/twofish
  5408. * @module crypto/cipher
  5409. */
  5410. 'use strict';
  5411. Object.defineProperty(exports, "__esModule", {
  5412. value: true
  5413. });
  5414. var _aes = _dereq_('./aes.js');
  5415. var _aes2 = _interopRequireDefault(_aes);
  5416. var _des = _dereq_('./des.js');
  5417. var _des2 = _interopRequireDefault(_des);
  5418. var _cast = _dereq_('./cast5.js');
  5419. var _cast2 = _interopRequireDefault(_cast);
  5420. var _twofish = _dereq_('./twofish.js');
  5421. var _twofish2 = _interopRequireDefault(_twofish);
  5422. var _blowfish = _dereq_('./blowfish.js');
  5423. var _blowfish2 = _interopRequireDefault(_blowfish);
  5424. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  5425. exports.default = {
  5426. /** @see module:crypto/cipher/aes */
  5427. aes128: _aes2.default[128],
  5428. aes192: _aes2.default[192],
  5429. aes256: _aes2.default[256],
  5430. /** @see module:crypto/cipher/des.originalDes */
  5431. des: _des2.default.originalDes,
  5432. /** @see module:crypto/cipher/des.des */
  5433. tripledes: _des2.default.des,
  5434. /** @see module:crypto/cipher/cast5 */
  5435. cast5: _cast2.default,
  5436. /** @see module:crypto/cipher/twofish */
  5437. twofish: _twofish2.default,
  5438. /** @see module:crypto/cipher/blowfish */
  5439. blowfish: _blowfish2.default,
  5440. /** Not implemented */
  5441. idea: function idea() {
  5442. throw new Error('IDEA symmetric-key algorithm not implemented');
  5443. }
  5444. };
  5445. },{"./aes.js":12,"./blowfish.js":13,"./cast5.js":14,"./des.js":15,"./twofish.js":17}],17:[function(_dereq_,module,exports){
  5446. "use strict";
  5447. Object.defineProperty(exports, "__esModule", {
  5448. value: true
  5449. });
  5450. exports.default = TF;
  5451. /* Modified by Recurity Labs GmbH
  5452. *
  5453. * Cipher.js
  5454. * A block-cipher algorithm implementation on JavaScript
  5455. * See Cipher.readme.txt for further information.
  5456. *
  5457. * Copyright(c) 2009 Atsushi Oka [ http://oka.nu/ ]
  5458. * This script file is distributed under the LGPL
  5459. *
  5460. * ACKNOWLEDGMENT
  5461. *
  5462. * The main subroutines are written by Michiel van Everdingen.
  5463. *
  5464. * Michiel van Everdingen
  5465. * http://home.versatel.nl/MAvanEverdingen/index.html
  5466. *
  5467. * All rights for these routines are reserved to Michiel van Everdingen.
  5468. *
  5469. */
  5470. /**
  5471. * @module crypto/cipher/twofish
  5472. */
  5473. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5474. //Math
  5475. ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5476. var MAXINT = 0xFFFFFFFF;
  5477. function rotw(w, n) {
  5478. return (w << n | w >>> 32 - n) & MAXINT;
  5479. }
  5480. function getW(a, i) {
  5481. return a[i] | a[i + 1] << 8 | a[i + 2] << 16 | a[i + 3] << 24;
  5482. }
  5483. function setW(a, i, w) {
  5484. a.splice(i, 4, w & 0xFF, w >>> 8 & 0xFF, w >>> 16 & 0xFF, w >>> 24 & 0xFF);
  5485. }
  5486. function getB(x, n) {
  5487. return x >>> n * 8 & 0xFF;
  5488. }
  5489. // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5490. // Twofish
  5491. // //////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
  5492. function createTwofish() {
  5493. //
  5494. var keyBytes = null;
  5495. var dataBytes = null;
  5496. var dataOffset = -1;
  5497. // var dataLength = -1;
  5498. var algorithmName = null;
  5499. // var idx2 = -1;
  5500. //
  5501. algorithmName = "twofish";
  5502. var tfsKey = [];
  5503. var tfsM = [[], [], [], []];
  5504. function tfsInit(key) {
  5505. keyBytes = key;
  5506. var i,
  5507. a,
  5508. b,
  5509. c,
  5510. d,
  5511. meKey = [],
  5512. moKey = [],
  5513. inKey = [];
  5514. var kLen;
  5515. var sKey = [];
  5516. var f01, f5b, fef;
  5517. var q0 = [[8, 1, 7, 13, 6, 15, 3, 2, 0, 11, 5, 9, 14, 12, 10, 4], [2, 8, 11, 13, 15, 7, 6, 14, 3, 1, 9, 4, 0, 10, 12, 5]];
  5518. var q1 = [[14, 12, 11, 8, 1, 2, 3, 5, 15, 4, 10, 6, 7, 0, 9, 13], [1, 14, 2, 11, 4, 12, 3, 7, 6, 13, 10, 5, 15, 9, 0, 8]];
  5519. var q2 = [[11, 10, 5, 14, 6, 13, 9, 0, 12, 8, 15, 3, 2, 4, 7, 1], [4, 12, 7, 5, 1, 6, 9, 10, 0, 14, 13, 8, 2, 11, 3, 15]];
  5520. var q3 = [[13, 7, 15, 4, 1, 2, 6, 14, 9, 11, 3, 0, 8, 5, 12, 10], [11, 9, 5, 1, 12, 3, 13, 14, 6, 4, 7, 15, 2, 0, 8, 10]];
  5521. var ror4 = [0, 8, 1, 9, 2, 10, 3, 11, 4, 12, 5, 13, 6, 14, 7, 15];
  5522. var ashx = [0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12, 5, 14, 7];
  5523. var q = [[], []];
  5524. var m = [[], [], [], []];
  5525. function ffm5b(x) {
  5526. return x ^ x >> 2 ^ [0, 90, 180, 238][x & 3];
  5527. }
  5528. function ffmEf(x) {
  5529. return x ^ x >> 1 ^ x >> 2 ^ [0, 238, 180, 90][x & 3];
  5530. }
  5531. function mdsRem(p, q) {
  5532. var i, t, u;
  5533. for (i = 0; i < 8; i++) {
  5534. t = q >>> 24;
  5535. q = q << 8 & MAXINT | p >>> 24;
  5536. p = p << 8 & MAXINT;
  5537. u = t << 1;
  5538. if (t & 128) {
  5539. u ^= 333;
  5540. }
  5541. q ^= t ^ u << 16;
  5542. u ^= t >>> 1;
  5543. if (t & 1) {
  5544. u ^= 166;
  5545. }
  5546. q ^= u << 24 | u << 8;
  5547. }
  5548. return q;
  5549. }
  5550. function qp(n, x) {
  5551. var a, b, c, d;
  5552. a = x >> 4;
  5553. b = x & 15;
  5554. c = q0[n][a ^ b];
  5555. d = q1[n][ror4[b] ^ ashx[a]];
  5556. return q3[n][ror4[d] ^ ashx[c]] << 4 | q2[n][c ^ d];
  5557. }
  5558. function hFun(x, key) {
  5559. var a = getB(x, 0),
  5560. b = getB(x, 1),
  5561. c = getB(x, 2),
  5562. d = getB(x, 3);
  5563. switch (kLen) {
  5564. case 4:
  5565. a = q[1][a] ^ getB(key[3], 0);
  5566. b = q[0][b] ^ getB(key[3], 1);
  5567. c = q[0][c] ^ getB(key[3], 2);
  5568. d = q[1][d] ^ getB(key[3], 3);
  5569. case 3:
  5570. a = q[1][a] ^ getB(key[2], 0);
  5571. b = q[1][b] ^ getB(key[2], 1);
  5572. c = q[0][c] ^ getB(key[2], 2);
  5573. d = q[0][d] ^ getB(key[2], 3);
  5574. case 2:
  5575. a = q[0][q[0][a] ^ getB(key[1], 0)] ^ getB(key[0], 0);
  5576. b = q[0][q[1][b] ^ getB(key[1], 1)] ^ getB(key[0], 1);
  5577. c = q[1][q[0][c] ^ getB(key[1], 2)] ^ getB(key[0], 2);
  5578. d = q[1][q[1][d] ^ getB(key[1], 3)] ^ getB(key[0], 3);
  5579. }
  5580. return m[0][a] ^ m[1][b] ^ m[2][c] ^ m[3][d];
  5581. }
  5582. keyBytes = keyBytes.slice(0, 32);
  5583. i = keyBytes.length;
  5584. while (i !== 16 && i !== 24 && i !== 32) {
  5585. keyBytes[i++] = 0;
  5586. }
  5587. for (i = 0; i < keyBytes.length; i += 4) {
  5588. inKey[i >> 2] = getW(keyBytes, i);
  5589. }
  5590. for (i = 0; i < 256; i++) {
  5591. q[0][i] = qp(0, i);
  5592. q[1][i] = qp(1, i);
  5593. }
  5594. for (i = 0; i < 256; i++) {
  5595. f01 = q[1][i];
  5596. f5b = ffm5b(f01);
  5597. fef = ffmEf(f01);
  5598. m[0][i] = f01 + (f5b << 8) + (fef << 16) + (fef << 24);
  5599. m[2][i] = f5b + (fef << 8) + (f01 << 16) + (fef << 24);
  5600. f01 = q[0][i];
  5601. f5b = ffm5b(f01);
  5602. fef = ffmEf(f01);
  5603. m[1][i] = fef + (fef << 8) + (f5b << 16) + (f01 << 24);
  5604. m[3][i] = f5b + (f01 << 8) + (fef << 16) + (f5b << 24);
  5605. }
  5606. kLen = inKey.length / 2;
  5607. for (i = 0; i < kLen; i++) {
  5608. a = inKey[i + i];
  5609. meKey[i] = a;
  5610. b = inKey[i + i + 1];
  5611. moKey[i] = b;
  5612. sKey[kLen - i - 1] = mdsRem(a, b);
  5613. }
  5614. for (i = 0; i < 40; i += 2) {
  5615. a = 0x1010101 * i;
  5616. b = a + 0x1010101;
  5617. a = hFun(a, meKey);
  5618. b = rotw(hFun(b, moKey), 8);
  5619. tfsKey[i] = a + b & MAXINT;
  5620. tfsKey[i + 1] = rotw(a + 2 * b, 9);
  5621. }
  5622. for (i = 0; i < 256; i++) {
  5623. a = b = c = d = i;
  5624. switch (kLen) {
  5625. case 4:
  5626. a = q[1][a] ^ getB(sKey[3], 0);
  5627. b = q[0][b] ^ getB(sKey[3], 1);
  5628. c = q[0][c] ^ getB(sKey[3], 2);
  5629. d = q[1][d] ^ getB(sKey[3], 3);
  5630. case 3:
  5631. a = q[1][a] ^ getB(sKey[2], 0);
  5632. b = q[1][b] ^ getB(sKey[2], 1);
  5633. c = q[0][c] ^ getB(sKey[2], 2);
  5634. d = q[0][d] ^ getB(sKey[2], 3);
  5635. case 2:
  5636. tfsM[0][i] = m[0][q[0][q[0][a] ^ getB(sKey[1], 0)] ^ getB(sKey[0], 0)];
  5637. tfsM[1][i] = m[1][q[0][q[1][b] ^ getB(sKey[1], 1)] ^ getB(sKey[0], 1)];
  5638. tfsM[2][i] = m[2][q[1][q[0][c] ^ getB(sKey[1], 2)] ^ getB(sKey[0], 2)];
  5639. tfsM[3][i] = m[3][q[1][q[1][d] ^ getB(sKey[1], 3)] ^ getB(sKey[0], 3)];
  5640. }
  5641. }
  5642. }
  5643. function tfsG0(x) {
  5644. return tfsM[0][getB(x, 0)] ^ tfsM[1][getB(x, 1)] ^ tfsM[2][getB(x, 2)] ^ tfsM[3][getB(x, 3)];
  5645. }
  5646. function tfsG1(x) {
  5647. return tfsM[0][getB(x, 3)] ^ tfsM[1][getB(x, 0)] ^ tfsM[2][getB(x, 1)] ^ tfsM[3][getB(x, 2)];
  5648. }
  5649. function tfsFrnd(r, blk) {
  5650. var a = tfsG0(blk[0]);
  5651. var b = tfsG1(blk[1]);
  5652. blk[2] = rotw(blk[2] ^ a + b + tfsKey[4 * r + 8] & MAXINT, 31);
  5653. blk[3] = rotw(blk[3], 1) ^ a + 2 * b + tfsKey[4 * r + 9] & MAXINT;
  5654. a = tfsG0(blk[2]);
  5655. b = tfsG1(blk[3]);
  5656. blk[0] = rotw(blk[0] ^ a + b + tfsKey[4 * r + 10] & MAXINT, 31);
  5657. blk[1] = rotw(blk[1], 1) ^ a + 2 * b + tfsKey[4 * r + 11] & MAXINT;
  5658. }
  5659. function tfsIrnd(i, blk) {
  5660. var a = tfsG0(blk[0]);
  5661. var b = tfsG1(blk[1]);
  5662. blk[2] = rotw(blk[2], 1) ^ a + b + tfsKey[4 * i + 10] & MAXINT;
  5663. blk[3] = rotw(blk[3] ^ a + 2 * b + tfsKey[4 * i + 11] & MAXINT, 31);
  5664. a = tfsG0(blk[2]);
  5665. b = tfsG1(blk[3]);
  5666. blk[0] = rotw(blk[0], 1) ^ a + b + tfsKey[4 * i + 8] & MAXINT;
  5667. blk[1] = rotw(blk[1] ^ a + 2 * b + tfsKey[4 * i + 9] & MAXINT, 31);
  5668. }
  5669. function tfsClose() {
  5670. tfsKey = [];
  5671. tfsM = [[], [], [], []];
  5672. }
  5673. function tfsEncrypt(data, offset) {
  5674. dataBytes = data;
  5675. dataOffset = offset;
  5676. var blk = [getW(dataBytes, dataOffset) ^ tfsKey[0], getW(dataBytes, dataOffset + 4) ^ tfsKey[1], getW(dataBytes, dataOffset + 8) ^ tfsKey[2], getW(dataBytes, dataOffset + 12) ^ tfsKey[3]];
  5677. for (var j = 0; j < 8; j++) {
  5678. tfsFrnd(j, blk);
  5679. }
  5680. setW(dataBytes, dataOffset, blk[2] ^ tfsKey[4]);
  5681. setW(dataBytes, dataOffset + 4, blk[3] ^ tfsKey[5]);
  5682. setW(dataBytes, dataOffset + 8, blk[0] ^ tfsKey[6]);
  5683. setW(dataBytes, dataOffset + 12, blk[1] ^ tfsKey[7]);
  5684. dataOffset += 16;
  5685. return dataBytes;
  5686. }
  5687. function tfsDecrypt(data, offset) {
  5688. dataBytes = data;
  5689. dataOffset = offset;
  5690. var blk = [getW(dataBytes, dataOffset) ^ tfsKey[4], getW(dataBytes, dataOffset + 4) ^ tfsKey[5], getW(dataBytes, dataOffset + 8) ^ tfsKey[6], getW(dataBytes, dataOffset + 12) ^ tfsKey[7]];
  5691. for (var j = 7; j >= 0; j--) {
  5692. tfsIrnd(j, blk);
  5693. }
  5694. setW(dataBytes, dataOffset, blk[2] ^ tfsKey[0]);
  5695. setW(dataBytes, dataOffset + 4, blk[3] ^ tfsKey[1]);
  5696. setW(dataBytes, dataOffset + 8, blk[0] ^ tfsKey[2]);
  5697. setW(dataBytes, dataOffset + 12, blk[1] ^ tfsKey[3]);
  5698. dataOffset += 16;
  5699. }
  5700. // added by Recurity Labs
  5701. function tfsFinal() {
  5702. return dataBytes;
  5703. }
  5704. return {
  5705. name: "twofish",
  5706. blocksize: 128 / 8,
  5707. open: tfsInit,
  5708. close: tfsClose,
  5709. encrypt: tfsEncrypt,
  5710. decrypt: tfsDecrypt,
  5711. // added by Recurity Labs
  5712. finalize: tfsFinal
  5713. };
  5714. }
  5715. // added by Recurity Labs
  5716. function TF(key) {
  5717. this.tf = createTwofish();
  5718. this.tf.open(toArray(key), 0);
  5719. this.encrypt = function (block) {
  5720. return this.tf.encrypt(toArray(block), 0);
  5721. };
  5722. }
  5723. function toArray(typedArray) {
  5724. // Array.apply([], typedArray) does not work in PhantomJS 1.9
  5725. var result = [];
  5726. for (var i = 0; i < typedArray.length; i++) {
  5727. result[i] = typedArray[i];
  5728. }
  5729. return result;
  5730. }
  5731. TF.keySize = TF.prototype.keySize = 32;
  5732. TF.blockSize = TF.prototype.blockSize = 16;
  5733. },{}],18:[function(_dereq_,module,exports){
  5734. // GPG4Browsers - An OpenPGP implementation in javascript
  5735. // Copyright (C) 2011 Recurity Labs GmbH
  5736. //
  5737. // This library is free software; you can redistribute it and/or
  5738. // modify it under the terms of the GNU Lesser General Public
  5739. // License as published by the Free Software Foundation; either
  5740. // version 3.0 of the License, or (at your option) any later version.
  5741. //
  5742. // This library is distributed in the hope that it will be useful,
  5743. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  5744. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  5745. // Lesser General Public License for more details.
  5746. //
  5747. // You should have received a copy of the GNU Lesser General Public
  5748. // License along with this library; if not, write to the Free Software
  5749. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  5750. // The GPG4Browsers crypto interface
  5751. /**
  5752. * @requires crypto/cipher
  5753. * @requires crypto/public_key
  5754. * @requires crypto/random
  5755. * @requires type/mpi
  5756. * @module crypto/crypto
  5757. */
  5758. 'use strict';
  5759. Object.defineProperty(exports, "__esModule", {
  5760. value: true
  5761. });
  5762. var _random = _dereq_('./random.js');
  5763. var _random2 = _interopRequireDefault(_random);
  5764. var _cipher = _dereq_('./cipher');
  5765. var _cipher2 = _interopRequireDefault(_cipher);
  5766. var _public_key = _dereq_('./public_key');
  5767. var _public_key2 = _interopRequireDefault(_public_key);
  5768. var _mpi = _dereq_('../type/mpi.js');
  5769. var _mpi2 = _interopRequireDefault(_mpi);
  5770. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  5771. exports.default = {
  5772. /**
  5773. * Encrypts data using the specified public key multiprecision integers
  5774. * and the specified algorithm.
  5775. * @param {module:enums.publicKey} algo Algorithm to be used (See {@link http://tools.ietf.org/html/rfc4880#section-9.1|RFC 4880 9.1})
  5776. * @param {Array<module:type/mpi>} publicMPIs Algorithm dependent multiprecision integers
  5777. * @param {module:type/mpi} data Data to be encrypted as MPI
  5778. * @return {Array<module:type/mpi>} if RSA an module:type/mpi;
  5779. * if elgamal encryption an array of two module:type/mpi is returned; otherwise null
  5780. */
  5781. publicKeyEncrypt: function publicKeyEncrypt(algo, publicMPIs, data) {
  5782. var result = function () {
  5783. var m;
  5784. switch (algo) {
  5785. case 'rsa_encrypt':
  5786. case 'rsa_encrypt_sign':
  5787. var rsa = new _public_key2.default.rsa();
  5788. var n = publicMPIs[0].toBigInteger();
  5789. var e = publicMPIs[1].toBigInteger();
  5790. m = data.toBigInteger();
  5791. return [rsa.encrypt(m, e, n)];
  5792. case 'elgamal':
  5793. var elgamal = new _public_key2.default.elgamal();
  5794. var p = publicMPIs[0].toBigInteger();
  5795. var g = publicMPIs[1].toBigInteger();
  5796. var y = publicMPIs[2].toBigInteger();
  5797. m = data.toBigInteger();
  5798. return elgamal.encrypt(m, g, p, y);
  5799. default:
  5800. return [];
  5801. }
  5802. }();
  5803. return result.map(function (bn) {
  5804. var mpi = new _mpi2.default();
  5805. mpi.fromBigInteger(bn);
  5806. return mpi;
  5807. });
  5808. },
  5809. /**
  5810. * Decrypts data using the specified public key multiprecision integers of the private key,
  5811. * the specified secretMPIs of the private key and the specified algorithm.
  5812. * @param {module:enums.publicKey} algo Algorithm to be used (See {@link http://tools.ietf.org/html/rfc4880#section-9.1|RFC 4880 9.1})
  5813. * @param {Array<module:type/mpi>} publicMPIs Algorithm dependent multiprecision integers
  5814. * of the public key part of the private key
  5815. * @param {Array<module:type/mpi>} secretMPIs Algorithm dependent multiprecision integers
  5816. * of the private key used
  5817. * @param {module:type/mpi} data Data to be encrypted as MPI
  5818. * @return {module:type/mpi} returns a big integer containing the decrypted data; otherwise null
  5819. */
  5820. publicKeyDecrypt: function publicKeyDecrypt(algo, keyIntegers, dataIntegers) {
  5821. var p;
  5822. var bn = function () {
  5823. switch (algo) {
  5824. case 'rsa_encrypt_sign':
  5825. case 'rsa_encrypt':
  5826. var rsa = new _public_key2.default.rsa();
  5827. // 0 and 1 are the public key.
  5828. var n = keyIntegers[0].toBigInteger();
  5829. var e = keyIntegers[1].toBigInteger();
  5830. // 2 to 5 are the private key.
  5831. var d = keyIntegers[2].toBigInteger();
  5832. p = keyIntegers[3].toBigInteger();
  5833. var q = keyIntegers[4].toBigInteger();
  5834. var u = keyIntegers[5].toBigInteger();
  5835. var m = dataIntegers[0].toBigInteger();
  5836. return rsa.decrypt(m, n, e, d, p, q, u);
  5837. case 'elgamal':
  5838. var elgamal = new _public_key2.default.elgamal();
  5839. var x = keyIntegers[3].toBigInteger();
  5840. var c1 = dataIntegers[0].toBigInteger();
  5841. var c2 = dataIntegers[1].toBigInteger();
  5842. p = keyIntegers[0].toBigInteger();
  5843. return elgamal.decrypt(c1, c2, p, x);
  5844. default:
  5845. return null;
  5846. }
  5847. }();
  5848. var result = new _mpi2.default();
  5849. result.fromBigInteger(bn);
  5850. return result;
  5851. },
  5852. /** Returns the number of integers comprising the private key of an algorithm
  5853. * @param {String} algo The public key algorithm
  5854. * @return {Integer} The number of integers.
  5855. */
  5856. getPrivateMpiCount: function getPrivateMpiCount(algo) {
  5857. switch (algo) {
  5858. case 'rsa_encrypt':
  5859. case 'rsa_encrypt_sign':
  5860. case 'rsa_sign':
  5861. // Algorithm-Specific Fields for RSA secret keys:
  5862. // - multiprecision integer (MPI) of RSA secret exponent d.
  5863. // - MPI of RSA secret prime value p.
  5864. // - MPI of RSA secret prime value q (p < q).
  5865. // - MPI of u, the multiplicative inverse of p, mod q.
  5866. return 4;
  5867. case 'elgamal':
  5868. // Algorithm-Specific Fields for Elgamal secret keys:
  5869. // - MPI of Elgamal secret exponent x.
  5870. return 1;
  5871. case 'dsa':
  5872. // Algorithm-Specific Fields for DSA secret keys:
  5873. // - MPI of DSA secret exponent x.
  5874. return 1;
  5875. default:
  5876. throw new Error('Unknown algorithm');
  5877. }
  5878. },
  5879. getPublicMpiCount: function getPublicMpiCount(algo) {
  5880. // - A series of multiprecision integers comprising the key material:
  5881. // Algorithm-Specific Fields for RSA public keys:
  5882. // - a multiprecision integer (MPI) of RSA public modulus n;
  5883. // - an MPI of RSA public encryption exponent e.
  5884. switch (algo) {
  5885. case 'rsa_encrypt':
  5886. case 'rsa_encrypt_sign':
  5887. case 'rsa_sign':
  5888. return 2;
  5889. // Algorithm-Specific Fields for Elgamal public keys:
  5890. // - MPI of Elgamal prime p;
  5891. // - MPI of Elgamal group generator g;
  5892. // - MPI of Elgamal public key value y (= g**x mod p where x is secret).
  5893. case 'elgamal':
  5894. return 3;
  5895. // Algorithm-Specific Fields for DSA public keys:
  5896. // - MPI of DSA prime p;
  5897. // - MPI of DSA group order q (q is a prime divisor of p-1);
  5898. // - MPI of DSA group generator g;
  5899. // - MPI of DSA public-key value y (= g**x mod p where x is secret).
  5900. case 'dsa':
  5901. return 4;
  5902. default:
  5903. throw new Error('Unknown algorithm.');
  5904. }
  5905. },
  5906. generateMpi: function generateMpi(algo, bits) {
  5907. switch (algo) {
  5908. case 'rsa_encrypt':
  5909. case 'rsa_encrypt_sign':
  5910. case 'rsa_sign':
  5911. //remember "publicKey" refers to the crypto/public_key dir
  5912. var rsa = new _public_key2.default.rsa();
  5913. return rsa.generate(bits, "10001").then(function (keyObject) {
  5914. var output = [];
  5915. output.push(keyObject.n);
  5916. output.push(keyObject.ee);
  5917. output.push(keyObject.d);
  5918. output.push(keyObject.p);
  5919. output.push(keyObject.q);
  5920. output.push(keyObject.u);
  5921. return mapResult(output);
  5922. });
  5923. default:
  5924. throw new Error('Unsupported algorithm for key generation.');
  5925. }
  5926. function mapResult(result) {
  5927. return result.map(function (bn) {
  5928. var mpi = new _mpi2.default();
  5929. mpi.fromBigInteger(bn);
  5930. return mpi;
  5931. });
  5932. }
  5933. },
  5934. /**
  5935. * generate random byte prefix as string for the specified algorithm
  5936. * @param {module:enums.symmetric} algo Algorithm to use (see {@link http://tools.ietf.org/html/rfc4880#section-9.2|RFC 4880 9.2})
  5937. * @return {Uint8Array} Random bytes with length equal to the block
  5938. * size of the cipher
  5939. */
  5940. getPrefixRandom: function getPrefixRandom(algo) {
  5941. return _random2.default.getRandomBytes(_cipher2.default[algo].blockSize);
  5942. },
  5943. /**
  5944. * Generating a session key for the specified symmetric algorithm
  5945. * @param {module:enums.symmetric} algo Algorithm to use (see {@link http://tools.ietf.org/html/rfc4880#section-9.2|RFC 4880 9.2})
  5946. * @return {Uint8Array} Random bytes as a string to be used as a key
  5947. */
  5948. generateSessionKey: function generateSessionKey(algo) {
  5949. return _random2.default.getRandomBytes(_cipher2.default[algo].keySize);
  5950. }
  5951. };
  5952. },{"../type/mpi.js":68,"./cipher":16,"./public_key":28,"./random.js":31}],19:[function(_dereq_,module,exports){
  5953. // OpenPGP.js - An OpenPGP implementation in javascript
  5954. // Copyright (C) 2016 Tankred Hase
  5955. //
  5956. // This library is free software; you can redistribute it and/or
  5957. // modify it under the terms of the GNU Lesser General Public
  5958. // License as published by the Free Software Foundation; either
  5959. // version 3.0 of the License, or (at your option) any later version.
  5960. //
  5961. // This library is distributed in the hope that it will be useful,
  5962. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  5963. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  5964. // Lesser General Public License for more details.
  5965. //
  5966. // You should have received a copy of the GNU Lesser General Public
  5967. // License along with this library; if not, write to the Free Software
  5968. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  5969. /**
  5970. * @fileoverview This module wraps native AES-GCM en/decryption for both
  5971. * the WebCrypto api as well as node.js' crypto api.
  5972. */
  5973. 'use strict';
  5974. Object.defineProperty(exports, "__esModule", {
  5975. value: true
  5976. });
  5977. exports.ivLength = undefined;
  5978. exports.encrypt = encrypt;
  5979. exports.decrypt = decrypt;
  5980. var _util = _dereq_('../util.js');
  5981. var _util2 = _interopRequireDefault(_util);
  5982. var _config = _dereq_('../config');
  5983. var _config2 = _interopRequireDefault(_config);
  5984. var _asmcryptoLite = _dereq_('asmcrypto-lite');
  5985. var _asmcryptoLite2 = _interopRequireDefault(_asmcryptoLite);
  5986. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  5987. var webCrypto = _util2.default.getWebCrypto(); // no GCM support in IE11, Safari 9
  5988. var nodeCrypto = _util2.default.getNodeCrypto();
  5989. var Buffer = _util2.default.getNodeBuffer();
  5990. var ivLength = exports.ivLength = 12; // size of the IV in bytes
  5991. var TAG_LEN = 16; // size of the tag in bytes
  5992. var ALGO = 'AES-GCM';
  5993. /**
  5994. * Encrypt plaintext input.
  5995. * @param {String} cipher The symmetric cipher algorithm to use e.g. 'aes128'
  5996. * @param {Uint8Array} plaintext The cleartext input to be encrypted
  5997. * @param {Uint8Array} key The encryption key
  5998. * @param {Uint8Array} iv The initialization vector (12 bytes)
  5999. * @return {Promise<Uint8Array>} The ciphertext output
  6000. */
  6001. function encrypt(cipher, plaintext, key, iv) {
  6002. if (cipher.substr(0, 3) !== 'aes') {
  6003. return Promise.reject(new Error('GCM mode supports only AES cipher'));
  6004. }
  6005. if (webCrypto && _config2.default.use_native && key.length !== 24) {
  6006. // WebCrypto (no 192 bit support) see: https://www.chromium.org/blink/webcrypto#TOC-AES-support
  6007. return webEncrypt(plaintext, key, iv);
  6008. } else if (nodeCrypto && _config2.default.use_native) {
  6009. // Node crypto library
  6010. return nodeEncrypt(plaintext, key, iv);
  6011. } else {
  6012. // asm.js fallback
  6013. return Promise.resolve(_asmcryptoLite2.default.AES_GCM.encrypt(plaintext, key, iv));
  6014. }
  6015. }
  6016. /**
  6017. * Decrypt ciphertext input.
  6018. * @param {String} cipher The symmetric cipher algorithm to use e.g. 'aes128'
  6019. * @param {Uint8Array} ciphertext The ciphertext input to be decrypted
  6020. * @param {Uint8Array} key The encryption key
  6021. * @param {Uint8Array} iv The initialization vector (12 bytes)
  6022. * @return {Promise<Uint8Array>} The plaintext output
  6023. */
  6024. function decrypt(cipher, ciphertext, key, iv) {
  6025. if (cipher.substr(0, 3) !== 'aes') {
  6026. return Promise.reject(new Error('GCM mode supports only AES cipher'));
  6027. }
  6028. if (webCrypto && _config2.default.use_native && key.length !== 24) {
  6029. // WebCrypto (no 192 bit support) see: https://www.chromium.org/blink/webcrypto#TOC-AES-support
  6030. return webDecrypt(ciphertext, key, iv);
  6031. } else if (nodeCrypto && _config2.default.use_native) {
  6032. // Node crypto library
  6033. return nodeDecrypt(ciphertext, key, iv);
  6034. } else {
  6035. // asm.js fallback
  6036. return Promise.resolve(_asmcryptoLite2.default.AES_GCM.decrypt(ciphertext, key, iv));
  6037. }
  6038. }
  6039. //////////////////////////
  6040. // //
  6041. // Helper functions //
  6042. // //
  6043. //////////////////////////
  6044. function webEncrypt(pt, key, iv) {
  6045. return webCrypto.importKey('raw', key, { name: ALGO }, false, ['encrypt']).then(function (keyObj) {
  6046. return webCrypto.encrypt({ name: ALGO, iv: iv }, keyObj, pt);
  6047. }).then(function (ct) {
  6048. return new Uint8Array(ct);
  6049. });
  6050. }
  6051. function webDecrypt(ct, key, iv) {
  6052. return webCrypto.importKey('raw', key, { name: ALGO }, false, ['decrypt']).then(function (keyObj) {
  6053. return webCrypto.decrypt({ name: ALGO, iv: iv }, keyObj, ct);
  6054. }).then(function (pt) {
  6055. return new Uint8Array(pt);
  6056. });
  6057. }
  6058. function nodeEncrypt(pt, key, iv) {
  6059. pt = new Buffer(pt);
  6060. key = new Buffer(key);
  6061. iv = new Buffer(iv);
  6062. var en = new nodeCrypto.createCipheriv('aes-' + key.length * 8 + '-gcm', key, iv);
  6063. var ct = Buffer.concat([en.update(pt), en.final(), en.getAuthTag()]); // append auth tag to ciphertext
  6064. return Promise.resolve(new Uint8Array(ct));
  6065. }
  6066. function nodeDecrypt(ct, key, iv) {
  6067. ct = new Buffer(ct);
  6068. key = new Buffer(key);
  6069. iv = new Buffer(iv);
  6070. var de = new nodeCrypto.createDecipheriv('aes-' + key.length * 8 + '-gcm', key, iv);
  6071. de.setAuthTag(ct.slice(ct.length - TAG_LEN, ct.length)); // read auth tag at end of ciphertext
  6072. var pt = Buffer.concat([de.update(ct.slice(0, ct.length - TAG_LEN)), de.final()]);
  6073. return Promise.resolve(new Uint8Array(pt));
  6074. }
  6075. },{"../config":10,"../util.js":70,"asmcrypto-lite":1}],20:[function(_dereq_,module,exports){
  6076. /**
  6077. * @requires crypto/hash/sha
  6078. * @requires crypto/hash/md5
  6079. * @requires crypto/hash/ripe-md
  6080. * @requires util
  6081. * @module crypto/hash
  6082. */
  6083. 'use strict';
  6084. Object.defineProperty(exports, "__esModule", {
  6085. value: true
  6086. });
  6087. var _sha = _dereq_('./sha.js');
  6088. var _sha2 = _interopRequireDefault(_sha);
  6089. var _asmcryptoLite = _dereq_('asmcrypto-lite');
  6090. var _asmcryptoLite2 = _interopRequireDefault(_asmcryptoLite);
  6091. var _rusha = _dereq_('rusha');
  6092. var _rusha2 = _interopRequireDefault(_rusha);
  6093. var _md = _dereq_('./md5.js');
  6094. var _md2 = _interopRequireDefault(_md);
  6095. var _ripeMd = _dereq_('./ripe-md.js');
  6096. var _ripeMd2 = _interopRequireDefault(_ripeMd);
  6097. var _util = _dereq_('../../util.js');
  6098. var _util2 = _interopRequireDefault(_util);
  6099. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  6100. var rusha = new _rusha2.default(),
  6101. nodeCrypto = _util2.default.getNodeCrypto(),
  6102. Buffer = _util2.default.getNodeBuffer();
  6103. function node_hash(type) {
  6104. return function (data) {
  6105. var shasum = nodeCrypto.createHash(type);
  6106. shasum.update(new Buffer(data));
  6107. return new Uint8Array(shasum.digest());
  6108. };
  6109. }
  6110. var hash_fns;
  6111. if (nodeCrypto) {
  6112. // Use Node native crypto for all hash functions
  6113. hash_fns = {
  6114. md5: node_hash('md5'),
  6115. sha1: node_hash('sha1'),
  6116. sha224: node_hash('sha224'),
  6117. sha256: node_hash('sha256'),
  6118. sha384: node_hash('sha384'),
  6119. sha512: node_hash('sha512'),
  6120. ripemd: node_hash('ripemd160')
  6121. };
  6122. } else {
  6123. // Use JS fallbacks
  6124. hash_fns = {
  6125. /** @see module:crypto/hash/md5 */
  6126. md5: _md2.default,
  6127. /** @see module:rusha */
  6128. sha1: function sha1(data) {
  6129. return _util2.default.str2Uint8Array(_util2.default.hex2bin(rusha.digest(data)));
  6130. },
  6131. /** @see module:crypto/hash/sha.sha224 */
  6132. sha224: _sha2.default.sha224,
  6133. /** @see module:asmcrypto */
  6134. sha256: _asmcryptoLite2.default.SHA256.bytes,
  6135. /** @see module:crypto/hash/sha.sha384 */
  6136. sha384: _sha2.default.sha384,
  6137. /** @see module:crypto/hash/sha.sha512 */
  6138. sha512: _sha2.default.sha512,
  6139. /** @see module:crypto/hash/ripe-md */
  6140. ripemd: _ripeMd2.default
  6141. };
  6142. }
  6143. exports.default = {
  6144. md5: hash_fns.md5,
  6145. sha1: hash_fns.sha1,
  6146. sha224: hash_fns.sha224,
  6147. sha256: hash_fns.sha256,
  6148. sha384: hash_fns.sha384,
  6149. sha512: hash_fns.sha512,
  6150. ripemd: hash_fns.ripemd,
  6151. /**
  6152. * Create a hash on the specified data using the specified algorithm
  6153. * @param {module:enums.hash} algo Hash algorithm type (see {@link http://tools.ietf.org/html/rfc4880#section-9.4|RFC 4880 9.4})
  6154. * @param {Uint8Array} data Data to be hashed
  6155. * @return {Uint8Array} hash value
  6156. */
  6157. digest: function digest(algo, data) {
  6158. switch (algo) {
  6159. case 1:
  6160. // - MD5 [HAC]
  6161. return this.md5(data);
  6162. case 2:
  6163. // - SHA-1 [FIPS180]
  6164. return this.sha1(data);
  6165. case 3:
  6166. // - RIPE-MD/160 [HAC]
  6167. return this.ripemd(data);
  6168. case 8:
  6169. // - SHA256 [FIPS180]
  6170. return this.sha256(data);
  6171. case 9:
  6172. // - SHA384 [FIPS180]
  6173. return this.sha384(data);
  6174. case 10:
  6175. // - SHA512 [FIPS180]
  6176. return this.sha512(data);
  6177. case 11:
  6178. // - SHA224 [FIPS180]
  6179. return this.sha224(data);
  6180. default:
  6181. throw new Error('Invalid hash function.');
  6182. }
  6183. },
  6184. /**
  6185. * Returns the hash size in bytes of the specified hash algorithm type
  6186. * @param {module:enums.hash} algo Hash algorithm type (See {@link http://tools.ietf.org/html/rfc4880#section-9.4|RFC 4880 9.4})
  6187. * @return {Integer} Size in bytes of the resulting hash
  6188. */
  6189. getHashByteLength: function getHashByteLength(algo) {
  6190. switch (algo) {
  6191. case 1:
  6192. // - MD5 [HAC]
  6193. return 16;
  6194. case 2:
  6195. // - SHA-1 [FIPS180]
  6196. case 3:
  6197. // - RIPE-MD/160 [HAC]
  6198. return 20;
  6199. case 8:
  6200. // - SHA256 [FIPS180]
  6201. return 32;
  6202. case 9:
  6203. // - SHA384 [FIPS180]
  6204. return 48;
  6205. case 10:
  6206. // - SHA512 [FIPS180]
  6207. return 64;
  6208. case 11:
  6209. // - SHA224 [FIPS180]
  6210. return 28;
  6211. default:
  6212. throw new Error('Invalid hash algorithm.');
  6213. }
  6214. }
  6215. };
  6216. },{"../../util.js":70,"./md5.js":21,"./ripe-md.js":22,"./sha.js":23,"asmcrypto-lite":1,"rusha":4}],21:[function(_dereq_,module,exports){
  6217. /**
  6218. * A fast MD5 JavaScript implementation
  6219. * Copyright (c) 2012 Joseph Myers
  6220. * http://www.myersdaily.org/joseph/javascript/md5-text.html
  6221. *
  6222. * Permission to use, copy, modify, and distribute this software
  6223. * and its documentation for any purposes and without
  6224. * fee is hereby granted provided that this copyright notice
  6225. * appears in all copies.
  6226. *
  6227. * Of course, this soft is provided "as is" without express or implied
  6228. * warranty of any kind.
  6229. */
  6230. /**
  6231. * @requires util
  6232. * @module crypto/hash/md5
  6233. */
  6234. 'use strict';
  6235. Object.defineProperty(exports, "__esModule", {
  6236. value: true
  6237. });
  6238. exports.default = function (entree) {
  6239. var hex = md5(_util2.default.Uint8Array2str(entree));
  6240. var bin = _util2.default.str2Uint8Array(_util2.default.hex2bin(hex));
  6241. return bin;
  6242. };
  6243. var _util = _dereq_('../../util.js');
  6244. var _util2 = _interopRequireDefault(_util);
  6245. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  6246. function md5cycle(x, k) {
  6247. var a = x[0],
  6248. b = x[1],
  6249. c = x[2],
  6250. d = x[3];
  6251. a = ff(a, b, c, d, k[0], 7, -680876936);
  6252. d = ff(d, a, b, c, k[1], 12, -389564586);
  6253. c = ff(c, d, a, b, k[2], 17, 606105819);
  6254. b = ff(b, c, d, a, k[3], 22, -1044525330);
  6255. a = ff(a, b, c, d, k[4], 7, -176418897);
  6256. d = ff(d, a, b, c, k[5], 12, 1200080426);
  6257. c = ff(c, d, a, b, k[6], 17, -1473231341);
  6258. b = ff(b, c, d, a, k[7], 22, -45705983);
  6259. a = ff(a, b, c, d, k[8], 7, 1770035416);
  6260. d = ff(d, a, b, c, k[9], 12, -1958414417);
  6261. c = ff(c, d, a, b, k[10], 17, -42063);
  6262. b = ff(b, c, d, a, k[11], 22, -1990404162);
  6263. a = ff(a, b, c, d, k[12], 7, 1804603682);
  6264. d = ff(d, a, b, c, k[13], 12, -40341101);
  6265. c = ff(c, d, a, b, k[14], 17, -1502002290);
  6266. b = ff(b, c, d, a, k[15], 22, 1236535329);
  6267. a = gg(a, b, c, d, k[1], 5, -165796510);
  6268. d = gg(d, a, b, c, k[6], 9, -1069501632);
  6269. c = gg(c, d, a, b, k[11], 14, 643717713);
  6270. b = gg(b, c, d, a, k[0], 20, -373897302);
  6271. a = gg(a, b, c, d, k[5], 5, -701558691);
  6272. d = gg(d, a, b, c, k[10], 9, 38016083);
  6273. c = gg(c, d, a, b, k[15], 14, -660478335);
  6274. b = gg(b, c, d, a, k[4], 20, -405537848);
  6275. a = gg(a, b, c, d, k[9], 5, 568446438);
  6276. d = gg(d, a, b, c, k[14], 9, -1019803690);
  6277. c = gg(c, d, a, b, k[3], 14, -187363961);
  6278. b = gg(b, c, d, a, k[8], 20, 1163531501);
  6279. a = gg(a, b, c, d, k[13], 5, -1444681467);
  6280. d = gg(d, a, b, c, k[2], 9, -51403784);
  6281. c = gg(c, d, a, b, k[7], 14, 1735328473);
  6282. b = gg(b, c, d, a, k[12], 20, -1926607734);
  6283. a = hh(a, b, c, d, k[5], 4, -378558);
  6284. d = hh(d, a, b, c, k[8], 11, -2022574463);
  6285. c = hh(c, d, a, b, k[11], 16, 1839030562);
  6286. b = hh(b, c, d, a, k[14], 23, -35309556);
  6287. a = hh(a, b, c, d, k[1], 4, -1530992060);
  6288. d = hh(d, a, b, c, k[4], 11, 1272893353);
  6289. c = hh(c, d, a, b, k[7], 16, -155497632);
  6290. b = hh(b, c, d, a, k[10], 23, -1094730640);
  6291. a = hh(a, b, c, d, k[13], 4, 681279174);
  6292. d = hh(d, a, b, c, k[0], 11, -358537222);
  6293. c = hh(c, d, a, b, k[3], 16, -722521979);
  6294. b = hh(b, c, d, a, k[6], 23, 76029189);
  6295. a = hh(a, b, c, d, k[9], 4, -640364487);
  6296. d = hh(d, a, b, c, k[12], 11, -421815835);
  6297. c = hh(c, d, a, b, k[15], 16, 530742520);
  6298. b = hh(b, c, d, a, k[2], 23, -995338651);
  6299. a = ii(a, b, c, d, k[0], 6, -198630844);
  6300. d = ii(d, a, b, c, k[7], 10, 1126891415);
  6301. c = ii(c, d, a, b, k[14], 15, -1416354905);
  6302. b = ii(b, c, d, a, k[5], 21, -57434055);
  6303. a = ii(a, b, c, d, k[12], 6, 1700485571);
  6304. d = ii(d, a, b, c, k[3], 10, -1894986606);
  6305. c = ii(c, d, a, b, k[10], 15, -1051523);
  6306. b = ii(b, c, d, a, k[1], 21, -2054922799);
  6307. a = ii(a, b, c, d, k[8], 6, 1873313359);
  6308. d = ii(d, a, b, c, k[15], 10, -30611744);
  6309. c = ii(c, d, a, b, k[6], 15, -1560198380);
  6310. b = ii(b, c, d, a, k[13], 21, 1309151649);
  6311. a = ii(a, b, c, d, k[4], 6, -145523070);
  6312. d = ii(d, a, b, c, k[11], 10, -1120210379);
  6313. c = ii(c, d, a, b, k[2], 15, 718787259);
  6314. b = ii(b, c, d, a, k[9], 21, -343485551);
  6315. x[0] = add32(a, x[0]);
  6316. x[1] = add32(b, x[1]);
  6317. x[2] = add32(c, x[2]);
  6318. x[3] = add32(d, x[3]);
  6319. }
  6320. /**
  6321. * MD5 hash
  6322. * @param {String} entree string to hash
  6323. */
  6324. function cmn(q, a, b, x, s, t) {
  6325. a = add32(add32(a, q), add32(x, t));
  6326. return add32(a << s | a >>> 32 - s, b);
  6327. }
  6328. function ff(a, b, c, d, x, s, t) {
  6329. return cmn(b & c | ~b & d, a, b, x, s, t);
  6330. }
  6331. function gg(a, b, c, d, x, s, t) {
  6332. return cmn(b & d | c & ~d, a, b, x, s, t);
  6333. }
  6334. function hh(a, b, c, d, x, s, t) {
  6335. return cmn(b ^ c ^ d, a, b, x, s, t);
  6336. }
  6337. function ii(a, b, c, d, x, s, t) {
  6338. return cmn(c ^ (b | ~d), a, b, x, s, t);
  6339. }
  6340. function md51(s) {
  6341. var n = s.length,
  6342. state = [1732584193, -271733879, -1732584194, 271733878],
  6343. i;
  6344. for (i = 64; i <= s.length; i += 64) {
  6345. md5cycle(state, md5blk(s.substring(i - 64, i)));
  6346. }
  6347. s = s.substring(i - 64);
  6348. var tail = [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0];
  6349. for (i = 0; i < s.length; i++) {
  6350. tail[i >> 2] |= s.charCodeAt(i) << (i % 4 << 3);
  6351. }
  6352. tail[i >> 2] |= 0x80 << (i % 4 << 3);
  6353. if (i > 55) {
  6354. md5cycle(state, tail);
  6355. for (i = 0; i < 16; i++) {
  6356. tail[i] = 0;
  6357. }
  6358. }
  6359. tail[14] = n * 8;
  6360. md5cycle(state, tail);
  6361. return state;
  6362. }
  6363. /* there needs to be support for Unicode here,
  6364. * unless we pretend that we can redefine the MD-5
  6365. * algorithm for multi-byte characters (perhaps
  6366. * by adding every four 16-bit characters and
  6367. * shortening the sum to 32 bits). Otherwise
  6368. * I suggest performing MD-5 as if every character
  6369. * was two bytes--e.g., 0040 0025 = @%--but then
  6370. * how will an ordinary MD-5 sum be matched?
  6371. * There is no way to standardize text to something
  6372. * like UTF-8 before transformation; speed cost is
  6373. * utterly prohibitive. The JavaScript standard
  6374. * itself needs to look at this: it should start
  6375. * providing access to strings as preformed UTF-8
  6376. * 8-bit unsigned value arrays.
  6377. */
  6378. function md5blk(s) {
  6379. /* I figured global was faster. */
  6380. var md5blks = [],
  6381. i; /* Andy King said do it this way. */
  6382. for (i = 0; i < 64; i += 4) {
  6383. md5blks[i >> 2] = s.charCodeAt(i) + (s.charCodeAt(i + 1) << 8) + (s.charCodeAt(i + 2) << 16) + (s.charCodeAt(i + 3) << 24);
  6384. }
  6385. return md5blks;
  6386. }
  6387. var hex_chr = '0123456789abcdef'.split('');
  6388. function rhex(n) {
  6389. var s = '',
  6390. j = 0;
  6391. for (; j < 4; j++) {
  6392. s += hex_chr[n >> j * 8 + 4 & 0x0F] + hex_chr[n >> j * 8 & 0x0F];
  6393. }
  6394. return s;
  6395. }
  6396. function hex(x) {
  6397. for (var i = 0; i < x.length; i++) {
  6398. x[i] = rhex(x[i]);
  6399. }
  6400. return x.join('');
  6401. }
  6402. function md5(s) {
  6403. return hex(md51(s));
  6404. }
  6405. /* this function is much faster,
  6406. so if possible we use it. Some IEs
  6407. are the only ones I know of that
  6408. need the idiotic second function,
  6409. generated by an if clause. */
  6410. function add32(a, b) {
  6411. return a + b & 0xFFFFFFFF;
  6412. }
  6413. },{"../../util.js":70}],22:[function(_dereq_,module,exports){
  6414. "use strict";
  6415. Object.defineProperty(exports, "__esModule", {
  6416. value: true
  6417. });
  6418. exports.default = RMDstring;
  6419. var _util = _dereq_("../../util.js");
  6420. var _util2 = _interopRequireDefault(_util);
  6421. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  6422. var RMDsize = 160; /*
  6423. * CryptoMX Tools
  6424. * Copyright (C) 2004 - 2006 Derek Buitenhuis
  6425. *
  6426. * This program is free software; you can redistribute it and/or
  6427. * modify it under the terms of the GNU General Public License
  6428. * as published by the Free Software Foundation; either version 2
  6429. * of the License, or (at your option) any later version.
  6430. *
  6431. * This program is distributed in the hope that it will be useful,
  6432. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  6433. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  6434. * GNU General Public License for more details.
  6435. *
  6436. * You should have received a copy of the GNU General Public License
  6437. * along with this program; if not, write to the Free Software
  6438. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  6439. */
  6440. /* Modified by Recurity Labs GmbH
  6441. */
  6442. /* Modified by ProtonTech AG
  6443. */
  6444. /**
  6445. * @requires util
  6446. * @module crypto/hash/ripe-md
  6447. */
  6448. var X = [];
  6449. function ROL(x, n) {
  6450. return new Number(x << n | x >>> 32 - n);
  6451. }
  6452. function F(x, y, z) {
  6453. return new Number(x ^ y ^ z);
  6454. }
  6455. function G(x, y, z) {
  6456. return new Number(x & y | ~x & z);
  6457. }
  6458. function H(x, y, z) {
  6459. return new Number((x | ~y) ^ z);
  6460. }
  6461. function I(x, y, z) {
  6462. return new Number(x & z | y & ~z);
  6463. }
  6464. function J(x, y, z) {
  6465. return new Number(x ^ (y | ~z));
  6466. }
  6467. function mixOneRound(a, b, c, d, e, x, s, roundNumber) {
  6468. switch (roundNumber) {
  6469. case 0:
  6470. a += F(b, c, d) + x + 0x00000000;
  6471. break;
  6472. case 1:
  6473. a += G(b, c, d) + x + 0x5a827999;
  6474. break;
  6475. case 2:
  6476. a += H(b, c, d) + x + 0x6ed9eba1;
  6477. break;
  6478. case 3:
  6479. a += I(b, c, d) + x + 0x8f1bbcdc;
  6480. break;
  6481. case 4:
  6482. a += J(b, c, d) + x + 0xa953fd4e;
  6483. break;
  6484. case 5:
  6485. a += J(b, c, d) + x + 0x50a28be6;
  6486. break;
  6487. case 6:
  6488. a += I(b, c, d) + x + 0x5c4dd124;
  6489. break;
  6490. case 7:
  6491. a += H(b, c, d) + x + 0x6d703ef3;
  6492. break;
  6493. case 8:
  6494. a += G(b, c, d) + x + 0x7a6d76e9;
  6495. break;
  6496. case 9:
  6497. a += F(b, c, d) + x + 0x00000000;
  6498. break;
  6499. default:
  6500. throw new Error("Bogus round number");
  6501. break;
  6502. }
  6503. a = ROL(a, s) + e;
  6504. c = ROL(c, 10);
  6505. a &= 0xffffffff;
  6506. b &= 0xffffffff;
  6507. c &= 0xffffffff;
  6508. d &= 0xffffffff;
  6509. e &= 0xffffffff;
  6510. var retBlock = [];
  6511. retBlock[0] = a;
  6512. retBlock[1] = b;
  6513. retBlock[2] = c;
  6514. retBlock[3] = d;
  6515. retBlock[4] = e;
  6516. retBlock[5] = x;
  6517. retBlock[6] = s;
  6518. return retBlock;
  6519. }
  6520. function MDinit(MDbuf) {
  6521. MDbuf[0] = 0x67452301;
  6522. MDbuf[1] = 0xefcdab89;
  6523. MDbuf[2] = 0x98badcfe;
  6524. MDbuf[3] = 0x10325476;
  6525. MDbuf[4] = 0xc3d2e1f0;
  6526. }
  6527. var ROLs = [[11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8], [7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12], [11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5], [11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12], [9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6], [8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6], [9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11], [9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5], [15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8], [8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11]];
  6528. var indexes = [[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15], [7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8], [3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12], [1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2], [4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13], [5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12], [6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2], [15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13], [8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14], [12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11]];
  6529. function compress(MDbuf, X) {
  6530. var blockA = [];
  6531. var blockB = [];
  6532. var retBlock;
  6533. var i, j;
  6534. for (i = 0; i < 5; i++) {
  6535. blockA[i] = new Number(MDbuf[i]);
  6536. blockB[i] = new Number(MDbuf[i]);
  6537. }
  6538. var step = 0;
  6539. for (j = 0; j < 5; j++) {
  6540. for (i = 0; i < 16; i++) {
  6541. retBlock = mixOneRound(blockA[(step + 0) % 5], blockA[(step + 1) % 5], blockA[(step + 2) % 5], blockA[(step + 3) % 5], blockA[(step + 4) % 5], X[indexes[j][i]], ROLs[j][i], j);
  6542. blockA[(step + 0) % 5] = retBlock[0];
  6543. blockA[(step + 1) % 5] = retBlock[1];
  6544. blockA[(step + 2) % 5] = retBlock[2];
  6545. blockA[(step + 3) % 5] = retBlock[3];
  6546. blockA[(step + 4) % 5] = retBlock[4];
  6547. step += 4;
  6548. }
  6549. }
  6550. step = 0;
  6551. for (j = 5; j < 10; j++) {
  6552. for (i = 0; i < 16; i++) {
  6553. retBlock = mixOneRound(blockB[(step + 0) % 5], blockB[(step + 1) % 5], blockB[(step + 2) % 5], blockB[(step + 3) % 5], blockB[(step + 4) % 5], X[indexes[j][i]], ROLs[j][i], j);
  6554. blockB[(step + 0) % 5] = retBlock[0];
  6555. blockB[(step + 1) % 5] = retBlock[1];
  6556. blockB[(step + 2) % 5] = retBlock[2];
  6557. blockB[(step + 3) % 5] = retBlock[3];
  6558. blockB[(step + 4) % 5] = retBlock[4];
  6559. step += 4;
  6560. }
  6561. }
  6562. blockB[3] += blockA[2] + MDbuf[1];
  6563. MDbuf[1] = MDbuf[2] + blockA[3] + blockB[4];
  6564. MDbuf[2] = MDbuf[3] + blockA[4] + blockB[0];
  6565. MDbuf[3] = MDbuf[4] + blockA[0] + blockB[1];
  6566. MDbuf[4] = MDbuf[0] + blockA[1] + blockB[2];
  6567. MDbuf[0] = blockB[3];
  6568. }
  6569. function zeroX(X) {
  6570. for (var i = 0; i < 16; i++) {
  6571. X[i] = 0;
  6572. }
  6573. }
  6574. function MDfinish(MDbuf, strptr, lswlen, mswlen) {
  6575. var X = new Array(16);
  6576. zeroX(X);
  6577. var j = 0;
  6578. for (var i = 0; i < (lswlen & 63); i++) {
  6579. X[i >>> 2] ^= (strptr.charCodeAt(j++) & 255) << 8 * (i & 3);
  6580. }
  6581. X[lswlen >>> 2 & 15] ^= 1 << 8 * (lswlen & 3) + 7;
  6582. if ((lswlen & 63) > 55) {
  6583. compress(MDbuf, X);
  6584. X = new Array(16);
  6585. zeroX(X);
  6586. }
  6587. X[14] = lswlen << 3;
  6588. X[15] = lswlen >>> 29 | mswlen << 3;
  6589. compress(MDbuf, X);
  6590. }
  6591. function BYTES_TO_DWORD(fourChars) {
  6592. var tmp = (fourChars.charCodeAt(3) & 255) << 24;
  6593. tmp |= (fourChars.charCodeAt(2) & 255) << 16;
  6594. tmp |= (fourChars.charCodeAt(1) & 255) << 8;
  6595. tmp |= fourChars.charCodeAt(0) & 255;
  6596. return tmp;
  6597. }
  6598. function RMD(message) {
  6599. var MDbuf = new Array(RMDsize / 32);
  6600. var hashcode = new Array(RMDsize / 8);
  6601. var length;
  6602. var nbytes;
  6603. MDinit(MDbuf);
  6604. length = message.length;
  6605. var X = new Array(16);
  6606. zeroX(X);
  6607. var i,
  6608. j = 0;
  6609. for (nbytes = length; nbytes > 63; nbytes -= 64) {
  6610. for (i = 0; i < 16; i++) {
  6611. X[i] = BYTES_TO_DWORD(message.substr(j, 4));
  6612. j += 4;
  6613. }
  6614. compress(MDbuf, X);
  6615. }
  6616. MDfinish(MDbuf, message.substr(j), length, 0);
  6617. for (i = 0; i < RMDsize / 8; i += 4) {
  6618. hashcode[i] = MDbuf[i >>> 2] & 255;
  6619. hashcode[i + 1] = MDbuf[i >>> 2] >>> 8 & 255;
  6620. hashcode[i + 2] = MDbuf[i >>> 2] >>> 16 & 255;
  6621. hashcode[i + 3] = MDbuf[i >>> 2] >>> 24 & 255;
  6622. }
  6623. return hashcode;
  6624. }
  6625. function RMDstring(message) {
  6626. var hashcode = RMD(_util2.default.Uint8Array2str(message));
  6627. var retString = "";
  6628. for (var i = 0; i < RMDsize / 8; i++) {
  6629. retString += String.fromCharCode(hashcode[i]);
  6630. }
  6631. return _util2.default.str2Uint8Array(retString);
  6632. }
  6633. },{"../../util.js":70}],23:[function(_dereq_,module,exports){
  6634. /**
  6635. * @preserve A JavaScript implementation of the SHA family of hashes, as
  6636. * defined in FIPS PUB 180-2 as well as the corresponding HMAC implementation
  6637. * as defined in FIPS PUB 198a
  6638. *
  6639. * Copyright Brian Turek 2008-2015
  6640. * Distributed under the BSD License
  6641. * See http://caligatio.github.com/jsSHA/ for more information
  6642. *
  6643. * Several functions taken from Paul Johnston
  6644. */
  6645. /**
  6646. * SUPPORTED_ALGS is the stub for a compile flag that will cause pruning of
  6647. * functions that are not needed when a limited number of SHA families are
  6648. * selected
  6649. *
  6650. * @define {number} ORed value of SHA variants to be supported
  6651. * 1 = SHA-1, 2 = SHA-224/SHA-256, 4 = SHA-384/SHA-512
  6652. */
  6653. "use strict";
  6654. Object.defineProperty(exports, "__esModule", {
  6655. value: true
  6656. });
  6657. var SUPPORTED_ALGS = 4 | 2 | 1;
  6658. /**
  6659. * Int_64 is a object for 2 32-bit numbers emulating a 64-bit number
  6660. *
  6661. * @private
  6662. * @constructor
  6663. * @this {Int_64}
  6664. * @param {number} msint_32 The most significant 32-bits of a 64-bit number
  6665. * @param {number} lsint_32 The least significant 32-bits of a 64-bit number
  6666. */
  6667. function Int_64(msint_32, lsint_32) {
  6668. this.highOrder = msint_32;
  6669. this.lowOrder = lsint_32;
  6670. }
  6671. /**
  6672. * Convert a string to an array of big-endian words
  6673. *
  6674. * @private
  6675. * @param {string} str String to be converted to binary representation
  6676. * @param {string} utfType The Unicode type, UTF8 or UTF16BE, UTF16LE, to
  6677. * use to encode the source string
  6678. * @return {{value : Array.<number>, binLen : number}} Hash list where
  6679. * "value" contains the output number array and "binLen" is the binary
  6680. * length of "value"
  6681. */
  6682. function str2binb(str, utfType) {
  6683. var bin = [],
  6684. codePnt,
  6685. binArr = [],
  6686. byteCnt = 0,
  6687. i,
  6688. j,
  6689. offset;
  6690. if ("UTF8" === utfType) {
  6691. for (i = 0; i < str.length; i += 1) {
  6692. codePnt = str.charCodeAt(i);
  6693. binArr = [];
  6694. if (0x80 > codePnt) {
  6695. binArr.push(codePnt);
  6696. } else if (0x800 > codePnt) {
  6697. binArr.push(0xC0 | codePnt >>> 6);
  6698. binArr.push(0x80 | codePnt & 0x3F);
  6699. } else if (0xd800 > codePnt || 0xe000 <= codePnt) {
  6700. binArr.push(0xe0 | codePnt >>> 12, 0x80 | codePnt >>> 6 & 0x3f, 0x80 | codePnt & 0x3f);
  6701. } else {
  6702. i += 1;
  6703. codePnt = 0x10000 + ((codePnt & 0x3ff) << 10 | str.charCodeAt(i) & 0x3ff);
  6704. binArr.push(0xf0 | codePnt >>> 18, 0x80 | codePnt >>> 12 & 0x3f, 0x80 | codePnt >>> 6 & 0x3f, 0x80 | codePnt & 0x3f);
  6705. }
  6706. for (j = 0; j < binArr.length; j += 1) {
  6707. offset = byteCnt >>> 2;
  6708. while (bin.length <= offset) {
  6709. bin.push(0);
  6710. }
  6711. bin[offset] |= binArr[j] << 24 - 8 * (byteCnt % 4);
  6712. byteCnt += 1;
  6713. }
  6714. }
  6715. } else if ("UTF16BE" === utfType || "UTF16LE" === utfType) {
  6716. for (i = 0; i < str.length; i += 1) {
  6717. codePnt = str.charCodeAt(i);
  6718. /* Internally strings are UTF-16BE so only change if UTF-16LE */
  6719. if ("UTF16LE" === utfType) {
  6720. j = codePnt & 0xFF;
  6721. codePnt = j << 8 | codePnt >> 8;
  6722. }
  6723. offset = byteCnt >>> 2;
  6724. while (bin.length <= offset) {
  6725. bin.push(0);
  6726. }
  6727. bin[offset] |= codePnt << 16 - 8 * (byteCnt % 4);
  6728. byteCnt += 2;
  6729. }
  6730. }
  6731. return { "value": bin, "binLen": byteCnt * 8 };
  6732. }
  6733. /**
  6734. * Convert a hex string to an array of big-endian words
  6735. *
  6736. * @private
  6737. * @param {string} str String to be converted to binary representation
  6738. * @return {{value : Array.<number>, binLen : number}} Hash list where
  6739. * "value" contains the output number array and "binLen" is the binary
  6740. * length of "value"
  6741. */
  6742. function hex2binb(str) {
  6743. var bin = [],
  6744. length = str.length,
  6745. i,
  6746. num,
  6747. offset;
  6748. if (0 !== length % 2) {
  6749. throw "String of HEX type must be in byte increments";
  6750. }
  6751. for (i = 0; i < length; i += 2) {
  6752. num = parseInt(str.substr(i, 2), 16);
  6753. if (!isNaN(num)) {
  6754. offset = i >>> 3;
  6755. while (bin.length <= offset) {
  6756. bin.push(0);
  6757. }
  6758. bin[i >>> 3] |= num << 24 - 4 * (i % 8);
  6759. } else {
  6760. throw "String of HEX type contains invalid characters";
  6761. }
  6762. }
  6763. return { "value": bin, "binLen": length * 4 };
  6764. }
  6765. /**
  6766. * Convert a string of raw bytes to an array of big-endian words
  6767. *
  6768. * @private
  6769. * @param {string} str String of raw bytes to be converted to binary representation
  6770. * @return {{value : Array.<number>, binLen : number}} Hash list where
  6771. * "value" contains the output number array and "binLen" is the binary
  6772. * length of "value"
  6773. */
  6774. function bytes2binb(str) {
  6775. var bin = [],
  6776. codePnt,
  6777. i,
  6778. offset;
  6779. for (i = 0; i < str.length; i += 1) {
  6780. codePnt = str.charCodeAt(i);
  6781. offset = i >>> 2;
  6782. if (bin.length <= offset) {
  6783. bin.push(0);
  6784. }
  6785. bin[offset] |= codePnt << 24 - 8 * (i % 4);
  6786. }
  6787. return { "value": bin, "binLen": str.length * 8 };
  6788. }
  6789. /**
  6790. * Convert a Uint8Array of raw bytes to an array of big-endian 32-bit words
  6791. *
  6792. * @private
  6793. * @param {Uint8Array} str String of raw bytes to be converted to binary representation
  6794. * @return {{value : Array.<number>, binLen : number}} Hash list where
  6795. * "value" contains the output array and "binLen" is the binary
  6796. * length of "value"
  6797. */
  6798. function typed2binb(array) {
  6799. var bin = [],
  6800. octet,
  6801. i,
  6802. offset;
  6803. for (i = 0; i < array.length; i += 1) {
  6804. octet = array[i];
  6805. offset = i >>> 2;
  6806. if (bin.length <= offset) {
  6807. bin.push(0);
  6808. }
  6809. bin[offset] |= octet << 24 - 8 * (i % 4);
  6810. }
  6811. return { "value": bin, "binLen": array.length * 8 };
  6812. }
  6813. /**
  6814. * Convert a base-64 string to an array of big-endian words
  6815. *
  6816. * @private
  6817. * @param {string} str String to be converted to binary representation
  6818. * @return {{value : Array.<number>, binLen : number}} Hash list where
  6819. * "value" contains the output number array and "binLen" is the binary
  6820. * length of "value"
  6821. */
  6822. function b642binb(str) {
  6823. var retVal = [],
  6824. byteCnt = 0,
  6825. index,
  6826. i,
  6827. j,
  6828. tmpInt,
  6829. strPart,
  6830. firstEqual,
  6831. offset,
  6832. b64Tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  6833. if (-1 === str.search(/^[a-zA-Z0-9=+\/]+$/)) {
  6834. throw "Invalid character in base-64 string";
  6835. }
  6836. firstEqual = str.indexOf('=');
  6837. str = str.replace(/\=/g, '');
  6838. if (-1 !== firstEqual && firstEqual < str.length) {
  6839. throw "Invalid '=' found in base-64 string";
  6840. }
  6841. for (i = 0; i < str.length; i += 4) {
  6842. strPart = str.substr(i, 4);
  6843. tmpInt = 0;
  6844. for (j = 0; j < strPart.length; j += 1) {
  6845. index = b64Tab.indexOf(strPart[j]);
  6846. tmpInt |= index << 18 - 6 * j;
  6847. }
  6848. for (j = 0; j < strPart.length - 1; j += 1) {
  6849. offset = byteCnt >>> 2;
  6850. while (retVal.length <= offset) {
  6851. retVal.push(0);
  6852. }
  6853. retVal[offset] |= (tmpInt >>> 16 - j * 8 & 0xFF) << 24 - 8 * (byteCnt % 4);
  6854. byteCnt += 1;
  6855. }
  6856. }
  6857. return { "value": retVal, "binLen": byteCnt * 8 };
  6858. }
  6859. /**
  6860. * Convert an array of big-endian words to a hex string.
  6861. *
  6862. * @private
  6863. * @param {Array.<number>} binarray Array of integers to be converted to
  6864. * hexidecimal representation
  6865. * @param {{outputUpper : boolean, b64Pad : string}} formatOpts Hash list
  6866. * containing validated output formatting options
  6867. * @return {string} Hexidecimal representation of the parameter in string
  6868. * form
  6869. */
  6870. function binb2hex(binarray, formatOpts) {
  6871. var hex_tab = "0123456789abcdef",
  6872. str = "",
  6873. length = binarray.length * 4,
  6874. i,
  6875. srcByte;
  6876. for (i = 0; i < length; i += 1) {
  6877. /* The below is more than a byte but it gets taken care of later */
  6878. srcByte = binarray[i >>> 2] >>> (3 - i % 4) * 8;
  6879. str += hex_tab.charAt(srcByte >>> 4 & 0xF) + hex_tab.charAt(srcByte & 0xF);
  6880. }
  6881. return formatOpts["outputUpper"] ? str.toUpperCase() : str;
  6882. }
  6883. /**
  6884. * Convert an array of big-endian words to a base-64 string
  6885. *
  6886. * @private
  6887. * @param {Array.<number>} binarray Array of integers to be converted to
  6888. * base-64 representation
  6889. * @param {{outputUpper : boolean, b64Pad : string}} formatOpts Hash list
  6890. * containing validated output formatting options
  6891. * @return {string} Base-64 encoded representation of the parameter in
  6892. * string form
  6893. */
  6894. function binb2b64(binarray, formatOpts) {
  6895. var str = "",
  6896. length = binarray.length * 4,
  6897. i,
  6898. j,
  6899. triplet,
  6900. offset,
  6901. int1,
  6902. int2,
  6903. b64Tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
  6904. for (i = 0; i < length; i += 3) {
  6905. offset = i + 1 >>> 2;
  6906. int1 = binarray.length <= offset ? 0 : binarray[offset];
  6907. offset = i + 2 >>> 2;
  6908. int2 = binarray.length <= offset ? 0 : binarray[offset];
  6909. triplet = (binarray[i >>> 2] >>> 8 * (3 - i % 4) & 0xFF) << 16 | (int1 >>> 8 * (3 - (i + 1) % 4) & 0xFF) << 8 | int2 >>> 8 * (3 - (i + 2) % 4) & 0xFF;
  6910. for (j = 0; j < 4; j += 1) {
  6911. if (i * 8 + j * 6 <= binarray.length * 32) {
  6912. str += b64Tab.charAt(triplet >>> 6 * (3 - j) & 0x3F);
  6913. } else {
  6914. str += formatOpts["b64Pad"];
  6915. }
  6916. }
  6917. }
  6918. return str;
  6919. }
  6920. /**
  6921. * Convert an array of big-endian words to raw bytes string
  6922. *
  6923. * @private
  6924. * @param {Array.<number>} binarray Array of integers to be converted to
  6925. * a raw bytes string representation
  6926. * @param {!Object} formatOpts Unused Hash list
  6927. * @return {string} Raw bytes representation of the parameter in string
  6928. * form
  6929. */
  6930. function binb2bytes(binarray, formatOpts) {
  6931. var str = "",
  6932. length = binarray.length * 4,
  6933. i,
  6934. srcByte;
  6935. for (i = 0; i < length; i += 1) {
  6936. srcByte = binarray[i >>> 2] >>> (3 - i % 4) * 8 & 0xFF;
  6937. str += String.fromCharCode(srcByte);
  6938. }
  6939. return str;
  6940. }
  6941. /**
  6942. * Convert an array of big-endian words to raw bytes Uint8Array
  6943. *
  6944. * @private
  6945. * @param {Array.<number>} binarray Array of integers to be converted to
  6946. * a raw bytes string representation
  6947. * @param {!Object} formatOpts Unused Hash list
  6948. * @return {Uint8Array} Raw bytes representation of the parameter
  6949. */
  6950. function binb2typed(binarray, formatOpts) {
  6951. var length = binarray.length * 4;
  6952. var arr = new Uint8Array(length),
  6953. i;
  6954. for (i = 0; i < length; i += 1) {
  6955. arr[i] = binarray[i >>> 2] >>> (3 - i % 4) * 8 & 0xFF;
  6956. }
  6957. return arr;
  6958. }
  6959. /**
  6960. * Validate hash list containing output formatting options, ensuring
  6961. * presence of every option or adding the default value
  6962. *
  6963. * @private
  6964. * @param {{outputUpper : boolean, b64Pad : string}|undefined} outputOpts
  6965. * Hash list of output formatting options
  6966. * @return {{outputUpper : boolean, b64Pad : string}} Validated hash list
  6967. * containing output formatting options
  6968. */
  6969. function getOutputOpts(outputOpts) {
  6970. var retVal = { "outputUpper": false, "b64Pad": "=" };
  6971. try {
  6972. if (outputOpts.hasOwnProperty("outputUpper")) {
  6973. retVal["outputUpper"] = outputOpts["outputUpper"];
  6974. }
  6975. if (outputOpts.hasOwnProperty("b64Pad")) {
  6976. retVal["b64Pad"] = outputOpts["b64Pad"];
  6977. }
  6978. } catch (ignore) {}
  6979. if ("boolean" !== typeof retVal["outputUpper"]) {
  6980. throw "Invalid outputUpper formatting option";
  6981. }
  6982. if ("string" !== typeof retVal["b64Pad"]) {
  6983. throw "Invalid b64Pad formatting option";
  6984. }
  6985. return retVal;
  6986. }
  6987. /**
  6988. * The 32-bit implementation of circular rotate left
  6989. *
  6990. * @private
  6991. * @param {number} x The 32-bit integer argument
  6992. * @param {number} n The number of bits to shift
  6993. * @return {number} The x shifted circularly by n bits
  6994. */
  6995. function rotl_32(x, n) {
  6996. return x << n | x >>> 32 - n;
  6997. }
  6998. /**
  6999. * The 32-bit implementation of circular rotate right
  7000. *
  7001. * @private
  7002. * @param {number} x The 32-bit integer argument
  7003. * @param {number} n The number of bits to shift
  7004. * @return {number} The x shifted circularly by n bits
  7005. */
  7006. function rotr_32(x, n) {
  7007. return x >>> n | x << 32 - n;
  7008. }
  7009. /**
  7010. * The 64-bit implementation of circular rotate right
  7011. *
  7012. * @private
  7013. * @param {Int_64} x The 64-bit integer argument
  7014. * @param {number} n The number of bits to shift
  7015. * @return {Int_64} The x shifted circularly by n bits
  7016. */
  7017. function rotr_64(x, n) {
  7018. var retVal = null,
  7019. tmp = new Int_64(x.highOrder, x.lowOrder);
  7020. if (32 >= n) {
  7021. retVal = new Int_64(tmp.highOrder >>> n | tmp.lowOrder << 32 - n & 0xFFFFFFFF, tmp.lowOrder >>> n | tmp.highOrder << 32 - n & 0xFFFFFFFF);
  7022. } else {
  7023. retVal = new Int_64(tmp.lowOrder >>> n - 32 | tmp.highOrder << 64 - n & 0xFFFFFFFF, tmp.highOrder >>> n - 32 | tmp.lowOrder << 64 - n & 0xFFFFFFFF);
  7024. }
  7025. return retVal;
  7026. }
  7027. /**
  7028. * The 32-bit implementation of shift right
  7029. *
  7030. * @private
  7031. * @param {number} x The 32-bit integer argument
  7032. * @param {number} n The number of bits to shift
  7033. * @return {number} The x shifted by n bits
  7034. */
  7035. function shr_32(x, n) {
  7036. return x >>> n;
  7037. }
  7038. /**
  7039. * The 64-bit implementation of shift right
  7040. *
  7041. * @private
  7042. * @param {Int_64} x The 64-bit integer argument
  7043. * @param {number} n The number of bits to shift
  7044. * @return {Int_64} The x shifted by n bits
  7045. */
  7046. function shr_64(x, n) {
  7047. var retVal = null;
  7048. if (32 >= n) {
  7049. retVal = new Int_64(x.highOrder >>> n, x.lowOrder >>> n | x.highOrder << 32 - n & 0xFFFFFFFF);
  7050. } else {
  7051. retVal = new Int_64(0, x.highOrder >>> n - 32);
  7052. }
  7053. return retVal;
  7054. }
  7055. /**
  7056. * The 32-bit implementation of the NIST specified Parity function
  7057. *
  7058. * @private
  7059. * @param {number} x The first 32-bit integer argument
  7060. * @param {number} y The second 32-bit integer argument
  7061. * @param {number} z The third 32-bit integer argument
  7062. * @return {number} The NIST specified output of the function
  7063. */
  7064. function parity_32(x, y, z) {
  7065. return x ^ y ^ z;
  7066. }
  7067. /**
  7068. * The 32-bit implementation of the NIST specified Ch function
  7069. *
  7070. * @private
  7071. * @param {number} x The first 32-bit integer argument
  7072. * @param {number} y The second 32-bit integer argument
  7073. * @param {number} z The third 32-bit integer argument
  7074. * @return {number} The NIST specified output of the function
  7075. */
  7076. function ch_32(x, y, z) {
  7077. return x & y ^ ~x & z;
  7078. }
  7079. /**
  7080. * The 64-bit implementation of the NIST specified Ch function
  7081. *
  7082. * @private
  7083. * @param {Int_64} x The first 64-bit integer argument
  7084. * @param {Int_64} y The second 64-bit integer argument
  7085. * @param {Int_64} z The third 64-bit integer argument
  7086. * @return {Int_64} The NIST specified output of the function
  7087. */
  7088. function ch_64(x, y, z) {
  7089. return new Int_64(x.highOrder & y.highOrder ^ ~x.highOrder & z.highOrder, x.lowOrder & y.lowOrder ^ ~x.lowOrder & z.lowOrder);
  7090. }
  7091. /**
  7092. * The 32-bit implementation of the NIST specified Maj function
  7093. *
  7094. * @private
  7095. * @param {number} x The first 32-bit integer argument
  7096. * @param {number} y The second 32-bit integer argument
  7097. * @param {number} z The third 32-bit integer argument
  7098. * @return {number} The NIST specified output of the function
  7099. */
  7100. function maj_32(x, y, z) {
  7101. return x & y ^ x & z ^ y & z;
  7102. }
  7103. /**
  7104. * The 64-bit implementation of the NIST specified Maj function
  7105. *
  7106. * @private
  7107. * @param {Int_64} x The first 64-bit integer argument
  7108. * @param {Int_64} y The second 64-bit integer argument
  7109. * @param {Int_64} z The third 64-bit integer argument
  7110. * @return {Int_64} The NIST specified output of the function
  7111. */
  7112. function maj_64(x, y, z) {
  7113. return new Int_64(x.highOrder & y.highOrder ^ x.highOrder & z.highOrder ^ y.highOrder & z.highOrder, x.lowOrder & y.lowOrder ^ x.lowOrder & z.lowOrder ^ y.lowOrder & z.lowOrder);
  7114. }
  7115. /**
  7116. * The 32-bit implementation of the NIST specified Sigma0 function
  7117. *
  7118. * @private
  7119. * @param {number} x The 32-bit integer argument
  7120. * @return {number} The NIST specified output of the function
  7121. */
  7122. function sigma0_32(x) {
  7123. return rotr_32(x, 2) ^ rotr_32(x, 13) ^ rotr_32(x, 22);
  7124. }
  7125. /**
  7126. * The 64-bit implementation of the NIST specified Sigma0 function
  7127. *
  7128. * @private
  7129. * @param {Int_64} x The 64-bit integer argument
  7130. * @return {Int_64} The NIST specified output of the function
  7131. */
  7132. function sigma0_64(x) {
  7133. var rotr28 = rotr_64(x, 28),
  7134. rotr34 = rotr_64(x, 34),
  7135. rotr39 = rotr_64(x, 39);
  7136. return new Int_64(rotr28.highOrder ^ rotr34.highOrder ^ rotr39.highOrder, rotr28.lowOrder ^ rotr34.lowOrder ^ rotr39.lowOrder);
  7137. }
  7138. /**
  7139. * The 32-bit implementation of the NIST specified Sigma1 function
  7140. *
  7141. * @private
  7142. * @param {number} x The 32-bit integer argument
  7143. * @return {number} The NIST specified output of the function
  7144. */
  7145. function sigma1_32(x) {
  7146. return rotr_32(x, 6) ^ rotr_32(x, 11) ^ rotr_32(x, 25);
  7147. }
  7148. /**
  7149. * The 64-bit implementation of the NIST specified Sigma1 function
  7150. *
  7151. * @private
  7152. * @param {Int_64} x The 64-bit integer argument
  7153. * @return {Int_64} The NIST specified output of the function
  7154. */
  7155. function sigma1_64(x) {
  7156. var rotr14 = rotr_64(x, 14),
  7157. rotr18 = rotr_64(x, 18),
  7158. rotr41 = rotr_64(x, 41);
  7159. return new Int_64(rotr14.highOrder ^ rotr18.highOrder ^ rotr41.highOrder, rotr14.lowOrder ^ rotr18.lowOrder ^ rotr41.lowOrder);
  7160. }
  7161. /**
  7162. * The 32-bit implementation of the NIST specified Gamma0 function
  7163. *
  7164. * @private
  7165. * @param {number} x The 32-bit integer argument
  7166. * @return {number} The NIST specified output of the function
  7167. */
  7168. function gamma0_32(x) {
  7169. return rotr_32(x, 7) ^ rotr_32(x, 18) ^ shr_32(x, 3);
  7170. }
  7171. /**
  7172. * The 64-bit implementation of the NIST specified Gamma0 function
  7173. *
  7174. * @private
  7175. * @param {Int_64} x The 64-bit integer argument
  7176. * @return {Int_64} The NIST specified output of the function
  7177. */
  7178. function gamma0_64(x) {
  7179. var rotr1 = rotr_64(x, 1),
  7180. rotr8 = rotr_64(x, 8),
  7181. shr7 = shr_64(x, 7);
  7182. return new Int_64(rotr1.highOrder ^ rotr8.highOrder ^ shr7.highOrder, rotr1.lowOrder ^ rotr8.lowOrder ^ shr7.lowOrder);
  7183. }
  7184. /**
  7185. * The 32-bit implementation of the NIST specified Gamma1 function
  7186. *
  7187. * @private
  7188. * @param {number} x The 32-bit integer argument
  7189. * @return {number} The NIST specified output of the function
  7190. */
  7191. function gamma1_32(x) {
  7192. return rotr_32(x, 17) ^ rotr_32(x, 19) ^ shr_32(x, 10);
  7193. }
  7194. /**
  7195. * The 64-bit implementation of the NIST specified Gamma1 function
  7196. *
  7197. * @private
  7198. * @param {Int_64} x The 64-bit integer argument
  7199. * @return {Int_64} The NIST specified output of the function
  7200. */
  7201. function gamma1_64(x) {
  7202. var rotr19 = rotr_64(x, 19),
  7203. rotr61 = rotr_64(x, 61),
  7204. shr6 = shr_64(x, 6);
  7205. return new Int_64(rotr19.highOrder ^ rotr61.highOrder ^ shr6.highOrder, rotr19.lowOrder ^ rotr61.lowOrder ^ shr6.lowOrder);
  7206. }
  7207. /**
  7208. * Add two 32-bit integers, wrapping at 2^32. This uses 16-bit operations
  7209. * internally to work around bugs in some JS interpreters.
  7210. *
  7211. * @private
  7212. * @param {number} a The first 32-bit integer argument to be added
  7213. * @param {number} b The second 32-bit integer argument to be added
  7214. * @return {number} The sum of a + b
  7215. */
  7216. function safeAdd_32_2(a, b) {
  7217. var lsw = (a & 0xFFFF) + (b & 0xFFFF),
  7218. msw = (a >>> 16) + (b >>> 16) + (lsw >>> 16);
  7219. return (msw & 0xFFFF) << 16 | lsw & 0xFFFF;
  7220. }
  7221. /**
  7222. * Add four 32-bit integers, wrapping at 2^32. This uses 16-bit operations
  7223. * internally to work around bugs in some JS interpreters.
  7224. *
  7225. * @private
  7226. * @param {number} a The first 32-bit integer argument to be added
  7227. * @param {number} b The second 32-bit integer argument to be added
  7228. * @param {number} c The third 32-bit integer argument to be added
  7229. * @param {number} d The fourth 32-bit integer argument to be added
  7230. * @return {number} The sum of a + b + c + d
  7231. */
  7232. function safeAdd_32_4(a, b, c, d) {
  7233. var lsw = (a & 0xFFFF) + (b & 0xFFFF) + (c & 0xFFFF) + (d & 0xFFFF),
  7234. msw = (a >>> 16) + (b >>> 16) + (c >>> 16) + (d >>> 16) + (lsw >>> 16);
  7235. return (msw & 0xFFFF) << 16 | lsw & 0xFFFF;
  7236. }
  7237. /**
  7238. * Add five 32-bit integers, wrapping at 2^32. This uses 16-bit operations
  7239. * internally to work around bugs in some JS interpreters.
  7240. *
  7241. * @private
  7242. * @param {number} a The first 32-bit integer argument to be added
  7243. * @param {number} b The second 32-bit integer argument to be added
  7244. * @param {number} c The third 32-bit integer argument to be added
  7245. * @param {number} d The fourth 32-bit integer argument to be added
  7246. * @param {number} e The fifth 32-bit integer argument to be added
  7247. * @return {number} The sum of a + b + c + d + e
  7248. */
  7249. function safeAdd_32_5(a, b, c, d, e) {
  7250. var lsw = (a & 0xFFFF) + (b & 0xFFFF) + (c & 0xFFFF) + (d & 0xFFFF) + (e & 0xFFFF),
  7251. msw = (a >>> 16) + (b >>> 16) + (c >>> 16) + (d >>> 16) + (e >>> 16) + (lsw >>> 16);
  7252. return (msw & 0xFFFF) << 16 | lsw & 0xFFFF;
  7253. }
  7254. /**
  7255. * Add two 64-bit integers, wrapping at 2^64. This uses 16-bit operations
  7256. * internally to work around bugs in some JS interpreters.
  7257. *
  7258. * @private
  7259. * @param {Int_64} x The first 64-bit integer argument to be added
  7260. * @param {Int_64} y The second 64-bit integer argument to be added
  7261. * @return {Int_64} The sum of x + y
  7262. */
  7263. function safeAdd_64_2(x, y) {
  7264. var lsw, msw, lowOrder, highOrder;
  7265. lsw = (x.lowOrder & 0xFFFF) + (y.lowOrder & 0xFFFF);
  7266. msw = (x.lowOrder >>> 16) + (y.lowOrder >>> 16) + (lsw >>> 16);
  7267. lowOrder = (msw & 0xFFFF) << 16 | lsw & 0xFFFF;
  7268. lsw = (x.highOrder & 0xFFFF) + (y.highOrder & 0xFFFF) + (msw >>> 16);
  7269. msw = (x.highOrder >>> 16) + (y.highOrder >>> 16) + (lsw >>> 16);
  7270. highOrder = (msw & 0xFFFF) << 16 | lsw & 0xFFFF;
  7271. return new Int_64(highOrder, lowOrder);
  7272. }
  7273. /**
  7274. * Add four 64-bit integers, wrapping at 2^64. This uses 16-bit operations
  7275. * internally to work around bugs in some JS interpreters.
  7276. *
  7277. * @private
  7278. * @param {Int_64} a The first 64-bit integer argument to be added
  7279. * @param {Int_64} b The second 64-bit integer argument to be added
  7280. * @param {Int_64} c The third 64-bit integer argument to be added
  7281. * @param {Int_64} d The fouth 64-bit integer argument to be added
  7282. * @return {Int_64} The sum of a + b + c + d
  7283. */
  7284. function safeAdd_64_4(a, b, c, d) {
  7285. var lsw, msw, lowOrder, highOrder;
  7286. lsw = (a.lowOrder & 0xFFFF) + (b.lowOrder & 0xFFFF) + (c.lowOrder & 0xFFFF) + (d.lowOrder & 0xFFFF);
  7287. msw = (a.lowOrder >>> 16) + (b.lowOrder >>> 16) + (c.lowOrder >>> 16) + (d.lowOrder >>> 16) + (lsw >>> 16);
  7288. lowOrder = (msw & 0xFFFF) << 16 | lsw & 0xFFFF;
  7289. lsw = (a.highOrder & 0xFFFF) + (b.highOrder & 0xFFFF) + (c.highOrder & 0xFFFF) + (d.highOrder & 0xFFFF) + (msw >>> 16);
  7290. msw = (a.highOrder >>> 16) + (b.highOrder >>> 16) + (c.highOrder >>> 16) + (d.highOrder >>> 16) + (lsw >>> 16);
  7291. highOrder = (msw & 0xFFFF) << 16 | lsw & 0xFFFF;
  7292. return new Int_64(highOrder, lowOrder);
  7293. }
  7294. /**
  7295. * Add five 64-bit integers, wrapping at 2^64. This uses 16-bit operations
  7296. * internally to work around bugs in some JS interpreters.
  7297. *
  7298. * @private
  7299. * @param {Int_64} a The first 64-bit integer argument to be added
  7300. * @param {Int_64} b The second 64-bit integer argument to be added
  7301. * @param {Int_64} c The third 64-bit integer argument to be added
  7302. * @param {Int_64} d The fouth 64-bit integer argument to be added
  7303. * @param {Int_64} e The fouth 64-bit integer argument to be added
  7304. * @return {Int_64} The sum of a + b + c + d + e
  7305. */
  7306. function safeAdd_64_5(a, b, c, d, e) {
  7307. var lsw, msw, lowOrder, highOrder;
  7308. lsw = (a.lowOrder & 0xFFFF) + (b.lowOrder & 0xFFFF) + (c.lowOrder & 0xFFFF) + (d.lowOrder & 0xFFFF) + (e.lowOrder & 0xFFFF);
  7309. msw = (a.lowOrder >>> 16) + (b.lowOrder >>> 16) + (c.lowOrder >>> 16) + (d.lowOrder >>> 16) + (e.lowOrder >>> 16) + (lsw >>> 16);
  7310. lowOrder = (msw & 0xFFFF) << 16 | lsw & 0xFFFF;
  7311. lsw = (a.highOrder & 0xFFFF) + (b.highOrder & 0xFFFF) + (c.highOrder & 0xFFFF) + (d.highOrder & 0xFFFF) + (e.highOrder & 0xFFFF) + (msw >>> 16);
  7312. msw = (a.highOrder >>> 16) + (b.highOrder >>> 16) + (c.highOrder >>> 16) + (d.highOrder >>> 16) + (e.highOrder >>> 16) + (lsw >>> 16);
  7313. highOrder = (msw & 0xFFFF) << 16 | lsw & 0xFFFF;
  7314. return new Int_64(highOrder, lowOrder);
  7315. }
  7316. /**
  7317. * Calculates the SHA-1 hash of the string set at instantiation
  7318. *
  7319. * @private
  7320. * @param {Array.<number>} message The binary array representation of the
  7321. * string to hash
  7322. * @param {number} messageLen The number of bits in the message
  7323. * @return {Array.<number>} The array of integers representing the SHA-1
  7324. * hash of message
  7325. */
  7326. function coreSHA1(message, messageLen) {
  7327. var W = [],
  7328. a,
  7329. b,
  7330. c,
  7331. d,
  7332. e,
  7333. T,
  7334. ch = ch_32,
  7335. parity = parity_32,
  7336. maj = maj_32,
  7337. rotl = rotl_32,
  7338. safeAdd_2 = safeAdd_32_2,
  7339. i,
  7340. t,
  7341. safeAdd_5 = safeAdd_32_5,
  7342. appendedMessageLength,
  7343. offset,
  7344. H = [0x67452301, 0xefcdab89, 0x98badcfe, 0x10325476, 0xc3d2e1f0];
  7345. offset = (messageLen + 65 >>> 9 << 4) + 15;
  7346. while (message.length <= offset) {
  7347. message.push(0);
  7348. }
  7349. /* Append '1' at the end of the binary string */
  7350. message[messageLen >>> 5] |= 0x80 << 24 - messageLen % 32;
  7351. /* Append length of binary string in the position such that the new
  7352. length is a multiple of 512. Logic does not work for even multiples
  7353. of 512 but there can never be even multiples of 512 */
  7354. message[offset] = messageLen;
  7355. appendedMessageLength = message.length;
  7356. for (i = 0; i < appendedMessageLength; i += 16) {
  7357. a = H[0];
  7358. b = H[1];
  7359. c = H[2];
  7360. d = H[3];
  7361. e = H[4];
  7362. for (t = 0; t < 80; t += 1) {
  7363. if (t < 16) {
  7364. W[t] = message[t + i];
  7365. } else {
  7366. W[t] = rotl(W[t - 3] ^ W[t - 8] ^ W[t - 14] ^ W[t - 16], 1);
  7367. }
  7368. if (t < 20) {
  7369. T = safeAdd_5(rotl(a, 5), ch(b, c, d), e, 0x5a827999, W[t]);
  7370. } else if (t < 40) {
  7371. T = safeAdd_5(rotl(a, 5), parity(b, c, d), e, 0x6ed9eba1, W[t]);
  7372. } else if (t < 60) {
  7373. T = safeAdd_5(rotl(a, 5), maj(b, c, d), e, 0x8f1bbcdc, W[t]);
  7374. } else {
  7375. T = safeAdd_5(rotl(a, 5), parity(b, c, d), e, 0xca62c1d6, W[t]);
  7376. }
  7377. e = d;
  7378. d = c;
  7379. c = rotl(b, 30);
  7380. b = a;
  7381. a = T;
  7382. }
  7383. H[0] = safeAdd_2(a, H[0]);
  7384. H[1] = safeAdd_2(b, H[1]);
  7385. H[2] = safeAdd_2(c, H[2]);
  7386. H[3] = safeAdd_2(d, H[3]);
  7387. H[4] = safeAdd_2(e, H[4]);
  7388. }
  7389. return H;
  7390. }
  7391. /**
  7392. * Calculates the desired SHA-2 hash of the string set at instantiation
  7393. *
  7394. * @private
  7395. * @param {Array.<number>} message The binary array representation of the
  7396. * string to hash
  7397. * @param {number} messageLen The number of bits in message
  7398. * @param {string} variant The desired SHA-2 variant
  7399. * @return {Array.<number>} The array of integers representing the SHA-2
  7400. * hash of message
  7401. */
  7402. function coreSHA2(message, messageLen, variant) {
  7403. var a,
  7404. b,
  7405. c,
  7406. d,
  7407. e,
  7408. f,
  7409. g,
  7410. h,
  7411. T1,
  7412. T2,
  7413. H,
  7414. numRounds,
  7415. lengthPosition,
  7416. i,
  7417. t,
  7418. binaryStringInc,
  7419. binaryStringMult,
  7420. safeAdd_2,
  7421. safeAdd_4,
  7422. safeAdd_5,
  7423. gamma0,
  7424. gamma1,
  7425. sigma0,
  7426. sigma1,
  7427. ch,
  7428. maj,
  7429. Int,
  7430. W = [],
  7431. int1,
  7432. int2,
  7433. offset,
  7434. appendedMessageLength,
  7435. retVal,
  7436. K = [0x428A2F98, 0x71374491, 0xB5C0FBCF, 0xE9B5DBA5, 0x3956C25B, 0x59F111F1, 0x923F82A4, 0xAB1C5ED5, 0xD807AA98, 0x12835B01, 0x243185BE, 0x550C7DC3, 0x72BE5D74, 0x80DEB1FE, 0x9BDC06A7, 0xC19BF174, 0xE49B69C1, 0xEFBE4786, 0x0FC19DC6, 0x240CA1CC, 0x2DE92C6F, 0x4A7484AA, 0x5CB0A9DC, 0x76F988DA, 0x983E5152, 0xA831C66D, 0xB00327C8, 0xBF597FC7, 0xC6E00BF3, 0xD5A79147, 0x06CA6351, 0x14292967, 0x27B70A85, 0x2E1B2138, 0x4D2C6DFC, 0x53380D13, 0x650A7354, 0x766A0ABB, 0x81C2C92E, 0x92722C85, 0xA2BFE8A1, 0xA81A664B, 0xC24B8B70, 0xC76C51A3, 0xD192E819, 0xD6990624, 0xF40E3585, 0x106AA070, 0x19A4C116, 0x1E376C08, 0x2748774C, 0x34B0BCB5, 0x391C0CB3, 0x4ED8AA4A, 0x5B9CCA4F, 0x682E6FF3, 0x748F82EE, 0x78A5636F, 0x84C87814, 0x8CC70208, 0x90BEFFFA, 0xA4506CEB, 0xBEF9A3F7, 0xC67178F2],
  7437. H_trunc = [0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939, 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4],
  7438. H_full = [0x6A09E667, 0xBB67AE85, 0x3C6EF372, 0xA54FF53A, 0x510E527F, 0x9B05688C, 0x1F83D9AB, 0x5BE0CD19];
  7439. /* Set up the various function handles and variable for the specific
  7440. * variant */
  7441. if ((variant === "SHA-224" || variant === "SHA-256") && 2 & SUPPORTED_ALGS) {
  7442. /* 32-bit variant */
  7443. numRounds = 64;
  7444. lengthPosition = (messageLen + 65 >>> 9 << 4) + 15;
  7445. binaryStringInc = 16;
  7446. binaryStringMult = 1;
  7447. Int = Number;
  7448. safeAdd_2 = safeAdd_32_2;
  7449. safeAdd_4 = safeAdd_32_4;
  7450. safeAdd_5 = safeAdd_32_5;
  7451. gamma0 = gamma0_32;
  7452. gamma1 = gamma1_32;
  7453. sigma0 = sigma0_32;
  7454. sigma1 = sigma1_32;
  7455. maj = maj_32;
  7456. ch = ch_32;
  7457. if ("SHA-224" === variant) {
  7458. H = H_trunc;
  7459. } else /* "SHA-256" === variant */
  7460. {
  7461. H = H_full;
  7462. }
  7463. } else if ((variant === "SHA-384" || variant === "SHA-512") && 4 & SUPPORTED_ALGS) {
  7464. /* 64-bit variant */
  7465. numRounds = 80;
  7466. lengthPosition = (messageLen + 128 >>> 10 << 5) + 31;
  7467. binaryStringInc = 32;
  7468. binaryStringMult = 2;
  7469. Int = Int_64;
  7470. safeAdd_2 = safeAdd_64_2;
  7471. safeAdd_4 = safeAdd_64_4;
  7472. safeAdd_5 = safeAdd_64_5;
  7473. gamma0 = gamma0_64;
  7474. gamma1 = gamma1_64;
  7475. sigma0 = sigma0_64;
  7476. sigma1 = sigma1_64;
  7477. maj = maj_64;
  7478. ch = ch_64;
  7479. K = [new Int(K[0], 0xd728ae22), new Int(K[1], 0x23ef65cd), new Int(K[2], 0xec4d3b2f), new Int(K[3], 0x8189dbbc), new Int(K[4], 0xf348b538), new Int(K[5], 0xb605d019), new Int(K[6], 0xaf194f9b), new Int(K[7], 0xda6d8118), new Int(K[8], 0xa3030242), new Int(K[9], 0x45706fbe), new Int(K[10], 0x4ee4b28c), new Int(K[11], 0xd5ffb4e2), new Int(K[12], 0xf27b896f), new Int(K[13], 0x3b1696b1), new Int(K[14], 0x25c71235), new Int(K[15], 0xcf692694), new Int(K[16], 0x9ef14ad2), new Int(K[17], 0x384f25e3), new Int(K[18], 0x8b8cd5b5), new Int(K[19], 0x77ac9c65), new Int(K[20], 0x592b0275), new Int(K[21], 0x6ea6e483), new Int(K[22], 0xbd41fbd4), new Int(K[23], 0x831153b5), new Int(K[24], 0xee66dfab), new Int(K[25], 0x2db43210), new Int(K[26], 0x98fb213f), new Int(K[27], 0xbeef0ee4), new Int(K[28], 0x3da88fc2), new Int(K[29], 0x930aa725), new Int(K[30], 0xe003826f), new Int(K[31], 0x0a0e6e70), new Int(K[32], 0x46d22ffc), new Int(K[33], 0x5c26c926), new Int(K[34], 0x5ac42aed), new Int(K[35], 0x9d95b3df), new Int(K[36], 0x8baf63de), new Int(K[37], 0x3c77b2a8), new Int(K[38], 0x47edaee6), new Int(K[39], 0x1482353b), new Int(K[40], 0x4cf10364), new Int(K[41], 0xbc423001), new Int(K[42], 0xd0f89791), new Int(K[43], 0x0654be30), new Int(K[44], 0xd6ef5218), new Int(K[45], 0x5565a910), new Int(K[46], 0x5771202a), new Int(K[47], 0x32bbd1b8), new Int(K[48], 0xb8d2d0c8), new Int(K[49], 0x5141ab53), new Int(K[50], 0xdf8eeb99), new Int(K[51], 0xe19b48a8), new Int(K[52], 0xc5c95a63), new Int(K[53], 0xe3418acb), new Int(K[54], 0x7763e373), new Int(K[55], 0xd6b2b8a3), new Int(K[56], 0x5defb2fc), new Int(K[57], 0x43172f60), new Int(K[58], 0xa1f0ab72), new Int(K[59], 0x1a6439ec), new Int(K[60], 0x23631e28), new Int(K[61], 0xde82bde9), new Int(K[62], 0xb2c67915), new Int(K[63], 0xe372532b), new Int(0xca273ece, 0xea26619c), new Int(0xd186b8c7, 0x21c0c207), new Int(0xeada7dd6, 0xcde0eb1e), new Int(0xf57d4f7f, 0xee6ed178), new Int(0x06f067aa, 0x72176fba), new Int(0x0a637dc5, 0xa2c898a6), new Int(0x113f9804, 0xbef90dae), new Int(0x1b710b35, 0x131c471b), new Int(0x28db77f5, 0x23047d84), new Int(0x32caab7b, 0x40c72493), new Int(0x3c9ebe0a, 0x15c9bebc), new Int(0x431d67c4, 0x9c100d4c), new Int(0x4cc5d4be, 0xcb3e42b6), new Int(0x597f299c, 0xfc657e2a), new Int(0x5fcb6fab, 0x3ad6faec), new Int(0x6c44198c, 0x4a475817)];
  7480. if ("SHA-384" === variant) {
  7481. H = [new Int(0xcbbb9d5d, H_trunc[0]), new Int(0x0629a292a, H_trunc[1]), new Int(0x9159015a, H_trunc[2]), new Int(0x0152fecd8, H_trunc[3]), new Int(0x67332667, H_trunc[4]), new Int(0x98eb44a87, H_trunc[5]), new Int(0xdb0c2e0d, H_trunc[6]), new Int(0x047b5481d, H_trunc[7])];
  7482. } else /* "SHA-512" === variant */
  7483. {
  7484. H = [new Int(H_full[0], 0xf3bcc908), new Int(H_full[1], 0x84caa73b), new Int(H_full[2], 0xfe94f82b), new Int(H_full[3], 0x5f1d36f1), new Int(H_full[4], 0xade682d1), new Int(H_full[5], 0x2b3e6c1f), new Int(H_full[6], 0xfb41bd6b), new Int(H_full[7], 0x137e2179)];
  7485. }
  7486. } else {
  7487. throw "Unexpected error in SHA-2 implementation";
  7488. }
  7489. while (message.length <= lengthPosition) {
  7490. message.push(0);
  7491. }
  7492. /* Append '1' at the end of the binary string */
  7493. message[messageLen >>> 5] |= 0x80 << 24 - messageLen % 32;
  7494. /* Append length of binary string in the position such that the new
  7495. * length is correct */
  7496. message[lengthPosition] = messageLen;
  7497. appendedMessageLength = message.length;
  7498. for (i = 0; i < appendedMessageLength; i += binaryStringInc) {
  7499. a = H[0];
  7500. b = H[1];
  7501. c = H[2];
  7502. d = H[3];
  7503. e = H[4];
  7504. f = H[5];
  7505. g = H[6];
  7506. h = H[7];
  7507. for (t = 0; t < numRounds; t += 1) {
  7508. if (t < 16) {
  7509. offset = t * binaryStringMult + i;
  7510. int1 = message.length <= offset ? 0 : message[offset];
  7511. int2 = message.length <= offset + 1 ? 0 : message[offset + 1];
  7512. /* Bit of a hack - for 32-bit, the second term is ignored */
  7513. W[t] = new Int(int1, int2);
  7514. } else {
  7515. W[t] = safeAdd_4(gamma1(W[t - 2]), W[t - 7], gamma0(W[t - 15]), W[t - 16]);
  7516. }
  7517. T1 = safeAdd_5(h, sigma1(e), ch(e, f, g), K[t], W[t]);
  7518. T2 = safeAdd_2(sigma0(a), maj(a, b, c));
  7519. h = g;
  7520. g = f;
  7521. f = e;
  7522. e = safeAdd_2(d, T1);
  7523. d = c;
  7524. c = b;
  7525. b = a;
  7526. a = safeAdd_2(T1, T2);
  7527. }
  7528. H[0] = safeAdd_2(a, H[0]);
  7529. H[1] = safeAdd_2(b, H[1]);
  7530. H[2] = safeAdd_2(c, H[2]);
  7531. H[3] = safeAdd_2(d, H[3]);
  7532. H[4] = safeAdd_2(e, H[4]);
  7533. H[5] = safeAdd_2(f, H[5]);
  7534. H[6] = safeAdd_2(g, H[6]);
  7535. H[7] = safeAdd_2(h, H[7]);
  7536. }
  7537. if ("SHA-224" === variant && 2 & SUPPORTED_ALGS) {
  7538. retVal = [H[0], H[1], H[2], H[3], H[4], H[5], H[6]];
  7539. } else if ("SHA-256" === variant && 2 & SUPPORTED_ALGS) {
  7540. retVal = H;
  7541. } else if ("SHA-384" === variant && 4 & SUPPORTED_ALGS) {
  7542. retVal = [H[0].highOrder, H[0].lowOrder, H[1].highOrder, H[1].lowOrder, H[2].highOrder, H[2].lowOrder, H[3].highOrder, H[3].lowOrder, H[4].highOrder, H[4].lowOrder, H[5].highOrder, H[5].lowOrder];
  7543. } else if ("SHA-512" === variant && 4 & SUPPORTED_ALGS) {
  7544. retVal = [H[0].highOrder, H[0].lowOrder, H[1].highOrder, H[1].lowOrder, H[2].highOrder, H[2].lowOrder, H[3].highOrder, H[3].lowOrder, H[4].highOrder, H[4].lowOrder, H[5].highOrder, H[5].lowOrder, H[6].highOrder, H[6].lowOrder, H[7].highOrder, H[7].lowOrder];
  7545. } else /* This should never be reached */
  7546. {
  7547. throw "Unexpected error in SHA-2 implementation";
  7548. }
  7549. return retVal;
  7550. }
  7551. /**
  7552. * jsSHA is the workhorse of the library. Instantiate it with the string to
  7553. * be hashed as the parameter
  7554. *
  7555. * @constructor
  7556. * @this {jsSHA}
  7557. * @param {string} srcString The string to be hashed
  7558. * @param {string} inputFormat The format of srcString, HEX, ASCII, TEXT,
  7559. * B64, or BYTES
  7560. * @param {string=} encoding The text encoding to use to encode the source
  7561. * string
  7562. */
  7563. var jsSHA = function jsSHA(srcString, inputFormat, encoding) {
  7564. var strBinLen = 0,
  7565. strToHash = [0],
  7566. utfType = '',
  7567. srcConvertRet = null;
  7568. utfType = encoding || "UTF8";
  7569. if (!("UTF8" === utfType || "UTF16BE" === utfType || "UTF16LE" === utfType)) {
  7570. throw "encoding must be UTF8, UTF16BE, or UTF16LE";
  7571. }
  7572. /* Convert the input string into the correct type */
  7573. if ("HEX" === inputFormat) {
  7574. if (0 !== srcString.length % 2) {
  7575. throw "srcString of HEX type must be in byte increments";
  7576. }
  7577. srcConvertRet = hex2binb(srcString);
  7578. strBinLen = srcConvertRet["binLen"];
  7579. strToHash = srcConvertRet["value"];
  7580. } else if ("TEXT" === inputFormat || "ASCII" === inputFormat) {
  7581. srcConvertRet = str2binb(srcString, utfType);
  7582. strBinLen = srcConvertRet["binLen"];
  7583. strToHash = srcConvertRet["value"];
  7584. } else if ("B64" === inputFormat) {
  7585. srcConvertRet = b642binb(srcString);
  7586. strBinLen = srcConvertRet["binLen"];
  7587. strToHash = srcConvertRet["value"];
  7588. } else if ("BYTES" === inputFormat) {
  7589. srcConvertRet = bytes2binb(srcString);
  7590. strBinLen = srcConvertRet["binLen"];
  7591. strToHash = srcConvertRet["value"];
  7592. } else if ("TYPED" === inputFormat) {
  7593. srcConvertRet = typed2binb(srcString);
  7594. strBinLen = srcConvertRet["binLen"];
  7595. strToHash = srcConvertRet["value"];
  7596. } else {
  7597. throw "inputFormat must be HEX, TEXT, ASCII, B64, BYTES, or TYPED";
  7598. }
  7599. /**
  7600. * Returns the desired SHA hash of the string specified at instantiation
  7601. * using the specified parameters
  7602. *
  7603. * @expose
  7604. * @param {string} variant The desired SHA variant (SHA-1, SHA-224,
  7605. * SHA-256, SHA-384, or SHA-512)
  7606. * @param {string} format The desired output formatting (B64, HEX, or BYTES)
  7607. * @param {number=} numRounds The number of rounds of hashing to be
  7608. * executed
  7609. * @param {{outputUpper : boolean, b64Pad : string}=} outputFormatOpts
  7610. * Hash list of output formatting options
  7611. * @return {string} The string representation of the hash in the format
  7612. * specified
  7613. */
  7614. this.getHash = function (variant, format, numRounds, outputFormatOpts) {
  7615. var formatFunc = null,
  7616. message = strToHash.slice(),
  7617. messageBinLen = strBinLen,
  7618. i;
  7619. /* Need to do argument patching since both numRounds and
  7620. outputFormatOpts are optional */
  7621. if (3 === arguments.length) {
  7622. if ("number" !== typeof numRounds) {
  7623. outputFormatOpts = numRounds;
  7624. numRounds = 1;
  7625. }
  7626. } else if (2 === arguments.length) {
  7627. numRounds = 1;
  7628. }
  7629. /* Validate the numRounds argument */
  7630. if (numRounds !== parseInt(numRounds, 10) || 1 > numRounds) {
  7631. throw "numRounds must a integer >= 1";
  7632. }
  7633. /* Validate the output format selection */
  7634. switch (format) {
  7635. case "HEX":
  7636. formatFunc = binb2hex;
  7637. break;
  7638. case "B64":
  7639. formatFunc = binb2b64;
  7640. break;
  7641. case "BYTES":
  7642. formatFunc = binb2bytes;
  7643. break;
  7644. case "TYPED":
  7645. formatFunc = binb2typed;
  7646. break;
  7647. default:
  7648. throw "format must be HEX, B64, or BYTES";
  7649. }
  7650. if ("SHA-1" === variant && 1 & SUPPORTED_ALGS) {
  7651. for (i = 0; i < numRounds; i += 1) {
  7652. message = coreSHA1(message, messageBinLen);
  7653. messageBinLen = 160;
  7654. }
  7655. } else if ("SHA-224" === variant && 2 & SUPPORTED_ALGS) {
  7656. for (i = 0; i < numRounds; i += 1) {
  7657. message = coreSHA2(message, messageBinLen, variant);
  7658. messageBinLen = 224;
  7659. }
  7660. } else if ("SHA-256" === variant && 2 & SUPPORTED_ALGS) {
  7661. for (i = 0; i < numRounds; i += 1) {
  7662. message = coreSHA2(message, messageBinLen, variant);
  7663. messageBinLen = 256;
  7664. }
  7665. } else if ("SHA-384" === variant && 4 & SUPPORTED_ALGS) {
  7666. for (i = 0; i < numRounds; i += 1) {
  7667. message = coreSHA2(message, messageBinLen, variant);
  7668. messageBinLen = 384;
  7669. }
  7670. } else if ("SHA-512" === variant && 4 & SUPPORTED_ALGS) {
  7671. for (i = 0; i < numRounds; i += 1) {
  7672. message = coreSHA2(message, messageBinLen, variant);
  7673. messageBinLen = 512;
  7674. }
  7675. } else {
  7676. throw "Chosen SHA variant is not supported";
  7677. }
  7678. return formatFunc(message, getOutputOpts(outputFormatOpts));
  7679. };
  7680. /**
  7681. * Returns the desired HMAC of the string specified at instantiation
  7682. * using the key and variant parameter
  7683. *
  7684. * @expose
  7685. * @param {string} key The key used to calculate the HMAC
  7686. * @param {string} inputFormat The format of key, HEX, TEXT, ASCII,
  7687. * B64, or BYTES
  7688. * @param {string} variant The desired SHA variant (SHA-1, SHA-224,
  7689. * SHA-256, SHA-384, or SHA-512)
  7690. * @param {string} outputFormat The desired output formatting
  7691. * (B64, HEX, or BYTES)
  7692. * @param {{outputUpper : boolean, b64Pad : string}=} outputFormatOpts
  7693. * associative array of output formatting options
  7694. * @return {string} The string representation of the hash in the format
  7695. * specified
  7696. */
  7697. this.getHMAC = function (key, inputFormat, variant, outputFormat, outputFormatOpts) {
  7698. var formatFunc,
  7699. keyToUse,
  7700. blockByteSize,
  7701. blockBitSize,
  7702. i,
  7703. retVal,
  7704. lastArrayIndex,
  7705. keyBinLen,
  7706. hashBitSize,
  7707. keyWithIPad = [],
  7708. keyWithOPad = [],
  7709. keyConvertRet = null;
  7710. /* Validate the output format selection */
  7711. switch (outputFormat) {
  7712. case "HEX":
  7713. formatFunc = binb2hex;
  7714. break;
  7715. case "B64":
  7716. formatFunc = binb2b64;
  7717. break;
  7718. case "BYTES":
  7719. formatFunc = binb2bytes;
  7720. break;
  7721. default:
  7722. throw "outputFormat must be HEX, B64, or BYTES";
  7723. }
  7724. /* Validate the hash variant selection and set needed variables */
  7725. if ("SHA-1" === variant && 1 & SUPPORTED_ALGS) {
  7726. blockByteSize = 64;
  7727. hashBitSize = 160;
  7728. } else if ("SHA-224" === variant && 2 & SUPPORTED_ALGS) {
  7729. blockByteSize = 64;
  7730. hashBitSize = 224;
  7731. } else if ("SHA-256" === variant && 2 & SUPPORTED_ALGS) {
  7732. blockByteSize = 64;
  7733. hashBitSize = 256;
  7734. } else if ("SHA-384" === variant && 4 & SUPPORTED_ALGS) {
  7735. blockByteSize = 128;
  7736. hashBitSize = 384;
  7737. } else if ("SHA-512" === variant && 4 & SUPPORTED_ALGS) {
  7738. blockByteSize = 128;
  7739. hashBitSize = 512;
  7740. } else {
  7741. throw "Chosen SHA variant is not supported";
  7742. }
  7743. /* Validate input format selection */
  7744. if ("HEX" === inputFormat) {
  7745. keyConvertRet = hex2binb(key);
  7746. keyBinLen = keyConvertRet["binLen"];
  7747. keyToUse = keyConvertRet["value"];
  7748. } else if ("TEXT" === inputFormat || "ASCII" === inputFormat) {
  7749. keyConvertRet = str2binb(key, utfType);
  7750. keyBinLen = keyConvertRet["binLen"];
  7751. keyToUse = keyConvertRet["value"];
  7752. } else if ("B64" === inputFormat) {
  7753. keyConvertRet = b642binb(key);
  7754. keyBinLen = keyConvertRet["binLen"];
  7755. keyToUse = keyConvertRet["value"];
  7756. } else if ("BYTES" === inputFormat) {
  7757. keyConvertRet = bytes2binb(key);
  7758. keyBinLen = keyConvertRet["binLen"];
  7759. keyToUse = keyConvertRet["value"];
  7760. } else {
  7761. throw "inputFormat must be HEX, TEXT, ASCII, B64, or BYTES";
  7762. }
  7763. /* These are used multiple times, calculate and store them */
  7764. blockBitSize = blockByteSize * 8;
  7765. lastArrayIndex = blockByteSize / 4 - 1;
  7766. /* Figure out what to do with the key based on its size relative to
  7767. * the hash's block size */
  7768. if (blockByteSize < keyBinLen / 8) {
  7769. if ("SHA-1" === variant && 1 & SUPPORTED_ALGS) {
  7770. keyToUse = coreSHA1(keyToUse, keyBinLen);
  7771. } else if (6 & SUPPORTED_ALGS) {
  7772. keyToUse = coreSHA2(keyToUse, keyBinLen, variant);
  7773. } else {
  7774. throw "Unexpected error in HMAC implementation";
  7775. }
  7776. /* For all variants, the block size is bigger than the output
  7777. * size so there will never be a useful byte at the end of the
  7778. * string */
  7779. while (keyToUse.length <= lastArrayIndex) {
  7780. keyToUse.push(0);
  7781. }
  7782. keyToUse[lastArrayIndex] &= 0xFFFFFF00;
  7783. } else if (blockByteSize > keyBinLen / 8) {
  7784. /* If the blockByteSize is greater than the key length, there
  7785. * will always be at LEAST one "useless" byte at the end of the
  7786. * string */
  7787. while (keyToUse.length <= lastArrayIndex) {
  7788. keyToUse.push(0);
  7789. }
  7790. keyToUse[lastArrayIndex] &= 0xFFFFFF00;
  7791. }
  7792. /* Create ipad and opad */
  7793. for (i = 0; i <= lastArrayIndex; i += 1) {
  7794. keyWithIPad[i] = keyToUse[i] ^ 0x36363636;
  7795. keyWithOPad[i] = keyToUse[i] ^ 0x5C5C5C5C;
  7796. }
  7797. /* Calculate the HMAC */
  7798. if ("SHA-1" === variant && 1 & SUPPORTED_ALGS) {
  7799. retVal = coreSHA1(keyWithOPad.concat(coreSHA1(keyWithIPad.concat(strToHash), blockBitSize + strBinLen)), blockBitSize + hashBitSize);
  7800. } else if (6 & SUPPORTED_ALGS) {
  7801. retVal = coreSHA2(keyWithOPad.concat(coreSHA2(keyWithIPad.concat(strToHash), blockBitSize + strBinLen, variant)), blockBitSize + hashBitSize, variant);
  7802. } else {
  7803. throw "Unexpected error in HMAC implementation";
  7804. }
  7805. return formatFunc(retVal, getOutputOpts(outputFormatOpts));
  7806. };
  7807. };
  7808. exports.default = {
  7809. /** SHA1 hash */
  7810. sha1: function sha1(str) {
  7811. var shaObj = new jsSHA(str, "TYPED", "UTF8");
  7812. return shaObj.getHash("SHA-1", "TYPED");
  7813. },
  7814. /** SHA224 hash */
  7815. sha224: function sha224(str) {
  7816. var shaObj = new jsSHA(str, "TYPED", "UTF8");
  7817. return shaObj.getHash("SHA-224", "TYPED");
  7818. },
  7819. /** SHA256 hash */
  7820. sha256: function sha256(str) {
  7821. var shaObj = new jsSHA(str, "TYPED", "UTF8");
  7822. return shaObj.getHash("SHA-256", "TYPED");
  7823. },
  7824. /** SHA384 hash */
  7825. sha384: function sha384(str) {
  7826. var shaObj = new jsSHA(str, "TYPED", "UTF8");
  7827. return shaObj.getHash("SHA-384", "TYPED");
  7828. },
  7829. /** SHA512 hash */
  7830. sha512: function sha512(str) {
  7831. var shaObj = new jsSHA(str, "TYPED", "UTF8");
  7832. return shaObj.getHash("SHA-512", "TYPED");
  7833. }
  7834. };
  7835. },{}],24:[function(_dereq_,module,exports){
  7836. /**
  7837. * @see module:crypto/crypto
  7838. * @module crypto
  7839. */
  7840. 'use strict';
  7841. Object.defineProperty(exports, "__esModule", {
  7842. value: true
  7843. });
  7844. var _cipher = _dereq_('./cipher');
  7845. var _cipher2 = _interopRequireDefault(_cipher);
  7846. var _hash = _dereq_('./hash');
  7847. var _hash2 = _interopRequireDefault(_hash);
  7848. var _cfb = _dereq_('./cfb');
  7849. var _cfb2 = _interopRequireDefault(_cfb);
  7850. var _gcm = _dereq_('./gcm');
  7851. var gcm = _interopRequireWildcard(_gcm);
  7852. var _public_key = _dereq_('./public_key');
  7853. var _public_key2 = _interopRequireDefault(_public_key);
  7854. var _signature = _dereq_('./signature');
  7855. var _signature2 = _interopRequireDefault(_signature);
  7856. var _random = _dereq_('./random');
  7857. var _random2 = _interopRequireDefault(_random);
  7858. var _pkcs = _dereq_('./pkcs1');
  7859. var _pkcs2 = _interopRequireDefault(_pkcs);
  7860. var _crypto = _dereq_('./crypto.js');
  7861. var _crypto2 = _interopRequireDefault(_crypto);
  7862. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  7863. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  7864. var mod = {
  7865. /** @see module:crypto/cipher */
  7866. cipher: _cipher2.default,
  7867. /** @see module:crypto/hash */
  7868. hash: _hash2.default,
  7869. /** @see module:crypto/cfb */
  7870. cfb: _cfb2.default,
  7871. /** @see module:crypto/gcm */
  7872. gcm: gcm,
  7873. /** @see module:crypto/public_key */
  7874. publicKey: _public_key2.default,
  7875. /** @see module:crypto/signature */
  7876. signature: _signature2.default,
  7877. /** @see module:crypto/random */
  7878. random: _random2.default,
  7879. /** @see module:crypto/pkcs1 */
  7880. pkcs1: _pkcs2.default
  7881. };
  7882. for (var i in _crypto2.default) {
  7883. mod[i] = _crypto2.default[i];
  7884. }
  7885. exports.default = mod;
  7886. },{"./cfb":11,"./cipher":16,"./crypto.js":18,"./gcm":19,"./hash":20,"./pkcs1":25,"./public_key":28,"./random":31,"./signature":32}],25:[function(_dereq_,module,exports){
  7887. // GPG4Browsers - An OpenPGP implementation in javascript
  7888. // Copyright (C) 2011 Recurity Labs GmbH
  7889. //
  7890. // This library is free software; you can redistribute it and/or
  7891. // modify it under the terms of the GNU Lesser General Public
  7892. // License as published by the Free Software Foundation; either
  7893. // version 3.0 of the License, or (at your option) any later version.
  7894. //
  7895. // This library is distributed in the hope that it will be useful,
  7896. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  7897. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  7898. // Lesser General Public License for more details.
  7899. //
  7900. // You should have received a copy of the GNU Lesser General Public
  7901. // License along with this library; if not, write to the Free Software
  7902. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  7903. /**
  7904. * PKCS1 encoding
  7905. * @requires crypto/crypto
  7906. * @requires crypto/hash
  7907. * @requires crypto/public_key/jsbn
  7908. * @requires crypto/random
  7909. * @requires util
  7910. * @module crypto/pkcs1
  7911. */
  7912. 'use strict';
  7913. Object.defineProperty(exports, "__esModule", {
  7914. value: true
  7915. });
  7916. var _random = _dereq_('./random.js');
  7917. var _random2 = _interopRequireDefault(_random);
  7918. var _util = _dereq_('../util.js');
  7919. var _util2 = _interopRequireDefault(_util);
  7920. var _jsbn = _dereq_('./public_key/jsbn.js');
  7921. var _jsbn2 = _interopRequireDefault(_jsbn);
  7922. var _hash = _dereq_('./hash');
  7923. var _hash2 = _interopRequireDefault(_hash);
  7924. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  7925. /**
  7926. * ASN1 object identifiers for hashes (See {@link http://tools.ietf.org/html/rfc4880#section-5.2.2})
  7927. */
  7928. var hash_headers = [];
  7929. hash_headers[1] = [0x30, 0x20, 0x30, 0x0c, 0x06, 0x08, 0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05, 0x05, 0x00, 0x04, 0x10];
  7930. hash_headers[2] = [0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2b, 0x0e, 0x03, 0x02, 0x1a, 0x05, 0x00, 0x04, 0x14];
  7931. hash_headers[3] = [0x30, 0x21, 0x30, 0x09, 0x06, 0x05, 0x2B, 0x24, 0x03, 0x02, 0x01, 0x05, 0x00, 0x04, 0x14];
  7932. hash_headers[8] = [0x30, 0x31, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x01, 0x05, 0x00, 0x04, 0x20];
  7933. hash_headers[9] = [0x30, 0x41, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x02, 0x05, 0x00, 0x04, 0x30];
  7934. hash_headers[10] = [0x30, 0x51, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x03, 0x05, 0x00, 0x04, 0x40];
  7935. hash_headers[11] = [0x30, 0x2d, 0x30, 0x0d, 0x06, 0x09, 0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x02, 0x04, 0x05, 0x00, 0x04, 0x1C];
  7936. /**
  7937. * Create padding with secure random data
  7938. * @private
  7939. * @param {Integer} length Length of the padding in bytes
  7940. * @return {String} Padding as string
  7941. */
  7942. function getPkcs1Padding(length) {
  7943. var result = '';
  7944. var randomByte;
  7945. while (result.length < length) {
  7946. randomByte = _random2.default.getSecureRandomOctet();
  7947. if (randomByte !== 0) {
  7948. result += String.fromCharCode(randomByte);
  7949. }
  7950. }
  7951. return result;
  7952. }
  7953. exports.default = {
  7954. eme: {
  7955. /**
  7956. * create a EME-PKCS1-v1_5 padding (See {@link http://tools.ietf.org/html/rfc4880#section-13.1.1|RFC 4880 13.1.1})
  7957. * @param {String} M message to be encoded
  7958. * @param {Integer} k the length in octets of the key modulus
  7959. * @return {String} EME-PKCS1 padded message
  7960. */
  7961. encode: function encode(M, k) {
  7962. var mLen = M.length;
  7963. // length checking
  7964. if (mLen > k - 11) {
  7965. throw new Error('Message too long');
  7966. }
  7967. // Generate an octet string PS of length k - mLen - 3 consisting of
  7968. // pseudo-randomly generated nonzero octets
  7969. var PS = getPkcs1Padding(k - mLen - 3);
  7970. // Concatenate PS, the message M, and other padding to form an
  7971. // encoded message EM of length k octets as EM = 0x00 || 0x02 || PS || 0x00 || M.
  7972. var EM = String.fromCharCode(0) + String.fromCharCode(2) + PS + String.fromCharCode(0) + M;
  7973. return EM;
  7974. },
  7975. /**
  7976. * decodes a EME-PKCS1-v1_5 padding (See {@link http://tools.ietf.org/html/rfc4880#section-13.1.2|RFC 4880 13.1.2})
  7977. * @param {String} EM encoded message, an octet string
  7978. * @return {String} message, an octet string
  7979. */
  7980. decode: function decode(EM) {
  7981. // leading zeros truncated by jsbn
  7982. if (EM.charCodeAt(0) !== 0) {
  7983. EM = String.fromCharCode(0) + EM;
  7984. }
  7985. var firstOct = EM.charCodeAt(0);
  7986. var secondOct = EM.charCodeAt(1);
  7987. var i = 2;
  7988. while (EM.charCodeAt(i) !== 0 && i < EM.length) {
  7989. i++;
  7990. }
  7991. var psLen = i - 2;
  7992. var separator = EM.charCodeAt(i++);
  7993. if (firstOct === 0 && secondOct === 2 && psLen >= 8 && separator === 0) {
  7994. return EM.substr(i);
  7995. } else {
  7996. throw new Error('Decryption error');
  7997. }
  7998. }
  7999. },
  8000. emsa: {
  8001. /**
  8002. * create a EMSA-PKCS1-v1_5 padding (See {@link http://tools.ietf.org/html/rfc4880#section-13.1.3|RFC 4880 13.1.3})
  8003. * @param {Integer} algo Hash algorithm type used
  8004. * @param {String} M message to be encoded
  8005. * @param {Integer} emLen intended length in octets of the encoded message
  8006. * @returns {String} encoded message
  8007. */
  8008. encode: function encode(algo, M, emLen) {
  8009. var i;
  8010. // Apply the hash function to the message M to produce a hash value H
  8011. var H = _util2.default.Uint8Array2str(_hash2.default.digest(algo, _util2.default.str2Uint8Array(M)));
  8012. if (H.length !== _hash2.default.getHashByteLength(algo)) {
  8013. throw new Error('Invalid hash length');
  8014. }
  8015. // produce an ASN.1 DER value for the hash function used.
  8016. // Let T be the full hash prefix
  8017. var T = '';
  8018. for (i = 0; i < hash_headers[algo].length; i++) {
  8019. T += String.fromCharCode(hash_headers[algo][i]);
  8020. }
  8021. // add hash value to prefix
  8022. T += H;
  8023. // and let tLen be the length in octets of T
  8024. var tLen = T.length;
  8025. if (emLen < tLen + 11) {
  8026. throw new Error('Intended encoded message length too short');
  8027. }
  8028. // an octet string PS consisting of emLen - tLen - 3 octets with hexadecimal value 0xFF
  8029. // The length of PS will be at least 8 octets
  8030. var PS = '';
  8031. for (i = 0; i < emLen - tLen - 3; i++) {
  8032. PS += String.fromCharCode(0xff);
  8033. }
  8034. // Concatenate PS, the hash prefix T, and other padding to form the
  8035. // encoded message EM as EM = 0x00 || 0x01 || PS || 0x00 || T.
  8036. var EM = String.fromCharCode(0x00) + String.fromCharCode(0x01) + PS + String.fromCharCode(0x00) + T;
  8037. return new _jsbn2.default(_util2.default.hexstrdump(EM), 16);
  8038. }
  8039. }
  8040. };
  8041. },{"../util.js":70,"./hash":20,"./public_key/jsbn.js":29,"./random.js":31}],26:[function(_dereq_,module,exports){
  8042. // GPG4Browsers - An OpenPGP implementation in javascript
  8043. // Copyright (C) 2011 Recurity Labs GmbH
  8044. //
  8045. // This library is free software; you can redistribute it and/or
  8046. // modify it under the terms of the GNU Lesser General Public
  8047. // License as published by the Free Software Foundation; either
  8048. // version 3.0 of the License, or (at your option) any later version.
  8049. //
  8050. // This library is distributed in the hope that it will be useful,
  8051. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  8052. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  8053. // Lesser General Public License for more details.
  8054. //
  8055. // You should have received a copy of the GNU Lesser General Public
  8056. // License along with this library; if not, write to the Free Software
  8057. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  8058. //
  8059. // A Digital signature algorithm implementation
  8060. /**
  8061. * @requires crypto/hash
  8062. * @requires crypto/public_key/jsbn
  8063. * @requires crypto/random
  8064. * @requires util
  8065. * @module crypto/public_key/dsa
  8066. */
  8067. 'use strict';
  8068. Object.defineProperty(exports, "__esModule", {
  8069. value: true
  8070. });
  8071. exports.default = DSA;
  8072. var _jsbn = _dereq_('./jsbn.js');
  8073. var _jsbn2 = _interopRequireDefault(_jsbn);
  8074. var _random = _dereq_('../random.js');
  8075. var _random2 = _interopRequireDefault(_random);
  8076. var _hash = _dereq_('../hash');
  8077. var _hash2 = _interopRequireDefault(_hash);
  8078. var _util = _dereq_('../../util.js');
  8079. var _util2 = _interopRequireDefault(_util);
  8080. var _config = _dereq_('../../config');
  8081. var _config2 = _interopRequireDefault(_config);
  8082. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8083. function DSA() {
  8084. // s1 = ((g**s) mod p) mod q
  8085. // s1 = ((s**-1)*(sha-1(m)+(s1*x) mod q)
  8086. function sign(hashalgo, m, g, p, q, x) {
  8087. // If the output size of the chosen hash is larger than the number of
  8088. // bits of q, the hash result is truncated to fit by taking the number
  8089. // of leftmost bits equal to the number of bits of q. This (possibly
  8090. // truncated) hash function result is treated as a number and used
  8091. // directly in the DSA signature algorithm.
  8092. var hashed_data = _util2.default.getLeftNBits(_util2.default.Uint8Array2str(_hash2.default.digest(hashalgo, _util2.default.str2Uint8Array(m))), q.bitLength());
  8093. var hash = new _jsbn2.default(_util2.default.hexstrdump(hashed_data), 16);
  8094. // FIPS-186-4, section 4.6:
  8095. // The values of r and s shall be checked to determine if r = 0 or s = 0.
  8096. // If either r = 0 or s = 0, a new value of k shall be generated, and the
  8097. // signature shall be recalculated. It is extremely unlikely that r = 0
  8098. // or s = 0 if signatures are generated properly.
  8099. var k, s1, s2;
  8100. while (true) {
  8101. k = _random2.default.getRandomBigIntegerInRange(_jsbn2.default.ONE, q.subtract(_jsbn2.default.ONE));
  8102. s1 = g.modPow(k, p).mod(q);
  8103. s2 = k.modInverse(q).multiply(hash.add(x.multiply(s1))).mod(q);
  8104. if (s1 !== 0 && s2 !== 0) {
  8105. break;
  8106. }
  8107. }
  8108. var result = [];
  8109. result[0] = s1.toMPI();
  8110. result[1] = s2.toMPI();
  8111. return result;
  8112. }
  8113. function select_hash_algorithm(q) {
  8114. var usersetting = _config2.default.prefer_hash_algorithm;
  8115. /*
  8116. * 1024-bit key, 160-bit q, SHA-1, SHA-224, SHA-256, SHA-384, or SHA-512 hash
  8117. * 2048-bit key, 224-bit q, SHA-224, SHA-256, SHA-384, or SHA-512 hash
  8118. * 2048-bit key, 256-bit q, SHA-256, SHA-384, or SHA-512 hash
  8119. * 3072-bit key, 256-bit q, SHA-256, SHA-384, or SHA-512 hash
  8120. */
  8121. switch (Math.round(q.bitLength() / 8)) {
  8122. case 20:
  8123. // 1024 bit
  8124. if (usersetting !== 2 && usersetting > 11 && usersetting !== 10 && usersetting < 8) {
  8125. return 2; // prefer sha1
  8126. }
  8127. return usersetting;
  8128. case 28:
  8129. // 2048 bit
  8130. if (usersetting > 11 && usersetting < 8) {
  8131. return 11;
  8132. }
  8133. return usersetting;
  8134. case 32:
  8135. // 4096 bit // prefer sha224
  8136. if (usersetting > 10 && usersetting < 8) {
  8137. return 8; // prefer sha256
  8138. }
  8139. return usersetting;
  8140. default:
  8141. _util2.default.print_debug("DSA select hash algorithm: returning null for an unknown length of q");
  8142. return null;
  8143. }
  8144. }
  8145. this.select_hash_algorithm = select_hash_algorithm;
  8146. function verify(hashalgo, s1, s2, m, p, q, g, y) {
  8147. var hashed_data = _util2.default.getLeftNBits(_util2.default.Uint8Array2str(_hash2.default.digest(hashalgo, _util2.default.str2Uint8Array(m))), q.bitLength());
  8148. var hash = new _jsbn2.default(_util2.default.hexstrdump(hashed_data), 16);
  8149. if (_jsbn2.default.ZERO.compareTo(s1) >= 0 || s1.compareTo(q) >= 0 || _jsbn2.default.ZERO.compareTo(s2) >= 0 || s2.compareTo(q) >= 0) {
  8150. _util2.default.print_debug("invalid DSA Signature");
  8151. return null;
  8152. }
  8153. var w = s2.modInverse(q);
  8154. if (_jsbn2.default.ZERO.compareTo(w) === 0) {
  8155. _util2.default.print_debug("invalid DSA Signature");
  8156. return null;
  8157. }
  8158. var u1 = hash.multiply(w).mod(q);
  8159. var u2 = s1.multiply(w).mod(q);
  8160. return g.modPow(u1, p).multiply(y.modPow(u2, p)).mod(p).mod(q);
  8161. }
  8162. this.sign = sign;
  8163. this.verify = verify;
  8164. }
  8165. },{"../../config":10,"../../util.js":70,"../hash":20,"../random.js":31,"./jsbn.js":29}],27:[function(_dereq_,module,exports){
  8166. // GPG4Browsers - An OpenPGP implementation in javascript
  8167. // Copyright (C) 2011 Recurity Labs GmbH
  8168. //
  8169. // This library is free software; you can redistribute it and/or
  8170. // modify it under the terms of the GNU Lesser General Public
  8171. // License as published by the Free Software Foundation; either
  8172. // version 3.0 of the License, or (at your option) any later version.
  8173. //
  8174. // This library is distributed in the hope that it will be useful,
  8175. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  8176. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  8177. // Lesser General Public License for more details.
  8178. //
  8179. // You should have received a copy of the GNU Lesser General Public
  8180. // License along with this library; if not, write to the Free Software
  8181. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  8182. //
  8183. // ElGamal implementation
  8184. /**
  8185. * @requires crypto/public_key/jsbn
  8186. * @requires crypto/random
  8187. * @requires util
  8188. * @module crypto/public_key/elgamal
  8189. */
  8190. 'use strict';
  8191. Object.defineProperty(exports, "__esModule", {
  8192. value: true
  8193. });
  8194. exports.default = Elgamal;
  8195. var _jsbn = _dereq_('./jsbn.js');
  8196. var _jsbn2 = _interopRequireDefault(_jsbn);
  8197. var _random = _dereq_('../random.js');
  8198. var _random2 = _interopRequireDefault(_random);
  8199. var _util = _dereq_('../../util.js');
  8200. var _util2 = _interopRequireDefault(_util);
  8201. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8202. function Elgamal() {
  8203. function encrypt(m, g, p, y) {
  8204. // choose k in {2,...,p-2}
  8205. var pMinus2 = p.subtract(_jsbn2.default.TWO);
  8206. var k = _random2.default.getRandomBigIntegerInRange(_jsbn2.default.ONE, pMinus2);
  8207. k = k.mod(pMinus2).add(_jsbn2.default.ONE);
  8208. var c = [];
  8209. c[0] = g.modPow(k, p);
  8210. c[1] = y.modPow(k, p).multiply(m).mod(p);
  8211. return c;
  8212. }
  8213. function decrypt(c1, c2, p, x) {
  8214. _util2.default.print_debug("Elgamal Decrypt:\nc1:" + _util2.default.hexstrdump(c1.toMPI()) + "\n" + "c2:" + _util2.default.hexstrdump(c2.toMPI()) + "\n" + "p:" + _util2.default.hexstrdump(p.toMPI()) + "\n" + "x:" + _util2.default.hexstrdump(x.toMPI()));
  8215. return c1.modPow(x, p).modInverse(p).multiply(c2).mod(p);
  8216. //var c = c1.pow(x).modInverse(p); // c0^-a mod p
  8217. //return c.multiply(c2).mod(p);
  8218. }
  8219. // signing and signature verification using Elgamal is not required by OpenPGP.
  8220. this.encrypt = encrypt;
  8221. this.decrypt = decrypt;
  8222. }
  8223. },{"../../util.js":70,"../random.js":31,"./jsbn.js":29}],28:[function(_dereq_,module,exports){
  8224. /**
  8225. * @requires crypto/public_key/dsa
  8226. * @requires crypto/public_key/elgamal
  8227. * @requires crypto/public_key/rsa
  8228. * @module crypto/public_key
  8229. */
  8230. 'use strict';
  8231. /** @see module:crypto/public_key/rsa */
  8232. Object.defineProperty(exports, "__esModule", {
  8233. value: true
  8234. });
  8235. var _rsa = _dereq_('./rsa.js');
  8236. var _rsa2 = _interopRequireDefault(_rsa);
  8237. var _elgamal = _dereq_('./elgamal.js');
  8238. var _elgamal2 = _interopRequireDefault(_elgamal);
  8239. var _dsa = _dereq_('./dsa.js');
  8240. var _dsa2 = _interopRequireDefault(_dsa);
  8241. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8242. /** @see module:crypto/public_key/elgamal */
  8243. exports.default = {
  8244. rsa: _rsa2.default,
  8245. elgamal: _elgamal2.default,
  8246. dsa: _dsa2.default
  8247. };
  8248. /** @see module:crypto/public_key/dsa */
  8249. },{"./dsa.js":26,"./elgamal.js":27,"./rsa.js":30}],29:[function(_dereq_,module,exports){
  8250. "use strict";
  8251. Object.defineProperty(exports, "__esModule", {
  8252. value: true
  8253. });
  8254. exports.default = BigInteger;
  8255. var _util = _dereq_("../../util.js");
  8256. var _util2 = _interopRequireDefault(_util);
  8257. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  8258. // Basic JavaScript BN library - subset useful for RSA encryption.
  8259. // Bits per digit
  8260. var dbits;
  8261. // JavaScript engine analysis
  8262. /*
  8263. * Copyright (c) 2003-2005 Tom Wu (tjw@cs.Stanford.EDU)
  8264. * All Rights Reserved.
  8265. *
  8266. * Modified by Recurity Labs GmbH
  8267. *
  8268. * Permission is hereby granted, free of charge, to any person obtaining
  8269. * a copy of this software and associated documentation files (the
  8270. * "Software"), to deal in the Software without restriction, including
  8271. * without limitation the rights to use, copy, modify, merge, publish,
  8272. * distribute, sublicense, and/or sell copies of the Software, and to
  8273. * permit persons to whom the Software is furnished to do so, subject to
  8274. * the following conditions:
  8275. *
  8276. * The above copyright notice and this permission notice shall be
  8277. * included in all copies or substantial portions of the Software.
  8278. *
  8279. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  8280. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  8281. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  8282. *
  8283. * IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  8284. * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  8285. * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
  8286. * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
  8287. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  8288. *
  8289. * In addition, the following condition applies:
  8290. *
  8291. * All redistributions must retain an intact copy of this copyright notice
  8292. * and disclaimer.
  8293. */
  8294. /**
  8295. * @requires util
  8296. * @module crypto/public_key/jsbn
  8297. */
  8298. var canary = 0xdeadbeefcafe;
  8299. var j_lm = (canary & 0xffffff) == 0xefcafe;
  8300. // (public) Constructor
  8301. function BigInteger(a, b, c) {
  8302. if (a != null) if ("number" == typeof a) this.fromNumber(a, b, c);else if (b == null && "string" != typeof a) this.fromString(a, 256);else this.fromString(a, b);
  8303. }
  8304. // return new, unset BigInteger
  8305. function nbi() {
  8306. return new BigInteger(null);
  8307. }
  8308. // am: Compute w_j += (x*this_i), propagate carries,
  8309. // c is initial carry, returns final carry.
  8310. // c < 3*dvalue, x < 2*dvalue, this_i < dvalue
  8311. // We need to select the fastest one that works in this environment.
  8312. // am1: use a single mult and divide to get the high bits,
  8313. // max digit bits should be 26 because
  8314. // max internal value = 2*dvalue^2-2*dvalue (< 2^53)
  8315. function am1(i, x, w, j, c, n) {
  8316. while (--n >= 0) {
  8317. var v = x * this[i++] + w[j] + c;
  8318. c = Math.floor(v / 0x4000000);
  8319. w[j++] = v & 0x3ffffff;
  8320. }
  8321. return c;
  8322. }
  8323. // am2 avoids a big mult-and-extract completely.
  8324. // Max digit bits should be <= 30 because we do bitwise ops
  8325. // on values up to 2*hdvalue^2-hdvalue-1 (< 2^31)
  8326. function am2(i, x, w, j, c, n) {
  8327. var xl = x & 0x7fff,
  8328. xh = x >> 15;
  8329. while (--n >= 0) {
  8330. var l = this[i] & 0x7fff;
  8331. var h = this[i++] >> 15;
  8332. var m = xh * l + h * xl;
  8333. l = xl * l + ((m & 0x7fff) << 15) + w[j] + (c & 0x3fffffff);
  8334. c = (l >>> 30) + (m >>> 15) + xh * h + (c >>> 30);
  8335. w[j++] = l & 0x3fffffff;
  8336. }
  8337. return c;
  8338. }
  8339. // Alternately, set max digit bits to 28 since some
  8340. // browsers slow down when dealing with 32-bit numbers.
  8341. function am3(i, x, w, j, c, n) {
  8342. var xl = x & 0x3fff,
  8343. xh = x >> 14;
  8344. while (--n >= 0) {
  8345. var l = this[i] & 0x3fff;
  8346. var h = this[i++] >> 14;
  8347. var m = xh * l + h * xl;
  8348. l = xl * l + ((m & 0x3fff) << 14) + w[j] + c;
  8349. c = (l >> 28) + (m >> 14) + xh * h;
  8350. w[j++] = l & 0xfffffff;
  8351. }
  8352. return c;
  8353. }
  8354. /*if(j_lm && (navigator != undefined &&
  8355. navigator.appName == "Microsoft Internet Explorer")) {
  8356. BigInteger.prototype.am = am2;
  8357. dbits = 30;
  8358. }
  8359. else if(j_lm && (navigator != undefined && navigator.appName != "Netscape")) {*/
  8360. BigInteger.prototype.am = am1;
  8361. dbits = 26;
  8362. /*}
  8363. else { // Mozilla/Netscape seems to prefer am3
  8364. BigInteger.prototype.am = am3;
  8365. dbits = 28;
  8366. }*/
  8367. BigInteger.prototype.DB = dbits;
  8368. BigInteger.prototype.DM = (1 << dbits) - 1;
  8369. BigInteger.prototype.DV = 1 << dbits;
  8370. var BI_FP = 52;
  8371. BigInteger.prototype.FV = Math.pow(2, BI_FP);
  8372. BigInteger.prototype.F1 = BI_FP - dbits;
  8373. BigInteger.prototype.F2 = 2 * dbits - BI_FP;
  8374. // Digit conversions
  8375. var BI_RM = "0123456789abcdefghijklmnopqrstuvwxyz";
  8376. var BI_RC = new Array();
  8377. var rr, vv;
  8378. rr = "0".charCodeAt(0);
  8379. for (vv = 0; vv <= 9; ++vv) {
  8380. BI_RC[rr++] = vv;
  8381. }rr = "a".charCodeAt(0);
  8382. for (vv = 10; vv < 36; ++vv) {
  8383. BI_RC[rr++] = vv;
  8384. }rr = "A".charCodeAt(0);
  8385. for (vv = 10; vv < 36; ++vv) {
  8386. BI_RC[rr++] = vv;
  8387. }function int2char(n) {
  8388. return BI_RM.charAt(n);
  8389. }
  8390. function intAt(s, i) {
  8391. var c = BI_RC[s.charCodeAt(i)];
  8392. return c == null ? -1 : c;
  8393. }
  8394. // (protected) copy this to r
  8395. function bnpCopyTo(r) {
  8396. for (var i = this.t - 1; i >= 0; --i) {
  8397. r[i] = this[i];
  8398. }r.t = this.t;
  8399. r.s = this.s;
  8400. }
  8401. // (protected) set from integer value x, -DV <= x < DV
  8402. function bnpFromInt(x) {
  8403. this.t = 1;
  8404. this.s = x < 0 ? -1 : 0;
  8405. if (x > 0) this[0] = x;else if (x < -1) this[0] = x + this.DV;else this.t = 0;
  8406. }
  8407. // return bigint initialized to value
  8408. function nbv(i) {
  8409. var r = nbi();
  8410. r.fromInt(i);
  8411. return r;
  8412. }
  8413. // (protected) set from string and radix
  8414. function bnpFromString(s, b) {
  8415. var k;
  8416. if (b == 16) k = 4;else if (b == 8) k = 3;else if (b == 256) k = 8; // byte array
  8417. else if (b == 2) k = 1;else if (b == 32) k = 5;else if (b == 4) k = 2;else {
  8418. this.fromRadix(s, b);
  8419. return;
  8420. }
  8421. this.t = 0;
  8422. this.s = 0;
  8423. var i = s.length,
  8424. mi = false,
  8425. sh = 0;
  8426. while (--i >= 0) {
  8427. var x = k == 8 ? s[i] & 0xff : intAt(s, i);
  8428. if (x < 0) {
  8429. if (s.charAt(i) == "-") mi = true;
  8430. continue;
  8431. }
  8432. mi = false;
  8433. if (sh == 0) this[this.t++] = x;else if (sh + k > this.DB) {
  8434. this[this.t - 1] |= (x & (1 << this.DB - sh) - 1) << sh;
  8435. this[this.t++] = x >> this.DB - sh;
  8436. } else this[this.t - 1] |= x << sh;
  8437. sh += k;
  8438. if (sh >= this.DB) sh -= this.DB;
  8439. }
  8440. if (k == 8 && (s[0] & 0x80) != 0) {
  8441. this.s = -1;
  8442. if (sh > 0) this[this.t - 1] |= (1 << this.DB - sh) - 1 << sh;
  8443. }
  8444. this.clamp();
  8445. if (mi) BigInteger.ZERO.subTo(this, this);
  8446. }
  8447. // (protected) clamp off excess high words
  8448. function bnpClamp() {
  8449. var c = this.s & this.DM;
  8450. while (this.t > 0 && this[this.t - 1] == c) {
  8451. --this.t;
  8452. }
  8453. }
  8454. // (public) return string representation in given radix
  8455. function bnToString(b) {
  8456. if (this.s < 0) return "-" + this.negate().toString(b);
  8457. var k;
  8458. if (b == 16) k = 4;else if (b == 8) k = 3;else if (b == 2) k = 1;else if (b == 32) k = 5;else if (b == 4) k = 2;else return this.toRadix(b);
  8459. var km = (1 << k) - 1,
  8460. d,
  8461. m = false,
  8462. r = "",
  8463. i = this.t;
  8464. var p = this.DB - i * this.DB % k;
  8465. if (i-- > 0) {
  8466. if (p < this.DB && (d = this[i] >> p) > 0) {
  8467. m = true;
  8468. r = int2char(d);
  8469. }
  8470. while (i >= 0) {
  8471. if (p < k) {
  8472. d = (this[i] & (1 << p) - 1) << k - p;
  8473. d |= this[--i] >> (p += this.DB - k);
  8474. } else {
  8475. d = this[i] >> (p -= k) & km;
  8476. if (p <= 0) {
  8477. p += this.DB;
  8478. --i;
  8479. }
  8480. }
  8481. if (d > 0) m = true;
  8482. if (m) r += int2char(d);
  8483. }
  8484. }
  8485. return m ? r : "0";
  8486. }
  8487. // (public) -this
  8488. function bnNegate() {
  8489. var r = nbi();
  8490. BigInteger.ZERO.subTo(this, r);
  8491. return r;
  8492. }
  8493. // (public) |this|
  8494. function bnAbs() {
  8495. return this.s < 0 ? this.negate() : this;
  8496. }
  8497. // (public) return + if this > a, - if this < a, 0 if equal
  8498. function bnCompareTo(a) {
  8499. var r = this.s - a.s;
  8500. if (r != 0) return r;
  8501. var i = this.t;
  8502. r = i - a.t;
  8503. if (r != 0) return this.s < 0 ? -r : r;
  8504. while (--i >= 0) {
  8505. if ((r = this[i] - a[i]) != 0) return r;
  8506. }return 0;
  8507. }
  8508. // returns bit length of the integer x
  8509. function nbits(x) {
  8510. var r = 1,
  8511. t;
  8512. if ((t = x >>> 16) != 0) {
  8513. x = t;
  8514. r += 16;
  8515. }
  8516. if ((t = x >> 8) != 0) {
  8517. x = t;
  8518. r += 8;
  8519. }
  8520. if ((t = x >> 4) != 0) {
  8521. x = t;
  8522. r += 4;
  8523. }
  8524. if ((t = x >> 2) != 0) {
  8525. x = t;
  8526. r += 2;
  8527. }
  8528. if ((t = x >> 1) != 0) {
  8529. x = t;
  8530. r += 1;
  8531. }
  8532. return r;
  8533. }
  8534. // (public) return the number of bits in "this"
  8535. function bnBitLength() {
  8536. if (this.t <= 0) return 0;
  8537. return this.DB * (this.t - 1) + nbits(this[this.t - 1] ^ this.s & this.DM);
  8538. }
  8539. // (protected) r = this << n*DB
  8540. function bnpDLShiftTo(n, r) {
  8541. var i;
  8542. for (i = this.t - 1; i >= 0; --i) {
  8543. r[i + n] = this[i];
  8544. }for (i = n - 1; i >= 0; --i) {
  8545. r[i] = 0;
  8546. }r.t = this.t + n;
  8547. r.s = this.s;
  8548. }
  8549. // (protected) r = this >> n*DB
  8550. function bnpDRShiftTo(n, r) {
  8551. for (var i = n; i < this.t; ++i) {
  8552. r[i - n] = this[i];
  8553. }r.t = Math.max(this.t - n, 0);
  8554. r.s = this.s;
  8555. }
  8556. // (protected) r = this << n
  8557. function bnpLShiftTo(n, r) {
  8558. var bs = n % this.DB;
  8559. var cbs = this.DB - bs;
  8560. var bm = (1 << cbs) - 1;
  8561. var ds = Math.floor(n / this.DB),
  8562. c = this.s << bs & this.DM,
  8563. i;
  8564. for (i = this.t - 1; i >= 0; --i) {
  8565. r[i + ds + 1] = this[i] >> cbs | c;
  8566. c = (this[i] & bm) << bs;
  8567. }
  8568. for (i = ds - 1; i >= 0; --i) {
  8569. r[i] = 0;
  8570. }r[ds] = c;
  8571. r.t = this.t + ds + 1;
  8572. r.s = this.s;
  8573. r.clamp();
  8574. }
  8575. // (protected) r = this >> n
  8576. function bnpRShiftTo(n, r) {
  8577. r.s = this.s;
  8578. var ds = Math.floor(n / this.DB);
  8579. if (ds >= this.t) {
  8580. r.t = 0;
  8581. return;
  8582. }
  8583. var bs = n % this.DB;
  8584. var cbs = this.DB - bs;
  8585. var bm = (1 << bs) - 1;
  8586. r[0] = this[ds] >> bs;
  8587. for (var i = ds + 1; i < this.t; ++i) {
  8588. r[i - ds - 1] |= (this[i] & bm) << cbs;
  8589. r[i - ds] = this[i] >> bs;
  8590. }
  8591. if (bs > 0) r[this.t - ds - 1] |= (this.s & bm) << cbs;
  8592. r.t = this.t - ds;
  8593. r.clamp();
  8594. }
  8595. // (protected) r = this - a
  8596. function bnpSubTo(a, r) {
  8597. var i = 0,
  8598. c = 0,
  8599. m = Math.min(a.t, this.t);
  8600. while (i < m) {
  8601. c += this[i] - a[i];
  8602. r[i++] = c & this.DM;
  8603. c >>= this.DB;
  8604. }
  8605. if (a.t < this.t) {
  8606. c -= a.s;
  8607. while (i < this.t) {
  8608. c += this[i];
  8609. r[i++] = c & this.DM;
  8610. c >>= this.DB;
  8611. }
  8612. c += this.s;
  8613. } else {
  8614. c += this.s;
  8615. while (i < a.t) {
  8616. c -= a[i];
  8617. r[i++] = c & this.DM;
  8618. c >>= this.DB;
  8619. }
  8620. c -= a.s;
  8621. }
  8622. r.s = c < 0 ? -1 : 0;
  8623. if (c < -1) r[i++] = this.DV + c;else if (c > 0) r[i++] = c;
  8624. r.t = i;
  8625. r.clamp();
  8626. }
  8627. // (protected) r = this * a, r != this,a (HAC 14.12)
  8628. // "this" should be the larger one if appropriate.
  8629. function bnpMultiplyTo(a, r) {
  8630. var x = this.abs(),
  8631. y = a.abs();
  8632. var i = x.t;
  8633. r.t = i + y.t;
  8634. while (--i >= 0) {
  8635. r[i] = 0;
  8636. }for (i = 0; i < y.t; ++i) {
  8637. r[i + x.t] = x.am(0, y[i], r, i, 0, x.t);
  8638. }r.s = 0;
  8639. r.clamp();
  8640. if (this.s != a.s) BigInteger.ZERO.subTo(r, r);
  8641. }
  8642. // (protected) r = this^2, r != this (HAC 14.16)
  8643. function bnpSquareTo(r) {
  8644. var x = this.abs();
  8645. var i = r.t = 2 * x.t;
  8646. while (--i >= 0) {
  8647. r[i] = 0;
  8648. }for (i = 0; i < x.t - 1; ++i) {
  8649. var c = x.am(i, x[i], r, 2 * i, 0, 1);
  8650. if ((r[i + x.t] += x.am(i + 1, 2 * x[i], r, 2 * i + 1, c, x.t - i - 1)) >= x.DV) {
  8651. r[i + x.t] -= x.DV;
  8652. r[i + x.t + 1] = 1;
  8653. }
  8654. }
  8655. if (r.t > 0) r[r.t - 1] += x.am(i, x[i], r, 2 * i, 0, 1);
  8656. r.s = 0;
  8657. r.clamp();
  8658. }
  8659. // (protected) divide this by m, quotient and remainder to q, r (HAC 14.20)
  8660. // r != q, this != m. q or r may be null.
  8661. function bnpDivRemTo(m, q, r) {
  8662. var pm = m.abs();
  8663. if (pm.t <= 0) return;
  8664. var pt = this.abs();
  8665. if (pt.t < pm.t) {
  8666. if (q != null) q.fromInt(0);
  8667. if (r != null) this.copyTo(r);
  8668. return;
  8669. }
  8670. if (r == null) r = nbi();
  8671. var y = nbi(),
  8672. ts = this.s,
  8673. ms = m.s;
  8674. var nsh = this.DB - nbits(pm[pm.t - 1]); // normalize modulus
  8675. if (nsh > 0) {
  8676. pm.lShiftTo(nsh, y);
  8677. pt.lShiftTo(nsh, r);
  8678. } else {
  8679. pm.copyTo(y);
  8680. pt.copyTo(r);
  8681. }
  8682. var ys = y.t;
  8683. var y0 = y[ys - 1];
  8684. if (y0 == 0) return;
  8685. var yt = y0 * (1 << this.F1) + (ys > 1 ? y[ys - 2] >> this.F2 : 0);
  8686. var d1 = this.FV / yt,
  8687. d2 = (1 << this.F1) / yt,
  8688. e = 1 << this.F2;
  8689. var i = r.t,
  8690. j = i - ys,
  8691. t = q == null ? nbi() : q;
  8692. y.dlShiftTo(j, t);
  8693. if (r.compareTo(t) >= 0) {
  8694. r[r.t++] = 1;
  8695. r.subTo(t, r);
  8696. }
  8697. BigInteger.ONE.dlShiftTo(ys, t);
  8698. t.subTo(y, y); // "negative" y so we can replace sub with am later
  8699. while (y.t < ys) {
  8700. y[y.t++] = 0;
  8701. }while (--j >= 0) {
  8702. // Estimate quotient digit
  8703. var qd = r[--i] == y0 ? this.DM : Math.floor(r[i] * d1 + (r[i - 1] + e) * d2);
  8704. if ((r[i] += y.am(0, qd, r, j, 0, ys)) < qd) {
  8705. // Try it out
  8706. y.dlShiftTo(j, t);
  8707. r.subTo(t, r);
  8708. while (r[i] < --qd) {
  8709. r.subTo(t, r);
  8710. }
  8711. }
  8712. }
  8713. if (q != null) {
  8714. r.drShiftTo(ys, q);
  8715. if (ts != ms) BigInteger.ZERO.subTo(q, q);
  8716. }
  8717. r.t = ys;
  8718. r.clamp();
  8719. if (nsh > 0) r.rShiftTo(nsh, r); // Denormalize remainder
  8720. if (ts < 0) BigInteger.ZERO.subTo(r, r);
  8721. }
  8722. // (public) this mod a
  8723. function bnMod(a) {
  8724. var r = nbi();
  8725. this.abs().divRemTo(a, null, r);
  8726. if (this.s < 0 && r.compareTo(BigInteger.ZERO) > 0) a.subTo(r, r);
  8727. return r;
  8728. }
  8729. // Modular reduction using "classic" algorithm
  8730. function Classic(m) {
  8731. this.m = m;
  8732. }
  8733. function cConvert(x) {
  8734. if (x.s < 0 || x.compareTo(this.m) >= 0) return x.mod(this.m);else return x;
  8735. }
  8736. function cRevert(x) {
  8737. return x;
  8738. }
  8739. function cReduce(x) {
  8740. x.divRemTo(this.m, null, x);
  8741. }
  8742. function cMulTo(x, y, r) {
  8743. x.multiplyTo(y, r);
  8744. this.reduce(r);
  8745. }
  8746. function cSqrTo(x, r) {
  8747. x.squareTo(r);
  8748. this.reduce(r);
  8749. }
  8750. Classic.prototype.convert = cConvert;
  8751. Classic.prototype.revert = cRevert;
  8752. Classic.prototype.reduce = cReduce;
  8753. Classic.prototype.mulTo = cMulTo;
  8754. Classic.prototype.sqrTo = cSqrTo;
  8755. // (protected) return "-1/this % 2^DB"; useful for Mont. reduction
  8756. // justification:
  8757. // xy == 1 (mod m)
  8758. // xy = 1+km
  8759. // xy(2-xy) = (1+km)(1-km)
  8760. // x[y(2-xy)] = 1-k^2m^2
  8761. // x[y(2-xy)] == 1 (mod m^2)
  8762. // if y is 1/x mod m, then y(2-xy) is 1/x mod m^2
  8763. // should reduce x and y(2-xy) by m^2 at each step to keep size bounded.
  8764. // JS multiply "overflows" differently from C/C++, so care is needed here.
  8765. function bnpInvDigit() {
  8766. if (this.t < 1) return 0;
  8767. var x = this[0];
  8768. if ((x & 1) == 0) return 0;
  8769. var y = x & 3; // y == 1/x mod 2^2
  8770. y = y * (2 - (x & 0xf) * y) & 0xf; // y == 1/x mod 2^4
  8771. y = y * (2 - (x & 0xff) * y) & 0xff; // y == 1/x mod 2^8
  8772. y = y * (2 - ((x & 0xffff) * y & 0xffff)) & 0xffff; // y == 1/x mod 2^16
  8773. // last step - calculate inverse mod DV directly;
  8774. // assumes 16 < DB <= 32 and assumes ability to handle 48-bit ints
  8775. y = y * (2 - x * y % this.DV) % this.DV; // y == 1/x mod 2^dbits
  8776. // we really want the negative inverse, and -DV < y < DV
  8777. return y > 0 ? this.DV - y : -y;
  8778. }
  8779. // Montgomery reduction
  8780. function Montgomery(m) {
  8781. this.m = m;
  8782. this.mp = m.invDigit();
  8783. this.mpl = this.mp & 0x7fff;
  8784. this.mph = this.mp >> 15;
  8785. this.um = (1 << m.DB - 15) - 1;
  8786. this.mt2 = 2 * m.t;
  8787. }
  8788. // xR mod m
  8789. function montConvert(x) {
  8790. var r = nbi();
  8791. x.abs().dlShiftTo(this.m.t, r);
  8792. r.divRemTo(this.m, null, r);
  8793. if (x.s < 0 && r.compareTo(BigInteger.ZERO) > 0) this.m.subTo(r, r);
  8794. return r;
  8795. }
  8796. // x/R mod m
  8797. function montRevert(x) {
  8798. var r = nbi();
  8799. x.copyTo(r);
  8800. this.reduce(r);
  8801. return r;
  8802. }
  8803. // x = x/R mod m (HAC 14.32)
  8804. function montReduce(x) {
  8805. while (x.t <= this.mt2) {
  8806. // pad x so am has enough room later
  8807. x[x.t++] = 0;
  8808. }for (var i = 0; i < this.m.t; ++i) {
  8809. // faster way of calculating u0 = x[i]*mp mod DV
  8810. var j = x[i] & 0x7fff;
  8811. var u0 = j * this.mpl + ((j * this.mph + (x[i] >> 15) * this.mpl & this.um) << 15) & x.DM;
  8812. // use am to combine the multiply-shift-add into one call
  8813. j = i + this.m.t;
  8814. x[j] += this.m.am(0, u0, x, i, 0, this.m.t);
  8815. // propagate carry
  8816. while (x[j] >= x.DV) {
  8817. x[j] -= x.DV;
  8818. x[++j]++;
  8819. }
  8820. }
  8821. x.clamp();
  8822. x.drShiftTo(this.m.t, x);
  8823. if (x.compareTo(this.m) >= 0) x.subTo(this.m, x);
  8824. }
  8825. // r = "x^2/R mod m"; x != r
  8826. function montSqrTo(x, r) {
  8827. x.squareTo(r);
  8828. this.reduce(r);
  8829. }
  8830. // r = "xy/R mod m"; x,y != r
  8831. function montMulTo(x, y, r) {
  8832. x.multiplyTo(y, r);
  8833. this.reduce(r);
  8834. }
  8835. Montgomery.prototype.convert = montConvert;
  8836. Montgomery.prototype.revert = montRevert;
  8837. Montgomery.prototype.reduce = montReduce;
  8838. Montgomery.prototype.mulTo = montMulTo;
  8839. Montgomery.prototype.sqrTo = montSqrTo;
  8840. // (protected) true iff this is even
  8841. function bnpIsEven() {
  8842. return (this.t > 0 ? this[0] & 1 : this.s) == 0;
  8843. }
  8844. // (protected) this^e, e < 2^32, doing sqr and mul with "r" (HAC 14.79)
  8845. function bnpExp(e, z) {
  8846. if (e > 0xffffffff || e < 1) return BigInteger.ONE;
  8847. var r = nbi(),
  8848. r2 = nbi(),
  8849. g = z.convert(this),
  8850. i = nbits(e) - 1;
  8851. g.copyTo(r);
  8852. while (--i >= 0) {
  8853. z.sqrTo(r, r2);
  8854. if ((e & 1 << i) > 0) z.mulTo(r2, g, r);else {
  8855. var t = r;
  8856. r = r2;
  8857. r2 = t;
  8858. }
  8859. }
  8860. return z.revert(r);
  8861. }
  8862. // (public) this^e % m, 0 <= e < 2^32
  8863. function bnModPowInt(e, m) {
  8864. var z;
  8865. if (e < 256 || m.isEven()) z = new Classic(m);else z = new Montgomery(m);
  8866. return this.exp(e, z);
  8867. }
  8868. // protected
  8869. BigInteger.prototype.copyTo = bnpCopyTo;
  8870. BigInteger.prototype.fromInt = bnpFromInt;
  8871. BigInteger.prototype.fromString = bnpFromString;
  8872. BigInteger.prototype.clamp = bnpClamp;
  8873. BigInteger.prototype.dlShiftTo = bnpDLShiftTo;
  8874. BigInteger.prototype.drShiftTo = bnpDRShiftTo;
  8875. BigInteger.prototype.lShiftTo = bnpLShiftTo;
  8876. BigInteger.prototype.rShiftTo = bnpRShiftTo;
  8877. BigInteger.prototype.subTo = bnpSubTo;
  8878. BigInteger.prototype.multiplyTo = bnpMultiplyTo;
  8879. BigInteger.prototype.squareTo = bnpSquareTo;
  8880. BigInteger.prototype.divRemTo = bnpDivRemTo;
  8881. BigInteger.prototype.invDigit = bnpInvDigit;
  8882. BigInteger.prototype.isEven = bnpIsEven;
  8883. BigInteger.prototype.exp = bnpExp;
  8884. // public
  8885. BigInteger.prototype.toString = bnToString;
  8886. BigInteger.prototype.negate = bnNegate;
  8887. BigInteger.prototype.abs = bnAbs;
  8888. BigInteger.prototype.compareTo = bnCompareTo;
  8889. BigInteger.prototype.bitLength = bnBitLength;
  8890. BigInteger.prototype.mod = bnMod;
  8891. BigInteger.prototype.modPowInt = bnModPowInt;
  8892. // "constants"
  8893. BigInteger.ZERO = nbv(0);
  8894. BigInteger.ONE = nbv(1);
  8895. BigInteger.TWO = nbv(2);
  8896. /*
  8897. * Copyright (c) 2003-2005 Tom Wu (tjw@cs.Stanford.EDU)
  8898. * All Rights Reserved.
  8899. *
  8900. * Modified by Recurity Labs GmbH
  8901. *
  8902. * Permission is hereby granted, free of charge, to any person obtaining
  8903. * a copy of this software and associated documentation files (the
  8904. * "Software"), to deal in the Software without restriction, including
  8905. * without limitation the rights to use, copy, modify, merge, publish,
  8906. * distribute, sublicense, and/or sell copies of the Software, and to
  8907. * permit persons to whom the Software is furnished to do so, subject to
  8908. * the following conditions:
  8909. *
  8910. * The above copyright notice and this permission notice shall be
  8911. * included in all copies or substantial portions of the Software.
  8912. *
  8913. * THE SOFTWARE IS PROVIDED "AS-IS" AND WITHOUT WARRANTY OF ANY KIND,
  8914. * EXPRESS, IMPLIED OR OTHERWISE, INCLUDING WITHOUT LIMITATION, ANY
  8915. * WARRANTY OF MERCHANTABILITY OR FITNESS FOR A PARTICULAR PURPOSE.
  8916. *
  8917. * IN NO EVENT SHALL TOM WU BE LIABLE FOR ANY SPECIAL, INCIDENTAL,
  8918. * INDIRECT OR CONSEQUENTIAL DAMAGES OF ANY KIND, OR ANY DAMAGES WHATSOEVER
  8919. * RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER OR NOT ADVISED OF
  8920. * THE POSSIBILITY OF DAMAGE, AND ON ANY THEORY OF LIABILITY, ARISING OUT
  8921. * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  8922. *
  8923. * In addition, the following condition applies:
  8924. *
  8925. * All redistributions must retain an intact copy of this copyright notice
  8926. * and disclaimer.
  8927. */
  8928. // Extended JavaScript BN functions, required for RSA private ops.
  8929. // Version 1.1: new BigInteger("0", 10) returns "proper" zero
  8930. // Version 1.2: square() API, isProbablePrime fix
  8931. // (public)
  8932. function bnClone() {
  8933. var r = nbi();
  8934. this.copyTo(r);
  8935. return r;
  8936. }
  8937. // (public) return value as integer
  8938. function bnIntValue() {
  8939. if (this.s < 0) {
  8940. if (this.t == 1) return this[0] - this.DV;else if (this.t == 0) return -1;
  8941. } else if (this.t == 1) return this[0];else if (this.t == 0) return 0;
  8942. // assumes 16 < DB < 32
  8943. return (this[1] & (1 << 32 - this.DB) - 1) << this.DB | this[0];
  8944. }
  8945. // (public) return value as byte
  8946. function bnByteValue() {
  8947. return this.t == 0 ? this.s : this[0] << 24 >> 24;
  8948. }
  8949. // (public) return value as short (assumes DB>=16)
  8950. function bnShortValue() {
  8951. return this.t == 0 ? this.s : this[0] << 16 >> 16;
  8952. }
  8953. // (protected) return x s.t. r^x < DV
  8954. function bnpChunkSize(r) {
  8955. return Math.floor(Math.LN2 * this.DB / Math.log(r));
  8956. }
  8957. // (public) 0 if this == 0, 1 if this > 0
  8958. function bnSigNum() {
  8959. if (this.s < 0) return -1;else if (this.t <= 0 || this.t == 1 && this[0] <= 0) return 0;else return 1;
  8960. }
  8961. // (protected) convert to radix string
  8962. function bnpToRadix(b) {
  8963. if (b == null) b = 10;
  8964. if (this.signum() == 0 || b < 2 || b > 36) return "0";
  8965. var cs = this.chunkSize(b);
  8966. var a = Math.pow(b, cs);
  8967. var d = nbv(a),
  8968. y = nbi(),
  8969. z = nbi(),
  8970. r = "";
  8971. this.divRemTo(d, y, z);
  8972. while (y.signum() > 0) {
  8973. r = (a + z.intValue()).toString(b).substr(1) + r;
  8974. y.divRemTo(d, y, z);
  8975. }
  8976. return z.intValue().toString(b) + r;
  8977. }
  8978. // (protected) convert from radix string
  8979. function bnpFromRadix(s, b) {
  8980. this.fromInt(0);
  8981. if (b == null) b = 10;
  8982. var cs = this.chunkSize(b);
  8983. var d = Math.pow(b, cs),
  8984. mi = false,
  8985. j = 0,
  8986. w = 0;
  8987. for (var i = 0; i < s.length; ++i) {
  8988. var x = intAt(s, i);
  8989. if (x < 0) {
  8990. if (s.charAt(i) == "-" && this.signum() == 0) mi = true;
  8991. continue;
  8992. }
  8993. w = b * w + x;
  8994. if (++j >= cs) {
  8995. this.dMultiply(d);
  8996. this.dAddOffset(w, 0);
  8997. j = 0;
  8998. w = 0;
  8999. }
  9000. }
  9001. if (j > 0) {
  9002. this.dMultiply(Math.pow(b, j));
  9003. this.dAddOffset(w, 0);
  9004. }
  9005. if (mi) BigInteger.ZERO.subTo(this, this);
  9006. }
  9007. // (protected) alternate constructor
  9008. function bnpFromNumber(a, b, c) {
  9009. if ("number" == typeof b) {
  9010. // new BigInteger(int,int,RNG)
  9011. if (a < 2) this.fromInt(1);else {
  9012. this.fromNumber(a, c);
  9013. if (!this.testBit(a - 1)) // force MSB set
  9014. this.bitwiseTo(BigInteger.ONE.shiftLeft(a - 1), op_or, this);
  9015. if (this.isEven()) this.dAddOffset(1, 0); // force odd
  9016. while (!this.isProbablePrime(b)) {
  9017. this.dAddOffset(2, 0);
  9018. if (this.bitLength() > a) this.subTo(BigInteger.ONE.shiftLeft(a - 1), this);
  9019. }
  9020. }
  9021. } else {
  9022. // new BigInteger(int,RNG)
  9023. var x = new Array(),
  9024. t = a & 7;
  9025. x.length = (a >> 3) + 1;
  9026. b.nextBytes(x);
  9027. if (t > 0) x[0] &= (1 << t) - 1;else x[0] = 0;
  9028. this.fromString(x, 256);
  9029. }
  9030. }
  9031. // (public) convert to bigendian byte array
  9032. function bnToByteArray() {
  9033. var i = this.t,
  9034. r = new Array();
  9035. r[0] = this.s;
  9036. var p = this.DB - i * this.DB % 8,
  9037. d,
  9038. k = 0;
  9039. if (i-- > 0) {
  9040. if (p < this.DB && (d = this[i] >> p) != (this.s & this.DM) >> p) r[k++] = d | this.s << this.DB - p;
  9041. while (i >= 0) {
  9042. if (p < 8) {
  9043. d = (this[i] & (1 << p) - 1) << 8 - p;
  9044. d |= this[--i] >> (p += this.DB - 8);
  9045. } else {
  9046. d = this[i] >> (p -= 8) & 0xff;
  9047. if (p <= 0) {
  9048. p += this.DB;
  9049. --i;
  9050. }
  9051. }
  9052. //if((d&0x80) != 0) d |= -256;
  9053. //if(k == 0 && (this.s&0x80) != (d&0x80)) ++k;
  9054. if (k > 0 || d != this.s) r[k++] = d;
  9055. }
  9056. }
  9057. return r;
  9058. }
  9059. function bnEquals(a) {
  9060. return this.compareTo(a) == 0;
  9061. }
  9062. function bnMin(a) {
  9063. return this.compareTo(a) < 0 ? this : a;
  9064. }
  9065. function bnMax(a) {
  9066. return this.compareTo(a) > 0 ? this : a;
  9067. }
  9068. // (protected) r = this op a (bitwise)
  9069. function bnpBitwiseTo(a, op, r) {
  9070. var i,
  9071. f,
  9072. m = Math.min(a.t, this.t);
  9073. for (i = 0; i < m; ++i) {
  9074. r[i] = op(this[i], a[i]);
  9075. }if (a.t < this.t) {
  9076. f = a.s & this.DM;
  9077. for (i = m; i < this.t; ++i) {
  9078. r[i] = op(this[i], f);
  9079. }r.t = this.t;
  9080. } else {
  9081. f = this.s & this.DM;
  9082. for (i = m; i < a.t; ++i) {
  9083. r[i] = op(f, a[i]);
  9084. }r.t = a.t;
  9085. }
  9086. r.s = op(this.s, a.s);
  9087. r.clamp();
  9088. }
  9089. // (public) this & a
  9090. function op_and(x, y) {
  9091. return x & y;
  9092. }
  9093. function bnAnd(a) {
  9094. var r = nbi();
  9095. this.bitwiseTo(a, op_and, r);
  9096. return r;
  9097. }
  9098. // (public) this | a
  9099. function op_or(x, y) {
  9100. return x | y;
  9101. }
  9102. function bnOr(a) {
  9103. var r = nbi();
  9104. this.bitwiseTo(a, op_or, r);
  9105. return r;
  9106. }
  9107. // (public) this ^ a
  9108. function op_xor(x, y) {
  9109. return x ^ y;
  9110. }
  9111. function bnXor(a) {
  9112. var r = nbi();
  9113. this.bitwiseTo(a, op_xor, r);
  9114. return r;
  9115. }
  9116. // (public) this & ~a
  9117. function op_andnot(x, y) {
  9118. return x & ~y;
  9119. }
  9120. function bnAndNot(a) {
  9121. var r = nbi();
  9122. this.bitwiseTo(a, op_andnot, r);
  9123. return r;
  9124. }
  9125. // (public) ~this
  9126. function bnNot() {
  9127. var r = nbi();
  9128. for (var i = 0; i < this.t; ++i) {
  9129. r[i] = this.DM & ~this[i];
  9130. }r.t = this.t;
  9131. r.s = ~this.s;
  9132. return r;
  9133. }
  9134. // (public) this << n
  9135. function bnShiftLeft(n) {
  9136. var r = nbi();
  9137. if (n < 0) this.rShiftTo(-n, r);else this.lShiftTo(n, r);
  9138. return r;
  9139. }
  9140. // (public) this >> n
  9141. function bnShiftRight(n) {
  9142. var r = nbi();
  9143. if (n < 0) this.lShiftTo(-n, r);else this.rShiftTo(n, r);
  9144. return r;
  9145. }
  9146. // return index of lowest 1-bit in x, x < 2^31
  9147. function lbit(x) {
  9148. if (x == 0) return -1;
  9149. var r = 0;
  9150. if ((x & 0xffff) == 0) {
  9151. x >>= 16;
  9152. r += 16;
  9153. }
  9154. if ((x & 0xff) == 0) {
  9155. x >>= 8;
  9156. r += 8;
  9157. }
  9158. if ((x & 0xf) == 0) {
  9159. x >>= 4;
  9160. r += 4;
  9161. }
  9162. if ((x & 3) == 0) {
  9163. x >>= 2;
  9164. r += 2;
  9165. }
  9166. if ((x & 1) == 0) ++r;
  9167. return r;
  9168. }
  9169. // (public) returns index of lowest 1-bit (or -1 if none)
  9170. function bnGetLowestSetBit() {
  9171. for (var i = 0; i < this.t; ++i) {
  9172. if (this[i] != 0) return i * this.DB + lbit(this[i]);
  9173. }if (this.s < 0) return this.t * this.DB;
  9174. return -1;
  9175. }
  9176. // return number of 1 bits in x
  9177. function cbit(x) {
  9178. var r = 0;
  9179. while (x != 0) {
  9180. x &= x - 1;
  9181. ++r;
  9182. }
  9183. return r;
  9184. }
  9185. // (public) return number of set bits
  9186. function bnBitCount() {
  9187. var r = 0,
  9188. x = this.s & this.DM;
  9189. for (var i = 0; i < this.t; ++i) {
  9190. r += cbit(this[i] ^ x);
  9191. }return r;
  9192. }
  9193. // (public) true iff nth bit is set
  9194. function bnTestBit(n) {
  9195. var j = Math.floor(n / this.DB);
  9196. if (j >= this.t) return this.s != 0;
  9197. return (this[j] & 1 << n % this.DB) != 0;
  9198. }
  9199. // (protected) this op (1<<n)
  9200. function bnpChangeBit(n, op) {
  9201. var r = BigInteger.ONE.shiftLeft(n);
  9202. this.bitwiseTo(r, op, r);
  9203. return r;
  9204. }
  9205. // (public) this | (1<<n)
  9206. function bnSetBit(n) {
  9207. return this.changeBit(n, op_or);
  9208. }
  9209. // (public) this & ~(1<<n)
  9210. function bnClearBit(n) {
  9211. return this.changeBit(n, op_andnot);
  9212. }
  9213. // (public) this ^ (1<<n)
  9214. function bnFlipBit(n) {
  9215. return this.changeBit(n, op_xor);
  9216. }
  9217. // (protected) r = this + a
  9218. function bnpAddTo(a, r) {
  9219. var i = 0,
  9220. c = 0,
  9221. m = Math.min(a.t, this.t);
  9222. while (i < m) {
  9223. c += this[i] + a[i];
  9224. r[i++] = c & this.DM;
  9225. c >>= this.DB;
  9226. }
  9227. if (a.t < this.t) {
  9228. c += a.s;
  9229. while (i < this.t) {
  9230. c += this[i];
  9231. r[i++] = c & this.DM;
  9232. c >>= this.DB;
  9233. }
  9234. c += this.s;
  9235. } else {
  9236. c += this.s;
  9237. while (i < a.t) {
  9238. c += a[i];
  9239. r[i++] = c & this.DM;
  9240. c >>= this.DB;
  9241. }
  9242. c += a.s;
  9243. }
  9244. r.s = c < 0 ? -1 : 0;
  9245. if (c > 0) r[i++] = c;else if (c < -1) r[i++] = this.DV + c;
  9246. r.t = i;
  9247. r.clamp();
  9248. }
  9249. // (public) this + a
  9250. function bnAdd(a) {
  9251. var r = nbi();
  9252. this.addTo(a, r);
  9253. return r;
  9254. }
  9255. // (public) this - a
  9256. function bnSubtract(a) {
  9257. var r = nbi();
  9258. this.subTo(a, r);
  9259. return r;
  9260. }
  9261. // (public) this * a
  9262. function bnMultiply(a) {
  9263. var r = nbi();
  9264. this.multiplyTo(a, r);
  9265. return r;
  9266. }
  9267. // (public) this^2
  9268. function bnSquare() {
  9269. var r = nbi();
  9270. this.squareTo(r);
  9271. return r;
  9272. }
  9273. // (public) this / a
  9274. function bnDivide(a) {
  9275. var r = nbi();
  9276. this.divRemTo(a, r, null);
  9277. return r;
  9278. }
  9279. // (public) this % a
  9280. function bnRemainder(a) {
  9281. var r = nbi();
  9282. this.divRemTo(a, null, r);
  9283. return r;
  9284. }
  9285. // (public) [this/a,this%a]
  9286. function bnDivideAndRemainder(a) {
  9287. var q = nbi(),
  9288. r = nbi();
  9289. this.divRemTo(a, q, r);
  9290. return new Array(q, r);
  9291. }
  9292. // (protected) this *= n, this >= 0, 1 < n < DV
  9293. function bnpDMultiply(n) {
  9294. this[this.t] = this.am(0, n - 1, this, 0, 0, this.t);
  9295. ++this.t;
  9296. this.clamp();
  9297. }
  9298. // (protected) this += n << w words, this >= 0
  9299. function bnpDAddOffset(n, w) {
  9300. if (n == 0) return;
  9301. while (this.t <= w) {
  9302. this[this.t++] = 0;
  9303. }this[w] += n;
  9304. while (this[w] >= this.DV) {
  9305. this[w] -= this.DV;
  9306. if (++w >= this.t) this[this.t++] = 0;
  9307. ++this[w];
  9308. }
  9309. }
  9310. // A "null" reducer
  9311. function NullExp() {}
  9312. function nNop(x) {
  9313. return x;
  9314. }
  9315. function nMulTo(x, y, r) {
  9316. x.multiplyTo(y, r);
  9317. }
  9318. function nSqrTo(x, r) {
  9319. x.squareTo(r);
  9320. }
  9321. NullExp.prototype.convert = nNop;
  9322. NullExp.prototype.revert = nNop;
  9323. NullExp.prototype.mulTo = nMulTo;
  9324. NullExp.prototype.sqrTo = nSqrTo;
  9325. // (public) this^e
  9326. function bnPow(e) {
  9327. return this.exp(e, new NullExp());
  9328. }
  9329. // (protected) r = lower n words of "this * a", a.t <= n
  9330. // "this" should be the larger one if appropriate.
  9331. function bnpMultiplyLowerTo(a, n, r) {
  9332. var i = Math.min(this.t + a.t, n);
  9333. r.s = 0; // assumes a,this >= 0
  9334. r.t = i;
  9335. while (i > 0) {
  9336. r[--i] = 0;
  9337. }var j;
  9338. for (j = r.t - this.t; i < j; ++i) {
  9339. r[i + this.t] = this.am(0, a[i], r, i, 0, this.t);
  9340. }for (j = Math.min(a.t, n); i < j; ++i) {
  9341. this.am(0, a[i], r, i, 0, n - i);
  9342. }r.clamp();
  9343. }
  9344. // (protected) r = "this * a" without lower n words, n > 0
  9345. // "this" should be the larger one if appropriate.
  9346. function bnpMultiplyUpperTo(a, n, r) {
  9347. --n;
  9348. var i = r.t = this.t + a.t - n;
  9349. r.s = 0; // assumes a,this >= 0
  9350. while (--i >= 0) {
  9351. r[i] = 0;
  9352. }for (i = Math.max(n - this.t, 0); i < a.t; ++i) {
  9353. r[this.t + i - n] = this.am(n - i, a[i], r, 0, 0, this.t + i - n);
  9354. }r.clamp();
  9355. r.drShiftTo(1, r);
  9356. }
  9357. // Barrett modular reduction
  9358. function Barrett(m) {
  9359. // setup Barrett
  9360. this.r2 = nbi();
  9361. this.q3 = nbi();
  9362. BigInteger.ONE.dlShiftTo(2 * m.t, this.r2);
  9363. this.mu = this.r2.divide(m);
  9364. this.m = m;
  9365. }
  9366. function barrettConvert(x) {
  9367. if (x.s < 0 || x.t > 2 * this.m.t) return x.mod(this.m);else if (x.compareTo(this.m) < 0) return x;else {
  9368. var r = nbi();
  9369. x.copyTo(r);
  9370. this.reduce(r);
  9371. return r;
  9372. }
  9373. }
  9374. function barrettRevert(x) {
  9375. return x;
  9376. }
  9377. // x = x mod m (HAC 14.42)
  9378. function barrettReduce(x) {
  9379. x.drShiftTo(this.m.t - 1, this.r2);
  9380. if (x.t > this.m.t + 1) {
  9381. x.t = this.m.t + 1;
  9382. x.clamp();
  9383. }
  9384. this.mu.multiplyUpperTo(this.r2, this.m.t + 1, this.q3);
  9385. this.m.multiplyLowerTo(this.q3, this.m.t + 1, this.r2);
  9386. while (x.compareTo(this.r2) < 0) {
  9387. x.dAddOffset(1, this.m.t + 1);
  9388. }x.subTo(this.r2, x);
  9389. while (x.compareTo(this.m) >= 0) {
  9390. x.subTo(this.m, x);
  9391. }
  9392. }
  9393. // r = x^2 mod m; x != r
  9394. function barrettSqrTo(x, r) {
  9395. x.squareTo(r);
  9396. this.reduce(r);
  9397. }
  9398. // r = x*y mod m; x,y != r
  9399. function barrettMulTo(x, y, r) {
  9400. x.multiplyTo(y, r);
  9401. this.reduce(r);
  9402. }
  9403. Barrett.prototype.convert = barrettConvert;
  9404. Barrett.prototype.revert = barrettRevert;
  9405. Barrett.prototype.reduce = barrettReduce;
  9406. Barrett.prototype.mulTo = barrettMulTo;
  9407. Barrett.prototype.sqrTo = barrettSqrTo;
  9408. // (public) this^e % m (HAC 14.85)
  9409. function bnModPow(e, m) {
  9410. var i = e.bitLength(),
  9411. k,
  9412. r = nbv(1),
  9413. z;
  9414. if (i <= 0) return r;else if (i < 18) k = 1;else if (i < 48) k = 3;else if (i < 144) k = 4;else if (i < 768) k = 5;else k = 6;
  9415. if (i < 8) z = new Classic(m);else if (m.isEven()) z = new Barrett(m);else z = new Montgomery(m);
  9416. // precomputation
  9417. var g = new Array(),
  9418. n = 3,
  9419. k1 = k - 1,
  9420. km = (1 << k) - 1;
  9421. g[1] = z.convert(this);
  9422. if (k > 1) {
  9423. var g2 = nbi();
  9424. z.sqrTo(g[1], g2);
  9425. while (n <= km) {
  9426. g[n] = nbi();
  9427. z.mulTo(g2, g[n - 2], g[n]);
  9428. n += 2;
  9429. }
  9430. }
  9431. var j = e.t - 1,
  9432. w,
  9433. is1 = true,
  9434. r2 = nbi(),
  9435. t;
  9436. i = nbits(e[j]) - 1;
  9437. while (j >= 0) {
  9438. if (i >= k1) w = e[j] >> i - k1 & km;else {
  9439. w = (e[j] & (1 << i + 1) - 1) << k1 - i;
  9440. if (j > 0) w |= e[j - 1] >> this.DB + i - k1;
  9441. }
  9442. n = k;
  9443. while ((w & 1) == 0) {
  9444. w >>= 1;
  9445. --n;
  9446. }
  9447. if ((i -= n) < 0) {
  9448. i += this.DB;
  9449. --j;
  9450. }
  9451. if (is1) {
  9452. // ret == 1, don't bother squaring or multiplying it
  9453. g[w].copyTo(r);
  9454. is1 = false;
  9455. } else {
  9456. while (n > 1) {
  9457. z.sqrTo(r, r2);
  9458. z.sqrTo(r2, r);
  9459. n -= 2;
  9460. }
  9461. if (n > 0) z.sqrTo(r, r2);else {
  9462. t = r;
  9463. r = r2;
  9464. r2 = t;
  9465. }
  9466. z.mulTo(r2, g[w], r);
  9467. }
  9468. while (j >= 0 && (e[j] & 1 << i) == 0) {
  9469. z.sqrTo(r, r2);
  9470. t = r;
  9471. r = r2;
  9472. r2 = t;
  9473. if (--i < 0) {
  9474. i = this.DB - 1;
  9475. --j;
  9476. }
  9477. }
  9478. }
  9479. return z.revert(r);
  9480. }
  9481. // (public) gcd(this,a) (HAC 14.54)
  9482. function bnGCD(a) {
  9483. var x = this.s < 0 ? this.negate() : this.clone();
  9484. var y = a.s < 0 ? a.negate() : a.clone();
  9485. if (x.compareTo(y) < 0) {
  9486. var t = x;
  9487. x = y;
  9488. y = t;
  9489. }
  9490. var i = x.getLowestSetBit(),
  9491. g = y.getLowestSetBit();
  9492. if (g < 0) return x;
  9493. if (i < g) g = i;
  9494. if (g > 0) {
  9495. x.rShiftTo(g, x);
  9496. y.rShiftTo(g, y);
  9497. }
  9498. while (x.signum() > 0) {
  9499. if ((i = x.getLowestSetBit()) > 0) x.rShiftTo(i, x);
  9500. if ((i = y.getLowestSetBit()) > 0) y.rShiftTo(i, y);
  9501. if (x.compareTo(y) >= 0) {
  9502. x.subTo(y, x);
  9503. x.rShiftTo(1, x);
  9504. } else {
  9505. y.subTo(x, y);
  9506. y.rShiftTo(1, y);
  9507. }
  9508. }
  9509. if (g > 0) y.lShiftTo(g, y);
  9510. return y;
  9511. }
  9512. // (protected) this % n, n < 2^26
  9513. function bnpModInt(n) {
  9514. if (n <= 0) return 0;
  9515. var d = this.DV % n,
  9516. r = this.s < 0 ? n - 1 : 0;
  9517. if (this.t > 0) if (d == 0) r = this[0] % n;else for (var i = this.t - 1; i >= 0; --i) {
  9518. r = (d * r + this[i]) % n;
  9519. }return r;
  9520. }
  9521. // (public) 1/this % m (HAC 14.61)
  9522. function bnModInverse(m) {
  9523. var ac = m.isEven();
  9524. if (this.isEven() && ac || m.signum() == 0) return BigInteger.ZERO;
  9525. var u = m.clone(),
  9526. v = this.clone();
  9527. var a = nbv(1),
  9528. b = nbv(0),
  9529. c = nbv(0),
  9530. d = nbv(1);
  9531. while (u.signum() != 0) {
  9532. while (u.isEven()) {
  9533. u.rShiftTo(1, u);
  9534. if (ac) {
  9535. if (!a.isEven() || !b.isEven()) {
  9536. a.addTo(this, a);
  9537. b.subTo(m, b);
  9538. }
  9539. a.rShiftTo(1, a);
  9540. } else if (!b.isEven()) b.subTo(m, b);
  9541. b.rShiftTo(1, b);
  9542. }
  9543. while (v.isEven()) {
  9544. v.rShiftTo(1, v);
  9545. if (ac) {
  9546. if (!c.isEven() || !d.isEven()) {
  9547. c.addTo(this, c);
  9548. d.subTo(m, d);
  9549. }
  9550. c.rShiftTo(1, c);
  9551. } else if (!d.isEven()) d.subTo(m, d);
  9552. d.rShiftTo(1, d);
  9553. }
  9554. if (u.compareTo(v) >= 0) {
  9555. u.subTo(v, u);
  9556. if (ac) a.subTo(c, a);
  9557. b.subTo(d, b);
  9558. } else {
  9559. v.subTo(u, v);
  9560. if (ac) c.subTo(a, c);
  9561. d.subTo(b, d);
  9562. }
  9563. }
  9564. if (v.compareTo(BigInteger.ONE) != 0) return BigInteger.ZERO;
  9565. if (d.compareTo(m) >= 0) return d.subtract(m);
  9566. if (d.signum() < 0) d.addTo(m, d);else return d;
  9567. if (d.signum() < 0) return d.add(m);else return d;
  9568. }
  9569. 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];
  9570. var lplim = (1 << 26) / lowprimes[lowprimes.length - 1];
  9571. // (public) test primality with certainty >= 1-.5^t
  9572. function bnIsProbablePrime(t) {
  9573. var i,
  9574. x = this.abs();
  9575. if (x.t == 1 && x[0] <= lowprimes[lowprimes.length - 1]) {
  9576. for (i = 0; i < lowprimes.length; ++i) {
  9577. if (x[0] == lowprimes[i]) return true;
  9578. }return false;
  9579. }
  9580. if (x.isEven()) return false;
  9581. i = 1;
  9582. while (i < lowprimes.length) {
  9583. var m = lowprimes[i],
  9584. j = i + 1;
  9585. while (j < lowprimes.length && m < lplim) {
  9586. m *= lowprimes[j++];
  9587. }m = x.modInt(m);
  9588. while (i < j) {
  9589. if (m % lowprimes[i++] == 0) return false;
  9590. }
  9591. }
  9592. return x.millerRabin(t);
  9593. }
  9594. /* added by Recurity Labs */
  9595. function nbits(x) {
  9596. var n = 1,
  9597. t;
  9598. if ((t = x >>> 16) != 0) {
  9599. x = t;
  9600. n += 16;
  9601. }
  9602. if ((t = x >> 8) != 0) {
  9603. x = t;
  9604. n += 8;
  9605. }
  9606. if ((t = x >> 4) != 0) {
  9607. x = t;
  9608. n += 4;
  9609. }
  9610. if ((t = x >> 2) != 0) {
  9611. x = t;
  9612. n += 2;
  9613. }
  9614. if ((t = x >> 1) != 0) {
  9615. x = t;
  9616. n += 1;
  9617. }
  9618. return n;
  9619. }
  9620. function bnToMPI() {
  9621. var ba = this.toByteArray();
  9622. var size = (ba.length - 1) * 8 + nbits(ba[0]);
  9623. var result = "";
  9624. result += String.fromCharCode((size & 0xFF00) >> 8);
  9625. result += String.fromCharCode(size & 0xFF);
  9626. result += _util2.default.bin2str(ba);
  9627. return result;
  9628. }
  9629. /* END of addition */
  9630. // (protected) true if probably prime (HAC 4.24, Miller-Rabin)
  9631. function bnpMillerRabin(t) {
  9632. var n1 = this.subtract(BigInteger.ONE);
  9633. var k = n1.getLowestSetBit();
  9634. if (k <= 0) return false;
  9635. var r = n1.shiftRight(k);
  9636. t = t + 1 >> 1;
  9637. if (t > lowprimes.length) t = lowprimes.length;
  9638. var a = nbi();
  9639. var j,
  9640. bases = [];
  9641. for (var i = 0; i < t; ++i) {
  9642. //Pick bases at random, instead of starting at 2
  9643. for (;;) {
  9644. j = lowprimes[Math.floor(Math.random() * lowprimes.length)];
  9645. if (bases.indexOf(j) == -1) break;
  9646. }
  9647. bases.push(j);
  9648. a.fromInt(j);
  9649. var y = a.modPow(r, this);
  9650. if (y.compareTo(BigInteger.ONE) != 0 && y.compareTo(n1) != 0) {
  9651. var j = 1;
  9652. while (j++ < k && y.compareTo(n1) != 0) {
  9653. y = y.modPowInt(2, this);
  9654. if (y.compareTo(BigInteger.ONE) == 0) return false;
  9655. }
  9656. if (y.compareTo(n1) != 0) return false;
  9657. }
  9658. }
  9659. return true;
  9660. }
  9661. // protected
  9662. BigInteger.prototype.chunkSize = bnpChunkSize;
  9663. BigInteger.prototype.toRadix = bnpToRadix;
  9664. BigInteger.prototype.fromRadix = bnpFromRadix;
  9665. BigInteger.prototype.fromNumber = bnpFromNumber;
  9666. BigInteger.prototype.bitwiseTo = bnpBitwiseTo;
  9667. BigInteger.prototype.changeBit = bnpChangeBit;
  9668. BigInteger.prototype.addTo = bnpAddTo;
  9669. BigInteger.prototype.dMultiply = bnpDMultiply;
  9670. BigInteger.prototype.dAddOffset = bnpDAddOffset;
  9671. BigInteger.prototype.multiplyLowerTo = bnpMultiplyLowerTo;
  9672. BigInteger.prototype.multiplyUpperTo = bnpMultiplyUpperTo;
  9673. BigInteger.prototype.modInt = bnpModInt;
  9674. BigInteger.prototype.millerRabin = bnpMillerRabin;
  9675. // public
  9676. BigInteger.prototype.clone = bnClone;
  9677. BigInteger.prototype.intValue = bnIntValue;
  9678. BigInteger.prototype.byteValue = bnByteValue;
  9679. BigInteger.prototype.shortValue = bnShortValue;
  9680. BigInteger.prototype.signum = bnSigNum;
  9681. BigInteger.prototype.toByteArray = bnToByteArray;
  9682. BigInteger.prototype.equals = bnEquals;
  9683. BigInteger.prototype.min = bnMin;
  9684. BigInteger.prototype.max = bnMax;
  9685. BigInteger.prototype.and = bnAnd;
  9686. BigInteger.prototype.or = bnOr;
  9687. BigInteger.prototype.xor = bnXor;
  9688. BigInteger.prototype.andNot = bnAndNot;
  9689. BigInteger.prototype.not = bnNot;
  9690. BigInteger.prototype.shiftLeft = bnShiftLeft;
  9691. BigInteger.prototype.shiftRight = bnShiftRight;
  9692. BigInteger.prototype.getLowestSetBit = bnGetLowestSetBit;
  9693. BigInteger.prototype.bitCount = bnBitCount;
  9694. BigInteger.prototype.testBit = bnTestBit;
  9695. BigInteger.prototype.setBit = bnSetBit;
  9696. BigInteger.prototype.clearBit = bnClearBit;
  9697. BigInteger.prototype.flipBit = bnFlipBit;
  9698. BigInteger.prototype.add = bnAdd;
  9699. BigInteger.prototype.subtract = bnSubtract;
  9700. BigInteger.prototype.multiply = bnMultiply;
  9701. BigInteger.prototype.divide = bnDivide;
  9702. BigInteger.prototype.remainder = bnRemainder;
  9703. BigInteger.prototype.divideAndRemainder = bnDivideAndRemainder;
  9704. BigInteger.prototype.modPow = bnModPow;
  9705. BigInteger.prototype.modInverse = bnModInverse;
  9706. BigInteger.prototype.pow = bnPow;
  9707. BigInteger.prototype.gcd = bnGCD;
  9708. BigInteger.prototype.isProbablePrime = bnIsProbablePrime;
  9709. BigInteger.prototype.toMPI = bnToMPI;
  9710. // JSBN-specific extension
  9711. BigInteger.prototype.square = bnSquare;
  9712. },{"../../util.js":70}],30:[function(_dereq_,module,exports){
  9713. // GPG4Browsers - An OpenPGP implementation in javascript
  9714. // Copyright (C) 2011 Recurity Labs GmbH
  9715. //
  9716. // This library is free software; you can redistribute it and/or
  9717. // modify it under the terms of the GNU Lesser General Public
  9718. // License as published by the Free Software Foundation; either
  9719. // version 3.0 of the License, or (at your option) any later version.
  9720. //
  9721. // This library is distributed in the hope that it will be useful,
  9722. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9723. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9724. // Lesser General Public License for more details.
  9725. //
  9726. // You should have received a copy of the GNU Lesser General Public
  9727. // License along with this library; if not, write to the Free Software
  9728. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  9729. //
  9730. // RSA implementation
  9731. /**
  9732. * @requires crypto/public_key/jsbn
  9733. * @requires crypto/random
  9734. * @requires util
  9735. * @module crypto/public_key/rsa
  9736. */
  9737. 'use strict';
  9738. Object.defineProperty(exports, "__esModule", {
  9739. value: true
  9740. });
  9741. exports.default = RSA;
  9742. var _jsbn = _dereq_('./jsbn.js');
  9743. var _jsbn2 = _interopRequireDefault(_jsbn);
  9744. var _util = _dereq_('../../util.js');
  9745. var _util2 = _interopRequireDefault(_util);
  9746. var _random = _dereq_('../random.js');
  9747. var _random2 = _interopRequireDefault(_random);
  9748. var _config = _dereq_('../../config');
  9749. var _config2 = _interopRequireDefault(_config);
  9750. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9751. function SecureRandom() {
  9752. function nextBytes(byteArray) {
  9753. for (var n = 0; n < byteArray.length; n++) {
  9754. byteArray[n] = _random2.default.getSecureRandomOctet();
  9755. }
  9756. }
  9757. this.nextBytes = nextBytes;
  9758. }
  9759. var blinder = _jsbn2.default.ZERO;
  9760. var unblinder = _jsbn2.default.ZERO;
  9761. function blind(m, n, e) {
  9762. if (unblinder.bitLength() === n.bitLength()) {
  9763. unblinder = unblinder.square().mod(n);
  9764. } else {
  9765. unblinder = _random2.default.getRandomBigIntegerInRange(_jsbn2.default.TWO, n);
  9766. }
  9767. blinder = unblinder.modInverse(n).modPow(e, n);
  9768. return m.multiply(blinder).mod(n);
  9769. }
  9770. function unblind(t, n) {
  9771. return t.multiply(unblinder).mod(n);
  9772. }
  9773. function RSA() {
  9774. /**
  9775. * This function uses jsbn Big Num library to decrypt RSA
  9776. * @param m
  9777. * message
  9778. * @param n
  9779. * RSA public modulus n as BigInteger
  9780. * @param e
  9781. * RSA public exponent as BigInteger
  9782. * @param d
  9783. * RSA d as BigInteger
  9784. * @param p
  9785. * RSA p as BigInteger
  9786. * @param q
  9787. * RSA q as BigInteger
  9788. * @param u
  9789. * RSA u as BigInteger
  9790. * @return {BigInteger} The decrypted value of the message
  9791. */
  9792. function decrypt(m, n, e, d, p, q, u) {
  9793. if (_config2.default.rsa_blinding) {
  9794. m = blind(m, n, e);
  9795. }
  9796. var xp = m.mod(p).modPow(d.mod(p.subtract(_jsbn2.default.ONE)), p);
  9797. var xq = m.mod(q).modPow(d.mod(q.subtract(_jsbn2.default.ONE)), q);
  9798. _util2.default.print_debug("rsa.js decrypt\nxpn:" + _util2.default.hexstrdump(xp.toMPI()) + "\nxqn:" + _util2.default.hexstrdump(xq.toMPI()));
  9799. var t = xq.subtract(xp);
  9800. if (t[0] === 0) {
  9801. t = xp.subtract(xq);
  9802. t = t.multiply(u).mod(q);
  9803. t = q.subtract(t);
  9804. } else {
  9805. t = t.multiply(u).mod(q);
  9806. }
  9807. t = t.multiply(p).add(xp);
  9808. if (_config2.default.rsa_blinding) {
  9809. t = unblind(t, n);
  9810. }
  9811. return t;
  9812. }
  9813. /**
  9814. * encrypt message
  9815. * @param m message as BigInteger
  9816. * @param e public MPI part as BigInteger
  9817. * @param n public MPI part as BigInteger
  9818. * @return BigInteger
  9819. */
  9820. function encrypt(m, e, n) {
  9821. return m.modPowInt(e, n);
  9822. }
  9823. /* Sign and Verify */
  9824. function sign(m, d, n) {
  9825. return m.modPow(d, n);
  9826. }
  9827. function verify(x, e, n) {
  9828. return x.modPowInt(e, n);
  9829. }
  9830. // "empty" RSA key constructor
  9831. function KeyObject() {
  9832. this.n = null;
  9833. this.e = 0;
  9834. this.ee = null;
  9835. this.d = null;
  9836. this.p = null;
  9837. this.q = null;
  9838. this.dmp1 = null;
  9839. this.dmq1 = null;
  9840. this.u = null;
  9841. }
  9842. // Generate a new random private key B bits long, using public expt E
  9843. function generate(B, E) {
  9844. var webCrypto = _util2.default.getWebCryptoAll();
  9845. //
  9846. // Native RSA keygen using Web Crypto
  9847. //
  9848. if (webCrypto) {
  9849. var Euint32 = new Uint32Array([parseInt(E, 16)]); // get integer of exponent
  9850. var Euint8 = new Uint8Array(Euint32.buffer); // get bytes of exponent
  9851. var keyGenOpt;
  9852. var keys;
  9853. if (window.crypto && window.crypto.webkitSubtle) {
  9854. // outdated spec implemented by Webkit
  9855. keyGenOpt = {
  9856. name: 'RSA-OAEP',
  9857. modulusLength: B, // the specified keysize in bits
  9858. publicExponent: Euint8.subarray(0, 3) // take three bytes (max 65537)
  9859. };
  9860. keys = webCrypto.generateKey(keyGenOpt, true, ['encrypt', 'decrypt']);
  9861. } else {
  9862. // current standard spec
  9863. keyGenOpt = {
  9864. name: 'RSASSA-PKCS1-v1_5',
  9865. modulusLength: B, // the specified keysize in bits
  9866. publicExponent: Euint8.subarray(0, 3), // take three bytes (max 65537)
  9867. hash: {
  9868. name: 'SHA-1' // not required for actual RSA keys, but for crypto api 'sign' and 'verify'
  9869. }
  9870. };
  9871. keys = webCrypto.generateKey(keyGenOpt, true, ['sign', 'verify']);
  9872. if (typeof keys.then !== 'function') {
  9873. // IE11 KeyOperation
  9874. keys = _util2.default.promisifyIE11Op(keys, 'Error generating RSA key pair.');
  9875. }
  9876. }
  9877. return keys.then(exportKey).then(function (key) {
  9878. if (key instanceof ArrayBuffer) {
  9879. // parse raw ArrayBuffer bytes to jwk/json (WebKit/Safari/IE11 quirk)
  9880. return decodeKey(JSON.parse(String.fromCharCode.apply(null, new Uint8Array(key))));
  9881. }
  9882. return decodeKey(key);
  9883. });
  9884. }
  9885. function exportKey(keypair) {
  9886. // export the generated keys as JsonWebKey (JWK)
  9887. // https://tools.ietf.org/html/draft-ietf-jose-json-web-key-33
  9888. var key = webCrypto.exportKey('jwk', keypair.privateKey);
  9889. if (typeof key.then !== 'function') {
  9890. // IE11 KeyOperation
  9891. key = _util2.default.promisifyIE11Op(key, 'Error exporting RSA key pair.');
  9892. }
  9893. return key;
  9894. }
  9895. function decodeKey(jwk) {
  9896. // map JWK parameters to local BigInteger type system
  9897. var key = new KeyObject();
  9898. key.n = toBigInteger(jwk.n);
  9899. key.ee = new _jsbn2.default(E, 16);
  9900. key.d = toBigInteger(jwk.d);
  9901. key.p = toBigInteger(jwk.p);
  9902. key.q = toBigInteger(jwk.q);
  9903. key.u = key.p.modInverse(key.q);
  9904. function toBigInteger(base64url) {
  9905. var base64 = base64url.replace(/\-/g, '+').replace(/_/g, '/');
  9906. var hex = _util2.default.hexstrdump(atob(base64));
  9907. return new _jsbn2.default(hex, 16);
  9908. }
  9909. return key;
  9910. }
  9911. //
  9912. // JS code
  9913. //
  9914. return new Promise(function (resolve) {
  9915. var key = new KeyObject();
  9916. var rng = new SecureRandom();
  9917. var qs = B >> 1;
  9918. key.e = parseInt(E, 16);
  9919. key.ee = new _jsbn2.default(E, 16);
  9920. for (;;) {
  9921. for (;;) {
  9922. key.p = new _jsbn2.default(B - qs, 1, rng);
  9923. if (key.p.subtract(_jsbn2.default.ONE).gcd(key.ee).compareTo(_jsbn2.default.ONE) === 0 && key.p.isProbablePrime(10)) {
  9924. break;
  9925. }
  9926. }
  9927. for (;;) {
  9928. key.q = new _jsbn2.default(qs, 1, rng);
  9929. if (key.q.subtract(_jsbn2.default.ONE).gcd(key.ee).compareTo(_jsbn2.default.ONE) === 0 && key.q.isProbablePrime(10)) {
  9930. break;
  9931. }
  9932. }
  9933. if (key.p.compareTo(key.q) <= 0) {
  9934. var t = key.p;
  9935. key.p = key.q;
  9936. key.q = t;
  9937. }
  9938. var p1 = key.p.subtract(_jsbn2.default.ONE);
  9939. var q1 = key.q.subtract(_jsbn2.default.ONE);
  9940. var phi = p1.multiply(q1);
  9941. if (phi.gcd(key.ee).compareTo(_jsbn2.default.ONE) === 0) {
  9942. key.n = key.p.multiply(key.q);
  9943. key.d = key.ee.modInverse(phi);
  9944. key.dmp1 = key.d.mod(p1);
  9945. key.dmq1 = key.d.mod(q1);
  9946. key.u = key.p.modInverse(key.q);
  9947. break;
  9948. }
  9949. }
  9950. resolve(key);
  9951. });
  9952. }
  9953. this.encrypt = encrypt;
  9954. this.decrypt = decrypt;
  9955. this.verify = verify;
  9956. this.sign = sign;
  9957. this.generate = generate;
  9958. this.keyObject = KeyObject;
  9959. }
  9960. },{"../../config":10,"../../util.js":70,"../random.js":31,"./jsbn.js":29}],31:[function(_dereq_,module,exports){
  9961. // GPG4Browsers - An OpenPGP implementation in javascript
  9962. // Copyright (C) 2011 Recurity Labs GmbH
  9963. //
  9964. // This library is free software; you can redistribute it and/or
  9965. // modify it under the terms of the GNU Lesser General Public
  9966. // License as published by the Free Software Foundation; either
  9967. // version 3.0 of the License, or (at your option) any later version.
  9968. //
  9969. // This library is distributed in the hope that it will be useful,
  9970. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9971. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  9972. // Lesser General Public License for more details.
  9973. //
  9974. // You should have received a copy of the GNU Lesser General Public
  9975. // License along with this library; if not, write to the Free Software
  9976. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  9977. // The GPG4Browsers crypto interface
  9978. /**
  9979. * @requires type/mpi
  9980. * @requires util
  9981. * @module crypto/random
  9982. */
  9983. 'use strict';
  9984. Object.defineProperty(exports, "__esModule", {
  9985. value: true
  9986. });
  9987. var _typeof = typeof Symbol === "function" && typeof Symbol.iterator === "symbol" ? function (obj) { return typeof obj; } : function (obj) { return obj && typeof Symbol === "function" && obj.constructor === Symbol ? "symbol" : typeof obj; };
  9988. var _mpi = _dereq_('../type/mpi.js');
  9989. var _mpi2 = _interopRequireDefault(_mpi);
  9990. var _util = _dereq_('../util.js');
  9991. var _util2 = _interopRequireDefault(_util);
  9992. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9993. var nodeCrypto = _util2.default.detectNode() && _dereq_('crypto');
  9994. exports.default = {
  9995. /**
  9996. * Retrieve secure random byte array of the specified length
  9997. * @param {Integer} length Length in bytes to generate
  9998. * @return {Uint8Array} Random byte array
  9999. */
  10000. getRandomBytes: function getRandomBytes(length) {
  10001. var result = new Uint8Array(length);
  10002. for (var i = 0; i < length; i++) {
  10003. result[i] = this.getSecureRandomOctet();
  10004. }
  10005. return result;
  10006. },
  10007. /**
  10008. * Return a secure random number in the specified range
  10009. * @param {Integer} from Min of the random number
  10010. * @param {Integer} to Max of the random number (max 32bit)
  10011. * @return {Integer} A secure random number
  10012. */
  10013. getSecureRandom: function getSecureRandom(from, to) {
  10014. var randUint = this.getSecureRandomUint();
  10015. var bits = (to - from).toString(2).length;
  10016. while ((randUint & Math.pow(2, bits) - 1) > to - from) {
  10017. randUint = this.getSecureRandomUint();
  10018. }
  10019. return from + Math.abs(randUint & Math.pow(2, bits) - 1);
  10020. },
  10021. getSecureRandomOctet: function getSecureRandomOctet() {
  10022. var buf = new Uint8Array(1);
  10023. this.getRandomValues(buf);
  10024. return buf[0];
  10025. },
  10026. getSecureRandomUint: function getSecureRandomUint() {
  10027. var buf = new Uint8Array(4);
  10028. var dv = new DataView(buf.buffer);
  10029. this.getRandomValues(buf);
  10030. return dv.getUint32(0);
  10031. },
  10032. /**
  10033. * Helper routine which calls platform specific crypto random generator
  10034. * @param {Uint8Array} buf
  10035. */
  10036. getRandomValues: function getRandomValues(buf) {
  10037. if (!(buf instanceof Uint8Array)) {
  10038. throw new Error('Invalid type: buf not an Uint8Array');
  10039. }
  10040. if (typeof window !== 'undefined' && window.crypto && window.crypto.getRandomValues) {
  10041. window.crypto.getRandomValues(buf);
  10042. } else if (typeof window !== 'undefined' && _typeof(window.msCrypto) === 'object' && typeof window.msCrypto.getRandomValues === 'function') {
  10043. window.msCrypto.getRandomValues(buf);
  10044. } else if (nodeCrypto) {
  10045. var bytes = nodeCrypto.randomBytes(buf.length);
  10046. buf.set(bytes);
  10047. } else if (this.randomBuffer.buffer) {
  10048. this.randomBuffer.get(buf);
  10049. } else {
  10050. throw new Error('No secure random number generator available.');
  10051. }
  10052. return buf;
  10053. },
  10054. /**
  10055. * Create a secure random big integer of bits length
  10056. * @param {Integer} bits Bit length of the MPI to create
  10057. * @return {BigInteger} Resulting big integer
  10058. */
  10059. getRandomBigInteger: function getRandomBigInteger(bits) {
  10060. if (bits < 1) {
  10061. throw new Error('Illegal parameter value: bits < 1');
  10062. }
  10063. var numBytes = Math.floor((bits + 7) / 8);
  10064. var randomBits = _util2.default.Uint8Array2str(this.getRandomBytes(numBytes));
  10065. if (bits % 8 > 0) {
  10066. randomBits = String.fromCharCode(Math.pow(2, bits % 8) - 1 & randomBits.charCodeAt(0)) + randomBits.substring(1);
  10067. }
  10068. var mpi = new _mpi2.default();
  10069. mpi.fromBytes(randomBits);
  10070. return mpi.toBigInteger();
  10071. },
  10072. getRandomBigIntegerInRange: function getRandomBigIntegerInRange(min, max) {
  10073. if (max.compareTo(min) <= 0) {
  10074. throw new Error('Illegal parameter value: max <= min');
  10075. }
  10076. var range = max.subtract(min);
  10077. var r = this.getRandomBigInteger(range.bitLength());
  10078. while (r.compareTo(range) > 0) {
  10079. r = this.getRandomBigInteger(range.bitLength());
  10080. }
  10081. return min.add(r);
  10082. },
  10083. randomBuffer: new RandomBuffer()
  10084. };
  10085. /**
  10086. * Buffer for secure random numbers
  10087. */
  10088. function RandomBuffer() {
  10089. this.buffer = null;
  10090. this.size = null;
  10091. }
  10092. /**
  10093. * Initialize buffer
  10094. * @param {Integer} size size of buffer
  10095. */
  10096. RandomBuffer.prototype.init = function (size) {
  10097. this.buffer = new Uint8Array(size);
  10098. this.size = 0;
  10099. };
  10100. /**
  10101. * Concat array of secure random numbers to buffer
  10102. * @param {Uint8Array} buf
  10103. */
  10104. RandomBuffer.prototype.set = function (buf) {
  10105. if (!this.buffer) {
  10106. throw new Error('RandomBuffer is not initialized');
  10107. }
  10108. if (!(buf instanceof Uint8Array)) {
  10109. throw new Error('Invalid type: buf not an Uint8Array');
  10110. }
  10111. var freeSpace = this.buffer.length - this.size;
  10112. if (buf.length > freeSpace) {
  10113. buf = buf.subarray(0, freeSpace);
  10114. }
  10115. // set buf with offset old size of buffer
  10116. this.buffer.set(buf, this.size);
  10117. this.size += buf.length;
  10118. };
  10119. /**
  10120. * Take numbers out of buffer and copy to array
  10121. * @param {Uint8Array} buf the destination array
  10122. */
  10123. RandomBuffer.prototype.get = function (buf) {
  10124. if (!this.buffer) {
  10125. throw new Error('RandomBuffer is not initialized');
  10126. }
  10127. if (!(buf instanceof Uint8Array)) {
  10128. throw new Error('Invalid type: buf not an Uint8Array');
  10129. }
  10130. if (this.size < buf.length) {
  10131. throw new Error('Random number buffer depleted');
  10132. }
  10133. for (var i = 0; i < buf.length; i++) {
  10134. buf[i] = this.buffer[--this.size];
  10135. // clear buffer value
  10136. this.buffer[this.size] = 0;
  10137. }
  10138. };
  10139. },{"../type/mpi.js":68,"../util.js":70,"crypto":"crypto"}],32:[function(_dereq_,module,exports){
  10140. /**
  10141. * @requires util
  10142. * @requires crypto/hash
  10143. * @requires crypto/pkcs1
  10144. * @requires crypto/public_key
  10145. * @module crypto/signature */
  10146. 'use strict';
  10147. Object.defineProperty(exports, "__esModule", {
  10148. value: true
  10149. });
  10150. var _util = _dereq_('../util');
  10151. var _util2 = _interopRequireDefault(_util);
  10152. var _public_key = _dereq_('./public_key');
  10153. var _public_key2 = _interopRequireDefault(_public_key);
  10154. var _pkcs = _dereq_('./pkcs1.js');
  10155. var _pkcs2 = _interopRequireDefault(_pkcs);
  10156. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10157. exports.default = {
  10158. /**
  10159. *
  10160. * @param {module:enums.publicKey} algo public Key algorithm
  10161. * @param {module:enums.hash} hash_algo Hash algorithm
  10162. * @param {Array<module:type/mpi>} msg_MPIs Signature multiprecision integers
  10163. * @param {Array<module:type/mpi>} publickey_MPIs Public key multiprecision integers
  10164. * @param {Uint8Array} data Data on where the signature was computed on.
  10165. * @return {Boolean} true if signature (sig_data was equal to data over hash)
  10166. */
  10167. verify: function verify(algo, hash_algo, msg_MPIs, publickey_MPIs, data) {
  10168. var m;
  10169. data = _util2.default.Uint8Array2str(data);
  10170. switch (algo) {
  10171. case 1:
  10172. // RSA (Encrypt or Sign) [HAC]
  10173. case 2:
  10174. // RSA Encrypt-Only [HAC]
  10175. case 3:
  10176. // RSA Sign-Only [HAC]
  10177. var rsa = new _public_key2.default.rsa();
  10178. var n = publickey_MPIs[0].toBigInteger();
  10179. var k = publickey_MPIs[0].byteLength();
  10180. var e = publickey_MPIs[1].toBigInteger();
  10181. m = msg_MPIs[0].toBigInteger();
  10182. var EM = rsa.verify(m, e, n);
  10183. var EM2 = _pkcs2.default.emsa.encode(hash_algo, data, k);
  10184. return EM.compareTo(EM2) === 0;
  10185. case 16:
  10186. // Elgamal (Encrypt-Only) [ELGAMAL] [HAC]
  10187. throw new Error("signing with Elgamal is not defined in the OpenPGP standard.");
  10188. case 17:
  10189. // DSA (Digital Signature Algorithm) [FIPS186] [HAC]
  10190. var dsa = new _public_key2.default.dsa();
  10191. var s1 = msg_MPIs[0].toBigInteger();
  10192. var s2 = msg_MPIs[1].toBigInteger();
  10193. var p = publickey_MPIs[0].toBigInteger();
  10194. var q = publickey_MPIs[1].toBigInteger();
  10195. var g = publickey_MPIs[2].toBigInteger();
  10196. var y = publickey_MPIs[3].toBigInteger();
  10197. m = data;
  10198. var dopublic = dsa.verify(hash_algo, s1, s2, m, p, q, g, y);
  10199. return dopublic.compareTo(s1) === 0;
  10200. default:
  10201. throw new Error('Invalid signature algorithm.');
  10202. }
  10203. },
  10204. /**
  10205. * Create a signature on data using the specified algorithm
  10206. * @param {module:enums.hash} hash_algo hash Algorithm to use (See {@link http://tools.ietf.org/html/rfc4880#section-9.4|RFC 4880 9.4})
  10207. * @param {module:enums.publicKey} algo Asymmetric cipher algorithm to use (See {@link http://tools.ietf.org/html/rfc4880#section-9.1|RFC 4880 9.1})
  10208. * @param {Array<module:type/mpi>} publicMPIs Public key multiprecision integers
  10209. * of the private key
  10210. * @param {Array<module:type/mpi>} secretMPIs Private key multiprecision
  10211. * integers which is used to sign the data
  10212. * @param {Uint8Array} data Data to be signed
  10213. * @return {Array<module:type/mpi>}
  10214. */
  10215. sign: function sign(hash_algo, algo, keyIntegers, data) {
  10216. data = _util2.default.Uint8Array2str(data);
  10217. var m;
  10218. switch (algo) {
  10219. case 1:
  10220. // RSA (Encrypt or Sign) [HAC]
  10221. case 2:
  10222. // RSA Encrypt-Only [HAC]
  10223. case 3:
  10224. // RSA Sign-Only [HAC]
  10225. var rsa = new _public_key2.default.rsa();
  10226. var d = keyIntegers[2].toBigInteger();
  10227. var n = keyIntegers[0].toBigInteger();
  10228. m = _pkcs2.default.emsa.encode(hash_algo, data, keyIntegers[0].byteLength());
  10229. return _util2.default.str2Uint8Array(rsa.sign(m, d, n).toMPI());
  10230. case 17:
  10231. // DSA (Digital Signature Algorithm) [FIPS186] [HAC]
  10232. var dsa = new _public_key2.default.dsa();
  10233. var p = keyIntegers[0].toBigInteger();
  10234. var q = keyIntegers[1].toBigInteger();
  10235. var g = keyIntegers[2].toBigInteger();
  10236. var x = keyIntegers[4].toBigInteger();
  10237. m = data;
  10238. var result = dsa.sign(hash_algo, m, g, p, q, x);
  10239. return _util2.default.str2Uint8Array(result[0].toString() + result[1].toString());
  10240. case 16:
  10241. // Elgamal (Encrypt-Only) [ELGAMAL] [HAC]
  10242. throw new Error('Signing with Elgamal is not defined in the OpenPGP standard.');
  10243. default:
  10244. throw new Error('Invalid signature algorithm.');
  10245. }
  10246. }
  10247. };
  10248. },{"../util":70,"./pkcs1.js":25,"./public_key":28}],33:[function(_dereq_,module,exports){
  10249. // GPG4Browsers - An OpenPGP implementation in javascript
  10250. // Copyright (C) 2011 Recurity Labs GmbH
  10251. //
  10252. // This library is free software; you can redistribute it and/or
  10253. // modify it under the terms of the GNU Lesser General Public
  10254. // License as published by the Free Software Foundation; either
  10255. // version 3.0 of the License, or (at your option) any later version.
  10256. //
  10257. // This library is distributed in the hope that it will be useful,
  10258. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  10259. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10260. // Lesser General Public License for more details.
  10261. //
  10262. // You should have received a copy of the GNU Lesser General Public
  10263. // License along with this library; if not, write to the Free Software
  10264. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  10265. /**
  10266. * @requires encoding/base64
  10267. * @requires enums
  10268. * @requires config
  10269. * @module encoding/armor
  10270. */
  10271. 'use strict';
  10272. Object.defineProperty(exports, "__esModule", {
  10273. value: true
  10274. });
  10275. var _base = _dereq_('./base64.js');
  10276. var _base2 = _interopRequireDefault(_base);
  10277. var _enums = _dereq_('../enums.js');
  10278. var _enums2 = _interopRequireDefault(_enums);
  10279. var _config = _dereq_('../config');
  10280. var _config2 = _interopRequireDefault(_config);
  10281. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10282. /**
  10283. * Finds out which Ascii Armoring type is used. Throws error if unknown type.
  10284. * @private
  10285. * @param {String} text [String] ascii armored text
  10286. * @returns {Integer} 0 = MESSAGE PART n of m
  10287. * 1 = MESSAGE PART n
  10288. * 2 = SIGNED MESSAGE
  10289. * 3 = PGP MESSAGE
  10290. * 4 = PUBLIC KEY BLOCK
  10291. * 5 = PRIVATE KEY BLOCK
  10292. * 6 = SIGNATURE
  10293. */
  10294. function getType(text) {
  10295. var reHeader = /^-----BEGIN PGP (MESSAGE, PART \d+\/\d+|MESSAGE, PART \d+|SIGNED MESSAGE|MESSAGE|PUBLIC KEY BLOCK|PRIVATE KEY BLOCK|SIGNATURE)-----$\n/m;
  10296. var header = text.match(reHeader);
  10297. if (!header) {
  10298. throw new Error('Unknown ASCII armor type');
  10299. }
  10300. // BEGIN PGP MESSAGE, PART X/Y
  10301. // Used for multi-part messages, where the armor is split amongst Y
  10302. // parts, and this is the Xth part out of Y.
  10303. if (/MESSAGE, PART \d+\/\d+/.test(header[1])) {
  10304. return _enums2.default.armor.multipart_section;
  10305. } else
  10306. // BEGIN PGP MESSAGE, PART X
  10307. // Used for multi-part messages, where this is the Xth part of an
  10308. // unspecified number of parts. Requires the MESSAGE-ID Armor
  10309. // Header to be used.
  10310. if (/MESSAGE, PART \d+/.test(header[1])) {
  10311. return _enums2.default.armor.multipart_last;
  10312. } else
  10313. // BEGIN PGP SIGNED MESSAGE
  10314. if (/SIGNED MESSAGE/.test(header[1])) {
  10315. return _enums2.default.armor.signed;
  10316. } else
  10317. // BEGIN PGP MESSAGE
  10318. // Used for signed, encrypted, or compressed files.
  10319. if (/MESSAGE/.test(header[1])) {
  10320. return _enums2.default.armor.message;
  10321. } else
  10322. // BEGIN PGP PUBLIC KEY BLOCK
  10323. // Used for armoring public keys.
  10324. if (/PUBLIC KEY BLOCK/.test(header[1])) {
  10325. return _enums2.default.armor.public_key;
  10326. } else
  10327. // BEGIN PGP PRIVATE KEY BLOCK
  10328. // Used for armoring private keys.
  10329. if (/PRIVATE KEY BLOCK/.test(header[1])) {
  10330. return _enums2.default.armor.private_key;
  10331. } else
  10332. // BEGIN PGP SIGNATURE
  10333. // Used for detached signatures, OpenPGP/MIME signatures, and
  10334. // cleartext signatures. Note that PGP 2.x uses BEGIN PGP MESSAGE
  10335. // for detached signatures.
  10336. if (/SIGNATURE/.test(header[1])) {
  10337. return _enums2.default.armor.signature;
  10338. }
  10339. }
  10340. /**
  10341. * Add additional information to the armor version of an OpenPGP binary
  10342. * packet block.
  10343. * @author Alex
  10344. * @version 2011-12-16
  10345. * @returns {String} The header information
  10346. */
  10347. function addheader() {
  10348. var result = "";
  10349. if (_config2.default.show_version) {
  10350. result += "Version: " + _config2.default.versionstring + '\r\n';
  10351. }
  10352. if (_config2.default.show_comment) {
  10353. result += "Comment: " + _config2.default.commentstring + '\r\n';
  10354. }
  10355. result += '\r\n';
  10356. return result;
  10357. }
  10358. /**
  10359. * Calculates a checksum over the given data and returns it base64 encoded
  10360. * @param {String} data Data to create a CRC-24 checksum for
  10361. * @return {String} Base64 encoded checksum
  10362. */
  10363. function getCheckSum(data) {
  10364. var c = createcrc24(data);
  10365. var bytes = new Uint8Array([c >> 16, c >> 8 & 0xFF, c & 0xFF]);
  10366. return _base2.default.encode(bytes);
  10367. }
  10368. /**
  10369. * Calculates the checksum over the given data and compares it with the
  10370. * given base64 encoded checksum
  10371. * @param {String} data Data to create a CRC-24 checksum for
  10372. * @param {String} checksum Base64 encoded checksum
  10373. * @return {Boolean} True if the given checksum is correct; otherwise false
  10374. */
  10375. function verifyCheckSum(data, checksum) {
  10376. var c = getCheckSum(data);
  10377. var d = checksum;
  10378. return c[0] === d[0] && c[1] === d[1] && c[2] === d[2] && c[3] === d[3];
  10379. }
  10380. /**
  10381. * Internal function to calculate a CRC-24 checksum over a given string (data)
  10382. * @param {String} data Data to create a CRC-24 checksum for
  10383. * @return {Integer} The CRC-24 checksum as number
  10384. */
  10385. var crc_table = [0x00000000, 0x00864cfb, 0x018ad50d, 0x010c99f6, 0x0393e6e1, 0x0315aa1a, 0x021933ec, 0x029f7f17, 0x07a18139, 0x0727cdc2, 0x062b5434, 0x06ad18cf, 0x043267d8, 0x04b42b23, 0x05b8b2d5, 0x053efe2e, 0x0fc54e89, 0x0f430272, 0x0e4f9b84, 0x0ec9d77f, 0x0c56a868, 0x0cd0e493, 0x0ddc7d65, 0x0d5a319e, 0x0864cfb0, 0x08e2834b, 0x09ee1abd, 0x09685646, 0x0bf72951, 0x0b7165aa, 0x0a7dfc5c, 0x0afbb0a7, 0x1f0cd1e9, 0x1f8a9d12, 0x1e8604e4, 0x1e00481f, 0x1c9f3708, 0x1c197bf3, 0x1d15e205, 0x1d93aefe, 0x18ad50d0, 0x182b1c2b, 0x192785dd, 0x19a1c926, 0x1b3eb631, 0x1bb8faca, 0x1ab4633c, 0x1a322fc7, 0x10c99f60, 0x104fd39b, 0x11434a6d, 0x11c50696, 0x135a7981, 0x13dc357a, 0x12d0ac8c, 0x1256e077, 0x17681e59, 0x17ee52a2, 0x16e2cb54, 0x166487af, 0x14fbf8b8, 0x147db443, 0x15712db5, 0x15f7614e, 0x3e19a3d2, 0x3e9fef29, 0x3f9376df, 0x3f153a24, 0x3d8a4533, 0x3d0c09c8, 0x3c00903e, 0x3c86dcc5, 0x39b822eb, 0x393e6e10, 0x3832f7e6, 0x38b4bb1d, 0x3a2bc40a, 0x3aad88f1, 0x3ba11107, 0x3b275dfc, 0x31dced5b, 0x315aa1a0, 0x30563856, 0x30d074ad, 0x324f0bba, 0x32c94741, 0x33c5deb7, 0x3343924c, 0x367d6c62, 0x36fb2099, 0x37f7b96f, 0x3771f594, 0x35ee8a83, 0x3568c678, 0x34645f8e, 0x34e21375, 0x2115723b, 0x21933ec0, 0x209fa736, 0x2019ebcd, 0x228694da, 0x2200d821, 0x230c41d7, 0x238a0d2c, 0x26b4f302, 0x2632bff9, 0x273e260f, 0x27b86af4, 0x252715e3, 0x25a15918, 0x24adc0ee, 0x242b8c15, 0x2ed03cb2, 0x2e567049, 0x2f5ae9bf, 0x2fdca544, 0x2d43da53, 0x2dc596a8, 0x2cc90f5e, 0x2c4f43a5, 0x2971bd8b, 0x29f7f170, 0x28fb6886, 0x287d247d, 0x2ae25b6a, 0x2a641791, 0x2b688e67, 0x2beec29c, 0x7c3347a4, 0x7cb50b5f, 0x7db992a9, 0x7d3fde52, 0x7fa0a145, 0x7f26edbe, 0x7e2a7448, 0x7eac38b3, 0x7b92c69d, 0x7b148a66, 0x7a181390, 0x7a9e5f6b, 0x7801207c, 0x78876c87, 0x798bf571, 0x790db98a, 0x73f6092d, 0x737045d6, 0x727cdc20, 0x72fa90db, 0x7065efcc, 0x70e3a337, 0x71ef3ac1, 0x7169763a, 0x74578814, 0x74d1c4ef, 0x75dd5d19, 0x755b11e2, 0x77c46ef5, 0x7742220e, 0x764ebbf8, 0x76c8f703, 0x633f964d, 0x63b9dab6, 0x62b54340, 0x62330fbb, 0x60ac70ac, 0x602a3c57, 0x6126a5a1, 0x61a0e95a, 0x649e1774, 0x64185b8f, 0x6514c279, 0x65928e82, 0x670df195, 0x678bbd6e, 0x66872498, 0x66016863, 0x6cfad8c4, 0x6c7c943f, 0x6d700dc9, 0x6df64132, 0x6f693e25, 0x6fef72de, 0x6ee3eb28, 0x6e65a7d3, 0x6b5b59fd, 0x6bdd1506, 0x6ad18cf0, 0x6a57c00b, 0x68c8bf1c, 0x684ef3e7, 0x69426a11, 0x69c426ea, 0x422ae476, 0x42aca88d, 0x43a0317b, 0x43267d80, 0x41b90297, 0x413f4e6c, 0x4033d79a, 0x40b59b61, 0x458b654f, 0x450d29b4, 0x4401b042, 0x4487fcb9, 0x461883ae, 0x469ecf55, 0x479256a3, 0x47141a58, 0x4defaaff, 0x4d69e604, 0x4c657ff2, 0x4ce33309, 0x4e7c4c1e, 0x4efa00e5, 0x4ff69913, 0x4f70d5e8, 0x4a4e2bc6, 0x4ac8673d, 0x4bc4fecb, 0x4b42b230, 0x49ddcd27, 0x495b81dc, 0x4857182a, 0x48d154d1, 0x5d26359f, 0x5da07964, 0x5cace092, 0x5c2aac69, 0x5eb5d37e, 0x5e339f85, 0x5f3f0673, 0x5fb94a88, 0x5a87b4a6, 0x5a01f85d, 0x5b0d61ab, 0x5b8b2d50, 0x59145247, 0x59921ebc, 0x589e874a, 0x5818cbb1, 0x52e37b16, 0x526537ed, 0x5369ae1b, 0x53efe2e0, 0x51709df7, 0x51f6d10c, 0x50fa48fa, 0x507c0401, 0x5542fa2f, 0x55c4b6d4, 0x54c82f22, 0x544e63d9, 0x56d11cce, 0x56575035, 0x575bc9c3, 0x57dd8538];
  10386. function createcrc24(input) {
  10387. var crc = 0xB704CE;
  10388. var index = 0;
  10389. while (input.length - index > 16) {
  10390. crc = crc << 8 ^ crc_table[(crc >> 16 ^ input[index]) & 0xff];
  10391. crc = crc << 8 ^ crc_table[(crc >> 16 ^ input[index + 1]) & 0xff];
  10392. crc = crc << 8 ^ crc_table[(crc >> 16 ^ input[index + 2]) & 0xff];
  10393. crc = crc << 8 ^ crc_table[(crc >> 16 ^ input[index + 3]) & 0xff];
  10394. crc = crc << 8 ^ crc_table[(crc >> 16 ^ input[index + 4]) & 0xff];
  10395. crc = crc << 8 ^ crc_table[(crc >> 16 ^ input[index + 5]) & 0xff];
  10396. crc = crc << 8 ^ crc_table[(crc >> 16 ^ input[index + 6]) & 0xff];
  10397. crc = crc << 8 ^ crc_table[(crc >> 16 ^ input[index + 7]) & 0xff];
  10398. crc = crc << 8 ^ crc_table[(crc >> 16 ^ input[index + 8]) & 0xff];
  10399. crc = crc << 8 ^ crc_table[(crc >> 16 ^ input[index + 9]) & 0xff];
  10400. crc = crc << 8 ^ crc_table[(crc >> 16 ^ input[index + 10]) & 0xff];
  10401. crc = crc << 8 ^ crc_table[(crc >> 16 ^ input[index + 11]) & 0xff];
  10402. crc = crc << 8 ^ crc_table[(crc >> 16 ^ input[index + 12]) & 0xff];
  10403. crc = crc << 8 ^ crc_table[(crc >> 16 ^ input[index + 13]) & 0xff];
  10404. crc = crc << 8 ^ crc_table[(crc >> 16 ^ input[index + 14]) & 0xff];
  10405. crc = crc << 8 ^ crc_table[(crc >> 16 ^ input[index + 15]) & 0xff];
  10406. index += 16;
  10407. }
  10408. for (var j = index; j < input.length; j++) {
  10409. crc = crc << 8 ^ crc_table[(crc >> 16 ^ input[index++]) & 0xff];
  10410. }
  10411. return crc & 0xffffff;
  10412. }
  10413. /**
  10414. * Splits a message into two parts, the headers and the body. This is an internal function
  10415. * @param {String} text OpenPGP armored message part
  10416. * @returns {(Boolean|Object)} Either false in case of an error
  10417. * or an object with attribute "headers" containing the headers and
  10418. * and an attribute "body" containing the body.
  10419. */
  10420. function splitHeaders(text) {
  10421. // empty line with whitespace characters
  10422. var reEmptyLine = /^[ \f\r\t\u00a0\u2000-\u200a\u202f\u205f\u3000]*\n/m;
  10423. var headers = '';
  10424. var body = text;
  10425. var matchResult = reEmptyLine.exec(text);
  10426. if (matchResult !== null) {
  10427. headers = text.slice(0, matchResult.index);
  10428. body = text.slice(matchResult.index + matchResult[0].length);
  10429. } else {
  10430. throw new Error('Mandatory blank line missing between armor headers and armor data');
  10431. }
  10432. headers = headers.split('\n');
  10433. // remove empty entry
  10434. headers.pop();
  10435. return { headers: headers, body: body };
  10436. }
  10437. /**
  10438. * Verify armored headers. RFC4880, section 6.3: "OpenPGP should consider improperly formatted
  10439. * Armor Headers to be corruption of the ASCII Armor."
  10440. * @private
  10441. * @param {Array<String>} headers Armor headers
  10442. */
  10443. function verifyHeaders(headers) {
  10444. for (var i = 0; i < headers.length; i++) {
  10445. if (!/^(Version|Comment|MessageID|Hash|Charset): .+$/.test(headers[i])) {
  10446. throw new Error('Improperly formatted armor header: ' + headers[i]);
  10447. }
  10448. }
  10449. }
  10450. /**
  10451. * Splits a message into two parts, the body and the checksum. This is an internal function
  10452. * @param {String} text OpenPGP armored message part
  10453. * @returns {(Boolean|Object)} Either false in case of an error
  10454. * or an object with attribute "body" containing the body
  10455. * and an attribute "checksum" containing the checksum.
  10456. */
  10457. function splitChecksum(text) {
  10458. var reChecksumStart = /^=/m;
  10459. var body = text;
  10460. var checksum = "";
  10461. var matchResult = reChecksumStart.exec(text);
  10462. if (matchResult !== null) {
  10463. body = text.slice(0, matchResult.index);
  10464. checksum = text.slice(matchResult.index + 1);
  10465. }
  10466. return { body: body, checksum: checksum };
  10467. }
  10468. /**
  10469. * DeArmor an OpenPGP armored message; verify the checksum and return
  10470. * the encoded bytes
  10471. * @param {String} text OpenPGP armored message
  10472. * @returns {Object} An object with attribute "text" containing the message text,
  10473. * an attribute "data" containing the bytes and "type" for the ASCII armor type
  10474. * @static
  10475. */
  10476. function dearmor(text) {
  10477. var reSplit = /^-----[^-]+-----$\n/m;
  10478. // remove trailing whitespace at end of line
  10479. text = text.replace(/[\t\r ]+\n/g, '\n');
  10480. var type = getType(text);
  10481. var splittext = text.split(reSplit);
  10482. // IE has a bug in split with a re. If the pattern matches the beginning of the
  10483. // string it doesn't create an empty array element 0. So we need to detect this
  10484. // so we know the index of the data we are interested in.
  10485. var indexBase = 1;
  10486. var result, checksum, msg;
  10487. if (text.search(reSplit) !== splittext[0].length) {
  10488. indexBase = 0;
  10489. }
  10490. if (type !== 2) {
  10491. msg = splitHeaders(splittext[indexBase]);
  10492. var msg_sum = splitChecksum(msg.body);
  10493. result = {
  10494. data: _base2.default.decode(msg_sum.body),
  10495. headers: msg.headers,
  10496. type: type
  10497. };
  10498. checksum = msg_sum.checksum;
  10499. } else {
  10500. // Reverse dash-escaping for msg
  10501. msg = splitHeaders(splittext[indexBase].replace(/^- /mg, ''));
  10502. var sig = splitHeaders(splittext[indexBase + 1].replace(/^- /mg, ''));
  10503. verifyHeaders(sig.headers);
  10504. var sig_sum = splitChecksum(sig.body);
  10505. result = {
  10506. text: msg.body.replace(/\n$/, '').replace(/\n/g, "\r\n"),
  10507. data: _base2.default.decode(sig_sum.body),
  10508. headers: msg.headers,
  10509. type: type
  10510. };
  10511. checksum = sig_sum.checksum;
  10512. }
  10513. checksum = checksum.substr(0, 4);
  10514. if (!verifyCheckSum(result.data, checksum)) {
  10515. throw new Error("Ascii armor integrity check on message failed: '" + checksum + "' should be '" + getCheckSum(result.data) + "'");
  10516. }
  10517. verifyHeaders(result.headers);
  10518. return result;
  10519. }
  10520. /**
  10521. * Armor an OpenPGP binary packet block
  10522. * @param {Integer} messagetype type of the message
  10523. * @param body
  10524. * @param {Integer} partindex
  10525. * @param {Integer} parttotal
  10526. * @returns {String} Armored text
  10527. * @static
  10528. */
  10529. function armor(messagetype, body, partindex, parttotal) {
  10530. var result = [];
  10531. switch (messagetype) {
  10532. case _enums2.default.armor.multipart_section:
  10533. result.push("-----BEGIN PGP MESSAGE, PART " + partindex + "/" + parttotal + "-----\r\n");
  10534. result.push(addheader());
  10535. result.push(_base2.default.encode(body));
  10536. result.push("\r\n=" + getCheckSum(body) + "\r\n");
  10537. result.push("-----END PGP MESSAGE, PART " + partindex + "/" + parttotal + "-----\r\n");
  10538. break;
  10539. case _enums2.default.armor.multipart_last:
  10540. result.push("-----BEGIN PGP MESSAGE, PART " + partindex + "-----\r\n");
  10541. result.push(addheader());
  10542. result.push(_base2.default.encode(body));
  10543. result.push("\r\n=" + getCheckSum(body) + "\r\n");
  10544. result.push("-----END PGP MESSAGE, PART " + partindex + "-----\r\n");
  10545. break;
  10546. case _enums2.default.armor.signed:
  10547. result.push("\r\n-----BEGIN PGP SIGNED MESSAGE-----\r\n");
  10548. result.push("Hash: " + body.hash + "\r\n\r\n");
  10549. result.push(body.text.replace(/\n-/g, "\n- -"));
  10550. result.push("\r\n-----BEGIN PGP SIGNATURE-----\r\n");
  10551. result.push(addheader());
  10552. result.push(_base2.default.encode(body.data));
  10553. result.push("\r\n=" + getCheckSum(body.data) + "\r\n");
  10554. result.push("-----END PGP SIGNATURE-----\r\n");
  10555. break;
  10556. case _enums2.default.armor.message:
  10557. result.push("-----BEGIN PGP MESSAGE-----\r\n");
  10558. result.push(addheader());
  10559. result.push(_base2.default.encode(body));
  10560. result.push("\r\n=" + getCheckSum(body) + "\r\n");
  10561. result.push("-----END PGP MESSAGE-----\r\n");
  10562. break;
  10563. case _enums2.default.armor.public_key:
  10564. result.push("-----BEGIN PGP PUBLIC KEY BLOCK-----\r\n");
  10565. result.push(addheader());
  10566. result.push(_base2.default.encode(body));
  10567. result.push("\r\n=" + getCheckSum(body) + "\r\n");
  10568. result.push("-----END PGP PUBLIC KEY BLOCK-----\r\n\r\n");
  10569. break;
  10570. case _enums2.default.armor.private_key:
  10571. result.push("-----BEGIN PGP PRIVATE KEY BLOCK-----\r\n");
  10572. result.push(addheader());
  10573. result.push(_base2.default.encode(body));
  10574. result.push("\r\n=" + getCheckSum(body) + "\r\n");
  10575. result.push("-----END PGP PRIVATE KEY BLOCK-----\r\n");
  10576. break;
  10577. case _enums2.default.armor.signature:
  10578. result.push("-----BEGIN PGP SIGNATURE-----\r\n");
  10579. result.push(addheader());
  10580. result.push(_base2.default.encode(body));
  10581. result.push("\r\n=" + getCheckSum(body) + "\r\n");
  10582. result.push("-----END PGP SIGNATURE-----\r\n");
  10583. break;
  10584. }
  10585. return result.join('');
  10586. }
  10587. exports.default = {
  10588. encode: armor,
  10589. decode: dearmor
  10590. };
  10591. },{"../config":10,"../enums.js":35,"./base64.js":34}],34:[function(_dereq_,module,exports){
  10592. /* OpenPGP radix-64/base64 string encoding/decoding
  10593. * Copyright 2005 Herbert Hanewinkel, www.haneWIN.de
  10594. * version 1.0, check www.haneWIN.de for the latest version
  10595. *
  10596. * This software is provided as-is, without express or implied warranty.
  10597. * Permission to use, copy, modify, distribute or sell this software, with or
  10598. * without fee, for any purpose and by any individual or organization, is hereby
  10599. * granted, provided that the above copyright notice and this paragraph appear
  10600. * in all copies. Distribution as a part of an application or binary must
  10601. * include the above copyright notice in the documentation and/or other materials
  10602. * provided with the application or distribution.
  10603. */
  10604. /**
  10605. * @module encoding/base64
  10606. */
  10607. 'use strict';
  10608. Object.defineProperty(exports, "__esModule", {
  10609. value: true
  10610. });
  10611. var b64s = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
  10612. /**
  10613. * Convert binary array to radix-64
  10614. * @param {Uint8Array} t Uint8Array to convert
  10615. * @returns {string} radix-64 version of input string
  10616. * @static
  10617. */
  10618. function s2r(t, o) {
  10619. // TODO check btoa alternative
  10620. var a, c, n;
  10621. var r = o ? o : [],
  10622. l = 0,
  10623. s = 0;
  10624. var tl = t.length;
  10625. for (n = 0; n < tl; n++) {
  10626. c = t[n];
  10627. if (s === 0) {
  10628. r.push(b64s.charAt(c >> 2 & 63));
  10629. a = (c & 3) << 4;
  10630. } else if (s === 1) {
  10631. r.push(b64s.charAt(a | c >> 4 & 15));
  10632. a = (c & 15) << 2;
  10633. } else if (s === 2) {
  10634. r.push(b64s.charAt(a | c >> 6 & 3));
  10635. l += 1;
  10636. if (l % 60 === 0) {
  10637. r.push("\n");
  10638. }
  10639. r.push(b64s.charAt(c & 63));
  10640. }
  10641. l += 1;
  10642. if (l % 60 === 0) {
  10643. r.push("\n");
  10644. }
  10645. s += 1;
  10646. if (s === 3) {
  10647. s = 0;
  10648. }
  10649. }
  10650. if (s > 0) {
  10651. r.push(b64s.charAt(a));
  10652. l += 1;
  10653. if (l % 60 === 0) {
  10654. r.push("\n");
  10655. }
  10656. r.push('=');
  10657. l += 1;
  10658. }
  10659. if (s === 1) {
  10660. if (l % 60 === 0) {
  10661. r.push("\n");
  10662. }
  10663. r.push('=');
  10664. }
  10665. if (o) {
  10666. return;
  10667. }
  10668. return r.join('');
  10669. }
  10670. /**
  10671. * Convert radix-64 to binary array
  10672. * @param {String} t radix-64 string to convert
  10673. * @returns {Uint8Array} binary array version of input string
  10674. * @static
  10675. */
  10676. function r2s(t) {
  10677. // TODO check atob alternative
  10678. var c, n;
  10679. var r = [],
  10680. s = 0,
  10681. a = 0;
  10682. var tl = t.length;
  10683. for (n = 0; n < tl; n++) {
  10684. c = b64s.indexOf(t.charAt(n));
  10685. if (c >= 0) {
  10686. if (s) {
  10687. r.push(a | c >> 6 - s & 255);
  10688. }
  10689. s = s + 2 & 7;
  10690. a = c << s & 255;
  10691. }
  10692. }
  10693. return new Uint8Array(r);
  10694. }
  10695. exports.default = {
  10696. encode: s2r,
  10697. decode: r2s
  10698. };
  10699. },{}],35:[function(_dereq_,module,exports){
  10700. 'use strict';
  10701. /**
  10702. * @module enums
  10703. */
  10704. Object.defineProperty(exports, "__esModule", {
  10705. value: true
  10706. });
  10707. exports.default = {
  10708. /** A string to key specifier type
  10709. * @enum {Integer}
  10710. * @readonly
  10711. */
  10712. s2k: {
  10713. simple: 0,
  10714. salted: 1,
  10715. iterated: 3,
  10716. gnu: 101
  10717. },
  10718. /** {@link http://tools.ietf.org/html/rfc4880#section-9.1|RFC4880, section 9.1}
  10719. * @enum {Integer}
  10720. * @readonly
  10721. */
  10722. publicKey: {
  10723. rsa_encrypt_sign: 1,
  10724. rsa_encrypt: 2,
  10725. rsa_sign: 3,
  10726. elgamal: 16,
  10727. dsa: 17
  10728. },
  10729. /** {@link http://tools.ietf.org/html/rfc4880#section-9.2|RFC4880, section 9.2}
  10730. * @enum {Integer}
  10731. * @readonly
  10732. */
  10733. symmetric: {
  10734. plaintext: 0,
  10735. /** Not implemented! */
  10736. idea: 1,
  10737. tripledes: 2,
  10738. cast5: 3,
  10739. blowfish: 4,
  10740. aes128: 7,
  10741. aes192: 8,
  10742. aes256: 9,
  10743. twofish: 10
  10744. },
  10745. /** {@link http://tools.ietf.org/html/rfc4880#section-9.3|RFC4880, section 9.3}
  10746. * @enum {Integer}
  10747. * @readonly
  10748. */
  10749. compression: {
  10750. uncompressed: 0,
  10751. /** RFC1951 */
  10752. zip: 1,
  10753. /** RFC1950 */
  10754. zlib: 2,
  10755. bzip2: 3
  10756. },
  10757. /** {@link http://tools.ietf.org/html/rfc4880#section-9.4|RFC4880, section 9.4}
  10758. * @enum {Integer}
  10759. * @readonly
  10760. */
  10761. hash: {
  10762. md5: 1,
  10763. sha1: 2,
  10764. ripemd: 3,
  10765. sha256: 8,
  10766. sha384: 9,
  10767. sha512: 10,
  10768. sha224: 11
  10769. },
  10770. /** A list of packet types and numeric tags associated with them.
  10771. * @enum {Integer}
  10772. * @readonly
  10773. */
  10774. packet: {
  10775. publicKeyEncryptedSessionKey: 1,
  10776. signature: 2,
  10777. symEncryptedSessionKey: 3,
  10778. onePassSignature: 4,
  10779. secretKey: 5,
  10780. publicKey: 6,
  10781. secretSubkey: 7,
  10782. compressed: 8,
  10783. symmetricallyEncrypted: 9,
  10784. marker: 10,
  10785. literal: 11,
  10786. trust: 12,
  10787. userid: 13,
  10788. publicSubkey: 14,
  10789. userAttribute: 17,
  10790. symEncryptedIntegrityProtected: 18,
  10791. modificationDetectionCode: 19,
  10792. symEncryptedAEADProtected: 20 // see IETF draft: https://tools.ietf.org/html/draft-ford-openpgp-format-00#section-2.1
  10793. },
  10794. /** Data types in the literal packet
  10795. * @enum {Integer}
  10796. * @readonly
  10797. */
  10798. literal: {
  10799. /** Binary data 'b' */
  10800. binary: 'b'.charCodeAt(),
  10801. /** Text data 't' */
  10802. text: 't'.charCodeAt(),
  10803. /** Utf8 data 'u' */
  10804. utf8: 'u'.charCodeAt()
  10805. },
  10806. /** One pass signature packet type
  10807. * @enum {Integer}
  10808. * @readonly
  10809. */
  10810. signature: {
  10811. /** 0x00: Signature of a binary document. */
  10812. binary: 0,
  10813. /** 0x01: Signature of a canonical text document.<br/>
  10814. * Canonicalyzing the document by converting line endings. */
  10815. text: 1,
  10816. /** 0x02: Standalone signature.<br/>
  10817. * This signature is a signature of only its own subpacket contents.
  10818. * It is calculated identically to a signature over a zero-lengh
  10819. * binary document. Note that it doesn't make sense to have a V3
  10820. * standalone signature. */
  10821. standalone: 2,
  10822. /** 0x10: Generic certification of a User ID and Public-Key packet.<br/>
  10823. * The issuer of this certification does not make any particular
  10824. * assertion as to how well the certifier has checked that the owner
  10825. * of the key is in fact the person described by the User ID. */
  10826. cert_generic: 16,
  10827. /** 0x11: Persona certification of a User ID and Public-Key packet.<br/>
  10828. * The issuer of this certification has not done any verification of
  10829. * the claim that the owner of this key is the User ID specified. */
  10830. cert_persona: 17,
  10831. /** 0x12: Casual certification of a User ID and Public-Key packet.<br/>
  10832. * The issuer of this certification has done some casual
  10833. * verification of the claim of identity. */
  10834. cert_casual: 18,
  10835. /** 0x13: Positive certification of a User ID and Public-Key packet.<br/>
  10836. * The issuer of this certification has done substantial
  10837. * verification of the claim of identity.<br/>
  10838. * <br/>
  10839. * Most OpenPGP implementations make their "key signatures" as 0x10
  10840. * certifications. Some implementations can issue 0x11-0x13
  10841. * certifications, but few differentiate between the types. */
  10842. cert_positive: 19,
  10843. /** 0x30: Certification revocation signature<br/>
  10844. * This signature revokes an earlier User ID certification signature
  10845. * (signature class 0x10 through 0x13) or direct-key signature
  10846. * (0x1F). It should be issued by the same key that issued the
  10847. * revoked signature or an authorized revocation key. The signature
  10848. * is computed over the same data as the certificate that it
  10849. * revokes, and should have a later creation date than that
  10850. * certificate. */
  10851. cert_revocation: 48,
  10852. /** 0x18: Subkey Binding Signature<br/>
  10853. * This signature is a statement by the top-level signing key that
  10854. * indicates that it owns the subkey. This signature is calculated
  10855. * directly on the primary key and subkey, and not on any User ID or
  10856. * other packets. A signature that binds a signing subkey MUST have
  10857. * an Embedded Signature subpacket in this binding signature that
  10858. * contains a 0x19 signature made by the signing subkey on the
  10859. * primary key and subkey. */
  10860. subkey_binding: 24,
  10861. /** 0x19: Primary Key Binding Signature<br/>
  10862. * This signature is a statement by a signing subkey, indicating
  10863. * that it is owned by the primary key and subkey. This signature
  10864. * is calculated the same way as a 0x18 signature: directly on the
  10865. * primary key and subkey, and not on any User ID or other packets.<br/>
  10866. * <br/>
  10867. * When a signature is made over a key, the hash data starts with the
  10868. * octet 0x99, followed by a two-octet length of the key, and then body
  10869. * of the key packet. (Note that this is an old-style packet header for
  10870. * a key packet with two-octet length.) A subkey binding signature
  10871. * (type 0x18) or primary key binding signature (type 0x19) then hashes
  10872. * the subkey using the same format as the main key (also using 0x99 as
  10873. * the first octet). */
  10874. key_binding: 25,
  10875. /** 0x1F: Signature directly on a key<br/>
  10876. * This signature is calculated directly on a key. It binds the
  10877. * information in the Signature subpackets to the key, and is
  10878. * appropriate to be used for subpackets that provide information
  10879. * about the key, such as the Revocation Key subpacket. It is also
  10880. * appropriate for statements that non-self certifiers want to make
  10881. * about the key itself, rather than the binding between a key and a
  10882. * name. */
  10883. key: 31,
  10884. /** 0x20: Key revocation signature<br/>
  10885. * The signature is calculated directly on the key being revoked. A
  10886. * revoked key is not to be used. Only revocation signatures by the
  10887. * key being revoked, or by an authorized revocation key, should be
  10888. * considered valid revocation signatures.a */
  10889. key_revocation: 32,
  10890. /** 0x28: Subkey revocation signature<br/>
  10891. * The signature is calculated directly on the subkey being revoked.
  10892. * A revoked subkey is not to be used. Only revocation signatures
  10893. * by the top-level signature key that is bound to this subkey, or
  10894. * by an authorized revocation key, should be considered valid
  10895. * revocation signatures.<br/>
  10896. * <br/>
  10897. * Key revocation signatures (types 0x20 and 0x28)
  10898. * hash only the key being revoked. */
  10899. subkey_revocation: 40,
  10900. /** 0x40: Timestamp signature.<br/>
  10901. * This signature is only meaningful for the timestamp contained in
  10902. * it. */
  10903. timestamp: 64,
  10904. /** 0x50: Third-Party Confirmation signature.<br/>
  10905. * This signature is a signature over some other OpenPGP Signature
  10906. * packet(s). It is analogous to a notary seal on the signed data.
  10907. * A third-party signature SHOULD include Signature Target
  10908. * subpacket(s) to give easy identification. Note that we really do
  10909. * mean SHOULD. There are plausible uses for this (such as a blind
  10910. * party that only sees the signature, not the key or source
  10911. * document) that cannot include a target subpacket. */
  10912. third_party: 80
  10913. },
  10914. /** Signature subpacket type
  10915. * @enum {Integer}
  10916. * @readonly
  10917. */
  10918. signatureSubpacket: {
  10919. signature_creation_time: 2,
  10920. signature_expiration_time: 3,
  10921. exportable_certification: 4,
  10922. trust_signature: 5,
  10923. regular_expression: 6,
  10924. revocable: 7,
  10925. key_expiration_time: 9,
  10926. placeholder_backwards_compatibility: 10,
  10927. preferred_symmetric_algorithms: 11,
  10928. revocation_key: 12,
  10929. issuer: 16,
  10930. notation_data: 20,
  10931. preferred_hash_algorithms: 21,
  10932. preferred_compression_algorithms: 22,
  10933. key_server_preferences: 23,
  10934. preferred_key_server: 24,
  10935. primary_user_id: 25,
  10936. policy_uri: 26,
  10937. key_flags: 27,
  10938. signers_user_id: 28,
  10939. reason_for_revocation: 29,
  10940. features: 30,
  10941. signature_target: 31,
  10942. embedded_signature: 32
  10943. },
  10944. /** Key flags
  10945. * @enum {Integer}
  10946. * @readonly
  10947. */
  10948. keyFlags: {
  10949. /** 0x01 - This key may be used to certify other keys. */
  10950. certify_keys: 1,
  10951. /** 0x02 - This key may be used to sign data. */
  10952. sign_data: 2,
  10953. /** 0x04 - This key may be used to encrypt communications. */
  10954. encrypt_communication: 4,
  10955. /** 0x08 - This key may be used to encrypt storage. */
  10956. encrypt_storage: 8,
  10957. /** 0x10 - The private component of this key may have been split
  10958. * by a secret-sharing mechanism. */
  10959. split_private_key: 16,
  10960. /** 0x20 - This key may be used for authentication. */
  10961. authentication: 32,
  10962. /** 0x80 - The private component of this key may be in the
  10963. * possession of more than one person. */
  10964. shared_private_key: 128
  10965. },
  10966. /** Key status
  10967. * @enum {Integer}
  10968. * @readonly
  10969. */
  10970. keyStatus: {
  10971. invalid: 0,
  10972. expired: 1,
  10973. revoked: 2,
  10974. valid: 3,
  10975. no_self_cert: 4
  10976. },
  10977. /** Armor type
  10978. * @enum {Integer}
  10979. * @readonly
  10980. */
  10981. armor: {
  10982. multipart_section: 0,
  10983. multipart_last: 1,
  10984. signed: 2,
  10985. message: 3,
  10986. public_key: 4,
  10987. private_key: 5,
  10988. signature: 6
  10989. },
  10990. /** Asserts validity and converts from string/integer to integer. */
  10991. write: function write(type, e) {
  10992. if (typeof e === 'number') {
  10993. e = this.read(type, e);
  10994. }
  10995. if (type[e] !== undefined) {
  10996. return type[e];
  10997. } else {
  10998. throw new Error('Invalid enum value.');
  10999. }
  11000. },
  11001. /** Converts from an integer to string. */
  11002. read: function read(type, e) {
  11003. for (var i in type) {
  11004. if (type[i] === parseInt(e)) {
  11005. return i;
  11006. }
  11007. }
  11008. throw new Error('Invalid enum value.');
  11009. }
  11010. };
  11011. },{}],36:[function(_dereq_,module,exports){
  11012. // OpenPGP.js - An OpenPGP implementation in javascript
  11013. // Copyright (C) 2015 Tankred Hase
  11014. //
  11015. // This library is free software; you can redistribute it and/or
  11016. // modify it under the terms of the GNU Lesser General Public
  11017. // License as published by the Free Software Foundation; either
  11018. // version 3.0 of the License, or (at your option) any later version.
  11019. //
  11020. // This library is distributed in the hope that it will be useful,
  11021. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11022. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11023. // Lesser General Public License for more details.
  11024. //
  11025. // You should have received a copy of the GNU Lesser General Public
  11026. // License along with this library; if not, write to the Free Software
  11027. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  11028. /**
  11029. * @fileoverview This class implements a client for the OpenPGP HTTP Keyserver Protocol (HKP)
  11030. * in order to lookup and upload keys on standard public key servers.
  11031. */
  11032. 'use strict';
  11033. Object.defineProperty(exports, "__esModule", {
  11034. value: true
  11035. });
  11036. exports.default = HKP;
  11037. var _config = _dereq_('./config');
  11038. var _config2 = _interopRequireDefault(_config);
  11039. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11040. /**
  11041. * Initialize the HKP client and configure it with the key server url and fetch function.
  11042. * @constructor
  11043. * @param {String} keyServerBaseUrl (optional) The HKP key server base url including
  11044. * the protocol to use e.g. https://pgp.mit.edu
  11045. */
  11046. function HKP(keyServerBaseUrl) {
  11047. this._baseUrl = keyServerBaseUrl ? keyServerBaseUrl : _config2.default.keyserver;
  11048. this._fetch = typeof window !== 'undefined' ? window.fetch : _dereq_('node-fetch');
  11049. }
  11050. /**
  11051. * Search for a public key on the key server either by key ID or part of the user ID.
  11052. * @param {String} options.keyID The long public key ID.
  11053. * @param {String} options.query This can be any part of the key user ID such as name
  11054. * or email address.
  11055. * @return {Promise<String>} The ascii armored public key.
  11056. */
  11057. HKP.prototype.lookup = function (options) {
  11058. var uri = this._baseUrl + '/pks/lookup?op=get&options=mr&search=',
  11059. fetch = this._fetch;
  11060. if (options.keyId) {
  11061. uri += '0x' + encodeURIComponent(options.keyId);
  11062. } else if (options.query) {
  11063. uri += encodeURIComponent(options.query);
  11064. } else {
  11065. throw new Error('You must provide a query parameter!');
  11066. }
  11067. return fetch(uri).then(function (response) {
  11068. if (response.status === 200) {
  11069. return response.text();
  11070. }
  11071. }).then(function (publicKeyArmored) {
  11072. if (!publicKeyArmored || publicKeyArmored.indexOf('-----END PGP PUBLIC KEY BLOCK-----') < 0) {
  11073. return;
  11074. }
  11075. return publicKeyArmored.trim();
  11076. });
  11077. };
  11078. /**
  11079. * Upload a public key to the server.
  11080. * @param {String} publicKeyArmored An ascii armored public key to be uploaded.
  11081. * @return {Promise}
  11082. */
  11083. HKP.prototype.upload = function (publicKeyArmored) {
  11084. var uri = this._baseUrl + '/pks/add',
  11085. fetch = this._fetch;
  11086. return fetch(uri, {
  11087. method: 'post',
  11088. headers: {
  11089. 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'
  11090. },
  11091. body: 'keytext=' + encodeURIComponent(publicKeyArmored)
  11092. });
  11093. };
  11094. },{"./config":10,"node-fetch":"node-fetch"}],37:[function(_dereq_,module,exports){
  11095. 'use strict';
  11096. /**
  11097. * Export high level api as default.
  11098. * Usage:
  11099. *
  11100. * import openpgp from 'openpgp.js'
  11101. * openpgp.encryptMessage(keys, text)
  11102. */
  11103. Object.defineProperty(exports, "__esModule", {
  11104. value: true
  11105. });
  11106. exports.HKP = exports.AsyncProxy = exports.Keyring = exports.crypto = exports.config = exports.enums = exports.armor = exports.Keyid = exports.S2K = exports.MPI = exports.packet = exports.util = exports.cleartext = exports.message = exports.signature = exports.key = undefined;
  11107. var _openpgp = _dereq_('./openpgp');
  11108. Object.keys(_openpgp).forEach(function (key) {
  11109. if (key === "default") return;
  11110. Object.defineProperty(exports, key, {
  11111. enumerable: true,
  11112. get: function get() {
  11113. return _openpgp[key];
  11114. }
  11115. });
  11116. });
  11117. var _util = _dereq_('./util');
  11118. Object.defineProperty(exports, 'util', {
  11119. enumerable: true,
  11120. get: function get() {
  11121. return _interopRequireDefault(_util).default;
  11122. }
  11123. });
  11124. var _packet = _dereq_('./packet');
  11125. Object.defineProperty(exports, 'packet', {
  11126. enumerable: true,
  11127. get: function get() {
  11128. return _interopRequireDefault(_packet).default;
  11129. }
  11130. });
  11131. var _mpi = _dereq_('./type/mpi');
  11132. Object.defineProperty(exports, 'MPI', {
  11133. enumerable: true,
  11134. get: function get() {
  11135. return _interopRequireDefault(_mpi).default;
  11136. }
  11137. });
  11138. var _s2k = _dereq_('./type/s2k');
  11139. Object.defineProperty(exports, 'S2K', {
  11140. enumerable: true,
  11141. get: function get() {
  11142. return _interopRequireDefault(_s2k).default;
  11143. }
  11144. });
  11145. var _keyid = _dereq_('./type/keyid');
  11146. Object.defineProperty(exports, 'Keyid', {
  11147. enumerable: true,
  11148. get: function get() {
  11149. return _interopRequireDefault(_keyid).default;
  11150. }
  11151. });
  11152. var _armor = _dereq_('./encoding/armor');
  11153. Object.defineProperty(exports, 'armor', {
  11154. enumerable: true,
  11155. get: function get() {
  11156. return _interopRequireDefault(_armor).default;
  11157. }
  11158. });
  11159. var _enums = _dereq_('./enums');
  11160. Object.defineProperty(exports, 'enums', {
  11161. enumerable: true,
  11162. get: function get() {
  11163. return _interopRequireDefault(_enums).default;
  11164. }
  11165. });
  11166. var _config = _dereq_('./config/config');
  11167. Object.defineProperty(exports, 'config', {
  11168. enumerable: true,
  11169. get: function get() {
  11170. return _interopRequireDefault(_config).default;
  11171. }
  11172. });
  11173. var _crypto = _dereq_('./crypto');
  11174. Object.defineProperty(exports, 'crypto', {
  11175. enumerable: true,
  11176. get: function get() {
  11177. return _interopRequireDefault(_crypto).default;
  11178. }
  11179. });
  11180. var _keyring = _dereq_('./keyring');
  11181. Object.defineProperty(exports, 'Keyring', {
  11182. enumerable: true,
  11183. get: function get() {
  11184. return _interopRequireDefault(_keyring).default;
  11185. }
  11186. });
  11187. var _async_proxy = _dereq_('./worker/async_proxy');
  11188. Object.defineProperty(exports, 'AsyncProxy', {
  11189. enumerable: true,
  11190. get: function get() {
  11191. return _interopRequireDefault(_async_proxy).default;
  11192. }
  11193. });
  11194. var _hkp = _dereq_('./hkp');
  11195. Object.defineProperty(exports, 'HKP', {
  11196. enumerable: true,
  11197. get: function get() {
  11198. return _interopRequireDefault(_hkp).default;
  11199. }
  11200. });
  11201. var openpgp = _interopRequireWildcard(_openpgp);
  11202. var _key = _dereq_('./key');
  11203. var keyMod = _interopRequireWildcard(_key);
  11204. var _signature = _dereq_('./signature');
  11205. var signatureMod = _interopRequireWildcard(_signature);
  11206. var _message = _dereq_('./message');
  11207. var messageMod = _interopRequireWildcard(_message);
  11208. var _cleartext = _dereq_('./cleartext');
  11209. var cleartextMod = _interopRequireWildcard(_cleartext);
  11210. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  11211. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11212. exports.default = openpgp;
  11213. /**
  11214. * Export each high level api function seperately.
  11215. * Usage:
  11216. *
  11217. * import { encryptMessage } from 'openpgp.js'
  11218. * encryptMessage(keys, text)
  11219. */
  11220. /**
  11221. * @see module:key
  11222. * @name module:openpgp.key
  11223. */
  11224. var key = exports.key = keyMod;
  11225. /**
  11226. * @see module:signature
  11227. * @name module:openpgp.signature
  11228. */
  11229. var signature = exports.signature = signatureMod;
  11230. /**
  11231. * @see module:message
  11232. * @name module:openpgp.message
  11233. */
  11234. var message = exports.message = messageMod;
  11235. /**
  11236. * @see module:cleartext
  11237. * @name module:openpgp.cleartext
  11238. */
  11239. var cleartext = exports.cleartext = cleartextMod;
  11240. /**
  11241. * @see module:util
  11242. * @name module:openpgp.util
  11243. */
  11244. },{"./cleartext":5,"./config/config":9,"./crypto":24,"./encoding/armor":33,"./enums":35,"./hkp":36,"./key":38,"./keyring":39,"./message":42,"./openpgp":43,"./packet":47,"./signature":66,"./type/keyid":67,"./type/mpi":68,"./type/s2k":69,"./util":70,"./worker/async_proxy":71}],38:[function(_dereq_,module,exports){
  11245. // GPG4Browsers - An OpenPGP implementation in javascript
  11246. // Copyright (C) 2011 Recurity Labs GmbH
  11247. //
  11248. // This library is free software; you can redistribute it and/or
  11249. // modify it under the terms of the GNU Lesser General Public
  11250. // License as published by the Free Software Foundation; either
  11251. // version 3.0 of the License, or (at your option) any later version.
  11252. //
  11253. // This library is distributed in the hope that it will be useful,
  11254. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11255. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11256. // Lesser General Public License for more details.
  11257. //
  11258. // You should have received a copy of the GNU Lesser General Public
  11259. // License along with this library; if not, write to the Free Software
  11260. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  11261. /**
  11262. * @requires config
  11263. * @requires encoding/armor
  11264. * @requires enums
  11265. * @requires packet
  11266. * @module key
  11267. */
  11268. 'use strict';
  11269. Object.defineProperty(exports, "__esModule", {
  11270. value: true
  11271. });
  11272. exports.Key = Key;
  11273. exports.readArmored = readArmored;
  11274. exports.generate = generate;
  11275. exports.reformat = reformat;
  11276. exports.getPreferredSymAlgo = getPreferredSymAlgo;
  11277. var _packet = _dereq_('./packet');
  11278. var _packet2 = _interopRequireDefault(_packet);
  11279. var _enums = _dereq_('./enums.js');
  11280. var _enums2 = _interopRequireDefault(_enums);
  11281. var _armor = _dereq_('./encoding/armor.js');
  11282. var _armor2 = _interopRequireDefault(_armor);
  11283. var _config = _dereq_('./config');
  11284. var _config2 = _interopRequireDefault(_config);
  11285. var _util = _dereq_('./util');
  11286. var _util2 = _interopRequireDefault(_util);
  11287. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11288. /**
  11289. * @class
  11290. * @classdesc Class that represents an OpenPGP key. Must contain a primary key.
  11291. * Can contain additional subkeys, signatures, user ids, user attributes.
  11292. * @param {module:packet/packetlist} packetlist The packets that form this key
  11293. */
  11294. function Key(packetlist) {
  11295. if (!(this instanceof Key)) {
  11296. return new Key(packetlist);
  11297. }
  11298. // same data as in packetlist but in structured form
  11299. this.primaryKey = null;
  11300. this.revocationSignature = null;
  11301. this.directSignatures = null;
  11302. this.users = null;
  11303. this.subKeys = null;
  11304. this.packetlist2structure(packetlist);
  11305. if (!this.primaryKey || !this.users) {
  11306. throw new Error('Invalid key: need at least key and user ID packet');
  11307. }
  11308. }
  11309. /**
  11310. * Transforms packetlist to structured key data
  11311. * @param {module:packet/packetlist} packetlist The packets that form a key
  11312. */
  11313. Key.prototype.packetlist2structure = function (packetlist) {
  11314. var user, primaryKeyId, subKey;
  11315. for (var i = 0; i < packetlist.length; i++) {
  11316. switch (packetlist[i].tag) {
  11317. case _enums2.default.packet.publicKey:
  11318. case _enums2.default.packet.secretKey:
  11319. this.primaryKey = packetlist[i];
  11320. primaryKeyId = this.primaryKey.getKeyId();
  11321. break;
  11322. case _enums2.default.packet.userid:
  11323. case _enums2.default.packet.userAttribute:
  11324. user = new User(packetlist[i]);
  11325. if (!this.users) {
  11326. this.users = [];
  11327. }
  11328. this.users.push(user);
  11329. break;
  11330. case _enums2.default.packet.publicSubkey:
  11331. case _enums2.default.packet.secretSubkey:
  11332. user = null;
  11333. if (!this.subKeys) {
  11334. this.subKeys = [];
  11335. }
  11336. subKey = new SubKey(packetlist[i]);
  11337. this.subKeys.push(subKey);
  11338. break;
  11339. case _enums2.default.packet.signature:
  11340. switch (packetlist[i].signatureType) {
  11341. case _enums2.default.signature.cert_generic:
  11342. case _enums2.default.signature.cert_persona:
  11343. case _enums2.default.signature.cert_casual:
  11344. case _enums2.default.signature.cert_positive:
  11345. if (!user) {
  11346. _util2.default.print_debug('Dropping certification signatures without preceding user packet');
  11347. continue;
  11348. }
  11349. if (packetlist[i].issuerKeyId.equals(primaryKeyId)) {
  11350. if (!user.selfCertifications) {
  11351. user.selfCertifications = [];
  11352. }
  11353. user.selfCertifications.push(packetlist[i]);
  11354. } else {
  11355. if (!user.otherCertifications) {
  11356. user.otherCertifications = [];
  11357. }
  11358. user.otherCertifications.push(packetlist[i]);
  11359. }
  11360. break;
  11361. case _enums2.default.signature.cert_revocation:
  11362. if (user) {
  11363. if (!user.revocationCertifications) {
  11364. user.revocationCertifications = [];
  11365. }
  11366. user.revocationCertifications.push(packetlist[i]);
  11367. } else {
  11368. if (!this.directSignatures) {
  11369. this.directSignatures = [];
  11370. }
  11371. this.directSignatures.push(packetlist[i]);
  11372. }
  11373. break;
  11374. case _enums2.default.signature.key:
  11375. if (!this.directSignatures) {
  11376. this.directSignatures = [];
  11377. }
  11378. this.directSignatures.push(packetlist[i]);
  11379. break;
  11380. case _enums2.default.signature.subkey_binding:
  11381. if (!subKey) {
  11382. _util2.default.print_debug('Dropping subkey binding signature without preceding subkey packet');
  11383. continue;
  11384. }
  11385. subKey.bindingSignature = packetlist[i];
  11386. break;
  11387. case _enums2.default.signature.key_revocation:
  11388. this.revocationSignature = packetlist[i];
  11389. break;
  11390. case _enums2.default.signature.subkey_revocation:
  11391. if (!subKey) {
  11392. _util2.default.print_debug('Dropping subkey revocation signature without preceding subkey packet');
  11393. continue;
  11394. }
  11395. subKey.revocationSignature = packetlist[i];
  11396. break;
  11397. }
  11398. break;
  11399. }
  11400. }
  11401. };
  11402. /**
  11403. * Transforms structured key data to packetlist
  11404. * @return {module:packet/packetlist} The packets that form a key
  11405. */
  11406. Key.prototype.toPacketlist = function () {
  11407. var packetlist = new _packet2.default.List();
  11408. packetlist.push(this.primaryKey);
  11409. packetlist.push(this.revocationSignature);
  11410. packetlist.concat(this.directSignatures);
  11411. var i;
  11412. for (i = 0; i < this.users.length; i++) {
  11413. packetlist.concat(this.users[i].toPacketlist());
  11414. }
  11415. if (this.subKeys) {
  11416. for (i = 0; i < this.subKeys.length; i++) {
  11417. packetlist.concat(this.subKeys[i].toPacketlist());
  11418. }
  11419. }
  11420. return packetlist;
  11421. };
  11422. /**
  11423. * Returns all the private and public subkey packets
  11424. * @returns {Array<(module:packet/public_subkey|module:packet/secret_subkey)>}
  11425. */
  11426. Key.prototype.getSubkeyPackets = function () {
  11427. var subKeys = [];
  11428. if (this.subKeys) {
  11429. for (var i = 0; i < this.subKeys.length; i++) {
  11430. subKeys.push(this.subKeys[i].subKey);
  11431. }
  11432. }
  11433. return subKeys;
  11434. };
  11435. /**
  11436. * Returns all the private and public key and subkey packets
  11437. * @returns {Array<(module:packet/public_subkey|module:packet/secret_subkey|module:packet/secret_key|module:packet/public_key)>}
  11438. */
  11439. Key.prototype.getAllKeyPackets = function () {
  11440. return [this.primaryKey].concat(this.getSubkeyPackets());
  11441. };
  11442. /**
  11443. * Returns key IDs of all key packets
  11444. * @returns {Array<module:type/keyid>}
  11445. */
  11446. Key.prototype.getKeyIds = function () {
  11447. var keyIds = [];
  11448. var keys = this.getAllKeyPackets();
  11449. for (var i = 0; i < keys.length; i++) {
  11450. keyIds.push(keys[i].getKeyId());
  11451. }
  11452. return keyIds;
  11453. };
  11454. /**
  11455. * Returns first key packet for given array of key IDs
  11456. * @param {Array<module:type/keyid>} keyIds
  11457. * @return {(module:packet/public_subkey|module:packet/public_key|
  11458. * module:packet/secret_subkey|module:packet/secret_key|null)}
  11459. */
  11460. Key.prototype.getKeyPacket = function (keyIds) {
  11461. var keys = this.getAllKeyPackets();
  11462. for (var i = 0; i < keys.length; i++) {
  11463. var keyId = keys[i].getKeyId();
  11464. for (var j = 0; j < keyIds.length; j++) {
  11465. if (keyId.equals(keyIds[j])) {
  11466. return keys[i];
  11467. }
  11468. }
  11469. }
  11470. return null;
  11471. };
  11472. /**
  11473. * Returns userids
  11474. * @return {Array<string>} array of userids
  11475. */
  11476. Key.prototype.getUserIds = function () {
  11477. var userids = [];
  11478. for (var i = 0; i < this.users.length; i++) {
  11479. if (this.users[i].userId) {
  11480. userids.push(_util2.default.Uint8Array2str(this.users[i].userId.write()));
  11481. }
  11482. }
  11483. return userids;
  11484. };
  11485. /**
  11486. * Returns true if this is a public key
  11487. * @return {Boolean}
  11488. */
  11489. Key.prototype.isPublic = function () {
  11490. return this.primaryKey.tag === _enums2.default.packet.publicKey;
  11491. };
  11492. /**
  11493. * Returns true if this is a private key
  11494. * @return {Boolean}
  11495. */
  11496. Key.prototype.isPrivate = function () {
  11497. return this.primaryKey.tag === _enums2.default.packet.secretKey;
  11498. };
  11499. /**
  11500. * Returns key as public key (shallow copy)
  11501. * @return {module:key~Key} new public Key
  11502. */
  11503. Key.prototype.toPublic = function () {
  11504. var packetlist = new _packet2.default.List();
  11505. var keyPackets = this.toPacketlist();
  11506. var bytes;
  11507. for (var i = 0; i < keyPackets.length; i++) {
  11508. switch (keyPackets[i].tag) {
  11509. case _enums2.default.packet.secretKey:
  11510. bytes = keyPackets[i].writePublicKey();
  11511. var pubKeyPacket = new _packet2.default.PublicKey();
  11512. pubKeyPacket.read(bytes);
  11513. packetlist.push(pubKeyPacket);
  11514. break;
  11515. case _enums2.default.packet.secretSubkey:
  11516. bytes = keyPackets[i].writePublicKey();
  11517. var pubSubkeyPacket = new _packet2.default.PublicSubkey();
  11518. pubSubkeyPacket.read(bytes);
  11519. packetlist.push(pubSubkeyPacket);
  11520. break;
  11521. default:
  11522. packetlist.push(keyPackets[i]);
  11523. }
  11524. }
  11525. return new Key(packetlist);
  11526. };
  11527. /**
  11528. * Returns ASCII armored text of key
  11529. * @return {String} ASCII armor
  11530. */
  11531. Key.prototype.armor = function () {
  11532. var type = this.isPublic() ? _enums2.default.armor.public_key : _enums2.default.armor.private_key;
  11533. return _armor2.default.encode(type, this.toPacketlist().write());
  11534. };
  11535. /**
  11536. * Returns first key packet or key packet by given keyId that is available for signing or signature verification
  11537. * @param {module:type/keyid} keyId, optional
  11538. * @return {(module:packet/secret_subkey|module:packet/secret_key|null)} key packet or null if no signing key has been found
  11539. */
  11540. Key.prototype.getSigningKeyPacket = function (keyId) {
  11541. var primaryUser = this.getPrimaryUser();
  11542. if (primaryUser && isValidSigningKeyPacket(this.primaryKey, primaryUser.selfCertificate) && (!keyId || this.primaryKey.getKeyId().equals(keyId))) {
  11543. return this.primaryKey;
  11544. }
  11545. if (this.subKeys) {
  11546. for (var i = 0; i < this.subKeys.length; i++) {
  11547. if (this.subKeys[i].isValidSigningKey(this.primaryKey) && (!keyId || this.subKeys[i].subKey.getKeyId().equals(keyId))) {
  11548. return this.subKeys[i].subKey;
  11549. }
  11550. }
  11551. }
  11552. return null;
  11553. };
  11554. /**
  11555. * Returns preferred signature hash algorithm of this key
  11556. * @return {String}
  11557. */
  11558. Key.prototype.getPreferredHashAlgorithm = function () {
  11559. var primaryUser = this.getPrimaryUser();
  11560. if (primaryUser && primaryUser.selfCertificate.preferredHashAlgorithms) {
  11561. return primaryUser.selfCertificate.preferredHashAlgorithms[0];
  11562. }
  11563. return _config2.default.prefer_hash_algorithm;
  11564. };
  11565. function isValidEncryptionKeyPacket(keyPacket, signature) {
  11566. return keyPacket.algorithm !== _enums2.default.read(_enums2.default.publicKey, _enums2.default.publicKey.dsa) && keyPacket.algorithm !== _enums2.default.read(_enums2.default.publicKey, _enums2.default.publicKey.rsa_sign) && (!signature.keyFlags || (signature.keyFlags[0] & _enums2.default.keyFlags.encrypt_communication) !== 0 || (signature.keyFlags[0] & _enums2.default.keyFlags.encrypt_storage) !== 0);
  11567. }
  11568. function isValidSigningKeyPacket(keyPacket, signature) {
  11569. return (keyPacket.algorithm === _enums2.default.read(_enums2.default.publicKey, _enums2.default.publicKey.dsa) || keyPacket.algorithm === _enums2.default.read(_enums2.default.publicKey, _enums2.default.publicKey.rsa_sign) || keyPacket.algorithm === _enums2.default.read(_enums2.default.publicKey, _enums2.default.publicKey.rsa_encrypt_sign)) && (!signature.keyFlags || (signature.keyFlags[0] & _enums2.default.keyFlags.sign_data) !== 0);
  11570. }
  11571. /**
  11572. * Returns the first valid encryption key packet for this key
  11573. * @returns {(module:packet/public_subkey|module:packet/secret_subkey|module:packet/secret_key|module:packet/public_key|null)} key packet or null if no encryption key has been found
  11574. */
  11575. Key.prototype.getEncryptionKeyPacket = function () {
  11576. // V4: by convention subkeys are prefered for encryption service
  11577. // V3: keys MUST NOT have subkeys
  11578. if (this.subKeys) {
  11579. for (var i = 0; i < this.subKeys.length; i++) {
  11580. if (this.subKeys[i].isValidEncryptionKey(this.primaryKey)) {
  11581. return this.subKeys[i].subKey;
  11582. }
  11583. }
  11584. }
  11585. // if no valid subkey for encryption, evaluate primary key
  11586. var primaryUser = this.getPrimaryUser();
  11587. if (primaryUser && primaryUser.selfCertificate && !primaryUser.selfCertificate.isExpired() && isValidEncryptionKeyPacket(this.primaryKey, primaryUser.selfCertificate)) {
  11588. return this.primaryKey;
  11589. }
  11590. return null;
  11591. };
  11592. /**
  11593. * Encrypts all secret key and subkey packets
  11594. * @param {String} passphrase
  11595. */
  11596. Key.prototype.encrypt = function (passphrase) {
  11597. if (!this.isPrivate()) {
  11598. throw new Error("Nothing to encrypt in a public key");
  11599. }
  11600. var keys = this.getAllKeyPackets();
  11601. for (var i = 0; i < keys.length; i++) {
  11602. keys[i].encrypt(passphrase);
  11603. keys[i].clearPrivateMPIs();
  11604. }
  11605. };
  11606. /**
  11607. * Decrypts all secret key and subkey packets
  11608. * @param {String} passphrase
  11609. * @return {Boolean} true if all key and subkey packets decrypted successfully
  11610. */
  11611. Key.prototype.decrypt = function (passphrase) {
  11612. if (this.isPrivate()) {
  11613. var keys = this.getAllKeyPackets();
  11614. for (var i = 0; i < keys.length; i++) {
  11615. var success = keys[i].decrypt(passphrase);
  11616. if (!success) {
  11617. return false;
  11618. }
  11619. }
  11620. } else {
  11621. throw new Error("Nothing to decrypt in a public key");
  11622. }
  11623. return true;
  11624. };
  11625. /**
  11626. * Decrypts specific key packets by key ID
  11627. * @param {Array<module:type/keyid>} keyIds
  11628. * @param {String} passphrase
  11629. * @return {Boolean} true if all key packets decrypted successfully
  11630. */
  11631. Key.prototype.decryptKeyPacket = function (keyIds, passphrase) {
  11632. if (this.isPrivate()) {
  11633. var keys = this.getAllKeyPackets();
  11634. for (var i = 0; i < keys.length; i++) {
  11635. var keyId = keys[i].getKeyId();
  11636. for (var j = 0; j < keyIds.length; j++) {
  11637. if (keyId.equals(keyIds[j])) {
  11638. var success = keys[i].decrypt(passphrase);
  11639. if (!success) {
  11640. return false;
  11641. }
  11642. }
  11643. }
  11644. }
  11645. } else {
  11646. throw new Error("Nothing to decrypt in a public key");
  11647. }
  11648. return true;
  11649. };
  11650. /**
  11651. * Verify primary key. Checks for revocation signatures, expiration time
  11652. * and valid self signature
  11653. * @return {module:enums.keyStatus} The status of the primary key
  11654. */
  11655. Key.prototype.verifyPrimaryKey = function () {
  11656. // check revocation signature
  11657. if (this.revocationSignature && !this.revocationSignature.isExpired() && (this.revocationSignature.verified || this.revocationSignature.verify(this.primaryKey, { key: this.primaryKey }))) {
  11658. return _enums2.default.keyStatus.revoked;
  11659. }
  11660. // check V3 expiration time
  11661. if (this.primaryKey.version === 3 && this.primaryKey.expirationTimeV3 !== 0 && Date.now() > this.primaryKey.created.getTime() + this.primaryKey.expirationTimeV3 * 24 * 3600 * 1000) {
  11662. return _enums2.default.keyStatus.expired;
  11663. }
  11664. // check for at least one self signature. Self signature of user ID not mandatory
  11665. // See {@link http://tools.ietf.org/html/rfc4880#section-11.1}
  11666. var selfSigned = false;
  11667. for (var i = 0; i < this.users.length; i++) {
  11668. if (this.users[i].userId && this.users[i].selfCertifications) {
  11669. selfSigned = true;
  11670. }
  11671. }
  11672. if (!selfSigned) {
  11673. return _enums2.default.keyStatus.no_self_cert;
  11674. }
  11675. // check for valid self signature
  11676. var primaryUser = this.getPrimaryUser();
  11677. if (!primaryUser) {
  11678. return _enums2.default.keyStatus.invalid;
  11679. }
  11680. // check V4 expiration time
  11681. if (this.primaryKey.version === 4 && primaryUser.selfCertificate.keyNeverExpires === false && Date.now() > this.primaryKey.created.getTime() + primaryUser.selfCertificate.keyExpirationTime * 1000) {
  11682. return _enums2.default.keyStatus.expired;
  11683. }
  11684. return _enums2.default.keyStatus.valid;
  11685. };
  11686. /**
  11687. * Returns the expiration time of the primary key or null if key does not expire
  11688. * @return {Date|null}
  11689. */
  11690. Key.prototype.getExpirationTime = function () {
  11691. if (this.primaryKey.version === 3) {
  11692. return getExpirationTime(this.primaryKey);
  11693. }
  11694. if (this.primaryKey.version === 4) {
  11695. var primaryUser = this.getPrimaryUser();
  11696. if (!primaryUser) {
  11697. return null;
  11698. }
  11699. return getExpirationTime(this.primaryKey, primaryUser.selfCertificate);
  11700. }
  11701. };
  11702. function getExpirationTime(keyPacket, selfCertificate) {
  11703. // check V3 expiration time
  11704. if (keyPacket.version === 3 && keyPacket.expirationTimeV3 !== 0) {
  11705. return new Date(keyPacket.created.getTime() + keyPacket.expirationTimeV3 * 24 * 3600 * 1000);
  11706. }
  11707. // check V4 expiration time
  11708. if (keyPacket.version === 4 && selfCertificate.keyNeverExpires === false) {
  11709. return new Date(keyPacket.created.getTime() + selfCertificate.keyExpirationTime * 1000);
  11710. }
  11711. return null;
  11712. }
  11713. /**
  11714. * Returns primary user and most significant (latest valid) self signature
  11715. * - if multiple users are marked as primary users returns the one with the latest self signature
  11716. * - if no primary user is found returns the user with the latest self signature
  11717. * @return {{user: Array<module:packet/User>, selfCertificate: Array<module:packet/signature>}|null} The primary user and the self signature
  11718. */
  11719. Key.prototype.getPrimaryUser = function () {
  11720. var primUser = [];
  11721. for (var i = 0; i < this.users.length; i++) {
  11722. if (!this.users[i].userId || !this.users[i].selfCertifications) {
  11723. continue;
  11724. }
  11725. for (var j = 0; j < this.users[i].selfCertifications.length; j++) {
  11726. primUser.push({ index: i, user: this.users[i], selfCertificate: this.users[i].selfCertifications[j] });
  11727. }
  11728. }
  11729. // sort by primary user flag and signature creation time
  11730. primUser = primUser.sort(function (a, b) {
  11731. if (a.selfCertificate.isPrimaryUserID > b.selfCertificate.isPrimaryUserID) {
  11732. return -1;
  11733. } else if (a.selfCertificate.isPrimaryUserID < b.selfCertificate.isPrimaryUserID) {
  11734. return 1;
  11735. } else if (a.selfCertificate.created > b.selfCertificate.created) {
  11736. return -1;
  11737. } else if (a.selfCertificate.created < b.selfCertificate.created) {
  11738. return 1;
  11739. } else {
  11740. return 0;
  11741. }
  11742. });
  11743. // return first valid
  11744. for (var k = 0; k < primUser.length; k++) {
  11745. if (primUser[k].user.isValidSelfCertificate(this.primaryKey, primUser[k].selfCertificate)) {
  11746. return primUser[k];
  11747. }
  11748. }
  11749. return null;
  11750. };
  11751. /**
  11752. * Update key with new components from specified key with same key ID:
  11753. * users, subkeys, certificates are merged into the destination key,
  11754. * duplicates are ignored.
  11755. * If the specified key is a private key and the destination key is public,
  11756. * the destination key is tranformed to a private key.
  11757. * @param {module:key~Key} key source key to merge
  11758. */
  11759. Key.prototype.update = function (key) {
  11760. var that = this;
  11761. if (key.verifyPrimaryKey() === _enums2.default.keyStatus.invalid) {
  11762. return;
  11763. }
  11764. if (this.primaryKey.getFingerprint() !== key.primaryKey.getFingerprint()) {
  11765. throw new Error('Key update method: fingerprints of keys not equal');
  11766. }
  11767. if (this.isPublic() && key.isPrivate()) {
  11768. // check for equal subkey packets
  11769. var equal = (this.subKeys && this.subKeys.length) === (key.subKeys && key.subKeys.length) && (!this.subKeys || this.subKeys.every(function (destSubKey) {
  11770. return key.subKeys.some(function (srcSubKey) {
  11771. return destSubKey.subKey.getFingerprint() === srcSubKey.subKey.getFingerprint();
  11772. });
  11773. }));
  11774. if (!equal) {
  11775. throw new Error('Cannot update public key with private key if subkey mismatch');
  11776. }
  11777. this.primaryKey = key.primaryKey;
  11778. }
  11779. // revocation signature
  11780. if (!this.revocationSignature && key.revocationSignature && !key.revocationSignature.isExpired() && (key.revocationSignature.verified || key.revocationSignature.verify(key.primaryKey, { key: key.primaryKey }))) {
  11781. this.revocationSignature = key.revocationSignature;
  11782. }
  11783. // direct signatures
  11784. mergeSignatures(key, this, 'directSignatures');
  11785. // users
  11786. key.users.forEach(function (srcUser) {
  11787. var found = false;
  11788. for (var i = 0; i < that.users.length; i++) {
  11789. if (srcUser.userId && srcUser.userId.userid === that.users[i].userId.userid || srcUser.userAttribute && srcUser.userAttribute.equals(that.users[i].userAttribute)) {
  11790. that.users[i].update(srcUser, that.primaryKey);
  11791. found = true;
  11792. break;
  11793. }
  11794. }
  11795. if (!found) {
  11796. that.users.push(srcUser);
  11797. }
  11798. });
  11799. // subkeys
  11800. if (key.subKeys) {
  11801. key.subKeys.forEach(function (srcSubKey) {
  11802. var found = false;
  11803. for (var i = 0; i < that.subKeys.length; i++) {
  11804. if (srcSubKey.subKey.getFingerprint() === that.subKeys[i].subKey.getFingerprint()) {
  11805. that.subKeys[i].update(srcSubKey, that.primaryKey);
  11806. found = true;
  11807. break;
  11808. }
  11809. }
  11810. if (!found) {
  11811. that.subKeys.push(srcSubKey);
  11812. }
  11813. });
  11814. }
  11815. };
  11816. /**
  11817. * Merges signatures from source[attr] to dest[attr]
  11818. * @private
  11819. * @param {Object} source
  11820. * @param {Object} dest
  11821. * @param {String} attr
  11822. * @param {Function} checkFn optional, signature only merged if true
  11823. */
  11824. function mergeSignatures(source, dest, attr, checkFn) {
  11825. source = source[attr];
  11826. if (source) {
  11827. if (!dest[attr]) {
  11828. dest[attr] = source;
  11829. } else {
  11830. source.forEach(function (sourceSig) {
  11831. if (!sourceSig.isExpired() && (!checkFn || checkFn(sourceSig)) && !dest[attr].some(function (destSig) {
  11832. return _util2.default.equalsUint8Array(destSig.signature, sourceSig.signature);
  11833. })) {
  11834. dest[attr].push(sourceSig);
  11835. }
  11836. });
  11837. }
  11838. }
  11839. }
  11840. // TODO
  11841. Key.prototype.revoke = function () {};
  11842. /**
  11843. * Signs primary user of key
  11844. * @param {Array<module:key~Key>} privateKey decrypted private keys for signing
  11845. * @return {module:key~Key} new public key with new certificate signature
  11846. */
  11847. Key.prototype.signPrimaryUser = function (privateKeys) {
  11848. var _ref = this.getPrimaryUser() || {};
  11849. var index = _ref.index;
  11850. var user = _ref.user;
  11851. if (!user) {
  11852. throw new Error('Could not find primary user');
  11853. }
  11854. user = user.sign(this.primaryKey, privateKeys);
  11855. var key = new Key(this.toPacketlist());
  11856. key.users[index] = user;
  11857. return key;
  11858. };
  11859. /**
  11860. * Signs all users of key
  11861. * @param {Array<module:key~Key>} privateKeys decrypted private keys for signing
  11862. * @return {module:key~Key} new public key with new certificate signature
  11863. */
  11864. Key.prototype.signAllUsers = function (privateKeys) {
  11865. var _this = this;
  11866. var users = this.users.map(function (user) {
  11867. return user.sign(_this.primaryKey, privateKeys);
  11868. });
  11869. var key = new Key(this.toPacketlist());
  11870. key.users = users;
  11871. return key;
  11872. };
  11873. /**
  11874. * Verifies primary user of key
  11875. * @param {Array<module:key~Key>} keys array of keys to verify certificate signatures
  11876. * @return {Array<({keyid: module:type/keyid, valid: Boolean})>} list of signer's keyid and validity of signature
  11877. */
  11878. Key.prototype.verifyPrimaryUser = function (keys) {
  11879. var _ref2 = this.getPrimaryUser() || {};
  11880. var user = _ref2.user;
  11881. if (!user) {
  11882. throw new Error('Could not find primary user');
  11883. }
  11884. return user.verifyAllSignatures(this.primaryKey, keys);
  11885. };
  11886. /**
  11887. * Verifies all users of key
  11888. * @param {Array<module:key~Key>} keys array of keys to verify certificate signatures
  11889. * @return {Array<({userid: String, keyid: module:type/keyid, valid: Boolean})>} list of userid, signer's keyid and validity of signature
  11890. */
  11891. Key.prototype.verifyAllUsers = function (keys) {
  11892. var _this2 = this;
  11893. return this.users.reduce(function (signatures, user) {
  11894. return signatures.concat(user.verifyAllSignatures(_this2.primaryKey, keys).map(function (signature) {
  11895. return {
  11896. userid: user.userId.userid,
  11897. keyid: signature.keyid,
  11898. valid: signature.valid
  11899. };
  11900. }));
  11901. }, []);
  11902. };
  11903. /**
  11904. * @class
  11905. * @classdesc Class that represents an user ID or attribute packet and the relevant signatures.
  11906. */
  11907. function User(userPacket) {
  11908. if (!(this instanceof User)) {
  11909. return new User(userPacket);
  11910. }
  11911. this.userId = userPacket.tag === _enums2.default.packet.userid ? userPacket : null;
  11912. this.userAttribute = userPacket.tag === _enums2.default.packet.userAttribute ? userPacket : null;
  11913. this.selfCertifications = null;
  11914. this.otherCertifications = null;
  11915. this.revocationCertifications = null;
  11916. }
  11917. /**
  11918. * Transforms structured user data to packetlist
  11919. * @return {module:packet/packetlist}
  11920. */
  11921. User.prototype.toPacketlist = function () {
  11922. var packetlist = new _packet2.default.List();
  11923. packetlist.push(this.userId || this.userAttribute);
  11924. packetlist.concat(this.revocationCertifications);
  11925. packetlist.concat(this.selfCertifications);
  11926. packetlist.concat(this.otherCertifications);
  11927. return packetlist;
  11928. };
  11929. /**
  11930. * Checks if a self signature of the user is revoked
  11931. * @param {module:packet/signature} certificate
  11932. * @param {module:packet/secret_key|module:packet/public_key} primaryKey The primary key packet
  11933. * @return {Boolean} True if the certificate is revoked
  11934. */
  11935. User.prototype.isRevoked = function (certificate, primaryKey) {
  11936. if (this.revocationCertifications) {
  11937. var that = this;
  11938. return this.revocationCertifications.some(function (revCert) {
  11939. return revCert.issuerKeyId.equals(certificate.issuerKeyId) && !revCert.isExpired() && (revCert.verified || revCert.verify(primaryKey, { userid: that.userId || that.userAttribute, key: primaryKey }));
  11940. });
  11941. } else {
  11942. return false;
  11943. }
  11944. };
  11945. /**
  11946. * Returns the most significant (latest valid) self signature of the user
  11947. * @param {module:packet/secret_key|module:packet/public_key} primaryKey The primary key packet
  11948. * @return {module:packet/signature} The self signature
  11949. */
  11950. User.prototype.getValidSelfCertificate = function (primaryKey) {
  11951. if (!this.selfCertifications) {
  11952. return null;
  11953. }
  11954. // most recent first
  11955. var validCert = this.selfCertifications.sort(function (a, b) {
  11956. a = a.created;
  11957. b = b.created;
  11958. return a > b ? -1 : a < b ? 1 : 0;
  11959. });
  11960. for (var i = 0; i < validCert.length; i++) {
  11961. if (this.isValidSelfCertificate(primaryKey, validCert[i])) {
  11962. return validCert[i];
  11963. }
  11964. }
  11965. return null;
  11966. };
  11967. /**
  11968. * Returns true if the self certificate is valid
  11969. * @param {module:packet/secret_key|module:packet/public_key} primaryKey The primary key packet
  11970. * @param {module:packet/signature} selfCertificate A self certificate of this user
  11971. * @return {Boolean}
  11972. */
  11973. User.prototype.isValidSelfCertificate = function (primaryKey, selfCertificate) {
  11974. if (this.isRevoked(selfCertificate, primaryKey)) {
  11975. return false;
  11976. }
  11977. if (!selfCertificate.isExpired() && (selfCertificate.verified || selfCertificate.verify(primaryKey, { userid: this.userId || this.userAttribute, key: primaryKey }))) {
  11978. return true;
  11979. }
  11980. return false;
  11981. };
  11982. /**
  11983. * Signs user
  11984. * @param {module:packet/secret_key|module:packet/public_key} primaryKey The primary key packet
  11985. * @param {Array<module:key~Key>} privateKeys decrypted private keys for signing
  11986. * @return {module:key~Key} new user with new certificate signatures
  11987. */
  11988. User.prototype.sign = function (primaryKey, privateKeys) {
  11989. var user, dataToSign, signingKeyPacket, signaturePacket;
  11990. dataToSign = {};
  11991. dataToSign.key = primaryKey;
  11992. dataToSign.userid = this.userId || this.userAttribute;
  11993. user = new User(this.userId || this.userAttribute);
  11994. user.otherCertifications = [];
  11995. privateKeys.forEach(function (privateKey) {
  11996. if (privateKey.isPublic()) {
  11997. throw new Error('Need private key for signing');
  11998. }
  11999. if (privateKey.primaryKey.getFingerprint() === primaryKey.getFingerprint()) {
  12000. throw new Error('Not implemented for self signing');
  12001. }
  12002. signingKeyPacket = privateKey.getSigningKeyPacket();
  12003. if (!signingKeyPacket) {
  12004. throw new Error('Could not find valid signing key packet');
  12005. }
  12006. if (!signingKeyPacket.isDecrypted) {
  12007. throw new Error('Private key is not decrypted.');
  12008. }
  12009. signaturePacket = new _packet2.default.Signature();
  12010. // Most OpenPGP implementations use generic certification (0x10)
  12011. signaturePacket.signatureType = _enums2.default.write(_enums2.default.signature, _enums2.default.signature.cert_generic);
  12012. signaturePacket.keyFlags = [_enums2.default.keyFlags.certify_keys | _enums2.default.keyFlags.sign_data];
  12013. signaturePacket.hashAlgorithm = privateKey.getPreferredHashAlgorithm();
  12014. signaturePacket.publicKeyAlgorithm = signingKeyPacket.algorithm;
  12015. signaturePacket.signingKeyId = signingKeyPacket.getKeyId();
  12016. signaturePacket.sign(signingKeyPacket, dataToSign);
  12017. user.otherCertifications.push(signaturePacket);
  12018. });
  12019. user.update(this, primaryKey);
  12020. return user;
  12021. };
  12022. /**
  12023. * Verifies all user signatures
  12024. * @param {module:packet/secret_key|module:packet/public_key} primaryKey The primary key packet
  12025. * @param {Array<module:key~Key>} keys array of keys to verify certificate signatures
  12026. * @return {Array<({keyid: module:type/keyid, valid: Boolean})>} list of signer's keyid and validity of signature
  12027. */
  12028. User.prototype.verifyAllSignatures = function (primaryKey, keys) {
  12029. var dataToVerify = { userid: this.userId || this.userAttribute, key: primaryKey };
  12030. var certificates = this.selfCertifications.concat(this.otherCertifications || []);
  12031. return certificates.map(function (signaturePacket) {
  12032. var keyPackets = keys.filter(function (key) {
  12033. return key.getSigningKeyPacket(signaturePacket.issuerKeyId);
  12034. });
  12035. var valid = null;
  12036. if (keyPackets.length > 0) {
  12037. valid = keyPackets.some(function (keyPacket) {
  12038. return signaturePacket.verify(keyPacket.primaryKey, dataToVerify);
  12039. });
  12040. }
  12041. return { keyid: signaturePacket.issuerKeyId, valid: valid };
  12042. });
  12043. };
  12044. /**
  12045. * Verify User. Checks for existence of self signatures, revocation signatures
  12046. * and validity of self signature
  12047. * @param {module:packet/secret_key|module:packet/public_key} primaryKey The primary key packet
  12048. * @return {module:enums.keyStatus} status of user
  12049. */
  12050. User.prototype.verify = function (primaryKey) {
  12051. if (!this.selfCertifications) {
  12052. return _enums2.default.keyStatus.no_self_cert;
  12053. }
  12054. var status;
  12055. for (var i = 0; i < this.selfCertifications.length; i++) {
  12056. if (this.isRevoked(this.selfCertifications[i], primaryKey)) {
  12057. status = _enums2.default.keyStatus.revoked;
  12058. continue;
  12059. }
  12060. if (!(this.selfCertifications[i].verified || this.selfCertifications[i].verify(primaryKey, { userid: this.userId || this.userAttribute, key: primaryKey }))) {
  12061. status = _enums2.default.keyStatus.invalid;
  12062. continue;
  12063. }
  12064. if (this.selfCertifications[i].isExpired()) {
  12065. status = _enums2.default.keyStatus.expired;
  12066. continue;
  12067. }
  12068. status = _enums2.default.keyStatus.valid;
  12069. break;
  12070. }
  12071. return status;
  12072. };
  12073. /**
  12074. * Update user with new components from specified user
  12075. * @param {module:key~User} user source user to merge
  12076. * @param {module:packet/signature} primaryKey primary key used for validation
  12077. */
  12078. User.prototype.update = function (user, primaryKey) {
  12079. var that = this;
  12080. // self signatures
  12081. mergeSignatures(user, this, 'selfCertifications', function (srcSelfSig) {
  12082. return srcSelfSig.verified || srcSelfSig.verify(primaryKey, { userid: that.userId || that.userAttribute, key: primaryKey });
  12083. });
  12084. // other signatures
  12085. mergeSignatures(user, this, 'otherCertifications');
  12086. // revocation signatures
  12087. mergeSignatures(user, this, 'revocationCertifications');
  12088. };
  12089. /**
  12090. * @class
  12091. * @classdesc Class that represents a subkey packet and the relevant signatures.
  12092. */
  12093. function SubKey(subKeyPacket) {
  12094. if (!(this instanceof SubKey)) {
  12095. return new SubKey(subKeyPacket);
  12096. }
  12097. this.subKey = subKeyPacket;
  12098. this.bindingSignature = null;
  12099. this.revocationSignature = null;
  12100. }
  12101. /**
  12102. * Transforms structured subkey data to packetlist
  12103. * @return {module:packet/packetlist}
  12104. */
  12105. SubKey.prototype.toPacketlist = function () {
  12106. var packetlist = new _packet2.default.List();
  12107. packetlist.push(this.subKey);
  12108. packetlist.push(this.revocationSignature);
  12109. packetlist.push(this.bindingSignature);
  12110. return packetlist;
  12111. };
  12112. /**
  12113. * Returns true if the subkey can be used for encryption
  12114. * @param {module:packet/secret_key|module:packet/public_key} primaryKey The primary key packet
  12115. * @return {Boolean}
  12116. */
  12117. SubKey.prototype.isValidEncryptionKey = function (primaryKey) {
  12118. return this.verify(primaryKey) === _enums2.default.keyStatus.valid && isValidEncryptionKeyPacket(this.subKey, this.bindingSignature);
  12119. };
  12120. /**
  12121. * Returns true if the subkey can be used for signing of data
  12122. * @param {module:packet/secret_key|module:packet/public_key} primaryKey The primary key packet
  12123. * @return {Boolean}
  12124. */
  12125. SubKey.prototype.isValidSigningKey = function (primaryKey) {
  12126. return this.verify(primaryKey) === _enums2.default.keyStatus.valid && isValidSigningKeyPacket(this.subKey, this.bindingSignature);
  12127. };
  12128. /**
  12129. * Verify subkey. Checks for revocation signatures, expiration time
  12130. * and valid binding signature
  12131. * @return {module:enums.keyStatus} The status of the subkey
  12132. */
  12133. SubKey.prototype.verify = function (primaryKey) {
  12134. // check subkey revocation signature
  12135. if (this.revocationSignature && !this.revocationSignature.isExpired() && (this.revocationSignature.verified || this.revocationSignature.verify(primaryKey, { key: primaryKey, bind: this.subKey }))) {
  12136. return _enums2.default.keyStatus.revoked;
  12137. }
  12138. // check V3 expiration time
  12139. if (this.subKey.version === 3 && this.subKey.expirationTimeV3 !== 0 && Date.now() > this.subKey.created.getTime() + this.subKey.expirationTimeV3 * 24 * 3600 * 1000) {
  12140. return _enums2.default.keyStatus.expired;
  12141. }
  12142. // check subkey binding signature
  12143. if (!this.bindingSignature) {
  12144. return _enums2.default.keyStatus.invalid;
  12145. }
  12146. if (this.bindingSignature.isExpired()) {
  12147. return _enums2.default.keyStatus.expired;
  12148. }
  12149. if (!(this.bindingSignature.verified || this.bindingSignature.verify(primaryKey, { key: primaryKey, bind: this.subKey }))) {
  12150. return _enums2.default.keyStatus.invalid;
  12151. }
  12152. // check V4 expiration time
  12153. if (this.subKey.version === 4 && this.bindingSignature.keyNeverExpires === false && Date.now() > this.subKey.created.getTime() + this.bindingSignature.keyExpirationTime * 1000) {
  12154. return _enums2.default.keyStatus.expired;
  12155. }
  12156. return _enums2.default.keyStatus.valid;
  12157. };
  12158. /**
  12159. * Returns the expiration time of the subkey or null if key does not expire
  12160. * @return {Date|null}
  12161. */
  12162. SubKey.prototype.getExpirationTime = function () {
  12163. return getExpirationTime(this.subKey, this.bindingSignature);
  12164. };
  12165. /**
  12166. * Update subkey with new components from specified subkey
  12167. * @param {module:key~SubKey} subKey source subkey to merge
  12168. * @param {module:packet/signature} primaryKey primary key used for validation
  12169. */
  12170. SubKey.prototype.update = function (subKey, primaryKey) {
  12171. if (subKey.verify(primaryKey) === _enums2.default.keyStatus.invalid) {
  12172. return;
  12173. }
  12174. if (this.subKey.getFingerprint() !== subKey.subKey.getFingerprint()) {
  12175. throw new Error('SubKey update method: fingerprints of subkeys not equal');
  12176. }
  12177. // key packet
  12178. if (this.subKey.tag === _enums2.default.packet.publicSubkey && subKey.subKey.tag === _enums2.default.packet.secretSubkey) {
  12179. this.subKey = subKey.subKey;
  12180. }
  12181. // binding signature
  12182. if (!this.bindingSignature && subKey.bindingSignature && (subKey.bindingSignature.verified || subKey.bindingSignature.verify(primaryKey, { key: primaryKey, bind: this.subKey }))) {
  12183. this.bindingSignature = subKey.bindingSignature;
  12184. }
  12185. // revocation signature
  12186. if (!this.revocationSignature && subKey.revocationSignature && !subKey.revocationSignature.isExpired() && (subKey.revocationSignature.verified || subKey.revocationSignature.verify(primaryKey, { key: primaryKey, bind: this.subKey }))) {
  12187. this.revocationSignature = subKey.revocationSignature;
  12188. }
  12189. };
  12190. /**
  12191. * Reads an OpenPGP armored text and returns one or multiple key objects
  12192. * @param {String} armoredText text to be parsed
  12193. * @return {{keys: Array<module:key~Key>, err: (Array<Error>|null)}} result object with key and error arrays
  12194. * @static
  12195. */
  12196. function readArmored(armoredText) {
  12197. var result = {};
  12198. result.keys = [];
  12199. try {
  12200. var input = _armor2.default.decode(armoredText);
  12201. if (!(input.type === _enums2.default.armor.public_key || input.type === _enums2.default.armor.private_key)) {
  12202. throw new Error('Armored text not of type key');
  12203. }
  12204. var packetlist = new _packet2.default.List();
  12205. packetlist.read(input.data);
  12206. var keyIndex = packetlist.indexOfTag(_enums2.default.packet.publicKey, _enums2.default.packet.secretKey);
  12207. if (keyIndex.length === 0) {
  12208. throw new Error('No key packet found in armored text');
  12209. }
  12210. for (var i = 0; i < keyIndex.length; i++) {
  12211. var oneKeyList = packetlist.slice(keyIndex[i], keyIndex[i + 1]);
  12212. try {
  12213. var newKey = new Key(oneKeyList);
  12214. result.keys.push(newKey);
  12215. } catch (e) {
  12216. result.err = result.err || [];
  12217. result.err.push(e);
  12218. }
  12219. }
  12220. } catch (e) {
  12221. result.err = result.err || [];
  12222. result.err.push(e);
  12223. }
  12224. return result;
  12225. }
  12226. /**
  12227. * Generates a new OpenPGP key. Currently only supports RSA keys.
  12228. * Primary and subkey will be of same type.
  12229. * @param {module:enums.publicKey} [options.keyType=module:enums.publicKey.rsa_encrypt_sign] to indicate what type of key to make.
  12230. * RSA is 1. See {@link http://tools.ietf.org/html/rfc4880#section-9.1}
  12231. * @param {Integer} options.numBits number of bits for the key creation.
  12232. * @param {String|Array<String>} options.userIds assumes already in form of "User Name <username@email.com>"
  12233. If array is used, the first userId is set as primary user Id
  12234. * @param {String} options.passphrase The passphrase used to encrypt the resulting private key
  12235. * @param {Boolean} [options.unlocked=false] The secret part of the generated key is unlocked
  12236. * @param {Number} [options.keyExpirationTime=0] The number of seconds after the key creation time that the key expires
  12237. * @return {module:key~Key}
  12238. * @static
  12239. */
  12240. function generate(options) {
  12241. var secretKeyPacket, secretSubkeyPacket;
  12242. return Promise.resolve().then(function () {
  12243. options.keyType = options.keyType || _enums2.default.publicKey.rsa_encrypt_sign;
  12244. if (options.keyType !== _enums2.default.publicKey.rsa_encrypt_sign) {
  12245. // RSA Encrypt-Only and RSA Sign-Only are deprecated and SHOULD NOT be generated
  12246. throw new Error('Only RSA Encrypt or Sign supported');
  12247. }
  12248. if (!options.passphrase) {
  12249. // Key without passphrase is unlocked by definition
  12250. options.unlocked = true;
  12251. }
  12252. if (String.prototype.isPrototypeOf(options.userIds) || typeof options.userIds === 'string') {
  12253. options.userIds = [options.userIds];
  12254. }
  12255. return Promise.all([generateSecretKey(), generateSecretSubkey()]).then(function () {
  12256. return wrapKeyObject(secretKeyPacket, secretSubkeyPacket, options);
  12257. });
  12258. });
  12259. function generateSecretKey() {
  12260. secretKeyPacket = new _packet2.default.SecretKey();
  12261. secretKeyPacket.algorithm = _enums2.default.read(_enums2.default.publicKey, options.keyType);
  12262. return secretKeyPacket.generate(options.numBits);
  12263. }
  12264. function generateSecretSubkey() {
  12265. secretSubkeyPacket = new _packet2.default.SecretSubkey();
  12266. secretSubkeyPacket.algorithm = _enums2.default.read(_enums2.default.publicKey, options.keyType);
  12267. return secretSubkeyPacket.generate(options.numBits);
  12268. }
  12269. }
  12270. /**
  12271. * Reformats and signs an OpenPGP with a given User ID. Currently only supports RSA keys.
  12272. * @param {module:key~Key} options.privateKey The private key to reformat
  12273. * @param {module:enums.publicKey} [options.keyType=module:enums.publicKey.rsa_encrypt_sign]
  12274. * @param {String|Array<String>} options.userIds assumes already in form of "User Name <username@email.com>"
  12275. If array is used, the first userId is set as primary user Id
  12276. * @param {String} options.passphrase The passphrase used to encrypt the resulting private key
  12277. * @param {Boolean} [options.unlocked=false] The secret part of the generated key is unlocked
  12278. * @param {Number} [options.keyExpirationTime=0] The number of seconds after the key creation time that the key expires
  12279. * @return {module:key~Key}
  12280. * @static
  12281. */
  12282. function reformat(options) {
  12283. var secretKeyPacket, secretSubkeyPacket;
  12284. return Promise.resolve().then(function () {
  12285. options.keyType = options.keyType || _enums2.default.publicKey.rsa_encrypt_sign;
  12286. if (options.keyType !== _enums2.default.publicKey.rsa_encrypt_sign) {
  12287. // RSA Encrypt-Only and RSA Sign-Only are deprecated and SHOULD NOT be generated
  12288. throw new Error('Only RSA Encrypt or Sign supported');
  12289. }
  12290. if (!options.passphrase) {
  12291. // Key without passphrase is unlocked by definition
  12292. options.unlocked = true;
  12293. }
  12294. if (String.prototype.isPrototypeOf(options.userIds) || typeof options.userIds === 'string') {
  12295. options.userIds = [options.userIds];
  12296. }
  12297. var packetlist = options.privateKey.toPacketlist();
  12298. for (var i = 0; i < packetlist.length; i++) {
  12299. if (packetlist[i].tag === _enums2.default.packet.secretKey) {
  12300. secretKeyPacket = packetlist[i];
  12301. } else if (packetlist[i].tag === _enums2.default.packet.secretSubkey) {
  12302. secretSubkeyPacket = packetlist[i];
  12303. }
  12304. }
  12305. return wrapKeyObject(secretKeyPacket, secretSubkeyPacket, options);
  12306. });
  12307. }
  12308. function wrapKeyObject(secretKeyPacket, secretSubkeyPacket, options) {
  12309. // set passphrase protection
  12310. if (options.passphrase) {
  12311. secretKeyPacket.encrypt(options.passphrase);
  12312. secretSubkeyPacket.encrypt(options.passphrase);
  12313. }
  12314. var packetlist = new _packet2.default.List();
  12315. packetlist.push(secretKeyPacket);
  12316. options.userIds.forEach(function (userId, index) {
  12317. var userIdPacket = new _packet2.default.Userid();
  12318. userIdPacket.read(_util2.default.str2Uint8Array(userId));
  12319. var dataToSign = {};
  12320. dataToSign.userid = userIdPacket;
  12321. dataToSign.key = secretKeyPacket;
  12322. var signaturePacket = new _packet2.default.Signature();
  12323. signaturePacket.signatureType = _enums2.default.signature.cert_generic;
  12324. signaturePacket.publicKeyAlgorithm = options.keyType;
  12325. signaturePacket.hashAlgorithm = _config2.default.prefer_hash_algorithm;
  12326. signaturePacket.keyFlags = [_enums2.default.keyFlags.certify_keys | _enums2.default.keyFlags.sign_data];
  12327. signaturePacket.preferredSymmetricAlgorithms = [];
  12328. // prefer aes256, aes128, then aes192 (no WebCrypto support: https://www.chromium.org/blink/webcrypto#TOC-AES-support)
  12329. signaturePacket.preferredSymmetricAlgorithms.push(_enums2.default.symmetric.aes256);
  12330. signaturePacket.preferredSymmetricAlgorithms.push(_enums2.default.symmetric.aes128);
  12331. signaturePacket.preferredSymmetricAlgorithms.push(_enums2.default.symmetric.aes192);
  12332. signaturePacket.preferredSymmetricAlgorithms.push(_enums2.default.symmetric.cast5);
  12333. signaturePacket.preferredSymmetricAlgorithms.push(_enums2.default.symmetric.tripledes);
  12334. signaturePacket.preferredHashAlgorithms = [];
  12335. // prefer fast asm.js implementations (SHA-256). SHA-1 will not be secure much longer...move to bottom of list
  12336. signaturePacket.preferredHashAlgorithms.push(_enums2.default.hash.sha256);
  12337. signaturePacket.preferredHashAlgorithms.push(_enums2.default.hash.sha512);
  12338. signaturePacket.preferredHashAlgorithms.push(_enums2.default.hash.sha1);
  12339. signaturePacket.preferredCompressionAlgorithms = [];
  12340. signaturePacket.preferredCompressionAlgorithms.push(_enums2.default.compression.zlib);
  12341. signaturePacket.preferredCompressionAlgorithms.push(_enums2.default.compression.zip);
  12342. if (index === 0) {
  12343. signaturePacket.isPrimaryUserID = true;
  12344. }
  12345. if (_config2.default.integrity_protect) {
  12346. signaturePacket.features = [];
  12347. signaturePacket.features.push(1); // Modification Detection
  12348. }
  12349. if (options.keyExpirationTime > 0) {
  12350. signaturePacket.keyExpirationTime = options.keyExpirationTime;
  12351. signaturePacket.keyNeverExpires = false;
  12352. }
  12353. signaturePacket.sign(secretKeyPacket, dataToSign);
  12354. packetlist.push(userIdPacket);
  12355. packetlist.push(signaturePacket);
  12356. });
  12357. var dataToSign = {};
  12358. dataToSign.key = secretKeyPacket;
  12359. dataToSign.bind = secretSubkeyPacket;
  12360. var subkeySignaturePacket = new _packet2.default.Signature();
  12361. subkeySignaturePacket.signatureType = _enums2.default.signature.subkey_binding;
  12362. subkeySignaturePacket.publicKeyAlgorithm = options.keyType;
  12363. subkeySignaturePacket.hashAlgorithm = _config2.default.prefer_hash_algorithm;
  12364. subkeySignaturePacket.keyFlags = [_enums2.default.keyFlags.encrypt_communication | _enums2.default.keyFlags.encrypt_storage];
  12365. subkeySignaturePacket.sign(secretKeyPacket, dataToSign);
  12366. packetlist.push(secretSubkeyPacket);
  12367. packetlist.push(subkeySignaturePacket);
  12368. if (!options.unlocked) {
  12369. secretKeyPacket.clearPrivateMPIs();
  12370. secretSubkeyPacket.clearPrivateMPIs();
  12371. }
  12372. return new Key(packetlist);
  12373. }
  12374. /**
  12375. * Returns the preferred symmetric algorithm for a set of keys
  12376. * @param {Array<module:key~Key>} keys Set of keys
  12377. * @return {enums.symmetric} Preferred symmetric algorithm
  12378. */
  12379. function getPreferredSymAlgo(keys) {
  12380. var prioMap = {};
  12381. keys.forEach(function (key) {
  12382. var primaryUser = key.getPrimaryUser();
  12383. if (!primaryUser || !primaryUser.selfCertificate.preferredSymmetricAlgorithms) {
  12384. return _config2.default.encryption_cipher;
  12385. }
  12386. primaryUser.selfCertificate.preferredSymmetricAlgorithms.forEach(function (algo, index) {
  12387. var entry = prioMap[algo] || (prioMap[algo] = { prio: 0, count: 0, algo: algo });
  12388. entry.prio += 64 >> index;
  12389. entry.count++;
  12390. });
  12391. });
  12392. var prefAlgo = { prio: 0, algo: _config2.default.encryption_cipher };
  12393. for (var algo in prioMap) {
  12394. try {
  12395. if (algo !== _enums2.default.symmetric.plaintext && algo !== _enums2.default.symmetric.idea && // not implemented
  12396. _enums2.default.read(_enums2.default.symmetric, algo) && // known algorithm
  12397. prioMap[algo].count === keys.length && // available for all keys
  12398. prioMap[algo].prio > prefAlgo.prio) {
  12399. prefAlgo = prioMap[algo];
  12400. }
  12401. } catch (e) {}
  12402. }
  12403. return prefAlgo.algo;
  12404. }
  12405. },{"./config":10,"./encoding/armor.js":33,"./enums.js":35,"./packet":47,"./util":70}],39:[function(_dereq_,module,exports){
  12406. 'use strict';
  12407. /**
  12408. * @see module:keyring/keyring
  12409. * @module keyring
  12410. */
  12411. Object.defineProperty(exports, "__esModule", {
  12412. value: true
  12413. });
  12414. var _keyring = _dereq_('./keyring.js');
  12415. var _keyring2 = _interopRequireDefault(_keyring);
  12416. var _localstore = _dereq_('./localstore.js');
  12417. var _localstore2 = _interopRequireDefault(_localstore);
  12418. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12419. _keyring2.default.localstore = _localstore2.default;
  12420. exports.default = _keyring2.default;
  12421. },{"./keyring.js":40,"./localstore.js":41}],40:[function(_dereq_,module,exports){
  12422. // GPG4Browsers - An OpenPGP implementation in javascript
  12423. // Copyright (C) 2011 Recurity Labs GmbH
  12424. //
  12425. // This library is free software; you can redistribute it and/or
  12426. // modify it under the terms of the GNU Lesser General Public
  12427. // License as published by the Free Software Foundation; either
  12428. // version 3.0 of the License, or (at your option) any later version.
  12429. //
  12430. // This library is distributed in the hope that it will be useful,
  12431. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12432. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12433. // Lesser General Public License for more details.
  12434. //
  12435. // You should have received a copy of the GNU Lesser General Public
  12436. // License along with this library; if not, write to the Free Software
  12437. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  12438. /**
  12439. * The class that deals with storage of the keyring. Currently the only option is to use HTML5 local storage.
  12440. * @requires enums
  12441. * @requires key
  12442. * @requires util
  12443. * @module keyring/keyring
  12444. */
  12445. 'use strict';
  12446. Object.defineProperty(exports, "__esModule", {
  12447. value: true
  12448. });
  12449. exports.default = Keyring;
  12450. var _key = _dereq_('../key.js');
  12451. var keyModule = _interopRequireWildcard(_key);
  12452. var _localstore = _dereq_('./localstore.js');
  12453. var _localstore2 = _interopRequireDefault(_localstore);
  12454. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12455. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  12456. /**
  12457. * Initialization routine for the keyring. This method reads the
  12458. * keyring from HTML5 local storage and initializes this instance.
  12459. * @constructor
  12460. * @param {class} [storeHandler] class implementing load() and store() methods
  12461. */
  12462. function Keyring(storeHandler) {
  12463. this.storeHandler = storeHandler || new _localstore2.default();
  12464. this.publicKeys = new KeyArray(this.storeHandler.loadPublic());
  12465. this.privateKeys = new KeyArray(this.storeHandler.loadPrivate());
  12466. }
  12467. /**
  12468. * Calls the storeHandler to save the keys
  12469. */
  12470. Keyring.prototype.store = function () {
  12471. this.storeHandler.storePublic(this.publicKeys.keys);
  12472. this.storeHandler.storePrivate(this.privateKeys.keys);
  12473. };
  12474. /**
  12475. * Clear the keyring - erase all the keys
  12476. */
  12477. Keyring.prototype.clear = function () {
  12478. this.publicKeys.keys = [];
  12479. this.privateKeys.keys = [];
  12480. };
  12481. /**
  12482. * Searches the keyring for keys having the specified key id
  12483. * @param {String} keyId provided as string of lowercase hex number
  12484. * withouth 0x prefix (can be 16-character key ID or fingerprint)
  12485. * @param {Boolean} deep if true search also in subkeys
  12486. * @return {Array<module:key~Key>|null} keys found or null
  12487. */
  12488. Keyring.prototype.getKeysForId = function (keyId, deep) {
  12489. var result = [];
  12490. result = result.concat(this.publicKeys.getForId(keyId, deep) || []);
  12491. result = result.concat(this.privateKeys.getForId(keyId, deep) || []);
  12492. return result.length ? result : null;
  12493. };
  12494. /**
  12495. * Removes keys having the specified key id from the keyring
  12496. * @param {String} keyId provided as string of lowercase hex number
  12497. * withouth 0x prefix (can be 16-character key ID or fingerprint)
  12498. * @return {Array<module:key~Key>|null} keys found or null
  12499. */
  12500. Keyring.prototype.removeKeysForId = function (keyId) {
  12501. var result = [];
  12502. result = result.concat(this.publicKeys.removeForId(keyId) || []);
  12503. result = result.concat(this.privateKeys.removeForId(keyId) || []);
  12504. return result.length ? result : null;
  12505. };
  12506. /**
  12507. * Get all public and private keys
  12508. * @return {Array<module:key~Key>} all keys
  12509. */
  12510. Keyring.prototype.getAllKeys = function () {
  12511. return this.publicKeys.keys.concat(this.privateKeys.keys);
  12512. };
  12513. /**
  12514. * Array of keys
  12515. * @param {Array<module:key~Key>} keys The keys to store in this array
  12516. */
  12517. function KeyArray(keys) {
  12518. this.keys = keys;
  12519. }
  12520. /**
  12521. * Searches all keys in the KeyArray matching the address or address part of the user ids
  12522. * @param {String} email email address to search for
  12523. * @return {Array<module:key~Key>} The public keys associated with provided email address.
  12524. */
  12525. KeyArray.prototype.getForAddress = function (email) {
  12526. var results = [];
  12527. for (var i = 0; i < this.keys.length; i++) {
  12528. if (emailCheck(email, this.keys[i])) {
  12529. results.push(this.keys[i]);
  12530. }
  12531. }
  12532. return results;
  12533. };
  12534. /**
  12535. * Checks a key to see if it matches the specified email address
  12536. * @private
  12537. * @param {String} email email address to search for
  12538. * @param {module:key~Key} key The key to be checked.
  12539. * @return {Boolean} True if the email address is defined in the specified key
  12540. */
  12541. function emailCheck(email, key) {
  12542. email = email.toLowerCase();
  12543. // escape email before using in regular expression
  12544. var emailEsc = email.replace(/[.*+?^${}()|[\]\\]/g, "\\$&");
  12545. var emailRegex = new RegExp('<' + emailEsc + '>');
  12546. var userIds = key.getUserIds();
  12547. for (var i = 0; i < userIds.length; i++) {
  12548. var userId = userIds[i].toLowerCase();
  12549. if (email === userId || emailRegex.test(userId)) {
  12550. return true;
  12551. }
  12552. }
  12553. return false;
  12554. }
  12555. /**
  12556. * Checks a key to see if it matches the specified keyid
  12557. * @private
  12558. * @param {String} keyId provided as string of lowercase hex number
  12559. * withouth 0x prefix (can be 16-character key ID or fingerprint)
  12560. * @param {module:packet/secret_key|public_key|public_subkey|secret_subkey} keypacket The keypacket to be checked
  12561. * @return {Boolean} True if keypacket has the specified keyid
  12562. */
  12563. function keyIdCheck(keyId, keypacket) {
  12564. if (keyId.length === 16) {
  12565. return keyId === keypacket.getKeyId().toHex();
  12566. } else {
  12567. return keyId === keypacket.getFingerprint();
  12568. }
  12569. }
  12570. /**
  12571. * Searches the KeyArray for a key having the specified key id
  12572. * @param {String} keyId provided as string of lowercase hex number
  12573. * withouth 0x prefix (can be 16-character key ID or fingerprint)
  12574. * @param {Boolean} deep if true search also in subkeys
  12575. * @return {module:key~Key|null} key found or null
  12576. */
  12577. KeyArray.prototype.getForId = function (keyId, deep) {
  12578. for (var i = 0; i < this.keys.length; i++) {
  12579. if (keyIdCheck(keyId, this.keys[i].primaryKey)) {
  12580. return this.keys[i];
  12581. }
  12582. if (deep && this.keys[i].subKeys) {
  12583. for (var j = 0; j < this.keys[i].subKeys.length; j++) {
  12584. if (keyIdCheck(keyId, this.keys[i].subKeys[j].subKey)) {
  12585. return this.keys[i];
  12586. }
  12587. }
  12588. }
  12589. }
  12590. return null;
  12591. };
  12592. /**
  12593. * Imports a key from an ascii armored message
  12594. * @param {String} armored message to read the keys/key from
  12595. * @return {Array<Error>|null} array of error objects or null
  12596. */
  12597. KeyArray.prototype.importKey = function (armored) {
  12598. var imported = keyModule.readArmored(armored);
  12599. var that = this;
  12600. imported.keys.forEach(function (key) {
  12601. // check if key already in key array
  12602. var keyidHex = key.primaryKey.getKeyId().toHex();
  12603. var keyFound = that.getForId(keyidHex);
  12604. if (keyFound) {
  12605. keyFound.update(key);
  12606. } else {
  12607. that.push(key);
  12608. }
  12609. });
  12610. return imported.err ? imported.err : null;
  12611. };
  12612. /**
  12613. * Add key to KeyArray
  12614. * @param {module:key~Key} key The key that will be added to the keyring
  12615. * @return {Number} The new length of the KeyArray
  12616. */
  12617. KeyArray.prototype.push = function (key) {
  12618. return this.keys.push(key);
  12619. };
  12620. /**
  12621. * Removes a key with the specified keyid from the keyring
  12622. * @param {String} keyId provided as string of lowercase hex number
  12623. * withouth 0x prefix (can be 16-character key ID or fingerprint)
  12624. * @return {module:key~Key|null} The key object which has been removed or null
  12625. */
  12626. KeyArray.prototype.removeForId = function (keyId) {
  12627. for (var i = 0; i < this.keys.length; i++) {
  12628. if (keyIdCheck(keyId, this.keys[i].primaryKey)) {
  12629. return this.keys.splice(i, 1)[0];
  12630. }
  12631. }
  12632. return null;
  12633. };
  12634. },{"../key.js":38,"./localstore.js":41}],41:[function(_dereq_,module,exports){
  12635. // GPG4Browsers - An OpenPGP implementation in javascript
  12636. // Copyright (C) 2011 Recurity Labs GmbH
  12637. //
  12638. // This library is free software; you can redistribute it and/or
  12639. // modify it under the terms of the GNU Lesser General Public
  12640. // License as published by the Free Software Foundation; either
  12641. // version 3.0 of the License, or (at your option) any later version.
  12642. //
  12643. // This library is distributed in the hope that it will be useful,
  12644. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12645. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12646. // Lesser General Public License for more details.
  12647. //
  12648. // You should have received a copy of the GNU Lesser General Public
  12649. // License along with this library; if not, write to the Free Software
  12650. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  12651. /**
  12652. * The class that deals with storage of the keyring. Currently the only option is to use HTML5 local storage.
  12653. * @requires config
  12654. * @module keyring/localstore
  12655. * @param {String} prefix prefix for itemnames in localstore
  12656. */
  12657. 'use strict';
  12658. Object.defineProperty(exports, "__esModule", {
  12659. value: true
  12660. });
  12661. exports.default = LocalStore;
  12662. var _config = _dereq_('../config');
  12663. var _config2 = _interopRequireDefault(_config);
  12664. var _key = _dereq_('../key.js');
  12665. var keyModule = _interopRequireWildcard(_key);
  12666. var _util = _dereq_('../util.js');
  12667. var _util2 = _interopRequireDefault(_util);
  12668. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  12669. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12670. function LocalStore(prefix) {
  12671. prefix = prefix || 'openpgp-';
  12672. this.publicKeysItem = prefix + this.publicKeysItem;
  12673. this.privateKeysItem = prefix + this.privateKeysItem;
  12674. if (typeof window !== 'undefined' && window.localStorage) {
  12675. this.storage = window.localStorage;
  12676. } else {
  12677. this.storage = new (_dereq_('node-localstorage').LocalStorage)(_config2.default.node_store);
  12678. }
  12679. }
  12680. /*
  12681. * Declare the localstore itemnames
  12682. */
  12683. LocalStore.prototype.publicKeysItem = 'public-keys';
  12684. LocalStore.prototype.privateKeysItem = 'private-keys';
  12685. /**
  12686. * Load the public keys from HTML5 local storage.
  12687. * @return {Array<module:key~Key>} array of keys retrieved from localstore
  12688. */
  12689. LocalStore.prototype.loadPublic = function () {
  12690. return loadKeys(this.storage, this.publicKeysItem);
  12691. };
  12692. /**
  12693. * Load the private keys from HTML5 local storage.
  12694. * @return {Array<module:key~Key>} array of keys retrieved from localstore
  12695. */
  12696. LocalStore.prototype.loadPrivate = function () {
  12697. return loadKeys(this.storage, this.privateKeysItem);
  12698. };
  12699. function loadKeys(storage, itemname) {
  12700. var armoredKeys = JSON.parse(storage.getItem(itemname));
  12701. var keys = [];
  12702. if (armoredKeys !== null && armoredKeys.length !== 0) {
  12703. var key;
  12704. for (var i = 0; i < armoredKeys.length; i++) {
  12705. key = keyModule.readArmored(armoredKeys[i]);
  12706. if (!key.err) {
  12707. keys.push(key.keys[0]);
  12708. } else {
  12709. _util2.default.print_debug("Error reading armored key from keyring index: " + i);
  12710. }
  12711. }
  12712. }
  12713. return keys;
  12714. }
  12715. /**
  12716. * Saves the current state of the public keys to HTML5 local storage.
  12717. * The key array gets stringified using JSON
  12718. * @param {Array<module:key~Key>} keys array of keys to save in localstore
  12719. */
  12720. LocalStore.prototype.storePublic = function (keys) {
  12721. storeKeys(this.storage, this.publicKeysItem, keys);
  12722. };
  12723. /**
  12724. * Saves the current state of the private keys to HTML5 local storage.
  12725. * The key array gets stringified using JSON
  12726. * @param {Array<module:key~Key>} keys array of keys to save in localstore
  12727. */
  12728. LocalStore.prototype.storePrivate = function (keys) {
  12729. storeKeys(this.storage, this.privateKeysItem, keys);
  12730. };
  12731. function storeKeys(storage, itemname, keys) {
  12732. var armoredKeys = [];
  12733. if (keys.length) {
  12734. for (var i = 0; i < keys.length; i++) {
  12735. armoredKeys.push(keys[i].armor());
  12736. }
  12737. storage.setItem(itemname, JSON.stringify(armoredKeys));
  12738. } else {
  12739. storage.removeItem(itemname);
  12740. }
  12741. }
  12742. },{"../config":10,"../key.js":38,"../util.js":70,"node-localstorage":"node-localstorage"}],42:[function(_dereq_,module,exports){
  12743. // GPG4Browsers - An OpenPGP implementation in javascript
  12744. // Copyright (C) 2011 Recurity Labs GmbH
  12745. //
  12746. // This library is free software; you can redistribute it and/or
  12747. // modify it under the terms of the GNU Lesser General Public
  12748. // License as published by the Free Software Foundation; either
  12749. // version 3.0 of the License, or (at your option) any later version.
  12750. //
  12751. // This library is distributed in the hope that it will be useful,
  12752. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12753. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12754. // Lesser General Public License for more details.
  12755. //
  12756. // You should have received a copy of the GNU Lesser General Public
  12757. // License along with this library; if not, write to the Free Software
  12758. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  12759. /**
  12760. * @requires config
  12761. * @requires crypto
  12762. * @requires encoding/armor
  12763. * @requires enums
  12764. * @requires packet
  12765. * @module message
  12766. */
  12767. 'use strict';
  12768. Object.defineProperty(exports, "__esModule", {
  12769. value: true
  12770. });
  12771. exports.Message = Message;
  12772. exports.encryptSessionKey = encryptSessionKey;
  12773. exports.readArmored = readArmored;
  12774. exports.read = read;
  12775. exports.readSignedContent = readSignedContent;
  12776. exports.fromText = fromText;
  12777. exports.fromBinary = fromBinary;
  12778. var _util = _dereq_('./util.js');
  12779. var _util2 = _interopRequireDefault(_util);
  12780. var _packet = _dereq_('./packet');
  12781. var _packet2 = _interopRequireDefault(_packet);
  12782. var _enums = _dereq_('./enums.js');
  12783. var _enums2 = _interopRequireDefault(_enums);
  12784. var _armor = _dereq_('./encoding/armor.js');
  12785. var _armor2 = _interopRequireDefault(_armor);
  12786. var _config = _dereq_('./config');
  12787. var _config2 = _interopRequireDefault(_config);
  12788. var _crypto = _dereq_('./crypto');
  12789. var _crypto2 = _interopRequireDefault(_crypto);
  12790. var _signature = _dereq_('./signature.js');
  12791. var sigModule = _interopRequireWildcard(_signature);
  12792. var _key = _dereq_('./key.js');
  12793. var keyModule = _interopRequireWildcard(_key);
  12794. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  12795. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  12796. /**
  12797. * @class
  12798. * @classdesc Class that represents an OpenPGP message.
  12799. * Can be an encrypted message, signed message, compressed message or literal message
  12800. * @param {module:packet/packetlist} packetlist The packets that form this message
  12801. * See {@link http://tools.ietf.org/html/rfc4880#section-11.3}
  12802. */
  12803. function Message(packetlist) {
  12804. if (!(this instanceof Message)) {
  12805. return new Message(packetlist);
  12806. }
  12807. this.packets = packetlist || new _packet2.default.List();
  12808. }
  12809. /**
  12810. * Returns the key IDs of the keys to which the session key is encrypted
  12811. * @return {Array<module:type/keyid>} array of keyid objects
  12812. */
  12813. Message.prototype.getEncryptionKeyIds = function () {
  12814. var keyIds = [];
  12815. var pkESKeyPacketlist = this.packets.filterByTag(_enums2.default.packet.publicKeyEncryptedSessionKey);
  12816. pkESKeyPacketlist.forEach(function (packet) {
  12817. keyIds.push(packet.publicKeyId);
  12818. });
  12819. return keyIds;
  12820. };
  12821. /**
  12822. * Returns the key IDs of the keys that signed the message
  12823. * @return {Array<module:type/keyid>} array of keyid objects
  12824. */
  12825. Message.prototype.getSigningKeyIds = function () {
  12826. var keyIds = [];
  12827. var msg = this.unwrapCompressed();
  12828. // search for one pass signatures
  12829. var onePassSigList = msg.packets.filterByTag(_enums2.default.packet.onePassSignature);
  12830. onePassSigList.forEach(function (packet) {
  12831. keyIds.push(packet.signingKeyId);
  12832. });
  12833. // if nothing found look for signature packets
  12834. if (!keyIds.length) {
  12835. var signatureList = msg.packets.filterByTag(_enums2.default.packet.signature);
  12836. signatureList.forEach(function (packet) {
  12837. keyIds.push(packet.issuerKeyId);
  12838. });
  12839. }
  12840. return keyIds;
  12841. };
  12842. /**
  12843. * Decrypt the message. Either a private key, a session key, or a password must be specified.
  12844. * @param {Key} privateKey (optional) private key with decrypted secret data
  12845. * @param {Object} sessionKey (optional) session key in the form: { data:Uint8Array, algorithm:String }
  12846. * @param {String} password (optional) password used to decrypt
  12847. * @return {Message} new message with decrypted content
  12848. */
  12849. Message.prototype.decrypt = function (privateKey, sessionKey, password) {
  12850. var _this = this;
  12851. return Promise.resolve().then(function () {
  12852. var keyObj = sessionKey || _this.decryptSessionKey(privateKey, password);
  12853. if (!keyObj || !_util2.default.isUint8Array(keyObj.data) || !_util2.default.isString(keyObj.algorithm)) {
  12854. throw new Error('Invalid session key for decryption.');
  12855. }
  12856. var symEncryptedPacketlist = _this.packets.filterByTag(_enums2.default.packet.symmetricallyEncrypted, _enums2.default.packet.symEncryptedIntegrityProtected, _enums2.default.packet.symEncryptedAEADProtected);
  12857. if (symEncryptedPacketlist.length === 0) {
  12858. return;
  12859. }
  12860. var symEncryptedPacket = symEncryptedPacketlist[0];
  12861. return symEncryptedPacket.decrypt(keyObj.algorithm, keyObj.data).then(function () {
  12862. var resultMsg = new Message(symEncryptedPacket.packets);
  12863. symEncryptedPacket.packets = new _packet2.default.List(); // remove packets after decryption
  12864. return resultMsg;
  12865. });
  12866. });
  12867. };
  12868. /**
  12869. * Decrypt an encrypted session key either with a private key or a password.
  12870. * @param {Key} privateKey (optional) private key with decrypted secret data
  12871. * @param {String} password (optional) password used to decrypt
  12872. * @return {Object} object with sessionKey, algorithm in the form:
  12873. * { data:Uint8Array, algorithm:String }
  12874. */
  12875. Message.prototype.decryptSessionKey = function (privateKey, password) {
  12876. var keyPacket;
  12877. if (password) {
  12878. var symEncryptedSessionKeyPacketlist = this.packets.filterByTag(_enums2.default.packet.symEncryptedSessionKey);
  12879. var symLength = symEncryptedSessionKeyPacketlist.length;
  12880. for (var i = 0; i < symLength; i++) {
  12881. keyPacket = symEncryptedSessionKeyPacketlist[i];
  12882. try {
  12883. keyPacket.decrypt(password);
  12884. break;
  12885. } catch (err) {
  12886. if (i === symLength - 1) {
  12887. throw err;
  12888. }
  12889. }
  12890. }
  12891. if (!keyPacket) {
  12892. throw new Error('No symmetrically encrypted session key packet found.');
  12893. }
  12894. } else if (privateKey) {
  12895. var encryptionKeyIds = this.getEncryptionKeyIds();
  12896. if (!encryptionKeyIds.length) {
  12897. // nothing to decrypt
  12898. return;
  12899. }
  12900. var privateKeyPacket = privateKey.getKeyPacket(encryptionKeyIds);
  12901. if (!privateKeyPacket.isDecrypted) {
  12902. throw new Error('Private key is not decrypted.');
  12903. }
  12904. var pkESKeyPacketlist = this.packets.filterByTag(_enums2.default.packet.publicKeyEncryptedSessionKey);
  12905. for (var j = 0; j < pkESKeyPacketlist.length; j++) {
  12906. if (pkESKeyPacketlist[j].publicKeyId.equals(privateKeyPacket.getKeyId())) {
  12907. keyPacket = pkESKeyPacketlist[j];
  12908. keyPacket.decrypt(privateKeyPacket);
  12909. break;
  12910. }
  12911. }
  12912. } else {
  12913. throw new Error('No key or password specified.');
  12914. }
  12915. if (keyPacket) {
  12916. return {
  12917. data: keyPacket.sessionKey,
  12918. algorithm: keyPacket.sessionKeyAlgorithm
  12919. };
  12920. }
  12921. };
  12922. /**
  12923. * Get literal data that is the body of the message
  12924. * @return {(Uint8Array|null)} literal body of the message as Uint8Array
  12925. */
  12926. Message.prototype.getLiteralData = function () {
  12927. var literal = this.packets.findPacket(_enums2.default.packet.literal);
  12928. return literal && literal.data || null;
  12929. };
  12930. /**
  12931. * Get filename from literal data packet
  12932. * @return {(String|null)} filename of literal data packet as string
  12933. */
  12934. Message.prototype.getFilename = function () {
  12935. var literal = this.packets.findPacket(_enums2.default.packet.literal);
  12936. return literal && literal.getFilename() || null;
  12937. };
  12938. /**
  12939. * Get literal data as text
  12940. * @return {(String|null)} literal body of the message interpreted as text
  12941. */
  12942. Message.prototype.getText = function () {
  12943. var literal = this.packets.findPacket(_enums2.default.packet.literal);
  12944. if (literal) {
  12945. return literal.getText();
  12946. } else {
  12947. return null;
  12948. }
  12949. };
  12950. /**
  12951. * Encrypt the message either with public keys, passwords, or both at once.
  12952. * @param {Array<Key>} keys (optional) public key(s) for message encryption
  12953. * @param {Array<String>} passwords (optional) password(s) for message encryption
  12954. * @return {Message} new message with encrypted content
  12955. */
  12956. Message.prototype.encrypt = function (keys, passwords) {
  12957. var _this2 = this;
  12958. var symAlgo = void 0,
  12959. msg = void 0,
  12960. symEncryptedPacket = void 0;
  12961. return Promise.resolve().then(function () {
  12962. if (keys) {
  12963. symAlgo = keyModule.getPreferredSymAlgo(keys);
  12964. } else if (passwords) {
  12965. symAlgo = _config2.default.encryption_cipher;
  12966. } else {
  12967. throw new Error('No keys or passwords');
  12968. }
  12969. var sessionKey = _crypto2.default.generateSessionKey(_enums2.default.read(_enums2.default.symmetric, symAlgo));
  12970. msg = encryptSessionKey(sessionKey, _enums2.default.read(_enums2.default.symmetric, symAlgo), keys, passwords);
  12971. if (_config2.default.aead_protect) {
  12972. symEncryptedPacket = new _packet2.default.SymEncryptedAEADProtected();
  12973. } else if (_config2.default.integrity_protect) {
  12974. symEncryptedPacket = new _packet2.default.SymEncryptedIntegrityProtected();
  12975. } else {
  12976. symEncryptedPacket = new _packet2.default.SymmetricallyEncrypted();
  12977. }
  12978. symEncryptedPacket.packets = _this2.packets;
  12979. return symEncryptedPacket.encrypt(_enums2.default.read(_enums2.default.symmetric, symAlgo), sessionKey);
  12980. }).then(function () {
  12981. msg.packets.push(symEncryptedPacket);
  12982. symEncryptedPacket.packets = new _packet2.default.List(); // remove packets after encryption
  12983. return msg;
  12984. });
  12985. };
  12986. /**
  12987. * Encrypt a session key either with public keys, passwords, or both at once.
  12988. * @param {Uint8Array} sessionKey session key for encryption
  12989. * @param {String} symAlgo session key algorithm
  12990. * @param {Array<Key>} publicKeys (optional) public key(s) for message encryption
  12991. * @param {Array<String>} passwords (optional) for message encryption
  12992. * @return {Message} new message with encrypted content
  12993. */
  12994. function encryptSessionKey(sessionKey, symAlgo, publicKeys, passwords) {
  12995. var packetlist = new _packet2.default.List();
  12996. if (publicKeys) {
  12997. publicKeys.forEach(function (key) {
  12998. var encryptionKeyPacket = key.getEncryptionKeyPacket();
  12999. if (encryptionKeyPacket) {
  13000. var pkESKeyPacket = new _packet2.default.PublicKeyEncryptedSessionKey();
  13001. pkESKeyPacket.publicKeyId = encryptionKeyPacket.getKeyId();
  13002. pkESKeyPacket.publicKeyAlgorithm = encryptionKeyPacket.algorithm;
  13003. pkESKeyPacket.sessionKey = sessionKey;
  13004. pkESKeyPacket.sessionKeyAlgorithm = symAlgo;
  13005. pkESKeyPacket.encrypt(encryptionKeyPacket);
  13006. delete pkESKeyPacket.sessionKey; // delete plaintext session key after encryption
  13007. packetlist.push(pkESKeyPacket);
  13008. } else {
  13009. throw new Error('Could not find valid key packet for encryption in key ' + key.primaryKey.getKeyId().toHex());
  13010. }
  13011. });
  13012. }
  13013. if (passwords) {
  13014. passwords.forEach(function (password) {
  13015. var symEncryptedSessionKeyPacket = new _packet2.default.SymEncryptedSessionKey();
  13016. symEncryptedSessionKeyPacket.sessionKey = sessionKey;
  13017. symEncryptedSessionKeyPacket.sessionKeyAlgorithm = symAlgo;
  13018. symEncryptedSessionKeyPacket.encrypt(password);
  13019. delete symEncryptedSessionKeyPacket.sessionKey; // delete plaintext session key after encryption
  13020. packetlist.push(symEncryptedSessionKeyPacket);
  13021. });
  13022. }
  13023. return new Message(packetlist);
  13024. }
  13025. /**
  13026. * Sign the message (the literal data packet of the message)
  13027. * @param {Array<module:key~Key>} privateKey private keys with decrypted secret key data for signing
  13028. * @param {Signature} signature (optional) any existing detached signature to add to the message
  13029. * @return {module:message~Message} new message with signed content
  13030. */
  13031. Message.prototype.sign = function () {
  13032. var privateKeys = arguments.length <= 0 || arguments[0] === undefined ? [] : arguments[0];
  13033. var signature = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
  13034. var packetlist = new _packet2.default.List();
  13035. var literalDataPacket = this.packets.findPacket(_enums2.default.packet.literal);
  13036. if (!literalDataPacket) {
  13037. throw new Error('No literal data packet to sign.');
  13038. }
  13039. var literalFormat = _enums2.default.write(_enums2.default.literal, literalDataPacket.format);
  13040. var signatureType = literalFormat === _enums2.default.literal.binary ? _enums2.default.signature.binary : _enums2.default.signature.text;
  13041. var i, signingKeyPacket, existingSigPacketlist, onePassSig;
  13042. if (signature) {
  13043. existingSigPacketlist = signature.packets.filterByTag(_enums2.default.packet.signature);
  13044. if (existingSigPacketlist.length) {
  13045. for (i = existingSigPacketlist.length - 1; i >= 0; i--) {
  13046. var sigPacket = existingSigPacketlist[i];
  13047. onePassSig = new _packet2.default.OnePassSignature();
  13048. onePassSig.type = signatureType;
  13049. onePassSig.hashAlgorithm = _config2.default.prefer_hash_algorithm;
  13050. onePassSig.publicKeyAlgorithm = sigPacket.publicKeyAlgorithm;
  13051. onePassSig.signingKeyId = sigPacket.issuerKeyId;
  13052. if (!privateKeys.length && i === 0) {
  13053. onePassSig.flags = 1;
  13054. }
  13055. packetlist.push(onePassSig);
  13056. }
  13057. }
  13058. }
  13059. for (i = 0; i < privateKeys.length; i++) {
  13060. if (privateKeys[i].isPublic()) {
  13061. throw new Error('Need private key for signing');
  13062. }
  13063. onePassSig = new _packet2.default.OnePassSignature();
  13064. onePassSig.type = signatureType;
  13065. //TODO get preferred hashg algo from key signature
  13066. onePassSig.hashAlgorithm = _config2.default.prefer_hash_algorithm;
  13067. signingKeyPacket = privateKeys[i].getSigningKeyPacket();
  13068. if (!signingKeyPacket) {
  13069. throw new Error('Could not find valid key packet for signing in key ' + privateKeys[i].primaryKey.getKeyId().toHex());
  13070. }
  13071. onePassSig.publicKeyAlgorithm = signingKeyPacket.algorithm;
  13072. onePassSig.signingKeyId = signingKeyPacket.getKeyId();
  13073. if (i === privateKeys.length - 1) {
  13074. onePassSig.flags = 1;
  13075. }
  13076. packetlist.push(onePassSig);
  13077. }
  13078. packetlist.push(literalDataPacket);
  13079. for (i = privateKeys.length - 1; i >= 0; i--) {
  13080. var signaturePacket = new _packet2.default.Signature();
  13081. signaturePacket.signatureType = signatureType;
  13082. signaturePacket.hashAlgorithm = _config2.default.prefer_hash_algorithm;
  13083. signaturePacket.publicKeyAlgorithm = signingKeyPacket.algorithm;
  13084. if (!signingKeyPacket.isDecrypted) {
  13085. throw new Error('Private key is not decrypted.');
  13086. }
  13087. signaturePacket.sign(signingKeyPacket, literalDataPacket);
  13088. packetlist.push(signaturePacket);
  13089. }
  13090. if (signature) {
  13091. packetlist.concat(existingSigPacketlist);
  13092. }
  13093. return new Message(packetlist);
  13094. };
  13095. /**
  13096. * Create a detached signature for the message (the literal data packet of the message)
  13097. * @param {Array<module:key~Key>} privateKey private keys with decrypted secret key data for signing
  13098. * @param {Signature} signature (optional) any existing detached signature
  13099. * @return {module:signature~Signature} new detached signature of message content
  13100. */
  13101. Message.prototype.signDetached = function () {
  13102. var privateKeys = arguments.length <= 0 || arguments[0] === undefined ? [] : arguments[0];
  13103. var signature = arguments.length <= 1 || arguments[1] === undefined ? null : arguments[1];
  13104. var packetlist = new _packet2.default.List();
  13105. var literalDataPacket = this.packets.findPacket(_enums2.default.packet.literal);
  13106. if (!literalDataPacket) {
  13107. throw new Error('No literal data packet to sign.');
  13108. }
  13109. var literalFormat = _enums2.default.write(_enums2.default.literal, literalDataPacket.format);
  13110. var signatureType = literalFormat === _enums2.default.literal.binary ? _enums2.default.signature.binary : _enums2.default.signature.text;
  13111. for (var i = 0; i < privateKeys.length; i++) {
  13112. var signingKeyPacket = privateKeys[i].getSigningKeyPacket();
  13113. var signaturePacket = new _packet2.default.Signature();
  13114. signaturePacket.signatureType = signatureType;
  13115. signaturePacket.hashAlgorithm = _config2.default.prefer_hash_algorithm;
  13116. signaturePacket.publicKeyAlgorithm = signingKeyPacket.algorithm;
  13117. if (!signingKeyPacket.isDecrypted) {
  13118. throw new Error('Private key is not decrypted.');
  13119. }
  13120. signaturePacket.sign(signingKeyPacket, literalDataPacket);
  13121. packetlist.push(signaturePacket);
  13122. }
  13123. if (signature) {
  13124. var existingSigPacketlist = signature.packets.filterByTag(_enums2.default.packet.signature);
  13125. packetlist.concat(existingSigPacketlist);
  13126. }
  13127. return new sigModule.Signature(packetlist);
  13128. };
  13129. /**
  13130. * Verify message signatures
  13131. * @param {Array<module:key~Key>} keys array of keys to verify signatures
  13132. * @return {Array<({keyid: module:type/keyid, valid: Boolean})>} list of signer's keyid and validity of signature
  13133. */
  13134. Message.prototype.verify = function (keys) {
  13135. var msg = this.unwrapCompressed();
  13136. var literalDataList = msg.packets.filterByTag(_enums2.default.packet.literal);
  13137. if (literalDataList.length !== 1) {
  13138. throw new Error('Can only verify message with one literal data packet.');
  13139. }
  13140. var signatureList = msg.packets.filterByTag(_enums2.default.packet.signature);
  13141. return createVerificationObjects(signatureList, literalDataList, keys);
  13142. };
  13143. /**
  13144. * Verify detached message signature
  13145. * @param {Array<module:key~Key>} keys array of keys to verify signatures
  13146. * @param {Signature}
  13147. * @return {Array<({keyid: module:type/keyid, valid: Boolean})>} list of signer's keyid and validity of signature
  13148. */
  13149. Message.prototype.verifyDetached = function (signature, keys) {
  13150. var msg = this.unwrapCompressed();
  13151. var literalDataList = msg.packets.filterByTag(_enums2.default.packet.literal);
  13152. if (literalDataList.length !== 1) {
  13153. throw new Error('Can only verify message with one literal data packet.');
  13154. }
  13155. var signatureList = signature.packets;
  13156. return createVerificationObjects(signatureList, literalDataList, keys);
  13157. };
  13158. /**
  13159. * Create list of objects containing signer's keyid and validity of signature
  13160. * @param {Array<module:packet/signature>} signatureList array of signature packets
  13161. * @param {Array<module:packet/literal>} literalDataList array of literal data packets
  13162. * @param {Array<module:key~Key>} keys array of keys to verify signatures
  13163. * @return {Array<({keyid: module:type/keyid, valid: Boolean})>} list of signer's keyid and validity of signature
  13164. */
  13165. function createVerificationObjects(signatureList, literalDataList, keys) {
  13166. var result = [];
  13167. for (var i = 0; i < signatureList.length; i++) {
  13168. var keyPacket = null;
  13169. for (var j = 0; j < keys.length; j++) {
  13170. keyPacket = keys[j].getSigningKeyPacket(signatureList[i].issuerKeyId);
  13171. if (keyPacket) {
  13172. break;
  13173. }
  13174. }
  13175. var verifiedSig = {};
  13176. if (keyPacket) {
  13177. //found a key packet that matches keyId of signature
  13178. verifiedSig.keyid = signatureList[i].issuerKeyId;
  13179. verifiedSig.valid = signatureList[i].verify(keyPacket, literalDataList[0]);
  13180. } else {
  13181. verifiedSig.keyid = signatureList[i].issuerKeyId;
  13182. verifiedSig.valid = null;
  13183. }
  13184. verifiedSig.signature = new sigModule.Signature([signatureList[i]]);
  13185. result.push(verifiedSig);
  13186. }
  13187. return result;
  13188. }
  13189. /**
  13190. * Unwrap compressed message
  13191. * @return {module:message~Message} message Content of compressed message
  13192. */
  13193. Message.prototype.unwrapCompressed = function () {
  13194. var compressed = this.packets.filterByTag(_enums2.default.packet.compressed);
  13195. if (compressed.length) {
  13196. return new Message(compressed[0].packets);
  13197. } else {
  13198. return this;
  13199. }
  13200. };
  13201. /**
  13202. * Returns ASCII armored text of message
  13203. * @return {String} ASCII armor
  13204. */
  13205. Message.prototype.armor = function () {
  13206. return _armor2.default.encode(_enums2.default.armor.message, this.packets.write());
  13207. };
  13208. /**
  13209. * reads an OpenPGP armored message and returns a message object
  13210. * @param {String} armoredText text to be parsed
  13211. * @return {module:message~Message} new message object
  13212. * @static
  13213. */
  13214. function readArmored(armoredText) {
  13215. //TODO how do we want to handle bad text? Exception throwing
  13216. //TODO don't accept non-message armored texts
  13217. var input = _armor2.default.decode(armoredText).data;
  13218. return read(input);
  13219. }
  13220. /**
  13221. * reads an OpenPGP message as byte array and returns a message object
  13222. * @param {Uint8Array} input binary message
  13223. * @return {Message} new message object
  13224. * @static
  13225. */
  13226. function read(input) {
  13227. var packetlist = new _packet2.default.List();
  13228. packetlist.read(input);
  13229. return new Message(packetlist);
  13230. }
  13231. /**
  13232. * Create a message object from signed content and a detached armored signature.
  13233. * @param {String} content An 8 bit ascii string containing e.g. a MIME subtree with text nodes or attachments
  13234. * @param {String} detachedSignature The detached ascii armored PGP signature
  13235. */
  13236. function readSignedContent(content, detachedSignature) {
  13237. var literalDataPacket = new _packet2.default.Literal();
  13238. literalDataPacket.setBytes(_util2.default.str2Uint8Array(content), _enums2.default.read(_enums2.default.literal, _enums2.default.literal.binary));
  13239. var packetlist = new _packet2.default.List();
  13240. packetlist.push(literalDataPacket);
  13241. var input = _armor2.default.decode(detachedSignature).data;
  13242. packetlist.read(input);
  13243. return new Message(packetlist);
  13244. }
  13245. /**
  13246. * creates new message object from text
  13247. * @param {String} text
  13248. * @param {String} filename (optional)
  13249. * @return {module:message~Message} new message object
  13250. * @static
  13251. */
  13252. function fromText(text, filename) {
  13253. var literalDataPacket = new _packet2.default.Literal();
  13254. // text will be converted to UTF8
  13255. literalDataPacket.setText(text);
  13256. if (filename !== undefined) {
  13257. literalDataPacket.setFilename(filename);
  13258. }
  13259. var literalDataPacketlist = new _packet2.default.List();
  13260. literalDataPacketlist.push(literalDataPacket);
  13261. return new Message(literalDataPacketlist);
  13262. }
  13263. /**
  13264. * creates new message object from binary data
  13265. * @param {Uint8Array} bytes
  13266. * @param {String} filename (optional)
  13267. * @return {module:message~Message} new message object
  13268. * @static
  13269. */
  13270. function fromBinary(bytes, filename) {
  13271. if (!_util2.default.isUint8Array(bytes)) {
  13272. throw new Error('Data must be in the form of a Uint8Array');
  13273. }
  13274. var literalDataPacket = new _packet2.default.Literal();
  13275. if (filename) {
  13276. literalDataPacket.setFilename(filename);
  13277. }
  13278. literalDataPacket.setBytes(bytes, _enums2.default.read(_enums2.default.literal, _enums2.default.literal.binary));
  13279. if (filename !== undefined) {
  13280. literalDataPacket.setFilename(filename);
  13281. }
  13282. var literalDataPacketlist = new _packet2.default.List();
  13283. literalDataPacketlist.push(literalDataPacket);
  13284. return new Message(literalDataPacketlist);
  13285. }
  13286. },{"./config":10,"./crypto":24,"./encoding/armor.js":33,"./enums.js":35,"./key.js":38,"./packet":47,"./signature.js":66,"./util.js":70}],43:[function(_dereq_,module,exports){
  13287. // OpenPGP.js - An OpenPGP implementation in javascript
  13288. // Copyright (C) 2016 Tankred Hase
  13289. //
  13290. // This library is free software; you can redistribute it and/or
  13291. // modify it under the terms of the GNU Lesser General Public
  13292. // License as published by the Free Software Foundation; either
  13293. // version 3.0 of the License, or (at your option) any later version.
  13294. //
  13295. // This library is distributed in the hope that it will be useful,
  13296. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  13297. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13298. // Lesser General Public License for more details.
  13299. //
  13300. // You should have received a copy of the GNU Lesser General Public
  13301. // License along with this library; if not, write to the Free Software
  13302. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  13303. /**
  13304. * @requires message
  13305. * @requires cleartext
  13306. * @requires key
  13307. * @requires config
  13308. * @requires util
  13309. * @module openpgp
  13310. */
  13311. /**
  13312. * @fileoverview The openpgp base module should provide all of the functionality
  13313. * to consume the openpgp.js library. All additional classes are documented
  13314. * for extending and developing on top of the base library.
  13315. */
  13316. 'use strict';
  13317. Object.defineProperty(exports, "__esModule", {
  13318. value: true
  13319. });
  13320. exports.initWorker = initWorker;
  13321. exports.getWorker = getWorker;
  13322. exports.destroyWorker = destroyWorker;
  13323. exports.generateKey = generateKey;
  13324. exports.reformatKey = reformatKey;
  13325. exports.decryptKey = decryptKey;
  13326. exports.encrypt = encrypt;
  13327. exports.decrypt = decrypt;
  13328. exports.sign = sign;
  13329. exports.verify = verify;
  13330. exports.encryptSessionKey = encryptSessionKey;
  13331. exports.decryptSessionKey = decryptSessionKey;
  13332. var _message = _dereq_('./message.js');
  13333. var messageLib = _interopRequireWildcard(_message);
  13334. var _cleartext = _dereq_('./cleartext.js');
  13335. var cleartext = _interopRequireWildcard(_cleartext);
  13336. var _key = _dereq_('./key.js');
  13337. var key = _interopRequireWildcard(_key);
  13338. var _config = _dereq_('./config/config.js');
  13339. var _config2 = _interopRequireDefault(_config);
  13340. var _util = _dereq_('./util');
  13341. var _util2 = _interopRequireDefault(_util);
  13342. var _async_proxy = _dereq_('./worker/async_proxy.js');
  13343. var _async_proxy2 = _interopRequireDefault(_async_proxy);
  13344. var _es6Promise = _dereq_('es6-promise');
  13345. var _es6Promise2 = _interopRequireDefault(_es6Promise);
  13346. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  13347. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  13348. _es6Promise2.default.polyfill(); // load ES6 Promises polyfill
  13349. //////////////////////////
  13350. // //
  13351. // Web Worker setup //
  13352. // //
  13353. //////////////////////////
  13354. var asyncProxy = void 0; // instance of the asyncproxy
  13355. /**
  13356. * Set the path for the web worker script and create an instance of the async proxy
  13357. * @param {String} path relative path to the worker scripts, default: 'openpgp.worker.js'
  13358. * @param {Object} worker alternative to path parameter: web worker initialized with 'openpgp.worker.js'
  13359. */
  13360. function initWorker() {
  13361. var _ref = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
  13362. var _ref$path = _ref.path;
  13363. var path = _ref$path === undefined ? 'openpgp.worker.js' : _ref$path;
  13364. var worker = _ref.worker;
  13365. if (worker || typeof window !== 'undefined' && window.Worker) {
  13366. asyncProxy = new _async_proxy2.default({ path: path, worker: worker, config: _config2.default });
  13367. return true;
  13368. }
  13369. }
  13370. /**
  13371. * Returns a reference to the async proxy if the worker was initialized with openpgp.initWorker()
  13372. * @return {module:worker/async_proxy~AsyncProxy|null} the async proxy or null if not initialized
  13373. */
  13374. function getWorker() {
  13375. return asyncProxy;
  13376. }
  13377. /**
  13378. * Cleanup the current instance of the web worker.
  13379. */
  13380. function destroyWorker() {
  13381. asyncProxy = undefined;
  13382. }
  13383. //////////////////////
  13384. // //
  13385. // Key handling //
  13386. // //
  13387. //////////////////////
  13388. /**
  13389. * Generates a new OpenPGP key pair. Currently only supports RSA keys. Primary and subkey will be of same type.
  13390. * @param {Array<Object>} userIds array of user IDs e.g. [{ name:'Phil Zimmermann', email:'phil@openpgp.org' }]
  13391. * @param {String} passphrase (optional) The passphrase used to encrypt the resulting private key
  13392. * @param {Number} numBits (optional) number of bits for the key creation. (should be 2048 or 4096)
  13393. * @param {Boolean} unlocked (optional) If the returned secret part of the generated key is unlocked
  13394. * @param {Number} keyExpirationTime (optional) The number of seconds after the key creation time that the key expires
  13395. * @return {Promise<Object>} The generated key object in the form:
  13396. * { key:Key, privateKeyArmored:String, publicKeyArmored:String }
  13397. * @static
  13398. */
  13399. function generateKey() {
  13400. var _ref2 = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
  13401. var _ref2$userIds = _ref2.userIds;
  13402. var userIds = _ref2$userIds === undefined ? [] : _ref2$userIds;
  13403. var passphrase = _ref2.passphrase;
  13404. var _ref2$numBits = _ref2.numBits;
  13405. var numBits = _ref2$numBits === undefined ? 2048 : _ref2$numBits;
  13406. var _ref2$unlocked = _ref2.unlocked;
  13407. var unlocked = _ref2$unlocked === undefined ? false : _ref2$unlocked;
  13408. var _ref2$keyExpirationTi = _ref2.keyExpirationTime;
  13409. var keyExpirationTime = _ref2$keyExpirationTi === undefined ? 0 : _ref2$keyExpirationTi;
  13410. var options = formatUserIds({ userIds: userIds, passphrase: passphrase, numBits: numBits, unlocked: unlocked, keyExpirationTime: keyExpirationTime });
  13411. if (!_util2.default.getWebCryptoAll() && asyncProxy) {
  13412. // use web worker if web crypto apis are not supported
  13413. return asyncProxy.delegate('generateKey', options);
  13414. }
  13415. return key.generate(options).then(function (newKey) {
  13416. return {
  13417. key: newKey,
  13418. privateKeyArmored: newKey.armor(),
  13419. publicKeyArmored: newKey.toPublic().armor()
  13420. };
  13421. }).catch(onError.bind(null, 'Error generating keypair'));
  13422. }
  13423. /**
  13424. * Reformats signature packets for a key and rewraps key object.
  13425. * @param {Array<Object>} userIds array of user IDs e.g. [{ name:'Phil Zimmermann', email:'phil@openpgp.org' }]
  13426. * @param {String} passphrase (optional) The passphrase used to encrypt the resulting private key
  13427. * @param {Boolean} unlocked (optional) If the returned secret part of the generated key is unlocked
  13428. * @param {Number} keyExpirationTime (optional) The number of seconds after the key creation time that the key expires
  13429. * @return {Promise<Object>} The generated key object in the form:
  13430. * { key:Key, privateKeyArmored:String, publicKeyArmored:String }
  13431. * @static
  13432. */
  13433. function reformatKey() {
  13434. var _ref3 = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
  13435. var privateKey = _ref3.privateKey;
  13436. var _ref3$userIds = _ref3.userIds;
  13437. var userIds = _ref3$userIds === undefined ? [] : _ref3$userIds;
  13438. var _ref3$passphrase = _ref3.passphrase;
  13439. var passphrase = _ref3$passphrase === undefined ? "" : _ref3$passphrase;
  13440. var _ref3$unlocked = _ref3.unlocked;
  13441. var unlocked = _ref3$unlocked === undefined ? false : _ref3$unlocked;
  13442. var _ref3$keyExpirationTi = _ref3.keyExpirationTime;
  13443. var keyExpirationTime = _ref3$keyExpirationTi === undefined ? 0 : _ref3$keyExpirationTi;
  13444. var options = formatUserIds({ privateKey: privateKey, userIds: userIds, passphrase: passphrase, unlocked: unlocked, keyExpirationTime: keyExpirationTime });
  13445. if (asyncProxy) {
  13446. return asyncProxy.delegate('reformatKey', options);
  13447. }
  13448. return key.reformat(options).then(function (newKey) {
  13449. return {
  13450. key: newKey,
  13451. privateKeyArmored: newKey.armor(),
  13452. publicKeyArmored: newKey.toPublic().armor()
  13453. };
  13454. }).catch(onError.bind(null, 'Error reformatting keypair'));
  13455. }
  13456. /**
  13457. * Unlock a private key with your passphrase.
  13458. * @param {Key} privateKey the private key that is to be decrypted
  13459. * @param {String} passphrase the user's passphrase chosen during key generation
  13460. * @return {Key} the unlocked private key
  13461. */
  13462. function decryptKey(_ref4) {
  13463. var privateKey = _ref4.privateKey;
  13464. var passphrase = _ref4.passphrase;
  13465. if (asyncProxy) {
  13466. // use web worker if available
  13467. return asyncProxy.delegate('decryptKey', { privateKey: privateKey, passphrase: passphrase });
  13468. }
  13469. return execute(function () {
  13470. if (!privateKey.decrypt(passphrase)) {
  13471. throw new Error('Invalid passphrase');
  13472. }
  13473. return {
  13474. key: privateKey
  13475. };
  13476. }, 'Error decrypting private key');
  13477. }
  13478. ///////////////////////////////////////////
  13479. // //
  13480. // Message encryption and decryption //
  13481. // //
  13482. ///////////////////////////////////////////
  13483. /**
  13484. * Encrypts message text/data with public keys, passwords or both at once. At least either public keys or passwords
  13485. * must be specified. If private keys are specified, those will be used to sign the message.
  13486. * @param {String|Uint8Array} data text/data to be encrypted as JavaScript binary string or Uint8Array
  13487. * @param {Key|Array<Key>} publicKeys (optional) array of keys or single key, used to encrypt the message
  13488. * @param {Key|Array<Key>} privateKeys (optional) private keys for signing. If omitted message will not be signed
  13489. * @param {String|Array<String>} passwords (optional) array of passwords or a single password to encrypt the message
  13490. * @param {String} filename (optional) a filename for the literal data packet
  13491. * @param {Boolean} armor (optional) if the return values should be ascii armored or the message/signature objects
  13492. * @param {Boolean} detached (optional) if the signature should be detached (if true, signature will be added to returned object)
  13493. * @param {Signature} signature (optional) a detached signature to add to the encrypted message
  13494. * @return {Promise<Object>} encrypted (and optionally signed message) in the form:
  13495. * {data: ASCII armored message if 'armor' is true,
  13496. * message: full Message object if 'armor' is false, signature: detached signature if 'detached' is true}
  13497. * @static
  13498. */
  13499. function encrypt(_ref5) {
  13500. var data = _ref5.data;
  13501. var publicKeys = _ref5.publicKeys;
  13502. var privateKeys = _ref5.privateKeys;
  13503. var passwords = _ref5.passwords;
  13504. var filename = _ref5.filename;
  13505. var _ref5$armor = _ref5.armor;
  13506. var armor = _ref5$armor === undefined ? true : _ref5$armor;
  13507. var _ref5$detached = _ref5.detached;
  13508. var detached = _ref5$detached === undefined ? false : _ref5$detached;
  13509. var _ref5$signature = _ref5.signature;
  13510. var signature = _ref5$signature === undefined ? null : _ref5$signature;
  13511. checkData(data);publicKeys = toArray(publicKeys);privateKeys = toArray(privateKeys);passwords = toArray(passwords);
  13512. if (!nativeAEAD() && asyncProxy) {
  13513. // use web worker if web crypto apis are not supported
  13514. return asyncProxy.delegate('encrypt', { data: data, publicKeys: publicKeys, privateKeys: privateKeys, passwords: passwords, filename: filename, armor: armor, detached: detached, signature: signature });
  13515. }
  13516. var result = {};
  13517. return Promise.resolve().then(function () {
  13518. var message = createMessage(data, filename);
  13519. if (!privateKeys) {
  13520. privateKeys = [];
  13521. }
  13522. if (privateKeys.length || signature) {
  13523. // sign the message only if private keys or signature is specified
  13524. if (detached) {
  13525. var detachedSignature = message.signDetached(privateKeys, signature);
  13526. if (armor) {
  13527. result.signature = detachedSignature.armor();
  13528. } else {
  13529. result.signature = detachedSignature;
  13530. }
  13531. } else {
  13532. message = message.sign(privateKeys, signature);
  13533. }
  13534. }
  13535. return message.encrypt(publicKeys, passwords);
  13536. }).then(function (message) {
  13537. if (armor) {
  13538. result.data = message.armor();
  13539. } else {
  13540. result.message = message;
  13541. }
  13542. return result;
  13543. }).catch(onError.bind(null, 'Error encrypting message'));
  13544. }
  13545. /**
  13546. * Decrypts a message with the user's private key, a session key or a password. Either a private key,
  13547. * a session key or a password must be specified.
  13548. * @param {Message} message the message object with the encrypted data
  13549. * @param {Key} privateKey (optional) private key with decrypted secret key data or session key
  13550. * @param {Key|Array<Key>} publicKeys (optional) array of public keys or single key, to verify signatures
  13551. * @param {Object} sessionKey (optional) session key in the form: { data:Uint8Array, algorithm:String }
  13552. * @param {String} password (optional) single password to decrypt the message
  13553. * @param {String} format (optional) return data format either as 'utf8' or 'binary'
  13554. * @param {Signature} signature (optional) detached signature for verification
  13555. * @return {Promise<Object>} decrypted and verified message in the form:
  13556. * { data:Uint8Array|String, filename:String, signatures:[{ keyid:String, valid:Boolean }] }
  13557. * @static
  13558. */
  13559. function decrypt(_ref6) {
  13560. var message = _ref6.message;
  13561. var privateKey = _ref6.privateKey;
  13562. var publicKeys = _ref6.publicKeys;
  13563. var sessionKey = _ref6.sessionKey;
  13564. var password = _ref6.password;
  13565. var _ref6$format = _ref6.format;
  13566. var format = _ref6$format === undefined ? 'utf8' : _ref6$format;
  13567. var _ref6$signature = _ref6.signature;
  13568. var signature = _ref6$signature === undefined ? null : _ref6$signature;
  13569. checkMessage(message);publicKeys = toArray(publicKeys);
  13570. if (!nativeAEAD() && asyncProxy) {
  13571. // use web worker if web crypto apis are not supported
  13572. return asyncProxy.delegate('decrypt', { message: message, privateKey: privateKey, publicKeys: publicKeys, sessionKey: sessionKey, password: password, format: format, signature: signature });
  13573. }
  13574. return message.decrypt(privateKey, sessionKey, password).then(function (message) {
  13575. var result = parseMessage(message, format);
  13576. if (result.data) {
  13577. // verify
  13578. if (!publicKeys) {
  13579. publicKeys = [];
  13580. }
  13581. if (signature) {
  13582. //detached signature
  13583. result.signatures = message.verifyDetached(signature, publicKeys);
  13584. } else {
  13585. result.signatures = message.verify(publicKeys);
  13586. }
  13587. }
  13588. return result;
  13589. }).catch(onError.bind(null, 'Error decrypting message'));
  13590. }
  13591. //////////////////////////////////////////
  13592. // //
  13593. // Message signing and verification //
  13594. // //
  13595. //////////////////////////////////////////
  13596. /**
  13597. * Signs a cleartext message.
  13598. * @param {String} data cleartext input to be signed
  13599. * @param {Key|Array<Key>} privateKeys array of keys or single key with decrypted secret key data to sign cleartext
  13600. * @param {Boolean} armor (optional) if the return value should be ascii armored or the message object
  13601. * @param {Boolean} detached (optional) if the return value should contain a detached signature
  13602. * @return {Promise<Object>} signed cleartext in the form:
  13603. * {data: ASCII armored message if 'armor' is true,
  13604. * message: full Message object if 'armor' is false, signature: detached signature if 'detached' is true}
  13605. * @static
  13606. */
  13607. function sign(_ref7) {
  13608. var data = _ref7.data;
  13609. var privateKeys = _ref7.privateKeys;
  13610. var _ref7$armor = _ref7.armor;
  13611. var armor = _ref7$armor === undefined ? true : _ref7$armor;
  13612. var _ref7$detached = _ref7.detached;
  13613. var detached = _ref7$detached === undefined ? false : _ref7$detached;
  13614. checkString(data);
  13615. privateKeys = toArray(privateKeys);
  13616. if (asyncProxy) {
  13617. // use web worker if available
  13618. return asyncProxy.delegate('sign', { data: data, privateKeys: privateKeys, armor: armor, detached: detached });
  13619. }
  13620. var result = {};
  13621. return execute(function () {
  13622. var cleartextMessage = new cleartext.CleartextMessage(data);
  13623. if (detached) {
  13624. var signature = cleartextMessage.signDetached(privateKeys);
  13625. if (armor) {
  13626. result.signature = signature.armor();
  13627. } else {
  13628. result.signature = signature;
  13629. }
  13630. } else {
  13631. cleartextMessage.sign(privateKeys);
  13632. }
  13633. if (armor) {
  13634. result.data = cleartextMessage.armor();
  13635. } else {
  13636. result.message = cleartextMessage;
  13637. }
  13638. return result;
  13639. }, 'Error signing cleartext message');
  13640. }
  13641. /**
  13642. * Verifies signatures of cleartext signed message
  13643. * @param {Key|Array<Key>} publicKeys array of publicKeys or single key, to verify signatures
  13644. * @param {CleartextMessage} message cleartext message object with signatures
  13645. * @param {Signature} signature (optional) detached signature for verification
  13646. * @return {Promise<Object>} cleartext with status of verified signatures in the form of:
  13647. * { data:String, signatures: [{ keyid:String, valid:Boolean }] }
  13648. * @static
  13649. */
  13650. function verify(_ref8) {
  13651. var message = _ref8.message;
  13652. var publicKeys = _ref8.publicKeys;
  13653. var _ref8$signature = _ref8.signature;
  13654. var signature = _ref8$signature === undefined ? null : _ref8$signature;
  13655. checkCleartextMessage(message);
  13656. publicKeys = toArray(publicKeys);
  13657. if (asyncProxy) {
  13658. // use web worker if available
  13659. return asyncProxy.delegate('verify', { message: message, publicKeys: publicKeys, signature: signature });
  13660. }
  13661. var result = {};
  13662. return execute(function () {
  13663. result.data = message.getText();
  13664. if (signature) {
  13665. //detached signature
  13666. result.signatures = message.verifyDetached(signature, publicKeys);
  13667. } else {
  13668. result.signatures = message.verify(publicKeys);
  13669. }
  13670. return result;
  13671. }, 'Error verifying cleartext signed message');
  13672. }
  13673. ///////////////////////////////////////////////
  13674. // //
  13675. // Session key encryption and decryption //
  13676. // //
  13677. ///////////////////////////////////////////////
  13678. /**
  13679. * Encrypt a symmetric session key with public keys, passwords, or both at once. At least either public keys
  13680. * or passwords must be specified.
  13681. * @param {Uint8Array} data the session key to be encrypted e.g. 16 random bytes (for aes128)
  13682. * @param {String} algorithm algorithm of the symmetric session key e.g. 'aes128' or 'aes256'
  13683. * @param {Key|Array<Key>} publicKeys (optional) array of public keys or single key, used to encrypt the key
  13684. * @param {String|Array<String>} passwords (optional) passwords for the message
  13685. * @return {Promise<Message>} the encrypted session key packets contained in a message object
  13686. * @static
  13687. */
  13688. function encryptSessionKey(_ref9) {
  13689. var data = _ref9.data;
  13690. var algorithm = _ref9.algorithm;
  13691. var publicKeys = _ref9.publicKeys;
  13692. var passwords = _ref9.passwords;
  13693. checkbinary(data);checkString(algorithm, 'algorithm');publicKeys = toArray(publicKeys);passwords = toArray(passwords);
  13694. if (asyncProxy) {
  13695. // use web worker if available
  13696. return asyncProxy.delegate('encryptSessionKey', { data: data, algorithm: algorithm, publicKeys: publicKeys, passwords: passwords });
  13697. }
  13698. return execute(function () {
  13699. return {
  13700. message: messageLib.encryptSessionKey(data, algorithm, publicKeys, passwords)
  13701. };
  13702. }, 'Error encrypting session key');
  13703. }
  13704. /**
  13705. * Decrypt a symmetric session key with a private key or password. Either a private key or
  13706. * a password must be specified.
  13707. * @param {Message} message a message object containing the encrypted session key packets
  13708. * @param {Key} privateKey (optional) private key with decrypted secret key data
  13709. * @param {String} password (optional) a single password to decrypt the session key
  13710. * @return {Promise<Object|undefined>} decrypted session key and algorithm in object form:
  13711. * { data:Uint8Array, algorithm:String }
  13712. * or 'undefined' if no key packets found
  13713. * @static
  13714. */
  13715. function decryptSessionKey(_ref10) {
  13716. var message = _ref10.message;
  13717. var privateKey = _ref10.privateKey;
  13718. var password = _ref10.password;
  13719. checkMessage(message);
  13720. if (asyncProxy) {
  13721. // use web worker if available
  13722. return asyncProxy.delegate('decryptSessionKey', { message: message, privateKey: privateKey, password: password });
  13723. }
  13724. return execute(function () {
  13725. return message.decryptSessionKey(privateKey, password);
  13726. }, 'Error decrypting session key');
  13727. }
  13728. //////////////////////////
  13729. // //
  13730. // Helper functions //
  13731. // //
  13732. //////////////////////////
  13733. /**
  13734. * Input validation
  13735. */
  13736. function checkString(data, name) {
  13737. if (!_util2.default.isString(data)) {
  13738. throw new Error('Parameter [' + (name || 'data') + '] must be of type String');
  13739. }
  13740. }
  13741. function checkbinary(data, name) {
  13742. if (!_util2.default.isUint8Array(data)) {
  13743. throw new Error('Parameter [' + (name || 'data') + '] must be of type Uint8Array');
  13744. }
  13745. }
  13746. function checkData(data, name) {
  13747. if (!_util2.default.isUint8Array(data) && !_util2.default.isString(data)) {
  13748. throw new Error('Parameter [' + (name || 'data') + '] must be of type String or Uint8Array');
  13749. }
  13750. }
  13751. function checkMessage(message) {
  13752. if (!messageLib.Message.prototype.isPrototypeOf(message)) {
  13753. throw new Error('Parameter [message] needs to be of type Message');
  13754. }
  13755. }
  13756. function checkCleartextMessage(message) {
  13757. if (!cleartext.CleartextMessage.prototype.isPrototypeOf(message)) {
  13758. throw new Error('Parameter [message] needs to be of type CleartextMessage');
  13759. }
  13760. }
  13761. /**
  13762. * Format user ids for internal use.
  13763. */
  13764. function formatUserIds(options) {
  13765. if (!options.userIds) {
  13766. return options;
  13767. }
  13768. options.userIds = toArray(options.userIds); // normalize to array
  13769. options.userIds = options.userIds.map(function (id) {
  13770. if (_util2.default.isString(id) && !_util2.default.isUserId(id)) {
  13771. throw new Error('Invalid user id format');
  13772. }
  13773. if (_util2.default.isUserId(id)) {
  13774. return id; // user id is already in correct format... no conversion necessary
  13775. }
  13776. // name and email address can be empty but must be of the correct type
  13777. id.name = id.name || '';
  13778. id.email = id.email || '';
  13779. if (!_util2.default.isString(id.name) || id.email && !_util2.default.isEmailAddress(id.email)) {
  13780. throw new Error('Invalid user id format');
  13781. }
  13782. id.name = id.name.trim();
  13783. if (id.name.length > 0) {
  13784. id.name += ' ';
  13785. }
  13786. return id.name + '<' + id.email + '>';
  13787. });
  13788. return options;
  13789. }
  13790. /**
  13791. * Normalize parameter to an array if it is not undefined.
  13792. * @param {Object} param the parameter to be normalized
  13793. * @return {Array<Object>|undefined} the resulting array or undefined
  13794. */
  13795. function toArray(param) {
  13796. if (param && !_util2.default.isArray(param)) {
  13797. param = [param];
  13798. }
  13799. return param;
  13800. }
  13801. /**
  13802. * Creates a message obejct either from a Uint8Array or a string.
  13803. * @param {String|Uint8Array} data the payload for the message
  13804. * @param {String} filename the literal data packet's filename
  13805. * @return {Message} a message object
  13806. */
  13807. function createMessage(data, filename) {
  13808. var msg = void 0;
  13809. if (_util2.default.isUint8Array(data)) {
  13810. msg = messageLib.fromBinary(data, filename);
  13811. } else if (_util2.default.isString(data)) {
  13812. msg = messageLib.fromText(data, filename);
  13813. } else {
  13814. throw new Error('Data must be of type String or Uint8Array');
  13815. }
  13816. return msg;
  13817. }
  13818. /**
  13819. * Parse the message given a certain format.
  13820. * @param {Message} message the message object to be parse
  13821. * @param {String} format the output format e.g. 'utf8' or 'binary'
  13822. * @return {Object} the parse data in the respective format
  13823. */
  13824. function parseMessage(message, format) {
  13825. if (format === 'binary') {
  13826. return {
  13827. data: message.getLiteralData(),
  13828. filename: message.getFilename()
  13829. };
  13830. } else if (format === 'utf8') {
  13831. return {
  13832. data: message.getText(),
  13833. filename: message.getFilename()
  13834. };
  13835. } else {
  13836. throw new Error('Invalid format');
  13837. }
  13838. }
  13839. /**
  13840. * Command pattern that wraps synchronous code into a promise.
  13841. * @param {function} cmd The synchronous function with a return value
  13842. * to be wrapped in a promise
  13843. * @param {String} message A human readable error Message
  13844. * @return {Promise} The promise wrapped around cmd
  13845. */
  13846. function execute(cmd, message) {
  13847. // wrap the sync cmd in a promise
  13848. var promise = new Promise(function (resolve) {
  13849. return resolve(cmd());
  13850. });
  13851. // handler error globally
  13852. return promise.catch(onError.bind(null, message));
  13853. }
  13854. /**
  13855. * Global error handler that logs the stack trace and rethrows a high lvl error message.
  13856. * @param {String} message A human readable high level error Message
  13857. * @param {Error} error The internal error that caused the failure
  13858. */
  13859. function onError(message, error) {
  13860. // log the stack trace
  13861. if (_config2.default.debug) {
  13862. console.error(error.stack);
  13863. }
  13864. // rethrow new high level error for api users
  13865. throw new Error(message + ': ' + error.message);
  13866. }
  13867. /**
  13868. * Check for AES-GCM support and configuration by the user. Only browsers that
  13869. * implement the current WebCrypto specification support native AES-GCM.
  13870. * @return {Boolean} If authenticated encryption should be used
  13871. */
  13872. function nativeAEAD() {
  13873. return _util2.default.getWebCrypto() && _config2.default.aead_protect;
  13874. }
  13875. },{"./cleartext.js":5,"./config/config.js":9,"./key.js":38,"./message.js":42,"./util":70,"./worker/async_proxy.js":71,"es6-promise":2}],44:[function(_dereq_,module,exports){
  13876. /**
  13877. * @requires enums
  13878. * @module packet
  13879. */
  13880. 'use strict';
  13881. Object.defineProperty(exports, "__esModule", {
  13882. value: true
  13883. });
  13884. exports.Trust = exports.Signature = exports.SecretSubkey = exports.Userid = exports.SecretKey = exports.OnePassSignature = exports.UserAttribute = exports.PublicSubkey = exports.Marker = exports.SymmetricallyEncrypted = exports.PublicKey = exports.Literal = exports.SymEncryptedSessionKey = exports.PublicKeyEncryptedSessionKey = exports.SymEncryptedAEADProtected = exports.SymEncryptedIntegrityProtected = exports.Compressed = undefined;
  13885. var _compressed = _dereq_('./compressed.js');
  13886. Object.defineProperty(exports, 'Compressed', {
  13887. enumerable: true,
  13888. get: function get() {
  13889. return _interopRequireDefault(_compressed).default;
  13890. }
  13891. });
  13892. var _sym_encrypted_integrity_protected = _dereq_('./sym_encrypted_integrity_protected.js');
  13893. Object.defineProperty(exports, 'SymEncryptedIntegrityProtected', {
  13894. enumerable: true,
  13895. get: function get() {
  13896. return _interopRequireDefault(_sym_encrypted_integrity_protected).default;
  13897. }
  13898. });
  13899. var _sym_encrypted_aead_protected = _dereq_('./sym_encrypted_aead_protected.js');
  13900. Object.defineProperty(exports, 'SymEncryptedAEADProtected', {
  13901. enumerable: true,
  13902. get: function get() {
  13903. return _interopRequireDefault(_sym_encrypted_aead_protected).default;
  13904. }
  13905. });
  13906. var _public_key_encrypted_session_key = _dereq_('./public_key_encrypted_session_key.js');
  13907. Object.defineProperty(exports, 'PublicKeyEncryptedSessionKey', {
  13908. enumerable: true,
  13909. get: function get() {
  13910. return _interopRequireDefault(_public_key_encrypted_session_key).default;
  13911. }
  13912. });
  13913. var _sym_encrypted_session_key = _dereq_('./sym_encrypted_session_key.js');
  13914. Object.defineProperty(exports, 'SymEncryptedSessionKey', {
  13915. enumerable: true,
  13916. get: function get() {
  13917. return _interopRequireDefault(_sym_encrypted_session_key).default;
  13918. }
  13919. });
  13920. var _literal = _dereq_('./literal.js');
  13921. Object.defineProperty(exports, 'Literal', {
  13922. enumerable: true,
  13923. get: function get() {
  13924. return _interopRequireDefault(_literal).default;
  13925. }
  13926. });
  13927. var _public_key = _dereq_('./public_key.js');
  13928. Object.defineProperty(exports, 'PublicKey', {
  13929. enumerable: true,
  13930. get: function get() {
  13931. return _interopRequireDefault(_public_key).default;
  13932. }
  13933. });
  13934. var _symmetrically_encrypted = _dereq_('./symmetrically_encrypted.js');
  13935. Object.defineProperty(exports, 'SymmetricallyEncrypted', {
  13936. enumerable: true,
  13937. get: function get() {
  13938. return _interopRequireDefault(_symmetrically_encrypted).default;
  13939. }
  13940. });
  13941. var _marker = _dereq_('./marker.js');
  13942. Object.defineProperty(exports, 'Marker', {
  13943. enumerable: true,
  13944. get: function get() {
  13945. return _interopRequireDefault(_marker).default;
  13946. }
  13947. });
  13948. var _public_subkey = _dereq_('./public_subkey.js');
  13949. Object.defineProperty(exports, 'PublicSubkey', {
  13950. enumerable: true,
  13951. get: function get() {
  13952. return _interopRequireDefault(_public_subkey).default;
  13953. }
  13954. });
  13955. var _user_attribute = _dereq_('./user_attribute.js');
  13956. Object.defineProperty(exports, 'UserAttribute', {
  13957. enumerable: true,
  13958. get: function get() {
  13959. return _interopRequireDefault(_user_attribute).default;
  13960. }
  13961. });
  13962. var _one_pass_signature = _dereq_('./one_pass_signature.js');
  13963. Object.defineProperty(exports, 'OnePassSignature', {
  13964. enumerable: true,
  13965. get: function get() {
  13966. return _interopRequireDefault(_one_pass_signature).default;
  13967. }
  13968. });
  13969. var _secret_key = _dereq_('./secret_key.js');
  13970. Object.defineProperty(exports, 'SecretKey', {
  13971. enumerable: true,
  13972. get: function get() {
  13973. return _interopRequireDefault(_secret_key).default;
  13974. }
  13975. });
  13976. var _userid = _dereq_('./userid.js');
  13977. Object.defineProperty(exports, 'Userid', {
  13978. enumerable: true,
  13979. get: function get() {
  13980. return _interopRequireDefault(_userid).default;
  13981. }
  13982. });
  13983. var _secret_subkey = _dereq_('./secret_subkey.js');
  13984. Object.defineProperty(exports, 'SecretSubkey', {
  13985. enumerable: true,
  13986. get: function get() {
  13987. return _interopRequireDefault(_secret_subkey).default;
  13988. }
  13989. });
  13990. var _signature = _dereq_('./signature.js');
  13991. Object.defineProperty(exports, 'Signature', {
  13992. enumerable: true,
  13993. get: function get() {
  13994. return _interopRequireDefault(_signature).default;
  13995. }
  13996. });
  13997. var _trust = _dereq_('./trust.js');
  13998. Object.defineProperty(exports, 'Trust', {
  13999. enumerable: true,
  14000. get: function get() {
  14001. return _interopRequireDefault(_trust).default;
  14002. }
  14003. });
  14004. exports.newPacketFromTag = newPacketFromTag;
  14005. exports.fromStructuredClone = fromStructuredClone;
  14006. var _enums = _dereq_('../enums.js');
  14007. var _enums2 = _interopRequireDefault(_enums);
  14008. var _all_packets = _dereq_('./all_packets.js');
  14009. var packets = _interopRequireWildcard(_all_packets);
  14010. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  14011. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14012. /**
  14013. * Allocate a new packet
  14014. * @param {String} tag property name from {@link module:enums.packet}
  14015. * @returns {Object} new packet object with type based on tag
  14016. */
  14017. function newPacketFromTag(tag) {
  14018. return new packets[packetClassFromTagName(tag)]();
  14019. }
  14020. /**
  14021. * Allocate a new packet from structured packet clone
  14022. * See {@link http://www.w3.org/html/wg/drafts/html/master/infrastructure.html#safe-passing-of-structured-data}
  14023. * @param {Object} packetClone packet clone
  14024. * @returns {Object} new packet object with data from packet clone
  14025. */
  14026. function fromStructuredClone(packetClone) {
  14027. var tagName = _enums2.default.read(_enums2.default.packet, packetClone.tag);
  14028. var packet = newPacketFromTag(tagName);
  14029. for (var attr in packetClone) {
  14030. if (packetClone.hasOwnProperty(attr)) {
  14031. packet[attr] = packetClone[attr];
  14032. }
  14033. }
  14034. if (packet.postCloneTypeFix) {
  14035. packet.postCloneTypeFix();
  14036. }
  14037. return packet;
  14038. }
  14039. /**
  14040. * Convert tag name to class name
  14041. * @param {String} tag property name from {@link module:enums.packet}
  14042. * @returns {String}
  14043. */
  14044. function packetClassFromTagName(tag) {
  14045. return tag.substr(0, 1).toUpperCase() + tag.substr(1);
  14046. }
  14047. },{"../enums.js":35,"./all_packets.js":44,"./compressed.js":46,"./literal.js":48,"./marker.js":49,"./one_pass_signature.js":50,"./public_key.js":53,"./public_key_encrypted_session_key.js":54,"./public_subkey.js":55,"./secret_key.js":56,"./secret_subkey.js":57,"./signature.js":58,"./sym_encrypted_aead_protected.js":59,"./sym_encrypted_integrity_protected.js":60,"./sym_encrypted_session_key.js":61,"./symmetrically_encrypted.js":62,"./trust.js":63,"./user_attribute.js":64,"./userid.js":65}],45:[function(_dereq_,module,exports){
  14048. // OpenPGP.js - An OpenPGP implementation in javascript
  14049. // Copyright (C) 2015 Tankred Hase
  14050. //
  14051. // This library is free software; you can redistribute it and/or
  14052. // modify it under the terms of the GNU Lesser General Public
  14053. // License as published by the Free Software Foundation; either
  14054. // version 3.0 of the License, or (at your option) any later version.
  14055. //
  14056. // This library is distributed in the hope that it will be useful,
  14057. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14058. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14059. // Lesser General Public License for more details.
  14060. //
  14061. // You should have received a copy of the GNU Lesser General Public
  14062. // License along with this library; if not, write to the Free Software
  14063. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  14064. /**
  14065. * @fileoverview This module implements packet list cloning required to
  14066. * pass certain object types beteen the web worker and main thread using
  14067. * the structured cloning algorithm.
  14068. */
  14069. 'use strict';
  14070. Object.defineProperty(exports, "__esModule", {
  14071. value: true
  14072. });
  14073. exports.clonePackets = clonePackets;
  14074. exports.parseClonedPackets = parseClonedPackets;
  14075. var _key = _dereq_('../key.js');
  14076. var key = _interopRequireWildcard(_key);
  14077. var _message = _dereq_('../message.js');
  14078. var message = _interopRequireWildcard(_message);
  14079. var _cleartext = _dereq_('../cleartext.js');
  14080. var cleartext = _interopRequireWildcard(_cleartext);
  14081. var _signature = _dereq_('../signature.js');
  14082. var signature = _interopRequireWildcard(_signature);
  14083. var _packetlist = _dereq_('./packetlist.js');
  14084. var _packetlist2 = _interopRequireDefault(_packetlist);
  14085. var _keyid = _dereq_('../type/keyid.js');
  14086. var _keyid2 = _interopRequireDefault(_keyid);
  14087. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14088. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  14089. //////////////////////////////
  14090. // //
  14091. // Packetlist --> Clone //
  14092. // //
  14093. //////////////////////////////
  14094. /**
  14095. * Create a packetlist from the correspoding object types.
  14096. * @param {Object} options the object passed to and from the web worker
  14097. * @return {Object} a mutated version of the options optject
  14098. */
  14099. function clonePackets(options) {
  14100. if (options.publicKeys) {
  14101. options.publicKeys = options.publicKeys.map(function (key) {
  14102. return key.toPacketlist();
  14103. });
  14104. }
  14105. if (options.privateKeys) {
  14106. options.privateKeys = options.privateKeys.map(function (key) {
  14107. return key.toPacketlist();
  14108. });
  14109. }
  14110. if (options.privateKey) {
  14111. options.privateKey = options.privateKey.toPacketlist();
  14112. }
  14113. if (options.key) {
  14114. options.key = options.key.toPacketlist();
  14115. }
  14116. if (options.message) {
  14117. //could be either a Message or CleartextMessage object
  14118. if (options.message instanceof message.Message) {
  14119. options.message = options.message.packets;
  14120. } else if (options.message instanceof cleartext.CleartextMessage) {
  14121. options.message.signature = options.message.signature.packets;
  14122. }
  14123. }
  14124. if (options.signature && options.signature instanceof signature.Signature) {
  14125. options.signature = options.signature.packets;
  14126. }
  14127. if (options.signatures) {
  14128. options.signatures = options.signatures.map(function (sig) {
  14129. return verificationObjectToClone(sig);
  14130. });
  14131. }
  14132. return options;
  14133. }
  14134. function verificationObjectToClone(verObject) {
  14135. verObject.signature = verObject.signature.packets;
  14136. return verObject;
  14137. }
  14138. //////////////////////////////
  14139. // //
  14140. // Clone --> Packetlist //
  14141. // //
  14142. //////////////////////////////
  14143. /**
  14144. * Creates an object with the correct prototype from a corresponding packetlist.
  14145. * @param {Object} options the object passed to and from the web worker
  14146. * @param {String} method the public api function name to be delegated to the worker
  14147. * @return {Object} a mutated version of the options optject
  14148. */
  14149. function parseClonedPackets(options, method) {
  14150. if (options.publicKeys) {
  14151. options.publicKeys = options.publicKeys.map(packetlistCloneToKey);
  14152. }
  14153. if (options.privateKeys) {
  14154. options.privateKeys = options.privateKeys.map(packetlistCloneToKey);
  14155. }
  14156. if (options.privateKey) {
  14157. options.privateKey = packetlistCloneToKey(options.privateKey);
  14158. }
  14159. if (options.key) {
  14160. options.key = packetlistCloneToKey(options.key);
  14161. }
  14162. if (options.message && (method === 'sign' || method === 'verify')) {
  14163. // sign and verify support only CleartextMessage
  14164. options.message = packetlistCloneToCleartextMessage(options.message);
  14165. } else if (options.message) {
  14166. options.message = packetlistCloneToMessage(options.message);
  14167. }
  14168. if (options.signatures) {
  14169. options.signatures = options.signatures.map(packetlistCloneToSignatures);
  14170. }
  14171. if (options.signature) {
  14172. options.signature = packetlistCloneToSignature(options.signature);
  14173. }
  14174. return options;
  14175. }
  14176. function packetlistCloneToKey(clone) {
  14177. var packetlist = _packetlist2.default.fromStructuredClone(clone);
  14178. return new key.Key(packetlist);
  14179. }
  14180. function packetlistCloneToMessage(clone) {
  14181. var packetlist = _packetlist2.default.fromStructuredClone(clone);
  14182. return new message.Message(packetlist);
  14183. }
  14184. function packetlistCloneToCleartextMessage(clone) {
  14185. var packetlist = _packetlist2.default.fromStructuredClone(clone.signature);
  14186. return new cleartext.CleartextMessage(clone.text, new signature.Signature(packetlist));
  14187. }
  14188. //verification objects
  14189. function packetlistCloneToSignatures(clone) {
  14190. clone.keyid = _keyid2.default.fromClone(clone.keyid);
  14191. clone.signature = new signature.Signature(clone.signature);
  14192. return clone;
  14193. }
  14194. function packetlistCloneToSignature(clone) {
  14195. if (typeof clone === "string") {
  14196. //signature is armored
  14197. return clone;
  14198. }
  14199. var packetlist = _packetlist2.default.fromStructuredClone(clone);
  14200. return new signature.Signature(packetlist);
  14201. }
  14202. },{"../cleartext.js":5,"../key.js":38,"../message.js":42,"../signature.js":66,"../type/keyid.js":67,"./packetlist.js":52}],46:[function(_dereq_,module,exports){
  14203. // GPG4Browsers - An OpenPGP implementation in javascript
  14204. // Copyright (C) 2011 Recurity Labs GmbH
  14205. //
  14206. // This library is free software; you can redistribute it and/or
  14207. // modify it under the terms of the GNU Lesser General Public
  14208. // License as published by the Free Software Foundation; either
  14209. // version 3.0 of the License, or (at your option) any later version.
  14210. //
  14211. // This library is distributed in the hope that it will be useful,
  14212. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14213. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14214. // Lesser General Public License for more details.
  14215. //
  14216. // You should have received a copy of the GNU Lesser General Public
  14217. // License along with this library; if not, write to the Free Software
  14218. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  14219. /**
  14220. * Implementation of the Compressed Data Packet (Tag 8)<br/>
  14221. * <br/>
  14222. * {@link http://tools.ietf.org/html/rfc4880#section-5.6|RFC4880 5.6}: The Compressed Data packet contains compressed data. Typically,
  14223. * this packet is found as the contents of an encrypted packet, or following
  14224. * a Signature or One-Pass Signature packet, and contains a literal data packet.
  14225. * @requires compression/zlib
  14226. * @requires compression/rawinflate
  14227. * @requires compression/rawdeflate
  14228. * @requires enums
  14229. * @requires util
  14230. * @module packet/compressed
  14231. */
  14232. 'use strict';
  14233. Object.defineProperty(exports, "__esModule", {
  14234. value: true
  14235. });
  14236. exports.default = Compressed;
  14237. var _enums = _dereq_('../enums.js');
  14238. var _enums2 = _interopRequireDefault(_enums);
  14239. var _util = _dereq_('../util.js');
  14240. var _util2 = _interopRequireDefault(_util);
  14241. var _zlibMin = _dereq_('../compression/zlib.min.js');
  14242. var _zlibMin2 = _interopRequireDefault(_zlibMin);
  14243. var _rawinflateMin = _dereq_('../compression/rawinflate.min.js');
  14244. var _rawinflateMin2 = _interopRequireDefault(_rawinflateMin);
  14245. var _rawdeflateMin = _dereq_('../compression/rawdeflate.min.js');
  14246. var _rawdeflateMin2 = _interopRequireDefault(_rawdeflateMin);
  14247. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14248. /**
  14249. * @constructor
  14250. */
  14251. function Compressed() {
  14252. /**
  14253. * Packet type
  14254. * @type {module:enums.packet}
  14255. */
  14256. this.tag = _enums2.default.packet.compressed;
  14257. /**
  14258. * List of packets
  14259. * @type {module:packet/packetlist}
  14260. */
  14261. this.packets = null;
  14262. /**
  14263. * Compression algorithm
  14264. * @type {compression}
  14265. */
  14266. this.algorithm = 'zip';
  14267. /**
  14268. * Compressed packet data
  14269. * @type {String}
  14270. */
  14271. this.compressed = null;
  14272. }
  14273. /**
  14274. * Parsing function for the packet.
  14275. * @param {String} bytes Payload of a tag 8 packet
  14276. */
  14277. Compressed.prototype.read = function (bytes) {
  14278. // One octet that gives the algorithm used to compress the packet.
  14279. this.algorithm = _enums2.default.read(_enums2.default.compression, bytes[0]);
  14280. // Compressed data, which makes up the remainder of the packet.
  14281. this.compressed = bytes.subarray(1, bytes.length);
  14282. this.decompress();
  14283. };
  14284. /**
  14285. * Return the compressed packet.
  14286. * @return {String} binary compressed packet
  14287. */
  14288. Compressed.prototype.write = function () {
  14289. if (this.compressed === null) {
  14290. this.compress();
  14291. }
  14292. return _util2.default.concatUint8Array(new Uint8Array([_enums2.default.write(_enums2.default.compression, this.algorithm)]), this.compressed);
  14293. };
  14294. /**
  14295. * Decompression method for decompressing the compressed data
  14296. * read by read_packet
  14297. */
  14298. Compressed.prototype.decompress = function () {
  14299. var decompressed, inflate;
  14300. switch (this.algorithm) {
  14301. case 'uncompressed':
  14302. decompressed = this.compressed;
  14303. break;
  14304. case 'zip':
  14305. inflate = new _rawinflateMin2.default.Zlib.RawInflate(this.compressed);
  14306. decompressed = inflate.decompress();
  14307. break;
  14308. case 'zlib':
  14309. inflate = new _zlibMin2.default.Zlib.Inflate(this.compressed);
  14310. decompressed = inflate.decompress();
  14311. break;
  14312. case 'bzip2':
  14313. // TODO: need to implement this
  14314. throw new Error('Compression algorithm BZip2 [BZ2] is not implemented.');
  14315. default:
  14316. throw new Error("Compression algorithm unknown :" + this.alogrithm);
  14317. }
  14318. this.packets.read(decompressed);
  14319. };
  14320. /**
  14321. * Compress the packet data (member decompressedData)
  14322. */
  14323. Compressed.prototype.compress = function () {
  14324. var uncompressed, deflate;
  14325. uncompressed = this.packets.write();
  14326. switch (this.algorithm) {
  14327. case 'uncompressed':
  14328. // - Uncompressed
  14329. this.compressed = uncompressed;
  14330. break;
  14331. case 'zip':
  14332. // - ZIP [RFC1951]
  14333. deflate = new _rawdeflateMin2.default.Zlib.RawDeflate(uncompressed);
  14334. this.compressed = deflate.compress();
  14335. break;
  14336. case 'zlib':
  14337. // - ZLIB [RFC1950]
  14338. deflate = new _zlibMin2.default.Zlib.Deflate(uncompressed);
  14339. this.compressed = deflate.compress();
  14340. break;
  14341. case 'bzip2':
  14342. // - BZip2 [BZ2]
  14343. // TODO: need to implement this
  14344. throw new Error("Compression algorithm BZip2 [BZ2] is not implemented.");
  14345. default:
  14346. throw new Error("Compression algorithm unknown :" + this.type);
  14347. }
  14348. };
  14349. },{"../compression/rawdeflate.min.js":6,"../compression/rawinflate.min.js":7,"../compression/zlib.min.js":8,"../enums.js":35,"../util.js":70}],47:[function(_dereq_,module,exports){
  14350. 'use strict';
  14351. Object.defineProperty(exports, "__esModule", {
  14352. value: true
  14353. });
  14354. var _all_packets = _dereq_('./all_packets.js');
  14355. var packets = _interopRequireWildcard(_all_packets);
  14356. var _clone = _dereq_('./clone.js');
  14357. var clone = _interopRequireWildcard(_clone);
  14358. var _packetlist = _dereq_('./packetlist.js');
  14359. var _packetlist2 = _interopRequireDefault(_packetlist);
  14360. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14361. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  14362. var mod = {
  14363. /** @see module:packet/packetlist */
  14364. List: _packetlist2.default,
  14365. /** @see module:packet/clone */
  14366. clone: clone
  14367. };
  14368. for (var i in packets) {
  14369. mod[i] = packets[i];
  14370. }
  14371. exports.default = mod;
  14372. },{"./all_packets.js":44,"./clone.js":45,"./packetlist.js":52}],48:[function(_dereq_,module,exports){
  14373. // GPG4Browsers - An OpenPGP implementation in javascript
  14374. // Copyright (C) 2011 Recurity Labs GmbH
  14375. //
  14376. // This library is free software; you can redistribute it and/or
  14377. // modify it under the terms of the GNU Lesser General Public
  14378. // License as published by the Free Software Foundation; either
  14379. // version 3.0 of the License, or (at your option) any later version.
  14380. //
  14381. // This library is distributed in the hope that it will be useful,
  14382. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14383. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14384. // Lesser General Public License for more details.
  14385. //
  14386. // You should have received a copy of the GNU Lesser General Public
  14387. // License along with this library; if not, write to the Free Software
  14388. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  14389. /**
  14390. * Implementation of the Literal Data Packet (Tag 11)<br/>
  14391. * <br/>
  14392. * {@link http://tools.ietf.org/html/rfc4880#section-5.9|RFC4880 5.9}: A Literal Data packet contains the body of a message; data that
  14393. * is not to be further interpreted.
  14394. * @requires enums
  14395. * @requires util
  14396. * @module packet/literal
  14397. */
  14398. 'use strict';
  14399. Object.defineProperty(exports, "__esModule", {
  14400. value: true
  14401. });
  14402. exports.default = Literal;
  14403. var _util = _dereq_('../util.js');
  14404. var _util2 = _interopRequireDefault(_util);
  14405. var _enums = _dereq_('../enums.js');
  14406. var _enums2 = _interopRequireDefault(_enums);
  14407. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14408. /**
  14409. * @constructor
  14410. */
  14411. function Literal() {
  14412. this.tag = _enums2.default.packet.literal;
  14413. this.format = 'utf8'; // default format for literal data packets
  14414. this.date = new Date();
  14415. this.data = new Uint8Array(0); // literal data representation
  14416. this.filename = 'msg.txt';
  14417. }
  14418. /**
  14419. * Set the packet data to a javascript native string, end of line
  14420. * will be normalized to \r\n and by default text is converted to UTF8
  14421. * @param {String} text Any native javascript string
  14422. */
  14423. Literal.prototype.setText = function (text) {
  14424. // normalize EOL to \r\n
  14425. text = text.replace(/\r\n/g, '\n').replace(/\r/g, '\n').replace(/\n/g, '\r\n');
  14426. // encode UTF8
  14427. this.data = this.format === 'utf8' ? _util2.default.str2Uint8Array(_util2.default.encode_utf8(text)) : _util2.default.str2Uint8Array(text);
  14428. };
  14429. /**
  14430. * Returns literal data packets as native JavaScript string
  14431. * with normalized end of line to \n
  14432. * @return {String} literal data as text
  14433. */
  14434. Literal.prototype.getText = function () {
  14435. // decode UTF8
  14436. var text = _util2.default.decode_utf8(_util2.default.Uint8Array2str(this.data));
  14437. // normalize EOL to \n
  14438. return text.replace(/\r\n/g, '\n');
  14439. };
  14440. /**
  14441. * Set the packet data to value represented by the provided string of bytes.
  14442. * @param {Uint8Array} bytes The string of bytes
  14443. * @param {utf8|binary|text} format The format of the string of bytes
  14444. */
  14445. Literal.prototype.setBytes = function (bytes, format) {
  14446. this.format = format;
  14447. this.data = bytes;
  14448. };
  14449. /**
  14450. * Get the byte sequence representing the literal packet data
  14451. * @returns {Uint8Array} A sequence of bytes
  14452. */
  14453. Literal.prototype.getBytes = function () {
  14454. return this.data;
  14455. };
  14456. /**
  14457. * Sets the filename of the literal packet data
  14458. * @param {String} filename Any native javascript string
  14459. */
  14460. Literal.prototype.setFilename = function (filename) {
  14461. this.filename = filename;
  14462. };
  14463. /**
  14464. * Get the filename of the literal packet data
  14465. * @returns {String} filename
  14466. */
  14467. Literal.prototype.getFilename = function () {
  14468. return this.filename;
  14469. };
  14470. /**
  14471. * Parsing function for a literal data packet (tag 11).
  14472. *
  14473. * @param {Uint8Array} input Payload of a tag 11 packet
  14474. * @return {module:packet/literal} object representation
  14475. */
  14476. Literal.prototype.read = function (bytes) {
  14477. // - A one-octet field that describes how the data is formatted.
  14478. var format = _enums2.default.read(_enums2.default.literal, bytes[0]);
  14479. var filename_len = bytes[1];
  14480. this.filename = _util2.default.decode_utf8(_util2.default.Uint8Array2str(bytes.subarray(2, 2 + filename_len)));
  14481. this.date = _util2.default.readDate(bytes.subarray(2 + filename_len, 2 + filename_len + 4));
  14482. var data = bytes.subarray(6 + filename_len, bytes.length);
  14483. this.setBytes(data, format);
  14484. };
  14485. /**
  14486. * Creates a string representation of the packet
  14487. *
  14488. * @return {Uint8Array} Uint8Array representation of the packet
  14489. */
  14490. Literal.prototype.write = function () {
  14491. var filename = _util2.default.str2Uint8Array(_util2.default.encode_utf8(this.filename));
  14492. var filename_length = new Uint8Array([filename.length]);
  14493. var format = new Uint8Array([_enums2.default.write(_enums2.default.literal, this.format)]);
  14494. var date = _util2.default.writeDate(this.date);
  14495. var data = this.getBytes();
  14496. return _util2.default.concatUint8Array([format, filename_length, filename, date, data]);
  14497. };
  14498. },{"../enums.js":35,"../util.js":70}],49:[function(_dereq_,module,exports){
  14499. // GPG4Browsers - An OpenPGP implementation in javascript
  14500. // Copyright (C) 2011 Recurity Labs GmbH
  14501. //
  14502. // This library is free software; you can redistribute it and/or
  14503. // modify it under the terms of the GNU Lesser General Public
  14504. // License as published by the Free Software Foundation; either
  14505. // version 3.0 of the License, or (at your option) any later version.
  14506. //
  14507. // This library is distributed in the hope that it will be useful,
  14508. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14509. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14510. // Lesser General Public License for more details.
  14511. //
  14512. // You should have received a copy of the GNU Lesser General Public
  14513. // License along with this library; if not, write to the Free Software
  14514. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  14515. /**
  14516. * Implementation of the strange "Marker packet" (Tag 10)<br/>
  14517. * <br/>
  14518. * {@link http://tools.ietf.org/html/rfc4880#section-5.8|RFC4880 5.8}: An experimental version of PGP used this packet as the Literal
  14519. * packet, but no released version of PGP generated Literal packets with this
  14520. * tag. With PGP 5.x, this packet has been reassigned and is reserved for use as
  14521. * the Marker packet.<br/>
  14522. * <br/>
  14523. * Such a packet MUST be ignored when received.
  14524. * @requires enums
  14525. * @module packet/marker
  14526. */
  14527. 'use strict';
  14528. Object.defineProperty(exports, "__esModule", {
  14529. value: true
  14530. });
  14531. exports.default = Marker;
  14532. var _enums = _dereq_('../enums.js');
  14533. var _enums2 = _interopRequireDefault(_enums);
  14534. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14535. /**
  14536. * @constructor
  14537. */
  14538. function Marker() {
  14539. this.tag = _enums2.default.packet.marker;
  14540. }
  14541. /**
  14542. * Parsing function for a literal data packet (tag 10).
  14543. *
  14544. * @param {String} input Payload of a tag 10 packet
  14545. * @param {Integer} position
  14546. * Position to start reading from the input string
  14547. * @param {Integer} len
  14548. * Length of the packet or the remaining length of
  14549. * input at position
  14550. * @return {module:packet/marker} Object representation
  14551. */
  14552. Marker.prototype.read = function (bytes) {
  14553. if (bytes[0] === 0x50 && // P
  14554. bytes[1] === 0x47 && // G
  14555. bytes[2] === 0x50) {
  14556. // P
  14557. return true;
  14558. }
  14559. // marker packet does not contain "PGP"
  14560. return false;
  14561. };
  14562. },{"../enums.js":35}],50:[function(_dereq_,module,exports){
  14563. // GPG4Browsers - An OpenPGP implementation in javascript
  14564. // Copyright (C) 2011 Recurity Labs GmbH
  14565. //
  14566. // This library is free software; you can redistribute it and/or
  14567. // modify it under the terms of the GNU Lesser General Public
  14568. // License as published by the Free Software Foundation; either
  14569. // version 3.0 of the License, or (at your option) any later version.
  14570. //
  14571. // This library is distributed in the hope that it will be useful,
  14572. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14573. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14574. // Lesser General Public License for more details.
  14575. //
  14576. // You should have received a copy of the GNU Lesser General Public
  14577. // License along with this library; if not, write to the Free Software
  14578. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  14579. /**
  14580. * Implementation of the One-Pass Signature Packets (Tag 4)<br/>
  14581. * <br/>
  14582. * {@link http://tools.ietf.org/html/rfc4880#section-5.4|RFC4880 5.4}: The One-Pass Signature packet precedes the signed data and contains
  14583. * enough information to allow the receiver to begin calculating any
  14584. * hashes needed to verify the signature. It allows the Signature
  14585. * packet to be placed at the end of the message, so that the signer
  14586. * can compute the entire signed message in one pass.
  14587. * @requires util
  14588. * @requires enums
  14589. * @requires type/keyid
  14590. * @module packet/one_pass_signature
  14591. */
  14592. 'use strict';
  14593. Object.defineProperty(exports, "__esModule", {
  14594. value: true
  14595. });
  14596. exports.default = OnePassSignature;
  14597. var _util = _dereq_('../util.js');
  14598. var _util2 = _interopRequireDefault(_util);
  14599. var _enums = _dereq_('../enums.js');
  14600. var _enums2 = _interopRequireDefault(_enums);
  14601. var _keyid = _dereq_('../type/keyid.js');
  14602. var _keyid2 = _interopRequireDefault(_keyid);
  14603. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14604. /**
  14605. * @constructor
  14606. */
  14607. function OnePassSignature() {
  14608. this.tag = _enums2.default.packet.onePassSignature; // The packet type
  14609. this.version = null; // A one-octet version number. The current version is 3.
  14610. this.type = null; // A one-octet signature type. Signature types are described in {@link http://tools.ietf.org/html/rfc4880#section-5.2.1|RFC4880 Section 5.2.1}.
  14611. this.hashAlgorithm = null; // A one-octet number describing the hash algorithm used. (See {@link http://tools.ietf.org/html/rfc4880#section-9.4|RFC4880 9.4})
  14612. this.publicKeyAlgorithm = null; // A one-octet number describing the public-key algorithm used. (See {@link http://tools.ietf.org/html/rfc4880#section-9.1|RFC4880 9.1})
  14613. this.signingKeyId = null; // An eight-octet number holding the Key ID of the signing key.
  14614. this.flags = null; // A one-octet number holding a flag showing whether the signature is nested. A zero value indicates that the next packet is another One-Pass Signature packet that describes another signature to be applied to the same message data.
  14615. }
  14616. /**
  14617. * parsing function for a one-pass signature packet (tag 4).
  14618. * @param {Uint8Array} bytes payload of a tag 4 packet
  14619. * @return {module:packet/one_pass_signature} object representation
  14620. */
  14621. OnePassSignature.prototype.read = function (bytes) {
  14622. var mypos = 0;
  14623. // A one-octet version number. The current version is 3.
  14624. this.version = bytes[mypos++];
  14625. // A one-octet signature type. Signature types are described in
  14626. // Section 5.2.1.
  14627. this.type = _enums2.default.read(_enums2.default.signature, bytes[mypos++]);
  14628. // A one-octet number describing the hash algorithm used.
  14629. this.hashAlgorithm = _enums2.default.read(_enums2.default.hash, bytes[mypos++]);
  14630. // A one-octet number describing the public-key algorithm used.
  14631. this.publicKeyAlgorithm = _enums2.default.read(_enums2.default.publicKey, bytes[mypos++]);
  14632. // An eight-octet number holding the Key ID of the signing key.
  14633. this.signingKeyId = new _keyid2.default();
  14634. this.signingKeyId.read(bytes.subarray(mypos, mypos + 8));
  14635. mypos += 8;
  14636. // A one-octet number holding a flag showing whether the signature
  14637. // is nested. A zero value indicates that the next packet is
  14638. // another One-Pass Signature packet that describes another
  14639. // signature to be applied to the same message data.
  14640. this.flags = bytes[mypos++];
  14641. return this;
  14642. };
  14643. /**
  14644. * creates a string representation of a one-pass signature packet
  14645. * @return {Uint8Array} a Uint8Array representation of a one-pass signature packet
  14646. */
  14647. OnePassSignature.prototype.write = function () {
  14648. var start = new Uint8Array([3, _enums2.default.write(_enums2.default.signature, this.type), _enums2.default.write(_enums2.default.hash, this.hashAlgorithm), _enums2.default.write(_enums2.default.publicKey, this.publicKeyAlgorithm)]);
  14649. var end = new Uint8Array([this.flags]);
  14650. return _util2.default.concatUint8Array([start, this.signingKeyId.write(), end]);
  14651. };
  14652. /**
  14653. * Fix custom types after cloning
  14654. */
  14655. OnePassSignature.prototype.postCloneTypeFix = function () {
  14656. this.signingKeyId = _keyid2.default.fromClone(this.signingKeyId);
  14657. };
  14658. },{"../enums.js":35,"../type/keyid.js":67,"../util.js":70}],51:[function(_dereq_,module,exports){
  14659. // GPG4Browsers - An OpenPGP implementation in javascript
  14660. // Copyright (C) 2011 Recurity Labs GmbH
  14661. //
  14662. // This library is free software; you can redistribute it and/or
  14663. // modify it under the terms of the GNU Lesser General Public
  14664. // License as published by the Free Software Foundation; either
  14665. // version 3.0 of the License, or (at your option) any later version.
  14666. //
  14667. // This library is distributed in the hope that it will be useful,
  14668. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  14669. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14670. // Lesser General Public License for more details.
  14671. //
  14672. // You should have received a copy of the GNU Lesser General Public
  14673. // License along with this library; if not, write to the Free Software
  14674. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  14675. /**
  14676. * @requires enums
  14677. * @requires util
  14678. * @module packet/packet
  14679. */
  14680. 'use strict';
  14681. Object.defineProperty(exports, "__esModule", {
  14682. value: true
  14683. });
  14684. var _util = _dereq_('../util.js');
  14685. var _util2 = _interopRequireDefault(_util);
  14686. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14687. exports.default = {
  14688. readSimpleLength: function readSimpleLength(bytes) {
  14689. var len = 0,
  14690. offset,
  14691. type = bytes[0];
  14692. if (type < 192) {
  14693. len = bytes[0];
  14694. offset = 1;
  14695. } else if (type < 255) {
  14696. len = (bytes[0] - 192 << 8) + bytes[1] + 192;
  14697. offset = 2;
  14698. } else if (type === 255) {
  14699. len = _util2.default.readNumber(bytes.subarray(1, 1 + 4));
  14700. offset = 5;
  14701. }
  14702. return {
  14703. len: len,
  14704. offset: offset
  14705. };
  14706. },
  14707. /**
  14708. * Encodes a given integer of length to the openpgp length specifier to a
  14709. * string
  14710. *
  14711. * @param {Integer} length The length to encode
  14712. * @return {Uint8Array} String with openpgp length representation
  14713. */
  14714. writeSimpleLength: function writeSimpleLength(length) {
  14715. if (length < 192) {
  14716. return new Uint8Array([length]);
  14717. } else if (length > 191 && length < 8384) {
  14718. /*
  14719. * let a = (total data packet length) - 192 let bc = two octet
  14720. * representation of a let d = b + 192
  14721. */
  14722. return new Uint8Array([(length - 192 >> 8) + 192, length - 192 & 0xFF]);
  14723. } else {
  14724. return _util2.default.concatUint8Array([new Uint8Array([255]), _util2.default.writeNumber(length, 4)]);
  14725. }
  14726. },
  14727. /**
  14728. * Writes a packet header version 4 with the given tag_type and length to a
  14729. * string
  14730. *
  14731. * @param {Integer} tag_type Tag type
  14732. * @param {Integer} length Length of the payload
  14733. * @return {String} String of the header
  14734. */
  14735. writeHeader: function writeHeader(tag_type, length) {
  14736. /* we're only generating v4 packet headers here */
  14737. return _util2.default.concatUint8Array([new Uint8Array([0xC0 | tag_type]), this.writeSimpleLength(length)]);
  14738. },
  14739. /**
  14740. * Writes a packet header Version 3 with the given tag_type and length to a
  14741. * string
  14742. *
  14743. * @param {Integer} tag_type Tag type
  14744. * @param {Integer} length Length of the payload
  14745. * @return {String} String of the header
  14746. */
  14747. writeOldHeader: function writeOldHeader(tag_type, length) {
  14748. if (length < 256) {
  14749. return new Uint8Array([0x80 | tag_type << 2, length]);
  14750. } else if (length < 65536) {
  14751. return _util2.default.concatUint8Array([new Uint8Array([0x80 | tag_type << 2 | 1]), _util2.default.writeNumber(length, 2)]);
  14752. } else {
  14753. return _util2.default.concatUint8Array([new Uint8Array([0x80 | tag_type << 2 | 2]), _util2.default.writeNumber(length, 4)]);
  14754. }
  14755. },
  14756. /**
  14757. * Generic static Packet Parser function
  14758. *
  14759. * @param {String} input Input stream as string
  14760. * @param {integer} position Position to start parsing
  14761. * @param {integer} len Length of the input from position on
  14762. * @return {Object} Returns a parsed module:packet/packet
  14763. */
  14764. read: function read(input, position, len) {
  14765. // some sanity checks
  14766. if (input === null || input.length <= position || input.subarray(position, input.length).length < 2 || (input[position] & 0x80) === 0) {
  14767. throw new Error("Error during parsing. This message / key probably does not conform to a valid OpenPGP format.");
  14768. }
  14769. var mypos = position;
  14770. var tag = -1;
  14771. var format = -1;
  14772. var packet_length;
  14773. format = 0; // 0 = old format; 1 = new format
  14774. if ((input[mypos] & 0x40) !== 0) {
  14775. format = 1;
  14776. }
  14777. var packet_length_type;
  14778. if (format) {
  14779. // new format header
  14780. tag = input[mypos] & 0x3F; // bit 5-0
  14781. } else {
  14782. // old format header
  14783. tag = (input[mypos] & 0x3F) >> 2; // bit 5-2
  14784. packet_length_type = input[mypos] & 0x03; // bit 1-0
  14785. }
  14786. // header octet parsing done
  14787. mypos++;
  14788. // parsed length from length field
  14789. var bodydata = null;
  14790. // used for partial body lengths
  14791. var real_packet_length = -1;
  14792. if (!format) {
  14793. // 4.2.1. Old Format Packet Lengths
  14794. switch (packet_length_type) {
  14795. case 0:
  14796. // The packet has a one-octet length. The header is 2 octets
  14797. // long.
  14798. packet_length = input[mypos++];
  14799. break;
  14800. case 1:
  14801. // The packet has a two-octet length. The header is 3 octets
  14802. // long.
  14803. packet_length = input[mypos++] << 8 | input[mypos++];
  14804. break;
  14805. case 2:
  14806. // The packet has a four-octet length. The header is 5
  14807. // octets long.
  14808. packet_length = input[mypos++] << 24 | input[mypos++] << 16 | input[mypos++] << 8 | input[mypos++];
  14809. break;
  14810. default:
  14811. // 3 - The packet is of indeterminate length. The header is 1
  14812. // octet long, and the implementation must determine how long
  14813. // the packet is. If the packet is in a file, this means that
  14814. // the packet extends until the end of the file. In general,
  14815. // an implementation SHOULD NOT use indeterminate-length
  14816. // packets except where the end of the data will be clear
  14817. // from the context, and even then it is better to use a
  14818. // definite length, or a new format header. The new format
  14819. // headers described below have a mechanism for precisely
  14820. // encoding data of indeterminate length.
  14821. packet_length = len;
  14822. break;
  14823. }
  14824. } else // 4.2.2. New Format Packet Lengths
  14825. {
  14826. // 4.2.2.1. One-Octet Lengths
  14827. if (input[mypos] < 192) {
  14828. packet_length = input[mypos++];
  14829. _util2.default.print_debug("1 byte length:" + packet_length);
  14830. // 4.2.2.2. Two-Octet Lengths
  14831. } else if (input[mypos] >= 192 && input[mypos] < 224) {
  14832. packet_length = (input[mypos++] - 192 << 8) + input[mypos++] + 192;
  14833. _util2.default.print_debug("2 byte length:" + packet_length);
  14834. // 4.2.2.4. Partial Body Lengths
  14835. } else if (input[mypos] > 223 && input[mypos] < 255) {
  14836. packet_length = 1 << (input[mypos++] & 0x1F);
  14837. _util2.default.print_debug("4 byte length:" + packet_length);
  14838. // EEEK, we're reading the full data here...
  14839. var mypos2 = mypos + packet_length;
  14840. bodydata = [input.subarray(mypos, mypos + packet_length)];
  14841. var tmplen;
  14842. while (true) {
  14843. if (input[mypos2] < 192) {
  14844. tmplen = input[mypos2++];
  14845. packet_length += tmplen;
  14846. bodydata.push(input.subarray(mypos2, mypos2 + tmplen));
  14847. mypos2 += tmplen;
  14848. break;
  14849. } else if (input[mypos2] >= 192 && input[mypos2] < 224) {
  14850. tmplen = (input[mypos2++] - 192 << 8) + input[mypos2++] + 192;
  14851. packet_length += tmplen;
  14852. bodydata.push(input.subarray(mypos2, mypos2 + tmplen));
  14853. mypos2 += tmplen;
  14854. break;
  14855. } else if (input[mypos2] > 223 && input[mypos2] < 255) {
  14856. tmplen = 1 << (input[mypos2++] & 0x1F);
  14857. packet_length += tmplen;
  14858. bodydata.push(input.subarray(mypos2, mypos2 + tmplen));
  14859. mypos2 += tmplen;
  14860. } else {
  14861. mypos2++;
  14862. tmplen = input[mypos2++] << 24 | input[mypos2++] << 16 | input[mypos2++] << 8 | input[mypos2++];
  14863. bodydata.push(input.subarray(mypos2, mypos2 + tmplen));
  14864. packet_length += tmplen;
  14865. mypos2 += tmplen;
  14866. break;
  14867. }
  14868. }
  14869. real_packet_length = mypos2 - mypos;
  14870. // 4.2.2.3. Five-Octet Lengths
  14871. } else {
  14872. mypos++;
  14873. packet_length = input[mypos++] << 24 | input[mypos++] << 16 | input[mypos++] << 8 | input[mypos++];
  14874. }
  14875. }
  14876. // if there was'nt a partial body length: use the specified
  14877. // packet_length
  14878. if (real_packet_length === -1) {
  14879. real_packet_length = packet_length;
  14880. }
  14881. if (bodydata === null) {
  14882. bodydata = input.subarray(mypos, mypos + real_packet_length);
  14883. } else if (bodydata instanceof Array) {
  14884. bodydata = _util2.default.concatUint8Array(bodydata);
  14885. }
  14886. return {
  14887. tag: tag,
  14888. packet: bodydata,
  14889. offset: mypos + real_packet_length
  14890. };
  14891. }
  14892. };
  14893. },{"../util.js":70}],52:[function(_dereq_,module,exports){
  14894. /**
  14895. * This class represents a list of openpgp packets.
  14896. * Take care when iterating over it - the packets themselves
  14897. * are stored as numerical indices.
  14898. * @requires util
  14899. * @requires enums
  14900. * @requires packet
  14901. * @requires packet/packet
  14902. * @module packet/packetlist
  14903. */
  14904. 'use strict';
  14905. Object.defineProperty(exports, "__esModule", {
  14906. value: true
  14907. });
  14908. exports.default = Packetlist;
  14909. var _util = _dereq_('../util');
  14910. var _util2 = _interopRequireDefault(_util);
  14911. var _packet = _dereq_('./packet.js');
  14912. var _packet2 = _interopRequireDefault(_packet);
  14913. var _all_packets = _dereq_('./all_packets.js');
  14914. var packets = _interopRequireWildcard(_all_packets);
  14915. var _enums = _dereq_('../enums.js');
  14916. var _enums2 = _interopRequireDefault(_enums);
  14917. function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) newObj[key] = obj[key]; } } newObj.default = obj; return newObj; } }
  14918. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  14919. /**
  14920. * @constructor
  14921. */
  14922. function Packetlist() {
  14923. /** The number of packets contained within the list.
  14924. * @readonly
  14925. * @type {Integer} */
  14926. this.length = 0;
  14927. }
  14928. /**
  14929. * Reads a stream of binary data and interprents it as a list of packets.
  14930. * @param {Uint8Array} A Uint8Array of bytes.
  14931. */
  14932. Packetlist.prototype.read = function (bytes) {
  14933. var i = 0;
  14934. while (i < bytes.length) {
  14935. var parsed = _packet2.default.read(bytes, i, bytes.length - i);
  14936. i = parsed.offset;
  14937. var pushed = false;
  14938. try {
  14939. var tag = _enums2.default.read(_enums2.default.packet, parsed.tag);
  14940. var packet = packets.newPacketFromTag(tag);
  14941. this.push(packet);
  14942. pushed = true;
  14943. packet.read(parsed.packet);
  14944. } catch (e) {
  14945. if (pushed) {
  14946. this.pop(); // drop unsupported packet
  14947. }
  14948. }
  14949. }
  14950. };
  14951. /**
  14952. * Creates a binary representation of openpgp objects contained within the
  14953. * class instance.
  14954. * @returns {Uint8Array} A Uint8Array containing valid openpgp packets.
  14955. */
  14956. Packetlist.prototype.write = function () {
  14957. var arr = [];
  14958. for (var i = 0; i < this.length; i++) {
  14959. var packetbytes = this[i].write();
  14960. arr.push(_packet2.default.writeHeader(this[i].tag, packetbytes.length));
  14961. arr.push(packetbytes);
  14962. }
  14963. return _util2.default.concatUint8Array(arr);
  14964. };
  14965. /**
  14966. * Adds a packet to the list. This is the only supported method of doing so;
  14967. * writing to packetlist[i] directly will result in an error.
  14968. */
  14969. Packetlist.prototype.push = function (packet) {
  14970. if (!packet) {
  14971. return;
  14972. }
  14973. packet.packets = packet.packets || new Packetlist();
  14974. this[this.length] = packet;
  14975. this.length++;
  14976. };
  14977. /**
  14978. * Remove a packet from the list and return it.
  14979. * @return {Object} The packet that was removed
  14980. */
  14981. Packetlist.prototype.pop = function () {
  14982. if (this.length === 0) {
  14983. return;
  14984. }
  14985. var packet = this[this.length - 1];
  14986. delete this[this.length - 1];
  14987. this.length--;
  14988. return packet;
  14989. };
  14990. /**
  14991. * Creates a new PacketList with all packets that pass the test implemented by the provided function.
  14992. */
  14993. Packetlist.prototype.filter = function (callback) {
  14994. var filtered = new Packetlist();
  14995. for (var i = 0; i < this.length; i++) {
  14996. if (callback(this[i], i, this)) {
  14997. filtered.push(this[i]);
  14998. }
  14999. }
  15000. return filtered;
  15001. };
  15002. /**
  15003. * Creates a new PacketList with all packets from the given types
  15004. */
  15005. Packetlist.prototype.filterByTag = function () {
  15006. var args = Array.prototype.slice.call(arguments);
  15007. var filtered = new Packetlist();
  15008. var that = this;
  15009. function handle(packetType) {
  15010. return that[i].tag === packetType;
  15011. }
  15012. for (var i = 0; i < this.length; i++) {
  15013. if (args.some(handle)) {
  15014. filtered.push(this[i]);
  15015. }
  15016. }
  15017. return filtered;
  15018. };
  15019. /**
  15020. * Executes the provided callback once for each element
  15021. */
  15022. Packetlist.prototype.forEach = function (callback) {
  15023. for (var i = 0; i < this.length; i++) {
  15024. callback(this[i]);
  15025. }
  15026. };
  15027. /**
  15028. * Traverses packet tree and returns first matching packet
  15029. * @param {module:enums.packet} type The packet type
  15030. * @return {module:packet/packet|null}
  15031. */
  15032. Packetlist.prototype.findPacket = function (type) {
  15033. var packetlist = this.filterByTag(type);
  15034. if (packetlist.length) {
  15035. return packetlist[0];
  15036. } else {
  15037. var found = null;
  15038. for (var i = 0; i < this.length; i++) {
  15039. if (this[i].packets.length) {
  15040. found = this[i].packets.findPacket(type);
  15041. if (found) {
  15042. return found;
  15043. }
  15044. }
  15045. }
  15046. }
  15047. return null;
  15048. };
  15049. /**
  15050. * Returns array of found indices by tag
  15051. */
  15052. Packetlist.prototype.indexOfTag = function () {
  15053. var args = Array.prototype.slice.call(arguments);
  15054. var tagIndex = [];
  15055. var that = this;
  15056. function handle(packetType) {
  15057. return that[i].tag === packetType;
  15058. }
  15059. for (var i = 0; i < this.length; i++) {
  15060. if (args.some(handle)) {
  15061. tagIndex.push(i);
  15062. }
  15063. }
  15064. return tagIndex;
  15065. };
  15066. /**
  15067. * Returns slice of packetlist
  15068. */
  15069. Packetlist.prototype.slice = function (begin, end) {
  15070. if (!end) {
  15071. end = this.length;
  15072. }
  15073. var part = new Packetlist();
  15074. for (var i = begin; i < end; i++) {
  15075. part.push(this[i]);
  15076. }
  15077. return part;
  15078. };
  15079. /**
  15080. * Concatenates packetlist or array of packets
  15081. */
  15082. Packetlist.prototype.concat = function (packetlist) {
  15083. if (packetlist) {
  15084. for (var i = 0; i < packetlist.length; i++) {
  15085. this.push(packetlist[i]);
  15086. }
  15087. }
  15088. };
  15089. /**
  15090. * Allocate a new packetlist from structured packetlist clone
  15091. * See {@link http://www.w3.org/html/wg/drafts/html/master/infrastructure.html#safe-passing-of-structured-data}
  15092. * @param {Object} packetClone packetlist clone
  15093. * @returns {Object} new packetlist object with data from packetlist clone
  15094. */
  15095. Packetlist.fromStructuredClone = function (packetlistClone) {
  15096. var packetlist = new Packetlist();
  15097. for (var i = 0; i < packetlistClone.length; i++) {
  15098. packetlist.push(packets.fromStructuredClone(packetlistClone[i]));
  15099. if (packetlist[i].packets.length !== 0) {
  15100. packetlist[i].packets = this.fromStructuredClone(packetlist[i].packets);
  15101. } else {
  15102. packetlist[i].packets = new Packetlist();
  15103. }
  15104. }
  15105. return packetlist;
  15106. };
  15107. },{"../enums.js":35,"../util":70,"./all_packets.js":44,"./packet.js":51}],53:[function(_dereq_,module,exports){
  15108. // GPG4Browsers - An OpenPGP implementation in javascript
  15109. // Copyright (C) 2011 Recurity Labs GmbH
  15110. //
  15111. // This library is free software; you can redistribute it and/or
  15112. // modify it under the terms of the GNU Lesser General Public
  15113. // License as published by the Free Software Foundation; either
  15114. // version 3.0 of the License, or (at your option) any later version.
  15115. //
  15116. // This library is distributed in the hope that it will be useful,
  15117. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15118. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15119. // Lesser General Public License for more details.
  15120. //
  15121. // You should have received a copy of the GNU Lesser General Public
  15122. // License along with this library; if not, write to the Free Software
  15123. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  15124. /**
  15125. * Implementation of the Key Material Packet (Tag 5,6,7,14)<br/>
  15126. * <br/>
  15127. * {@link http://tools.ietf.org/html/rfc4880#section-5.5|RFC4480 5.5}:
  15128. * A key material packet contains all the information about a public or
  15129. * private key. There are four variants of this packet type, and two
  15130. * major versions. Consequently, this section is complex.
  15131. * @requires crypto
  15132. * @requires enums
  15133. * @requires type/keyid
  15134. * @requires type/mpi
  15135. * @requires util
  15136. * @module packet/public_key
  15137. */
  15138. 'use strict';
  15139. Object.defineProperty(exports, "__esModule", {
  15140. value: true
  15141. });
  15142. exports.default = PublicKey;
  15143. var _util = _dereq_('../util.js');
  15144. var _util2 = _interopRequireDefault(_util);
  15145. var _mpi = _dereq_('../type/mpi.js');
  15146. var _mpi2 = _interopRequireDefault(_mpi);
  15147. var _keyid = _dereq_('../type/keyid.js');
  15148. var _keyid2 = _interopRequireDefault(_keyid);
  15149. var _enums = _dereq_('../enums.js');
  15150. var _enums2 = _interopRequireDefault(_enums);
  15151. var _crypto = _dereq_('../crypto');
  15152. var _crypto2 = _interopRequireDefault(_crypto);
  15153. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15154. /**
  15155. * @constructor
  15156. */
  15157. function PublicKey() {
  15158. this.tag = _enums2.default.packet.publicKey;
  15159. this.version = 4;
  15160. /** Key creation date.
  15161. * @type {Date} */
  15162. this.created = new Date();
  15163. /** A list of multiprecision integers
  15164. * @type {module:type/mpi} */
  15165. this.mpi = [];
  15166. /** Public key algorithm
  15167. * @type {module:enums.publicKey} */
  15168. this.algorithm = 'rsa_sign';
  15169. // time in days (V3 only)
  15170. this.expirationTimeV3 = 0;
  15171. /**
  15172. * Fingerprint in lowercase hex
  15173. * @type {String}
  15174. */
  15175. this.fingerprint = null;
  15176. /**
  15177. * Keyid
  15178. * @type {module:type/keyid}
  15179. */
  15180. this.keyid = null;
  15181. }
  15182. /**
  15183. * Internal Parser for public keys as specified in {@link http://tools.ietf.org/html/rfc4880#section-5.5.2|RFC 4880 section 5.5.2 Public-Key Packet Formats}
  15184. * called by read_tag&lt;num&gt;
  15185. * @param {Uint8Array} bytes Input array to read the packet from
  15186. * @return {Object} This object with attributes set by the parser
  15187. */
  15188. PublicKey.prototype.read = function (bytes) {
  15189. var pos = 0;
  15190. // A one-octet version number (3 or 4).
  15191. this.version = bytes[pos++];
  15192. if (this.version === 3 || this.version === 4) {
  15193. // - A four-octet number denoting the time that the key was created.
  15194. this.created = _util2.default.readDate(bytes.subarray(pos, pos + 4));
  15195. pos += 4;
  15196. if (this.version === 3) {
  15197. // - A two-octet number denoting the time in days that this key is
  15198. // valid. If this number is zero, then it does not expire.
  15199. this.expirationTimeV3 = _util2.default.readNumber(bytes.subarray(pos, pos + 2));
  15200. pos += 2;
  15201. }
  15202. // - A one-octet number denoting the public-key algorithm of this key.
  15203. this.algorithm = _enums2.default.read(_enums2.default.publicKey, bytes[pos++]);
  15204. var mpicount = _crypto2.default.getPublicMpiCount(this.algorithm);
  15205. this.mpi = [];
  15206. var bmpi = bytes.subarray(pos, bytes.length);
  15207. var p = 0;
  15208. for (var i = 0; i < mpicount && p < bmpi.length; i++) {
  15209. this.mpi[i] = new _mpi2.default();
  15210. p += this.mpi[i].read(bmpi.subarray(p, bmpi.length));
  15211. if (p > bmpi.length) {
  15212. throw new Error('Error reading MPI @:' + p);
  15213. }
  15214. }
  15215. return p + 6;
  15216. } else {
  15217. throw new Error('Version ' + this.version + ' of the key packet is unsupported.');
  15218. }
  15219. };
  15220. /**
  15221. * Alias of read()
  15222. * @see module:packet/public_key~PublicKey#read
  15223. */
  15224. PublicKey.prototype.readPublicKey = PublicKey.prototype.read;
  15225. /**
  15226. * Same as write_private_key, but has less information because of
  15227. * public key.
  15228. * @return {Uint8Array} OpenPGP packet body contents,
  15229. */
  15230. PublicKey.prototype.write = function () {
  15231. var arr = [];
  15232. // Version
  15233. arr.push(new Uint8Array([this.version]));
  15234. arr.push(_util2.default.writeDate(this.created));
  15235. if (this.version === 3) {
  15236. arr.push(_util2.default.writeNumber(this.expirationTimeV3, 2));
  15237. }
  15238. arr.push(new Uint8Array([_enums2.default.write(_enums2.default.publicKey, this.algorithm)]));
  15239. var mpicount = _crypto2.default.getPublicMpiCount(this.algorithm);
  15240. for (var i = 0; i < mpicount; i++) {
  15241. arr.push(this.mpi[i].write());
  15242. }
  15243. return _util2.default.concatUint8Array(arr);
  15244. };
  15245. /**
  15246. * Alias of write()
  15247. * @see module:packet/public_key~PublicKey#write
  15248. */
  15249. PublicKey.prototype.writePublicKey = PublicKey.prototype.write;
  15250. /**
  15251. * Write an old version packet - it's used by some of the internal routines.
  15252. */
  15253. PublicKey.prototype.writeOld = function () {
  15254. var bytes = this.writePublicKey();
  15255. return _util2.default.concatUint8Array([new Uint8Array([0x99]), _util2.default.writeNumber(bytes.length, 2), bytes]);
  15256. };
  15257. /**
  15258. * Calculates the key id of the key
  15259. * @return {String} A 8 byte key id
  15260. */
  15261. PublicKey.prototype.getKeyId = function () {
  15262. if (this.keyid) {
  15263. return this.keyid;
  15264. }
  15265. this.keyid = new _keyid2.default();
  15266. if (this.version === 4) {
  15267. this.keyid.read(_util2.default.str2Uint8Array(_util2.default.hex2bin(this.getFingerprint()).substr(12, 8)));
  15268. } else if (this.version === 3) {
  15269. var arr = this.mpi[0].write();
  15270. this.keyid.read(arr.subarray(arr.length - 8, arr.length));
  15271. }
  15272. return this.keyid;
  15273. };
  15274. /**
  15275. * Calculates the fingerprint of the key
  15276. * @return {String} A string containing the fingerprint in lowercase hex
  15277. */
  15278. PublicKey.prototype.getFingerprint = function () {
  15279. if (this.fingerprint) {
  15280. return this.fingerprint;
  15281. }
  15282. var toHash = '';
  15283. if (this.version === 4) {
  15284. toHash = this.writeOld();
  15285. this.fingerprint = _util2.default.Uint8Array2str(_crypto2.default.hash.sha1(toHash));
  15286. } else if (this.version === 3) {
  15287. var mpicount = _crypto2.default.getPublicMpiCount(this.algorithm);
  15288. for (var i = 0; i < mpicount; i++) {
  15289. toHash += this.mpi[i].toBytes();
  15290. }
  15291. this.fingerprint = _util2.default.Uint8Array2str(_crypto2.default.hash.md5(_util2.default.str2Uint8Array(toHash)));
  15292. }
  15293. this.fingerprint = _util2.default.hexstrdump(this.fingerprint);
  15294. return this.fingerprint;
  15295. };
  15296. /**
  15297. * Returns bit size of key
  15298. * @return {int} Number of bits
  15299. */
  15300. PublicKey.prototype.getBitSize = function () {
  15301. return this.mpi[0].byteLength() * 8;
  15302. };
  15303. /**
  15304. * Fix custom types after cloning
  15305. */
  15306. PublicKey.prototype.postCloneTypeFix = function () {
  15307. for (var i = 0; i < this.mpi.length; i++) {
  15308. this.mpi[i] = _mpi2.default.fromClone(this.mpi[i]);
  15309. }
  15310. if (this.keyid) {
  15311. this.keyid = _keyid2.default.fromClone(this.keyid);
  15312. }
  15313. };
  15314. },{"../crypto":24,"../enums.js":35,"../type/keyid.js":67,"../type/mpi.js":68,"../util.js":70}],54:[function(_dereq_,module,exports){
  15315. // GPG4Browsers - An OpenPGP implementation in javascript
  15316. // Copyright (C) 2011 Recurity Labs GmbH
  15317. //
  15318. // This library is free software; you can redistribute it and/or
  15319. // modify it under the terms of the GNU Lesser General Public
  15320. // License as published by the Free Software Foundation; either
  15321. // version 3.0 of the License, or (at your option) any later version.
  15322. //
  15323. // This library is distributed in the hope that it will be useful,
  15324. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15325. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15326. // Lesser General Public License for more details.
  15327. //
  15328. // You should have received a copy of the GNU Lesser General Public
  15329. // License along with this library; if not, write to the Free Software
  15330. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  15331. /**
  15332. * Public-Key Encrypted Session Key Packets (Tag 1)<br/>
  15333. * <br/>
  15334. * {@link http://tools.ietf.org/html/rfc4880#section-5.1|RFC4880 5.1}: A Public-Key Encrypted Session Key packet holds the session key
  15335. * used to encrypt a message. Zero or more Public-Key Encrypted Session Key
  15336. * packets and/or Symmetric-Key Encrypted Session Key packets may precede a
  15337. * Symmetrically Encrypted Data Packet, which holds an encrypted message. The
  15338. * message is encrypted with the session key, and the session key is itself
  15339. * encrypted and stored in the Encrypted Session Key packet(s). The
  15340. * Symmetrically Encrypted Data Packet is preceded by one Public-Key Encrypted
  15341. * Session Key packet for each OpenPGP key to which the message is encrypted.
  15342. * The recipient of the message finds a session key that is encrypted to their
  15343. * public key, decrypts the session key, and then uses the session key to
  15344. * decrypt the message.
  15345. * @requires crypto
  15346. * @requires enums
  15347. * @requires type/keyid
  15348. * @requires type/mpi
  15349. * @requires util
  15350. * @module packet/public_key_encrypted_session_key
  15351. */
  15352. 'use strict';
  15353. Object.defineProperty(exports, "__esModule", {
  15354. value: true
  15355. });
  15356. exports.default = PublicKeyEncryptedSessionKey;
  15357. var _keyid = _dereq_('../type/keyid.js');
  15358. var _keyid2 = _interopRequireDefault(_keyid);
  15359. var _util = _dereq_('../util.js');
  15360. var _util2 = _interopRequireDefault(_util);
  15361. var _mpi = _dereq_('../type/mpi.js');
  15362. var _mpi2 = _interopRequireDefault(_mpi);
  15363. var _enums = _dereq_('../enums.js');
  15364. var _enums2 = _interopRequireDefault(_enums);
  15365. var _crypto = _dereq_('../crypto');
  15366. var _crypto2 = _interopRequireDefault(_crypto);
  15367. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15368. /**
  15369. * @constructor
  15370. */
  15371. function PublicKeyEncryptedSessionKey() {
  15372. this.tag = _enums2.default.packet.publicKeyEncryptedSessionKey;
  15373. this.version = 3;
  15374. this.publicKeyId = new _keyid2.default();
  15375. this.publicKeyAlgorithm = 'rsa_encrypt';
  15376. this.sessionKey = null;
  15377. this.sessionKeyAlgorithm = 'aes256';
  15378. /** @type {Array<module:type/mpi>} */
  15379. this.encrypted = [];
  15380. }
  15381. /**
  15382. * Parsing function for a publickey encrypted session key packet (tag 1).
  15383. *
  15384. * @param {Uint8Array} input Payload of a tag 1 packet
  15385. * @param {Integer} position Position to start reading from the input string
  15386. * @param {Integer} len Length of the packet or the remaining length of
  15387. * input at position
  15388. * @return {module:packet/public_key_encrypted_session_key} Object representation
  15389. */
  15390. PublicKeyEncryptedSessionKey.prototype.read = function (bytes) {
  15391. this.version = bytes[0];
  15392. this.publicKeyId.read(bytes.subarray(1, bytes.length));
  15393. this.publicKeyAlgorithm = _enums2.default.read(_enums2.default.publicKey, bytes[9]);
  15394. var i = 10;
  15395. var integerCount = function (algo) {
  15396. switch (algo) {
  15397. case 'rsa_encrypt':
  15398. case 'rsa_encrypt_sign':
  15399. return 1;
  15400. case 'elgamal':
  15401. return 2;
  15402. default:
  15403. throw new Error("Invalid algorithm.");
  15404. }
  15405. }(this.publicKeyAlgorithm);
  15406. this.encrypted = [];
  15407. for (var j = 0; j < integerCount; j++) {
  15408. var mpi = new _mpi2.default();
  15409. i += mpi.read(bytes.subarray(i, bytes.length));
  15410. this.encrypted.push(mpi);
  15411. }
  15412. };
  15413. /**
  15414. * Create a string representation of a tag 1 packet
  15415. *
  15416. * @return {Uint8Array} The Uint8Array representation
  15417. */
  15418. PublicKeyEncryptedSessionKey.prototype.write = function () {
  15419. var arr = [new Uint8Array([this.version]), this.publicKeyId.write(), new Uint8Array([_enums2.default.write(_enums2.default.publicKey, this.publicKeyAlgorithm)])];
  15420. for (var i = 0; i < this.encrypted.length; i++) {
  15421. arr.push(this.encrypted[i].write());
  15422. }
  15423. return _util2.default.concatUint8Array(arr);
  15424. };
  15425. PublicKeyEncryptedSessionKey.prototype.encrypt = function (key) {
  15426. var data = String.fromCharCode(_enums2.default.write(_enums2.default.symmetric, this.sessionKeyAlgorithm));
  15427. data += _util2.default.Uint8Array2str(this.sessionKey);
  15428. var checksum = _util2.default.calc_checksum(this.sessionKey);
  15429. data += _util2.default.Uint8Array2str(_util2.default.writeNumber(checksum, 2));
  15430. var mpi = new _mpi2.default();
  15431. mpi.fromBytes(_crypto2.default.pkcs1.eme.encode(data, key.mpi[0].byteLength()));
  15432. this.encrypted = _crypto2.default.publicKeyEncrypt(this.publicKeyAlgorithm, key.mpi, mpi);
  15433. };
  15434. /**
  15435. * Decrypts the session key (only for public key encrypted session key
  15436. * packets (tag 1)
  15437. *
  15438. * @param {module:packet/secret_key} key
  15439. * Private key with secMPIs unlocked
  15440. * @return {String} The unencrypted session key
  15441. */
  15442. PublicKeyEncryptedSessionKey.prototype.decrypt = function (key) {
  15443. var result = _crypto2.default.publicKeyDecrypt(this.publicKeyAlgorithm, key.mpi, this.encrypted).toBytes();
  15444. var checksum = _util2.default.readNumber(_util2.default.str2Uint8Array(result.substr(result.length - 2)));
  15445. var decoded = _crypto2.default.pkcs1.eme.decode(result);
  15446. key = _util2.default.str2Uint8Array(decoded.substring(1, decoded.length - 2));
  15447. if (checksum !== _util2.default.calc_checksum(key)) {
  15448. throw new Error('Checksum mismatch');
  15449. } else {
  15450. this.sessionKey = key;
  15451. this.sessionKeyAlgorithm = _enums2.default.read(_enums2.default.symmetric, decoded.charCodeAt(0));
  15452. }
  15453. };
  15454. /**
  15455. * Fix custom types after cloning
  15456. */
  15457. PublicKeyEncryptedSessionKey.prototype.postCloneTypeFix = function () {
  15458. this.publicKeyId = _keyid2.default.fromClone(this.publicKeyId);
  15459. for (var i = 0; i < this.encrypted.length; i++) {
  15460. this.encrypted[i] = _mpi2.default.fromClone(this.encrypted[i]);
  15461. }
  15462. };
  15463. },{"../crypto":24,"../enums.js":35,"../type/keyid.js":67,"../type/mpi.js":68,"../util.js":70}],55:[function(_dereq_,module,exports){
  15464. // GPG4Browsers - An OpenPGP implementation in javascript
  15465. // Copyright (C) 2011 Recurity Labs GmbH
  15466. //
  15467. // This library is free software; you can redistribute it and/or
  15468. // modify it under the terms of the GNU Lesser General Public
  15469. // License as published by the Free Software Foundation; either
  15470. // version 3.0 of the License, or (at your option) any later version.
  15471. //
  15472. // This library is distributed in the hope that it will be useful,
  15473. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15474. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15475. // Lesser General Public License for more details.
  15476. //
  15477. // You should have received a copy of the GNU Lesser General Public
  15478. // License along with this library; if not, write to the Free Software
  15479. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  15480. /**
  15481. * @requires packet/public_key
  15482. * @requires enums
  15483. * @module packet/public_subkey
  15484. */
  15485. 'use strict';
  15486. Object.defineProperty(exports, "__esModule", {
  15487. value: true
  15488. });
  15489. exports.default = PublicSubkey;
  15490. var _public_key = _dereq_('./public_key.js');
  15491. var _public_key2 = _interopRequireDefault(_public_key);
  15492. var _enums = _dereq_('../enums.js');
  15493. var _enums2 = _interopRequireDefault(_enums);
  15494. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15495. /**
  15496. * @constructor
  15497. * @extends module:packet/public_key
  15498. */
  15499. function PublicSubkey() {
  15500. _public_key2.default.call(this);
  15501. this.tag = _enums2.default.packet.publicSubkey;
  15502. }
  15503. PublicSubkey.prototype = new _public_key2.default();
  15504. PublicSubkey.prototype.constructor = PublicSubkey;
  15505. },{"../enums.js":35,"./public_key.js":53}],56:[function(_dereq_,module,exports){
  15506. // GPG4Browsers - An OpenPGP implementation in javascript
  15507. // Copyright (C) 2011 Recurity Labs GmbH
  15508. //
  15509. // This library is free software; you can redistribute it and/or
  15510. // modify it under the terms of the GNU Lesser General Public
  15511. // License as published by the Free Software Foundation; either
  15512. // version 3.0 of the License, or (at your option) any later version.
  15513. //
  15514. // This library is distributed in the hope that it will be useful,
  15515. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15516. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15517. // Lesser General Public License for more details.
  15518. //
  15519. // You should have received a copy of the GNU Lesser General Public
  15520. // License along with this library; if not, write to the Free Software
  15521. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  15522. /**
  15523. * Implementation of the Key Material Packet (Tag 5,6,7,14)<br/>
  15524. * <br/>
  15525. * {@link http://tools.ietf.org/html/rfc4880#section-5.5|RFC4480 5.5}:
  15526. * A key material packet contains all the information about a public or
  15527. * private key. There are four variants of this packet type, and two
  15528. * major versions. Consequently, this section is complex.
  15529. * @requires crypto
  15530. * @requires enums
  15531. * @requires packet/public_key
  15532. * @requires type/mpi
  15533. * @requires type/s2k
  15534. * @requires util
  15535. * @module packet/secret_key
  15536. */
  15537. 'use strict';
  15538. Object.defineProperty(exports, "__esModule", {
  15539. value: true
  15540. });
  15541. exports.default = SecretKey;
  15542. var _public_key = _dereq_('./public_key.js');
  15543. var _public_key2 = _interopRequireDefault(_public_key);
  15544. var _enums = _dereq_('../enums.js');
  15545. var _enums2 = _interopRequireDefault(_enums);
  15546. var _util = _dereq_('../util.js');
  15547. var _util2 = _interopRequireDefault(_util);
  15548. var _crypto = _dereq_('../crypto');
  15549. var _crypto2 = _interopRequireDefault(_crypto);
  15550. var _mpi = _dereq_('../type/mpi.js');
  15551. var _mpi2 = _interopRequireDefault(_mpi);
  15552. var _s2k = _dereq_('../type/s2k.js');
  15553. var _s2k2 = _interopRequireDefault(_s2k);
  15554. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15555. /**
  15556. * @constructor
  15557. * @extends module:packet/public_key
  15558. */
  15559. function SecretKey() {
  15560. _public_key2.default.call(this);
  15561. this.tag = _enums2.default.packet.secretKey;
  15562. // encrypted secret-key data
  15563. this.encrypted = null;
  15564. // indicator if secret-key data is available in decrypted form
  15565. this.isDecrypted = false;
  15566. }
  15567. SecretKey.prototype = new _public_key2.default();
  15568. SecretKey.prototype.constructor = SecretKey;
  15569. function get_hash_len(hash) {
  15570. if (hash === 'sha1') {
  15571. return 20;
  15572. } else {
  15573. return 2;
  15574. }
  15575. }
  15576. function get_hash_fn(hash) {
  15577. if (hash === 'sha1') {
  15578. return _crypto2.default.hash.sha1;
  15579. } else {
  15580. return function (c) {
  15581. return _util2.default.writeNumber(_util2.default.calc_checksum(c), 2);
  15582. };
  15583. }
  15584. }
  15585. // Helper function
  15586. function parse_cleartext_mpi(hash_algorithm, cleartext, algorithm) {
  15587. var hashlen = get_hash_len(hash_algorithm),
  15588. hashfn = get_hash_fn(hash_algorithm);
  15589. var hashtext = _util2.default.Uint8Array2str(cleartext.subarray(cleartext.length - hashlen, cleartext.length));
  15590. cleartext = cleartext.subarray(0, cleartext.length - hashlen);
  15591. var hash = _util2.default.Uint8Array2str(hashfn(cleartext));
  15592. if (hash !== hashtext) {
  15593. return new Error("Hash mismatch.");
  15594. }
  15595. var mpis = _crypto2.default.getPrivateMpiCount(algorithm);
  15596. var j = 0;
  15597. var mpi = [];
  15598. for (var i = 0; i < mpis && j < cleartext.length; i++) {
  15599. mpi[i] = new _mpi2.default();
  15600. j += mpi[i].read(cleartext.subarray(j, cleartext.length));
  15601. }
  15602. return mpi;
  15603. }
  15604. function write_cleartext_mpi(hash_algorithm, algorithm, mpi) {
  15605. var arr = [];
  15606. var discard = _crypto2.default.getPublicMpiCount(algorithm);
  15607. for (var i = discard; i < mpi.length; i++) {
  15608. arr.push(mpi[i].write());
  15609. }
  15610. var bytes = _util2.default.concatUint8Array(arr);
  15611. var hash = get_hash_fn(hash_algorithm)(bytes);
  15612. return _util2.default.concatUint8Array([bytes, hash]);
  15613. }
  15614. // 5.5.3. Secret-Key Packet Formats
  15615. /**
  15616. * Internal parser for private keys as specified in {@link http://tools.ietf.org/html/rfc4880#section-5.5.3|RFC 4880 section 5.5.3}
  15617. * @param {String} bytes Input string to read the packet from
  15618. */
  15619. SecretKey.prototype.read = function (bytes) {
  15620. // - A Public-Key or Public-Subkey packet, as described above.
  15621. var len = this.readPublicKey(bytes);
  15622. bytes = bytes.subarray(len, bytes.length);
  15623. // - One octet indicating string-to-key usage conventions. Zero
  15624. // indicates that the secret-key data is not encrypted. 255 or 254
  15625. // indicates that a string-to-key specifier is being given. Any
  15626. // other value is a symmetric-key encryption algorithm identifier.
  15627. var isEncrypted = bytes[0];
  15628. if (isEncrypted) {
  15629. this.encrypted = bytes;
  15630. } else {
  15631. // - Plain or encrypted multiprecision integers comprising the secret
  15632. // key data. These algorithm-specific fields are as described
  15633. // below.
  15634. var parsedMPI = parse_cleartext_mpi('mod', bytes.subarray(1, bytes.length), this.algorithm);
  15635. if (parsedMPI instanceof Error) {
  15636. throw parsedMPI;
  15637. }
  15638. this.mpi = this.mpi.concat(parsedMPI);
  15639. this.isDecrypted = true;
  15640. }
  15641. };
  15642. /** Creates an OpenPGP key packet for the given key.
  15643. * @return {String} A string of bytes containing the secret key OpenPGP packet
  15644. */
  15645. SecretKey.prototype.write = function () {
  15646. var arr = [this.writePublicKey()];
  15647. if (!this.encrypted) {
  15648. arr.push(new Uint8Array([0]));
  15649. arr.push(write_cleartext_mpi('mod', this.algorithm, this.mpi));
  15650. } else {
  15651. arr.push(this.encrypted);
  15652. }
  15653. return _util2.default.concatUint8Array(arr);
  15654. };
  15655. /** Encrypt the payload. By default, we use aes256 and iterated, salted string
  15656. * to key specifier. If the key is in a decrypted state (isDecrypted === true)
  15657. * and the passphrase is empty or undefined, the key will be set as not encrypted.
  15658. * This can be used to remove passphrase protection after calling decrypt().
  15659. * @param {String} passphrase
  15660. */
  15661. SecretKey.prototype.encrypt = function (passphrase) {
  15662. if (this.isDecrypted && !passphrase) {
  15663. this.encrypted = null;
  15664. return;
  15665. } else if (!passphrase) {
  15666. throw new Error('The key must be decrypted before removing passphrase protection.');
  15667. }
  15668. var s2k = new _s2k2.default(),
  15669. symmetric = 'aes256',
  15670. cleartext = write_cleartext_mpi('sha1', this.algorithm, this.mpi),
  15671. key = produceEncryptionKey(s2k, passphrase, symmetric),
  15672. blockLen = _crypto2.default.cipher[symmetric].blockSize,
  15673. iv = _crypto2.default.random.getRandomBytes(blockLen);
  15674. var arr = [new Uint8Array([254, _enums2.default.write(_enums2.default.symmetric, symmetric)])];
  15675. arr.push(s2k.write());
  15676. arr.push(iv);
  15677. arr.push(_crypto2.default.cfb.normalEncrypt(symmetric, key, cleartext, iv));
  15678. this.encrypted = _util2.default.concatUint8Array(arr);
  15679. };
  15680. function produceEncryptionKey(s2k, passphrase, algorithm) {
  15681. return s2k.produce_key(passphrase, _crypto2.default.cipher[algorithm].keySize);
  15682. }
  15683. /**
  15684. * Decrypts the private key MPIs which are needed to use the key.
  15685. * @link module:packet/secret_key.isDecrypted should be
  15686. * false otherwise a call to this function is not needed
  15687. *
  15688. * @param {String} str_passphrase The passphrase for this private key
  15689. * as string
  15690. * @return {Boolean} True if the passphrase was correct or MPI already
  15691. * decrypted; false if not
  15692. */
  15693. SecretKey.prototype.decrypt = function (passphrase) {
  15694. if (this.isDecrypted) {
  15695. return true;
  15696. }
  15697. var i = 0,
  15698. symmetric,
  15699. key;
  15700. var s2k_usage = this.encrypted[i++];
  15701. // - [Optional] If string-to-key usage octet was 255 or 254, a one-
  15702. // octet symmetric encryption algorithm.
  15703. if (s2k_usage === 255 || s2k_usage === 254) {
  15704. symmetric = this.encrypted[i++];
  15705. symmetric = _enums2.default.read(_enums2.default.symmetric, symmetric);
  15706. // - [Optional] If string-to-key usage octet was 255 or 254, a
  15707. // string-to-key specifier. The length of the string-to-key
  15708. // specifier is implied by its type, as described above.
  15709. var s2k = new _s2k2.default();
  15710. i += s2k.read(this.encrypted.subarray(i, this.encrypted.length));
  15711. key = produceEncryptionKey(s2k, passphrase, symmetric);
  15712. } else {
  15713. symmetric = s2k_usage;
  15714. symmetric = _enums2.default.read(_enums2.default.symmetric, symmetric);
  15715. key = _crypto2.default.hash.md5(passphrase);
  15716. }
  15717. // - [Optional] If secret data is encrypted (string-to-key usage octet
  15718. // not zero), an Initial Vector (IV) of the same length as the
  15719. // cipher's block size.
  15720. var iv = this.encrypted.subarray(i, i + _crypto2.default.cipher[symmetric].blockSize);
  15721. i += iv.length;
  15722. var cleartext,
  15723. ciphertext = this.encrypted.subarray(i, this.encrypted.length);
  15724. cleartext = _crypto2.default.cfb.normalDecrypt(symmetric, key, ciphertext, iv);
  15725. var hash = s2k_usage === 254 ? 'sha1' : 'mod';
  15726. var parsedMPI = parse_cleartext_mpi(hash, cleartext, this.algorithm);
  15727. if (parsedMPI instanceof Error) {
  15728. return false;
  15729. }
  15730. this.mpi = this.mpi.concat(parsedMPI);
  15731. this.isDecrypted = true;
  15732. this.encrypted = null;
  15733. return true;
  15734. };
  15735. SecretKey.prototype.generate = function (bits) {
  15736. var self = this;
  15737. return _crypto2.default.generateMpi(self.algorithm, bits).then(function (mpi) {
  15738. self.mpi = mpi;
  15739. self.isDecrypted = true;
  15740. });
  15741. };
  15742. /**
  15743. * Clear private MPIs, return to initial state
  15744. */
  15745. SecretKey.prototype.clearPrivateMPIs = function () {
  15746. if (!this.encrypted) {
  15747. throw new Error('If secret key is not encrypted, clearing private MPIs is irreversible.');
  15748. }
  15749. this.mpi = this.mpi.slice(0, _crypto2.default.getPublicMpiCount(this.algorithm));
  15750. this.isDecrypted = false;
  15751. };
  15752. },{"../crypto":24,"../enums.js":35,"../type/mpi.js":68,"../type/s2k.js":69,"../util.js":70,"./public_key.js":53}],57:[function(_dereq_,module,exports){
  15753. // GPG4Browsers - An OpenPGP implementation in javascript
  15754. // Copyright (C) 2011 Recurity Labs GmbH
  15755. //
  15756. // This library is free software; you can redistribute it and/or
  15757. // modify it under the terms of the GNU Lesser General Public
  15758. // License as published by the Free Software Foundation; either
  15759. // version 3.0 of the License, or (at your option) any later version.
  15760. //
  15761. // This library is distributed in the hope that it will be useful,
  15762. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15763. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15764. // Lesser General Public License for more details.
  15765. //
  15766. // You should have received a copy of the GNU Lesser General Public
  15767. // License along with this library; if not, write to the Free Software
  15768. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  15769. /**
  15770. * @requires packet/secret_key
  15771. * @requires enums
  15772. * @module packet/secret_subkey
  15773. */
  15774. 'use strict';
  15775. Object.defineProperty(exports, "__esModule", {
  15776. value: true
  15777. });
  15778. exports.default = SecretSubkey;
  15779. var _secret_key = _dereq_('./secret_key.js');
  15780. var _secret_key2 = _interopRequireDefault(_secret_key);
  15781. var _enums = _dereq_('../enums.js');
  15782. var _enums2 = _interopRequireDefault(_enums);
  15783. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15784. /**
  15785. * @constructor
  15786. * @extends module:packet/secret_key
  15787. */
  15788. function SecretSubkey() {
  15789. _secret_key2.default.call(this);
  15790. this.tag = _enums2.default.packet.secretSubkey;
  15791. }
  15792. SecretSubkey.prototype = new _secret_key2.default();
  15793. SecretSubkey.prototype.constructor = SecretSubkey;
  15794. },{"../enums.js":35,"./secret_key.js":56}],58:[function(_dereq_,module,exports){
  15795. 'use strict';
  15796. Object.defineProperty(exports, "__esModule", {
  15797. value: true
  15798. });
  15799. exports.default = Signature;
  15800. var _util = _dereq_('../util.js');
  15801. var _util2 = _interopRequireDefault(_util);
  15802. var _packet = _dereq_('./packet.js');
  15803. var _packet2 = _interopRequireDefault(_packet);
  15804. var _enums = _dereq_('../enums.js');
  15805. var _enums2 = _interopRequireDefault(_enums);
  15806. var _crypto = _dereq_('../crypto');
  15807. var _crypto2 = _interopRequireDefault(_crypto);
  15808. var _mpi = _dereq_('../type/mpi.js');
  15809. var _mpi2 = _interopRequireDefault(_mpi);
  15810. var _keyid = _dereq_('../type/keyid.js');
  15811. var _keyid2 = _interopRequireDefault(_keyid);
  15812. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  15813. /**
  15814. * @constructor
  15815. */
  15816. // GPG4Browsers - An OpenPGP implementation in javascript
  15817. // Copyright (C) 2011 Recurity Labs GmbH
  15818. //
  15819. // This library is free software; you can redistribute it and/or
  15820. // modify it under the terms of the GNU Lesser General Public
  15821. // License as published by the Free Software Foundation; either
  15822. // version 3.0 of the License, or (at your option) any later version.
  15823. //
  15824. // This library is distributed in the hope that it will be useful,
  15825. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15826. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  15827. // Lesser General Public License for more details.
  15828. //
  15829. // You should have received a copy of the GNU Lesser General Public
  15830. // License along with this library; if not, write to the Free Software
  15831. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  15832. /**
  15833. * Implementation of the Signature Packet (Tag 2)<br/>
  15834. * <br/>
  15835. * {@link http://tools.ietf.org/html/rfc4880#section-5.2|RFC4480 5.2}:
  15836. * A Signature packet describes a binding between some public key and
  15837. * some data. The most common signatures are a signature of a file or a
  15838. * block of text, and a signature that is a certification of a User ID.
  15839. * @requires crypto
  15840. * @requires enums
  15841. * @requires packet/packet
  15842. * @requires type/keyid
  15843. * @requires type/mpi
  15844. * @requires util
  15845. * @module packet/signature
  15846. */
  15847. function Signature() {
  15848. this.tag = _enums2.default.packet.signature;
  15849. this.version = 4;
  15850. this.signatureType = null;
  15851. this.hashAlgorithm = null;
  15852. this.publicKeyAlgorithm = null;
  15853. this.signatureData = null;
  15854. this.unhashedSubpackets = null;
  15855. this.signedHashValue = null;
  15856. this.created = new Date();
  15857. this.signatureExpirationTime = null;
  15858. this.signatureNeverExpires = true;
  15859. this.exportable = null;
  15860. this.trustLevel = null;
  15861. this.trustAmount = null;
  15862. this.regularExpression = null;
  15863. this.revocable = null;
  15864. this.keyExpirationTime = null;
  15865. this.keyNeverExpires = null;
  15866. this.preferredSymmetricAlgorithms = null;
  15867. this.revocationKeyClass = null;
  15868. this.revocationKeyAlgorithm = null;
  15869. this.revocationKeyFingerprint = null;
  15870. this.issuerKeyId = new _keyid2.default();
  15871. this.notation = null;
  15872. this.preferredHashAlgorithms = null;
  15873. this.preferredCompressionAlgorithms = null;
  15874. this.keyServerPreferences = null;
  15875. this.preferredKeyServer = null;
  15876. this.isPrimaryUserID = null;
  15877. this.policyURI = null;
  15878. this.keyFlags = null;
  15879. this.signersUserId = null;
  15880. this.reasonForRevocationFlag = null;
  15881. this.reasonForRevocationString = null;
  15882. this.features = null;
  15883. this.signatureTargetPublicKeyAlgorithm = null;
  15884. this.signatureTargetHashAlgorithm = null;
  15885. this.signatureTargetHash = null;
  15886. this.embeddedSignature = null;
  15887. this.verified = false;
  15888. }
  15889. /**
  15890. * parsing function for a signature packet (tag 2).
  15891. * @param {String} bytes payload of a tag 2 packet
  15892. * @param {Integer} position position to start reading from the bytes string
  15893. * @param {Integer} len length of the packet or the remaining length of bytes at position
  15894. * @return {module:packet/signature} object representation
  15895. */
  15896. Signature.prototype.read = function (bytes) {
  15897. var _this = this;
  15898. var i = 0;
  15899. this.version = bytes[i++];
  15900. // switch on version (3 and 4)
  15901. var sigpos;
  15902. var sigDataLength;
  15903. (function () {
  15904. switch (_this.version) {
  15905. case 3:
  15906. // One-octet length of following hashed material. MUST be 5.
  15907. if (bytes[i++] !== 5) {
  15908. _util2.default.print_debug("packet/signature.js\n" + 'invalid One-octet length of following hashed material.' + 'MUST be 5. @:' + (i - 1));
  15909. }
  15910. sigpos = i;
  15911. // One-octet signature type.
  15912. _this.signatureType = bytes[i++];
  15913. // Four-octet creation time.
  15914. _this.created = _util2.default.readDate(bytes.subarray(i, i + 4));
  15915. i += 4;
  15916. // storing data appended to data which gets verified
  15917. _this.signatureData = bytes.subarray(sigpos, i);
  15918. // Eight-octet Key ID of signer.
  15919. _this.issuerKeyId.read(bytes.subarray(i, i + 8));
  15920. i += 8;
  15921. // One-octet public-key algorithm.
  15922. _this.publicKeyAlgorithm = bytes[i++];
  15923. // One-octet hash algorithm.
  15924. _this.hashAlgorithm = bytes[i++];
  15925. break;
  15926. case 4:
  15927. _this.signatureType = bytes[i++];
  15928. _this.publicKeyAlgorithm = bytes[i++];
  15929. _this.hashAlgorithm = bytes[i++];
  15930. var subpackets = function subpackets(bytes) {
  15931. // Two-octet scalar octet count for following subpacket data.
  15932. var subpacket_length = _util2.default.readNumber(bytes.subarray(0, 2));
  15933. var i = 2;
  15934. // subpacket data set (zero or more subpackets)
  15935. while (i < 2 + subpacket_length) {
  15936. var len = _packet2.default.readSimpleLength(bytes.subarray(i, bytes.length));
  15937. i += len.offset;
  15938. this.read_sub_packet(bytes.subarray(i, i + len.len));
  15939. i += len.len;
  15940. }
  15941. return i;
  15942. };
  15943. // hashed subpackets
  15944. i += subpackets.call(_this, bytes.subarray(i, bytes.length), true);
  15945. // A V4 signature hashes the packet body
  15946. // starting from its first field, the version number, through the end
  15947. // of the hashed subpacket data. Thus, the fields hashed are the
  15948. // signature version, the signature type, the public-key algorithm, the
  15949. // hash algorithm, the hashed subpacket length, and the hashed
  15950. // subpacket body.
  15951. _this.signatureData = bytes.subarray(0, i);
  15952. sigDataLength = i;
  15953. // unhashed subpackets
  15954. i += subpackets.call(_this, bytes.subarray(i, bytes.length), false);
  15955. _this.unhashedSubpackets = bytes.subarray(sigDataLength, i);
  15956. break;
  15957. default:
  15958. throw new Error('Version ' + _this.version + ' of the signature is unsupported.');
  15959. }
  15960. // Two-octet field holding left 16 bits of signed hash value.
  15961. })();
  15962. this.signedHashValue = bytes.subarray(i, i + 2);
  15963. i += 2;
  15964. this.signature = bytes.subarray(i, bytes.length);
  15965. };
  15966. Signature.prototype.write = function () {
  15967. var arr = [];
  15968. switch (this.version) {
  15969. case 3:
  15970. arr.push(new Uint8Array([3, 5])); // version, One-octet length of following hashed material. MUST be 5
  15971. arr.push(new Uint8Array([this.signatureType]));
  15972. arr.push(_util2.default.writeDate(this.created));
  15973. arr.push(this.issuerKeyId.write());
  15974. arr.push(new Uint8Array([_enums2.default.write(_enums2.default.publicKey, this.publicKeyAlgorithm), _enums2.default.write(_enums2.default.hash, this.hashAlgorithm)]));
  15975. break;
  15976. case 4:
  15977. arr.push(this.signatureData);
  15978. arr.push(this.unhashedSubpackets ? this.unhashedSubpackets : _util2.default.writeNumber(0, 2));
  15979. break;
  15980. }
  15981. arr.push(this.signedHashValue);
  15982. arr.push(this.signature);
  15983. return _util2.default.concatUint8Array(arr);
  15984. };
  15985. /**
  15986. * Signs provided data. This needs to be done prior to serialization.
  15987. * @param {module:packet/secret_key} key private key used to sign the message.
  15988. * @param {Object} data Contains packets to be signed.
  15989. */
  15990. Signature.prototype.sign = function (key, data) {
  15991. var signatureType = _enums2.default.write(_enums2.default.signature, this.signatureType),
  15992. publicKeyAlgorithm = _enums2.default.write(_enums2.default.publicKey, this.publicKeyAlgorithm),
  15993. hashAlgorithm = _enums2.default.write(_enums2.default.hash, this.hashAlgorithm);
  15994. var arr = [new Uint8Array([4, signatureType, publicKeyAlgorithm, hashAlgorithm])];
  15995. this.issuerKeyId = key.getKeyId();
  15996. // Add hashed subpackets
  15997. arr.push(this.write_all_sub_packets());
  15998. this.signatureData = _util2.default.concatUint8Array(arr);
  15999. var trailer = this.calculateTrailer();
  16000. var toHash = null;
  16001. switch (this.version) {
  16002. case 3:
  16003. toHash = _util2.default.concatUint8Array([this.toSign(signatureType, data), new Uint8Array([signatureType]), _util2.default.writeDate(this.created)]);
  16004. break;
  16005. case 4:
  16006. toHash = _util2.default.concatUint8Array([this.toSign(signatureType, data), this.signatureData, trailer]);
  16007. break;
  16008. default:
  16009. throw new Error('Version ' + this.version + ' of the signature is unsupported.');
  16010. }
  16011. var hash = _crypto2.default.hash.digest(hashAlgorithm, toHash);
  16012. this.signedHashValue = hash.subarray(0, 2);
  16013. this.signature = _crypto2.default.signature.sign(hashAlgorithm, publicKeyAlgorithm, key.mpi, toHash);
  16014. };
  16015. /**
  16016. * Creates string of bytes with all subpacket data
  16017. * @return {String} a string-representation of a all subpacket data
  16018. */
  16019. Signature.prototype.write_all_sub_packets = function () {
  16020. var sub = _enums2.default.signatureSubpacket;
  16021. var arr = [];
  16022. var bytes;
  16023. if (this.created !== null) {
  16024. arr.push(write_sub_packet(sub.signature_creation_time, _util2.default.writeDate(this.created)));
  16025. }
  16026. if (this.signatureExpirationTime !== null) {
  16027. arr.push(write_sub_packet(sub.signature_expiration_time, _util2.default.writeNumber(this.signatureExpirationTime, 4)));
  16028. }
  16029. if (this.exportable !== null) {
  16030. arr.push(write_sub_packet(sub.exportable_certification, new Uint8Array([this.exportable ? 1 : 0])));
  16031. }
  16032. if (this.trustLevel !== null) {
  16033. bytes = new Uint8Array([this.trustLevel, this.trustAmount]);
  16034. arr.push(write_sub_packet(sub.trust_signature, bytes));
  16035. }
  16036. if (this.regularExpression !== null) {
  16037. arr.push(write_sub_packet(sub.regular_expression, this.regularExpression));
  16038. }
  16039. if (this.revocable !== null) {
  16040. arr.push(write_sub_packet(sub.revocable, new Uint8Array([this.revocable ? 1 : 0])));
  16041. }
  16042. if (this.keyExpirationTime !== null) {
  16043. arr.push(write_sub_packet(sub.key_expiration_time, _util2.default.writeNumber(this.keyExpirationTime, 4)));
  16044. }
  16045. if (this.preferredSymmetricAlgorithms !== null) {
  16046. bytes = _util2.default.str2Uint8Array(_util2.default.bin2str(this.preferredSymmetricAlgorithms));
  16047. arr.push(write_sub_packet(sub.preferred_symmetric_algorithms, bytes));
  16048. }
  16049. if (this.revocationKeyClass !== null) {
  16050. bytes = new Uint8Array([this.revocationKeyClass, this.revocationKeyAlgorithm]);
  16051. bytes = _util2.default.concatUint8Array([bytes, this.revocationKeyFingerprint]);
  16052. arr.push(write_sub_packet(sub.revocation_key, bytes));
  16053. }
  16054. if (!this.issuerKeyId.isNull()) {
  16055. arr.push(write_sub_packet(sub.issuer, this.issuerKeyId.write()));
  16056. }
  16057. if (this.notation !== null) {
  16058. for (var name in this.notation) {
  16059. if (this.notation.hasOwnProperty(name)) {
  16060. var value = this.notation[name];
  16061. bytes = [new Uint8Array([0x80, 0, 0, 0])];
  16062. // 2 octets of name length
  16063. bytes.push(_util2.default.writeNumber(name.length, 2));
  16064. // 2 octets of value length
  16065. bytes.push(_util2.default.writeNumber(value.length, 2));
  16066. bytes.push(_util2.default.str2Uint8Array(name + value));
  16067. bytes = _util2.default.concatUint8Array(bytes);
  16068. arr.push(write_sub_packet(sub.notation_data, bytes));
  16069. }
  16070. }
  16071. }
  16072. if (this.preferredHashAlgorithms !== null) {
  16073. bytes = _util2.default.str2Uint8Array(_util2.default.bin2str(this.preferredHashAlgorithms));
  16074. arr.push(write_sub_packet(sub.preferred_hash_algorithms, bytes));
  16075. }
  16076. if (this.preferredCompressionAlgorithms !== null) {
  16077. bytes = _util2.default.str2Uint8Array(_util2.default.bin2str(this.preferredCompressionAlgorithms));
  16078. arr.push(write_sub_packet(sub.preferred_compression_algorithms, bytes));
  16079. }
  16080. if (this.keyServerPreferences !== null) {
  16081. bytes = _util2.default.str2Uint8Array(_util2.default.bin2str(this.keyServerPreferences));
  16082. arr.push(write_sub_packet(sub.key_server_preferences, bytes));
  16083. }
  16084. if (this.preferredKeyServer !== null) {
  16085. arr.push(write_sub_packet(sub.preferred_key_server, _util2.default.str2Uint8Array(this.preferredKeyServer)));
  16086. }
  16087. if (this.isPrimaryUserID !== null) {
  16088. arr.push(write_sub_packet(sub.primary_user_id, new Uint8Array([this.isPrimaryUserID ? 1 : 0])));
  16089. }
  16090. if (this.policyURI !== null) {
  16091. arr.push(write_sub_packet(sub.policy_uri, _util2.default.str2Uint8Array(this.policyURI)));
  16092. }
  16093. if (this.keyFlags !== null) {
  16094. bytes = _util2.default.str2Uint8Array(_util2.default.bin2str(this.keyFlags));
  16095. arr.push(write_sub_packet(sub.key_flags, bytes));
  16096. }
  16097. if (this.signersUserId !== null) {
  16098. arr.push(write_sub_packet(sub.signers_user_id, _util2.default.str2Uint8Array(this.signersUserId)));
  16099. }
  16100. if (this.reasonForRevocationFlag !== null) {
  16101. bytes = _util2.default.str2Uint8Array(String.fromCharCode(this.reasonForRevocationFlag) + this.reasonForRevocationString);
  16102. arr.push(write_sub_packet(sub.reason_for_revocation, bytes));
  16103. }
  16104. if (this.features !== null) {
  16105. bytes = _util2.default.str2Uint8Array(_util2.default.bin2str(this.features));
  16106. arr.push(write_sub_packet(sub.features, bytes));
  16107. }
  16108. if (this.signatureTargetPublicKeyAlgorithm !== null) {
  16109. bytes = [new Uint8Array([this.signatureTargetPublicKeyAlgorithm, this.signatureTargetHashAlgorithm])];
  16110. bytes.push(_util2.default.str2Uint8Array(this.signatureTargetHash));
  16111. bytes = _util2.default.concatUint8Array(bytes);
  16112. arr.push(write_sub_packet(sub.signature_target, bytes));
  16113. }
  16114. if (this.embeddedSignature !== null) {
  16115. arr.push(write_sub_packet(sub.embedded_signature, this.embeddedSignature.write()));
  16116. }
  16117. var result = _util2.default.concatUint8Array(arr);
  16118. var length = _util2.default.writeNumber(result.length, 2);
  16119. return _util2.default.concatUint8Array([length, result]);
  16120. };
  16121. /**
  16122. * creates a string representation of a sub signature packet (See {@link http://tools.ietf.org/html/rfc4880#section-5.2.3.1|RFC 4880 5.2.3.1})
  16123. * @param {Integer} type subpacket signature type. Signature types as described
  16124. * in {@link http://tools.ietf.org/html/rfc4880#section-5.2.3.2|RFC4880 Section 5.2.3.2}
  16125. * @param {String} data data to be included
  16126. * @return {String} a string-representation of a sub signature packet (See {@link http://tools.ietf.org/html/rfc4880#section-5.2.3.1|RFC 4880 5.2.3.1})
  16127. */
  16128. function write_sub_packet(type, data) {
  16129. var arr = [];
  16130. arr.push(_packet2.default.writeSimpleLength(data.length + 1));
  16131. arr.push(new Uint8Array([type]));
  16132. arr.push(data);
  16133. return _util2.default.concatUint8Array(arr);
  16134. }
  16135. // V4 signature sub packets
  16136. Signature.prototype.read_sub_packet = function (bytes) {
  16137. var mypos = 0;
  16138. function read_array(prop, bytes) {
  16139. this[prop] = [];
  16140. for (var i = 0; i < bytes.length; i++) {
  16141. this[prop].push(bytes[i]);
  16142. }
  16143. }
  16144. // The leftwost bit denotes a "critical" packet, but we ignore it.
  16145. var type = bytes[mypos++] & 0x7F;
  16146. var seconds;
  16147. // subpacket type
  16148. switch (type) {
  16149. case 2:
  16150. // Signature Creation Time
  16151. this.created = _util2.default.readDate(bytes.subarray(mypos, bytes.length));
  16152. break;
  16153. case 3:
  16154. // Signature Expiration Time in seconds
  16155. seconds = _util2.default.readNumber(bytes.subarray(mypos, bytes.length));
  16156. this.signatureNeverExpires = seconds === 0;
  16157. this.signatureExpirationTime = seconds;
  16158. break;
  16159. case 4:
  16160. // Exportable Certification
  16161. this.exportable = bytes[mypos++] === 1;
  16162. break;
  16163. case 5:
  16164. // Trust Signature
  16165. this.trustLevel = bytes[mypos++];
  16166. this.trustAmount = bytes[mypos++];
  16167. break;
  16168. case 6:
  16169. // Regular Expression
  16170. this.regularExpression = bytes[mypos];
  16171. break;
  16172. case 7:
  16173. // Revocable
  16174. this.revocable = bytes[mypos++] === 1;
  16175. break;
  16176. case 9:
  16177. // Key Expiration Time in seconds
  16178. seconds = _util2.default.readNumber(bytes.subarray(mypos, bytes.length));
  16179. this.keyExpirationTime = seconds;
  16180. this.keyNeverExpires = seconds === 0;
  16181. break;
  16182. case 11:
  16183. // Preferred Symmetric Algorithms
  16184. read_array.call(this, 'preferredSymmetricAlgorithms', bytes.subarray(mypos, bytes.length));
  16185. break;
  16186. case 12:
  16187. // Revocation Key
  16188. // (1 octet of class, 1 octet of public-key algorithm ID, 20
  16189. // octets of
  16190. // fingerprint)
  16191. this.revocationKeyClass = bytes[mypos++];
  16192. this.revocationKeyAlgorithm = bytes[mypos++];
  16193. this.revocationKeyFingerprint = bytes.subarray(mypos, 20);
  16194. break;
  16195. case 16:
  16196. // Issuer
  16197. this.issuerKeyId.read(bytes.subarray(mypos, bytes.length));
  16198. break;
  16199. case 20:
  16200. // Notation Data
  16201. // We don't know how to handle anything but a text flagged data.
  16202. if (bytes[mypos] === 0x80) {
  16203. // We extract key/value tuple from the byte stream.
  16204. mypos += 4;
  16205. var m = _util2.default.readNumber(bytes.subarray(mypos, mypos + 2));
  16206. mypos += 2;
  16207. var n = _util2.default.readNumber(bytes.subarray(mypos, mypos + 2));
  16208. mypos += 2;
  16209. var name = _util2.default.Uint8Array2str(bytes.subarray(mypos, mypos + m)),
  16210. value = _util2.default.Uint8Array2str(bytes.subarray(mypos + m, mypos + m + n));
  16211. this.notation = this.notation || {};
  16212. this.notation[name] = value;
  16213. } else {
  16214. _util2.default.print_debug("Unsupported notation flag " + bytes[mypos]);
  16215. }
  16216. break;
  16217. case 21:
  16218. // Preferred Hash Algorithms
  16219. read_array.call(this, 'preferredHashAlgorithms', bytes.subarray(mypos, bytes.length));
  16220. break;
  16221. case 22:
  16222. // Preferred Compression Algorithms
  16223. read_array.call(this, 'preferredCompressionAlgorithms', bytes.subarray(mypos, bytes.length));
  16224. break;
  16225. case 23:
  16226. // Key Server Preferences
  16227. read_array.call(this, 'keyServerPreferencess', bytes.subarray(mypos, bytes.length));
  16228. break;
  16229. case 24:
  16230. // Preferred Key Server
  16231. this.preferredKeyServer = _util2.default.Uint8Array2str(bytes.subarray(mypos, bytes.length));
  16232. break;
  16233. case 25:
  16234. // Primary User ID
  16235. this.isPrimaryUserID = bytes[mypos++] !== 0;
  16236. break;
  16237. case 26:
  16238. // Policy URI
  16239. this.policyURI = _util2.default.Uint8Array2str(bytes.subarray(mypos, bytes.length));
  16240. break;
  16241. case 27:
  16242. // Key Flags
  16243. read_array.call(this, 'keyFlags', bytes.subarray(mypos, bytes.length));
  16244. break;
  16245. case 28:
  16246. // Signer's User ID
  16247. this.signersUserId += _util2.default.Uint8Array2str(bytes.subarray(mypos, bytes.length));
  16248. break;
  16249. case 29:
  16250. // Reason for Revocation
  16251. this.reasonForRevocationFlag = bytes[mypos++];
  16252. this.reasonForRevocationString = _util2.default.Uint8Array2str(bytes.subarray(mypos, bytes.length));
  16253. break;
  16254. case 30:
  16255. // Features
  16256. read_array.call(this, 'features', bytes.subarray(mypos, bytes.length));
  16257. break;
  16258. case 31:
  16259. // Signature Target
  16260. // (1 octet public-key algorithm, 1 octet hash algorithm, N octets hash)
  16261. this.signatureTargetPublicKeyAlgorithm = bytes[mypos++];
  16262. this.signatureTargetHashAlgorithm = bytes[mypos++];
  16263. var len = _crypto2.default.getHashByteLength(this.signatureTargetHashAlgorithm);
  16264. this.signatureTargetHash = _util2.default.Uint8Array2str(bytes.subarray(mypos, mypos + len));
  16265. break;
  16266. case 32:
  16267. // Embedded Signature
  16268. this.embeddedSignature = new Signature();
  16269. this.embeddedSignature.read(bytes.subarray(mypos, bytes.length));
  16270. break;
  16271. default:
  16272. _util2.default.print_debug("Unknown signature subpacket type " + type + " @:" + mypos);
  16273. }
  16274. };
  16275. // Produces data to produce signature on
  16276. Signature.prototype.toSign = function (type, data) {
  16277. var t = _enums2.default.signature;
  16278. switch (type) {
  16279. case t.binary:
  16280. case t.text:
  16281. return data.getBytes();
  16282. case t.standalone:
  16283. return new Uint8Array(0);
  16284. case t.cert_generic:
  16285. case t.cert_persona:
  16286. case t.cert_casual:
  16287. case t.cert_positive:
  16288. case t.cert_revocation:
  16289. var packet, tag;
  16290. if (data.userid !== undefined) {
  16291. tag = 0xB4;
  16292. packet = data.userid;
  16293. } else if (data.userattribute !== undefined) {
  16294. tag = 0xD1;
  16295. packet = data.userattribute;
  16296. } else {
  16297. throw new Error('Either a userid or userattribute packet needs to be ' + 'supplied for certification.');
  16298. }
  16299. var bytes = packet.write();
  16300. if (this.version === 4) {
  16301. return _util2.default.concatUint8Array([this.toSign(t.key, data), new Uint8Array([tag]), _util2.default.writeNumber(bytes.length, 4), bytes]);
  16302. } else if (this.version === 3) {
  16303. return _util2.default.concatUint8Array([this.toSign(t.key, data), bytes]);
  16304. }
  16305. break;
  16306. case t.subkey_binding:
  16307. case t.subkey_revocation:
  16308. case t.key_binding:
  16309. return _util2.default.concatUint8Array([this.toSign(t.key, data), this.toSign(t.key, {
  16310. key: data.bind
  16311. })]);
  16312. case t.key:
  16313. if (data.key === undefined) {
  16314. throw new Error('Key packet is required for this signature.');
  16315. }
  16316. return data.key.writeOld();
  16317. case t.key_revocation:
  16318. return this.toSign(t.key, data);
  16319. case t.timestamp:
  16320. return new Uint8Array(0);
  16321. case t.third_party:
  16322. throw new Error('Not implemented');
  16323. default:
  16324. throw new Error('Unknown signature type.');
  16325. }
  16326. };
  16327. Signature.prototype.calculateTrailer = function () {
  16328. // calculating the trailer
  16329. // V3 signatures don't have a trailer
  16330. if (this.version === 3) {
  16331. return new Uint8Array(0);
  16332. }
  16333. var first = new Uint8Array([4, 0xFF]); //Version, ?
  16334. return _util2.default.concatUint8Array([first, _util2.default.writeNumber(this.signatureData.length, 4)]);
  16335. };
  16336. /**
  16337. * verifys the signature packet. Note: not signature types are implemented
  16338. * @param {String|Object} data data which on the signature applies
  16339. * @param {module:packet/public_subkey|module:packet/public_key|
  16340. * module:packet/secret_subkey|module:packet/secret_key} key the public key to verify the signature
  16341. * @return {boolean} True if message is verified, else false.
  16342. */
  16343. Signature.prototype.verify = function (key, data) {
  16344. var signatureType = _enums2.default.write(_enums2.default.signature, this.signatureType),
  16345. publicKeyAlgorithm = _enums2.default.write(_enums2.default.publicKey, this.publicKeyAlgorithm),
  16346. hashAlgorithm = _enums2.default.write(_enums2.default.hash, this.hashAlgorithm);
  16347. var bytes = this.toSign(signatureType, data),
  16348. trailer = this.calculateTrailer();
  16349. var mpicount = 0;
  16350. // Algorithm-Specific Fields for RSA signatures:
  16351. // - multiprecision number (MPI) of RSA signature value m**d mod n.
  16352. if (publicKeyAlgorithm > 0 && publicKeyAlgorithm < 4) {
  16353. mpicount = 1;
  16354. }
  16355. // Algorithm-Specific Fields for DSA signatures:
  16356. // - MPI of DSA value r.
  16357. // - MPI of DSA value s.
  16358. else if (publicKeyAlgorithm === 17) {
  16359. mpicount = 2;
  16360. }
  16361. var mpi = [],
  16362. i = 0;
  16363. for (var j = 0; j < mpicount; j++) {
  16364. mpi[j] = new _mpi2.default();
  16365. i += mpi[j].read(this.signature.subarray(i, this.signature.length));
  16366. }
  16367. this.verified = _crypto2.default.signature.verify(publicKeyAlgorithm, hashAlgorithm, mpi, key.mpi, _util2.default.concatUint8Array([bytes, this.signatureData, trailer]));
  16368. return this.verified;
  16369. };
  16370. /**
  16371. * Verifies signature expiration date
  16372. * @return {Boolean} true if expired
  16373. */
  16374. Signature.prototype.isExpired = function () {
  16375. if (!this.signatureNeverExpires) {
  16376. return Date.now() > this.created.getTime() + this.signatureExpirationTime * 1000;
  16377. }
  16378. return false;
  16379. };
  16380. /**
  16381. * Fix custom types after cloning
  16382. */
  16383. Signature.prototype.postCloneTypeFix = function () {
  16384. this.issuerKeyId = _keyid2.default.fromClone(this.issuerKeyId);
  16385. };
  16386. },{"../crypto":24,"../enums.js":35,"../type/keyid.js":67,"../type/mpi.js":68,"../util.js":70,"./packet.js":51}],59:[function(_dereq_,module,exports){
  16387. // OpenPGP.js - An OpenPGP implementation in javascript
  16388. // Copyright (C) 2016 Tankred Hase
  16389. //
  16390. // This library is free software; you can redistribute it and/or
  16391. // modify it under the terms of the GNU Lesser General Public
  16392. // License as published by the Free Software Foundation; either
  16393. // version 3.0 of the License, or (at your option) any later version.
  16394. //
  16395. // This library is distributed in the hope that it will be useful,
  16396. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16397. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16398. // Lesser General Public License for more details.
  16399. //
  16400. // You should have received a copy of the GNU Lesser General Public
  16401. // License along with this library; if not, write to the Free Software
  16402. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16403. /**
  16404. * Implementation of the Symmetrically Encrypted Authenticated Encryption with Additional Data (AEAD) Protected Data Packet
  16405. * {@link https://tools.ietf.org/html/draft-ford-openpgp-format-00#section-2.1}: AEAD Protected Data Packet
  16406. */
  16407. 'use strict';
  16408. Object.defineProperty(exports, "__esModule", {
  16409. value: true
  16410. });
  16411. exports.default = SymEncryptedAEADProtected;
  16412. var _util = _dereq_('../util.js');
  16413. var _util2 = _interopRequireDefault(_util);
  16414. var _crypto = _dereq_('../crypto');
  16415. var _crypto2 = _interopRequireDefault(_crypto);
  16416. var _enums = _dereq_('../enums.js');
  16417. var _enums2 = _interopRequireDefault(_enums);
  16418. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16419. var VERSION = 1; // A one-octet version number of the data packet.
  16420. var IV_LEN = _crypto2.default.gcm.ivLength; // currently only AES-GCM is supported
  16421. /**
  16422. * @constructor
  16423. */
  16424. function SymEncryptedAEADProtected() {
  16425. this.tag = _enums2.default.packet.symEncryptedAEADProtected;
  16426. this.version = VERSION;
  16427. this.iv = null;
  16428. this.encrypted = null;
  16429. this.packets = null;
  16430. }
  16431. /**
  16432. * Parse an encrypted payload of bytes in the order: version, IV, ciphertext (see specification)
  16433. */
  16434. SymEncryptedAEADProtected.prototype.read = function (bytes) {
  16435. var offset = 0;
  16436. if (bytes[offset] !== VERSION) {
  16437. // The only currently defined value is 1.
  16438. throw new Error('Invalid packet version.');
  16439. }
  16440. offset++;
  16441. this.iv = bytes.subarray(offset, IV_LEN + offset);
  16442. offset += IV_LEN;
  16443. this.encrypted = bytes.subarray(offset, bytes.length);
  16444. };
  16445. /**
  16446. * Write the encrypted payload of bytes in the order: version, IV, ciphertext (see specification)
  16447. * @return {Uint8Array} The encrypted payload
  16448. */
  16449. SymEncryptedAEADProtected.prototype.write = function () {
  16450. return _util2.default.concatUint8Array([new Uint8Array([this.version]), this.iv, this.encrypted]);
  16451. };
  16452. /**
  16453. * Decrypt the encrypted payload.
  16454. * @param {String} sessionKeyAlgorithm The session key's cipher algorithm e.g. 'aes128'
  16455. * @param {Uint8Array} key The session key used to encrypt the payload
  16456. * @return {Promise<undefined>} Nothing is returned
  16457. */
  16458. SymEncryptedAEADProtected.prototype.decrypt = function (sessionKeyAlgorithm, key) {
  16459. var _this = this;
  16460. return _crypto2.default.gcm.decrypt(sessionKeyAlgorithm, this.encrypted, key, this.iv).then(function (decrypted) {
  16461. _this.packets.read(decrypted);
  16462. });
  16463. };
  16464. /**
  16465. * Encrypt the packet list payload.
  16466. * @param {String} sessionKeyAlgorithm The session key's cipher algorithm e.g. 'aes128'
  16467. * @param {Uint8Array} key The session key used to encrypt the payload
  16468. * @return {Promise<undefined>} Nothing is returned
  16469. */
  16470. SymEncryptedAEADProtected.prototype.encrypt = function (sessionKeyAlgorithm, key) {
  16471. var _this2 = this;
  16472. this.iv = _crypto2.default.random.getRandomValues(new Uint8Array(IV_LEN)); // generate new random IV
  16473. return _crypto2.default.gcm.encrypt(sessionKeyAlgorithm, this.packets.write(), key, this.iv).then(function (encrypted) {
  16474. _this2.encrypted = encrypted;
  16475. });
  16476. };
  16477. },{"../crypto":24,"../enums.js":35,"../util.js":70}],60:[function(_dereq_,module,exports){
  16478. // GPG4Browsers - An OpenPGP implementation in javascript
  16479. // Copyright (C) 2011 Recurity Labs GmbH
  16480. //
  16481. // This library is free software; you can redistribute it and/or
  16482. // modify it under the terms of the GNU Lesser General Public
  16483. // License as published by the Free Software Foundation; either
  16484. // version 3.0 of the License, or (at your option) any later version.
  16485. //
  16486. // This library is distributed in the hope that it will be useful,
  16487. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16488. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16489. // Lesser General Public License for more details.
  16490. //
  16491. // You should have received a copy of the GNU Lesser General Public
  16492. // License along with this library; if not, write to the Free Software
  16493. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16494. /**
  16495. * Implementation of the Sym. Encrypted Integrity Protected Data
  16496. * Packet (Tag 18)<br/>
  16497. * <br/>
  16498. * {@link http://tools.ietf.org/html/rfc4880#section-5.13|RFC4880 5.13}:
  16499. * The Symmetrically Encrypted Integrity Protected Data packet is
  16500. * a variant of the Symmetrically Encrypted Data packet. It is a new feature
  16501. * created for OpenPGP that addresses the problem of detecting a modification to
  16502. * encrypted data. It is used in combination with a Modification Detection Code
  16503. * packet.
  16504. * @requires crypto
  16505. * @requires util
  16506. * @requires enums
  16507. * @requires config
  16508. * @module packet/sym_encrypted_integrity_protected
  16509. */
  16510. 'use strict';
  16511. Object.defineProperty(exports, "__esModule", {
  16512. value: true
  16513. });
  16514. exports.default = SymEncryptedIntegrityProtected;
  16515. var _util = _dereq_('../util.js');
  16516. var _util2 = _interopRequireDefault(_util);
  16517. var _crypto = _dereq_('../crypto');
  16518. var _crypto2 = _interopRequireDefault(_crypto);
  16519. var _enums = _dereq_('../enums.js');
  16520. var _enums2 = _interopRequireDefault(_enums);
  16521. var _asmcryptoLite = _dereq_('asmcrypto-lite');
  16522. var _asmcryptoLite2 = _interopRequireDefault(_asmcryptoLite);
  16523. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16524. var nodeCrypto = _util2.default.getNodeCrypto();
  16525. var Buffer = _util2.default.getNodeBuffer();
  16526. var VERSION = 1; // A one-octet version number of the data packet.
  16527. /**
  16528. * @constructor
  16529. */
  16530. function SymEncryptedIntegrityProtected() {
  16531. this.tag = _enums2.default.packet.symEncryptedIntegrityProtected;
  16532. this.version = VERSION;
  16533. /** The encrypted payload. */
  16534. this.encrypted = null; // string
  16535. /**
  16536. * If after decrypting the packet this is set to true,
  16537. * a modification has been detected and thus the contents
  16538. * should be discarded.
  16539. * @type {Boolean}
  16540. */
  16541. this.modification = false;
  16542. this.packets = null;
  16543. }
  16544. SymEncryptedIntegrityProtected.prototype.read = function (bytes) {
  16545. // - A one-octet version number. The only currently defined value is 1.
  16546. if (bytes[0] !== VERSION) {
  16547. throw new Error('Invalid packet version.');
  16548. }
  16549. // - Encrypted data, the output of the selected symmetric-key cipher
  16550. // operating in Cipher Feedback mode with shift amount equal to the
  16551. // block size of the cipher (CFB-n where n is the block size).
  16552. this.encrypted = bytes.subarray(1, bytes.length);
  16553. };
  16554. SymEncryptedIntegrityProtected.prototype.write = function () {
  16555. return _util2.default.concatUint8Array([new Uint8Array([VERSION]), this.encrypted]);
  16556. };
  16557. /**
  16558. * Encrypt the payload in the packet.
  16559. * @param {String} sessionKeyAlgorithm The selected symmetric encryption algorithm to be used e.g. 'aes128'
  16560. * @param {Uint8Array} key The key of cipher blocksize length to be used
  16561. * @return {Promise}
  16562. */
  16563. SymEncryptedIntegrityProtected.prototype.encrypt = function (sessionKeyAlgorithm, key) {
  16564. var bytes = this.packets.write();
  16565. var prefixrandom = _crypto2.default.getPrefixRandom(sessionKeyAlgorithm);
  16566. var repeat = new Uint8Array([prefixrandom[prefixrandom.length - 2], prefixrandom[prefixrandom.length - 1]]);
  16567. var prefix = _util2.default.concatUint8Array([prefixrandom, repeat]);
  16568. var mdc = new Uint8Array([0xD3, 0x14]); // modification detection code packet
  16569. var tohash = _util2.default.concatUint8Array([bytes, mdc]);
  16570. var hash = _crypto2.default.hash.sha1(_util2.default.concatUint8Array([prefix, tohash]));
  16571. tohash = _util2.default.concatUint8Array([tohash, hash]);
  16572. if (sessionKeyAlgorithm.substr(0, 3) === 'aes') {
  16573. // AES optimizations. Native code for node, asmCrypto for browser.
  16574. this.encrypted = aesEncrypt(sessionKeyAlgorithm, prefix, tohash, key);
  16575. } else {
  16576. this.encrypted = _crypto2.default.cfb.encrypt(prefixrandom, sessionKeyAlgorithm, tohash, key, false);
  16577. this.encrypted = this.encrypted.subarray(0, prefix.length + tohash.length);
  16578. }
  16579. return Promise.resolve();
  16580. };
  16581. /**
  16582. * Decrypts the encrypted data contained in the packet.
  16583. * @param {String} sessionKeyAlgorithm The selected symmetric encryption algorithm to be used e.g. 'aes128'
  16584. * @param {Uint8Array} key The key of cipher blocksize length to be used
  16585. * @return {Promise}
  16586. */
  16587. SymEncryptedIntegrityProtected.prototype.decrypt = function (sessionKeyAlgorithm, key) {
  16588. var decrypted = void 0;
  16589. if (sessionKeyAlgorithm.substr(0, 3) === 'aes') {
  16590. // AES optimizations. Native code for node, asmCrypto for browser.
  16591. decrypted = aesDecrypt(sessionKeyAlgorithm, this.encrypted, key);
  16592. } else {
  16593. decrypted = _crypto2.default.cfb.decrypt(sessionKeyAlgorithm, key, this.encrypted, false);
  16594. }
  16595. // there must be a modification detection code packet as the
  16596. // last packet and everything gets hashed except the hash itself
  16597. var prefix = _crypto2.default.cfb.mdc(sessionKeyAlgorithm, key, this.encrypted);
  16598. var bytes = decrypted.subarray(0, decrypted.length - 20);
  16599. var tohash = _util2.default.concatUint8Array([prefix, bytes]);
  16600. this.hash = _util2.default.Uint8Array2str(_crypto2.default.hash.sha1(tohash));
  16601. var mdc = _util2.default.Uint8Array2str(decrypted.subarray(decrypted.length - 20, decrypted.length));
  16602. if (this.hash !== mdc) {
  16603. throw new Error('Modification detected.');
  16604. } else {
  16605. this.packets.read(decrypted.subarray(0, decrypted.length - 22));
  16606. }
  16607. return Promise.resolve();
  16608. };
  16609. //////////////////////////
  16610. // //
  16611. // Helper functions //
  16612. // //
  16613. //////////////////////////
  16614. function aesEncrypt(algo, prefix, pt, key) {
  16615. if (nodeCrypto) {
  16616. // Node crypto library.
  16617. return nodeEncrypt(algo, prefix, pt, key);
  16618. } else {
  16619. // asm.js fallback
  16620. return _asmcryptoLite2.default.AES_CFB.encrypt(_util2.default.concatUint8Array([prefix, pt]), key);
  16621. }
  16622. }
  16623. function aesDecrypt(algo, ct, key) {
  16624. var pt = void 0;
  16625. if (nodeCrypto) {
  16626. // Node crypto library.
  16627. pt = nodeDecrypt(algo, ct, key);
  16628. } else {
  16629. // asm.js fallback
  16630. pt = _asmcryptoLite2.default.AES_CFB.decrypt(ct, key);
  16631. }
  16632. return pt.subarray(_crypto2.default.cipher[algo].blockSize + 2, pt.length); // Remove random prefix
  16633. }
  16634. function nodeEncrypt(algo, prefix, pt, key) {
  16635. key = new Buffer(key);
  16636. var iv = new Buffer(new Uint8Array(_crypto2.default.cipher[algo].blockSize));
  16637. var cipherObj = new nodeCrypto.createCipheriv('aes-' + algo.substr(3, 3) + '-cfb', key, iv);
  16638. var ct = cipherObj.update(new Buffer(_util2.default.concatUint8Array([prefix, pt])));
  16639. return new Uint8Array(ct);
  16640. }
  16641. function nodeDecrypt(algo, ct, key) {
  16642. ct = new Buffer(ct);
  16643. key = new Buffer(key);
  16644. var iv = new Buffer(new Uint8Array(_crypto2.default.cipher[algo].blockSize));
  16645. var decipherObj = new nodeCrypto.createDecipheriv('aes-' + algo.substr(3, 3) + '-cfb', key, iv);
  16646. var pt = decipherObj.update(ct);
  16647. return new Uint8Array(pt);
  16648. }
  16649. },{"../crypto":24,"../enums.js":35,"../util.js":70,"asmcrypto-lite":1}],61:[function(_dereq_,module,exports){
  16650. // GPG4Browsers - An OpenPGP implementation in javascript
  16651. // Copyright (C) 2011 Recurity Labs GmbH
  16652. //
  16653. // This library is free software; you can redistribute it and/or
  16654. // modify it under the terms of the GNU Lesser General Public
  16655. // License as published by the Free Software Foundation; either
  16656. // version 3.0 of the License, or (at your option) any later version.
  16657. //
  16658. // This library is distributed in the hope that it will be useful,
  16659. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16660. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16661. // Lesser General Public License for more details.
  16662. //
  16663. // You should have received a copy of the GNU Lesser General Public
  16664. // License along with this library; if not, write to the Free Software
  16665. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16666. /**
  16667. * Public-Key Encrypted Session Key Packets (Tag 1)<br/>
  16668. * <br/>
  16669. * {@link http://tools.ietf.org/html/rfc4880#section-5.1|RFC4880 5.1}: A Public-Key Encrypted Session Key packet holds the session key
  16670. * used to encrypt a message. Zero or more Public-Key Encrypted Session Key
  16671. * packets and/or Symmetric-Key Encrypted Session Key packets may precede a
  16672. * Symmetrically Encrypted Data Packet, which holds an encrypted message. The
  16673. * message is encrypted with the session key, and the session key is itself
  16674. * encrypted and stored in the Encrypted Session Key packet(s). The
  16675. * Symmetrically Encrypted Data Packet is preceded by one Public-Key Encrypted
  16676. * Session Key packet for each OpenPGP key to which the message is encrypted.
  16677. * The recipient of the message finds a session key that is encrypted to their
  16678. * public key, decrypts the session key, and then uses the session key to
  16679. * decrypt the message.
  16680. * @requires util
  16681. * @requires crypto
  16682. * @requires enums
  16683. * @requires type/s2k
  16684. * @module packet/sym_encrypted_session_key
  16685. */
  16686. 'use strict';
  16687. Object.defineProperty(exports, "__esModule", {
  16688. value: true
  16689. });
  16690. exports.default = SymEncryptedSessionKey;
  16691. var _util = _dereq_('../util.js');
  16692. var _util2 = _interopRequireDefault(_util);
  16693. var _s2k = _dereq_('../type/s2k.js');
  16694. var _s2k2 = _interopRequireDefault(_s2k);
  16695. var _enums = _dereq_('../enums.js');
  16696. var _enums2 = _interopRequireDefault(_enums);
  16697. var _crypto = _dereq_('../crypto');
  16698. var _crypto2 = _interopRequireDefault(_crypto);
  16699. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16700. /**
  16701. * @constructor
  16702. */
  16703. function SymEncryptedSessionKey() {
  16704. this.tag = _enums2.default.packet.symEncryptedSessionKey;
  16705. this.version = 4;
  16706. this.sessionKey = null;
  16707. this.sessionKeyEncryptionAlgorithm = null;
  16708. this.sessionKeyAlgorithm = 'aes256';
  16709. this.encrypted = null;
  16710. this.s2k = new _s2k2.default();
  16711. }
  16712. /**
  16713. * Parsing function for a symmetric encrypted session key packet (tag 3).
  16714. *
  16715. * @param {Uint8Array} input Payload of a tag 1 packet
  16716. * @param {Integer} position Position to start reading from the input string
  16717. * @param {Integer} len
  16718. * Length of the packet or the remaining length of
  16719. * input at position
  16720. * @return {module:packet/sym_encrypted_session_key} Object representation
  16721. */
  16722. SymEncryptedSessionKey.prototype.read = function (bytes) {
  16723. // A one-octet version number. The only currently defined version is 4.
  16724. this.version = bytes[0];
  16725. // A one-octet number describing the symmetric algorithm used.
  16726. var algo = _enums2.default.read(_enums2.default.symmetric, bytes[1]);
  16727. // A string-to-key (S2K) specifier, length as defined above.
  16728. var s2klength = this.s2k.read(bytes.subarray(2, bytes.length));
  16729. // Optionally, the encrypted session key itself, which is decrypted
  16730. // with the string-to-key object.
  16731. var done = s2klength + 2;
  16732. if (done < bytes.length) {
  16733. this.encrypted = bytes.subarray(done, bytes.length);
  16734. this.sessionKeyEncryptionAlgorithm = algo;
  16735. } else {
  16736. this.sessionKeyAlgorithm = algo;
  16737. }
  16738. };
  16739. SymEncryptedSessionKey.prototype.write = function () {
  16740. var algo = this.encrypted === null ? this.sessionKeyAlgorithm : this.sessionKeyEncryptionAlgorithm;
  16741. var bytes = _util2.default.concatUint8Array([new Uint8Array([this.version, _enums2.default.write(_enums2.default.symmetric, algo)]), this.s2k.write()]);
  16742. if (this.encrypted !== null) {
  16743. bytes = _util2.default.concatUint8Array([bytes, this.encrypted]);
  16744. }
  16745. return bytes;
  16746. };
  16747. /**
  16748. * Decrypts the session key (only for public key encrypted session key
  16749. * packets (tag 1)
  16750. *
  16751. * @return {Uint8Array} The unencrypted session key
  16752. */
  16753. SymEncryptedSessionKey.prototype.decrypt = function (passphrase) {
  16754. var algo = this.sessionKeyEncryptionAlgorithm !== null ? this.sessionKeyEncryptionAlgorithm : this.sessionKeyAlgorithm;
  16755. var length = _crypto2.default.cipher[algo].keySize;
  16756. var key = this.s2k.produce_key(passphrase, length);
  16757. if (this.encrypted === null) {
  16758. this.sessionKey = key;
  16759. } else {
  16760. var decrypted = _crypto2.default.cfb.normalDecrypt(algo, key, this.encrypted, null);
  16761. this.sessionKeyAlgorithm = _enums2.default.read(_enums2.default.symmetric, decrypted[0]);
  16762. this.sessionKey = decrypted.subarray(1, decrypted.length);
  16763. }
  16764. };
  16765. SymEncryptedSessionKey.prototype.encrypt = function (passphrase) {
  16766. var algo = this.sessionKeyEncryptionAlgorithm !== null ? this.sessionKeyEncryptionAlgorithm : this.sessionKeyAlgorithm;
  16767. this.sessionKeyEncryptionAlgorithm = algo;
  16768. var length = _crypto2.default.cipher[algo].keySize;
  16769. var key = this.s2k.produce_key(passphrase, length);
  16770. var algo_enum = new Uint8Array([_enums2.default.write(_enums2.default.symmetric, this.sessionKeyAlgorithm)]);
  16771. var private_key;
  16772. if (this.sessionKey === null) {
  16773. this.sessionKey = _crypto2.default.getRandomBytes(_crypto2.default.cipher[this.sessionKeyAlgorithm].keySize);
  16774. }
  16775. private_key = _util2.default.concatUint8Array([algo_enum, this.sessionKey]);
  16776. this.encrypted = _crypto2.default.cfb.normalEncrypt(algo, key, private_key, null);
  16777. };
  16778. /**
  16779. * Fix custom types after cloning
  16780. */
  16781. SymEncryptedSessionKey.prototype.postCloneTypeFix = function () {
  16782. this.s2k = _s2k2.default.fromClone(this.s2k);
  16783. };
  16784. },{"../crypto":24,"../enums.js":35,"../type/s2k.js":69,"../util.js":70}],62:[function(_dereq_,module,exports){
  16785. // GPG4Browsers - An OpenPGP implementation in javascript
  16786. // Copyright (C) 2011 Recurity Labs GmbH
  16787. //
  16788. // This library is free software; you can redistribute it and/or
  16789. // modify it under the terms of the GNU Lesser General Public
  16790. // License as published by the Free Software Foundation; either
  16791. // version 3.0 of the License, or (at your option) any later version.
  16792. //
  16793. // This library is distributed in the hope that it will be useful,
  16794. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16795. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16796. // Lesser General Public License for more details.
  16797. //
  16798. // You should have received a copy of the GNU Lesser General Public
  16799. // License along with this library; if not, write to the Free Software
  16800. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16801. /**
  16802. * Implementation of the Symmetrically Encrypted Data Packet (Tag 9)<br/>
  16803. * <br/>
  16804. * {@link http://tools.ietf.org/html/rfc4880#section-5.7|RFC4880 5.7}: The Symmetrically Encrypted Data packet contains data encrypted
  16805. * with a symmetric-key algorithm. When it has been decrypted, it contains other
  16806. * packets (usually a literal data packet or compressed data packet, but in
  16807. * theory other Symmetrically Encrypted Data packets or sequences of packets
  16808. * that form whole OpenPGP messages).
  16809. * @requires crypto
  16810. * @requires enums
  16811. * @module packet/symmetrically_encrypted
  16812. */
  16813. 'use strict';
  16814. Object.defineProperty(exports, "__esModule", {
  16815. value: true
  16816. });
  16817. exports.default = SymmetricallyEncrypted;
  16818. var _crypto = _dereq_('../crypto');
  16819. var _crypto2 = _interopRequireDefault(_crypto);
  16820. var _enums = _dereq_('../enums.js');
  16821. var _enums2 = _interopRequireDefault(_enums);
  16822. var _config = _dereq_('../config');
  16823. var _config2 = _interopRequireDefault(_config);
  16824. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16825. /**
  16826. * @constructor
  16827. */
  16828. function SymmetricallyEncrypted() {
  16829. this.tag = _enums2.default.packet.symmetricallyEncrypted;
  16830. this.encrypted = null;
  16831. /** Decrypted packets contained within.
  16832. * @type {module:packet/packetlist} */
  16833. this.packets = null;
  16834. this.ignore_mdc_error = _config2.default.ignore_mdc_error;
  16835. }
  16836. SymmetricallyEncrypted.prototype.read = function (bytes) {
  16837. this.encrypted = bytes;
  16838. };
  16839. SymmetricallyEncrypted.prototype.write = function () {
  16840. return this.encrypted;
  16841. };
  16842. /**
  16843. * Symmetrically decrypt the packet data
  16844. *
  16845. * @param {module:enums.symmetric} sessionKeyAlgorithm
  16846. * Symmetric key algorithm to use // See {@link http://tools.ietf.org/html/rfc4880#section-9.2|RFC4880 9.2}
  16847. * @param {String} key
  16848. * Key as string with the corresponding length to the
  16849. * algorithm
  16850. */
  16851. SymmetricallyEncrypted.prototype.decrypt = function (sessionKeyAlgorithm, key) {
  16852. var decrypted = _crypto2.default.cfb.decrypt(sessionKeyAlgorithm, key, this.encrypted, true);
  16853. // for modern cipher (blocklength != 64 bit, except for Twofish) MDC is required
  16854. if (!this.ignore_mdc_error && (sessionKeyAlgorithm === 'aes128' || sessionKeyAlgorithm === 'aes192' || sessionKeyAlgorithm === 'aes256')) {
  16855. throw new Error('Decryption failed due to missing MDC in combination with modern cipher.');
  16856. }
  16857. this.packets.read(decrypted);
  16858. return Promise.resolve();
  16859. };
  16860. SymmetricallyEncrypted.prototype.encrypt = function (algo, key) {
  16861. var data = this.packets.write();
  16862. this.encrypted = _crypto2.default.cfb.encrypt(_crypto2.default.getPrefixRandom(algo), algo, data, key, true);
  16863. return Promise.resolve();
  16864. };
  16865. },{"../config":10,"../crypto":24,"../enums.js":35}],63:[function(_dereq_,module,exports){
  16866. /**
  16867. * @requires enums
  16868. * @module packet/trust
  16869. */
  16870. 'use strict';
  16871. Object.defineProperty(exports, "__esModule", {
  16872. value: true
  16873. });
  16874. exports.default = Trust;
  16875. var _enums = _dereq_('../enums.js');
  16876. var _enums2 = _interopRequireDefault(_enums);
  16877. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16878. /**
  16879. * @constructor
  16880. */
  16881. function Trust() {
  16882. this.tag = _enums2.default.packet.trust;
  16883. }
  16884. /**
  16885. * Parsing function for a trust packet (tag 12).
  16886. * Currently empty as we ignore trust packets
  16887. * @param {String} byptes payload of a tag 12 packet
  16888. */
  16889. Trust.prototype.read = function () {};
  16890. },{"../enums.js":35}],64:[function(_dereq_,module,exports){
  16891. // GPG4Browsers - An OpenPGP implementation in javascript
  16892. // Copyright (C) 2011 Recurity Labs GmbH
  16893. //
  16894. // This library is free software; you can redistribute it and/or
  16895. // modify it under the terms of the GNU Lesser General Public
  16896. // License as published by the Free Software Foundation; either
  16897. // version 3.0 of the License, or (at your option) any later version.
  16898. //
  16899. // This library is distributed in the hope that it will be useful,
  16900. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16901. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16902. // Lesser General Public License for more details.
  16903. //
  16904. // You should have received a copy of the GNU Lesser General Public
  16905. // License along with this library; if not, write to the Free Software
  16906. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  16907. /**
  16908. * Implementation of the User Attribute Packet (Tag 17)<br/>
  16909. * <br/>
  16910. * The User Attribute packet is a variation of the User ID packet. It
  16911. * is capable of storing more types of data than the User ID packet,
  16912. * which is limited to text. Like the User ID packet, a User Attribute
  16913. * packet may be certified by the key owner ("self-signed") or any other
  16914. * key owner who cares to certify it. Except as noted, a User Attribute
  16915. * packet may be used anywhere that a User ID packet may be used.
  16916. * <br/>
  16917. * While User Attribute packets are not a required part of the OpenPGP
  16918. * standard, implementations SHOULD provide at least enough
  16919. * compatibility to properly handle a certification signature on the
  16920. * User Attribute packet. A simple way to do this is by treating the
  16921. * User Attribute packet as a User ID packet with opaque contents, but
  16922. * an implementation may use any method desired.
  16923. * module packet/user_attribute
  16924. * @requires enums
  16925. * @module packet/user_attribute
  16926. */
  16927. 'use strict';
  16928. Object.defineProperty(exports, "__esModule", {
  16929. value: true
  16930. });
  16931. exports.default = UserAttribute;
  16932. var _util = _dereq_('../util.js');
  16933. var _util2 = _interopRequireDefault(_util);
  16934. var _packet = _dereq_('./packet.js');
  16935. var _packet2 = _interopRequireDefault(_packet);
  16936. var _enums = _dereq_('../enums.js');
  16937. var _enums2 = _interopRequireDefault(_enums);
  16938. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  16939. /**
  16940. * @constructor
  16941. */
  16942. function UserAttribute() {
  16943. this.tag = _enums2.default.packet.userAttribute;
  16944. this.attributes = [];
  16945. }
  16946. /**
  16947. * parsing function for a user attribute packet (tag 17).
  16948. * @param {Uint8Array} input payload of a tag 17 packet
  16949. */
  16950. UserAttribute.prototype.read = function (bytes) {
  16951. var i = 0;
  16952. while (i < bytes.length) {
  16953. var len = _packet2.default.readSimpleLength(bytes.subarray(i, bytes.length));
  16954. i += len.offset;
  16955. this.attributes.push(_util2.default.Uint8Array2str(bytes.subarray(i, i + len.len)));
  16956. i += len.len;
  16957. }
  16958. };
  16959. /**
  16960. * Creates a binary representation of the user attribute packet
  16961. * @return {Uint8Array} string representation
  16962. */
  16963. UserAttribute.prototype.write = function () {
  16964. var arr = [];
  16965. for (var i = 0; i < this.attributes.length; i++) {
  16966. arr.push(_packet2.default.writeSimpleLength(this.attributes[i].length));
  16967. arr.push(_util2.default.str2Uint8Array(this.attributes[i]));
  16968. }
  16969. return _util2.default.concatUint8Array(arr);
  16970. };
  16971. /**
  16972. * Compare for equality
  16973. * @param {module:user_attribute~UserAttribute} usrAttr
  16974. * @return {Boolean} true if equal
  16975. */
  16976. UserAttribute.prototype.equals = function (usrAttr) {
  16977. if (!usrAttr || !(usrAttr instanceof UserAttribute)) {
  16978. return false;
  16979. }
  16980. return this.attributes.every(function (attr, index) {
  16981. return attr === usrAttr.attributes[index];
  16982. });
  16983. };
  16984. },{"../enums.js":35,"../util.js":70,"./packet.js":51}],65:[function(_dereq_,module,exports){
  16985. // GPG4Browsers - An OpenPGP implementation in javascript
  16986. // Copyright (C) 2011 Recurity Labs GmbH
  16987. //
  16988. // This library is free software; you can redistribute it and/or
  16989. // modify it under the terms of the GNU Lesser General Public
  16990. // License as published by the Free Software Foundation; either
  16991. // version 3.0 of the License, or (at your option) any later version.
  16992. //
  16993. // This library is distributed in the hope that it will be useful,
  16994. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  16995. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  16996. // Lesser General Public License for more details.
  16997. //
  16998. // You should have received a copy of the GNU Lesser General Public
  16999. // License along with this library; if not, write to the Free Software
  17000. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17001. /**
  17002. * Implementation of the User ID Packet (Tag 13)<br/>
  17003. * <br/>
  17004. * A User ID packet consists of UTF-8 text that is intended to represent
  17005. * the name and email address of the key holder. By convention, it
  17006. * includes an RFC 2822 [RFC2822] mail name-addr, but there are no
  17007. * restrictions on its content. The packet length in the header
  17008. * specifies the length of the User ID.
  17009. * @requires util
  17010. * @requires enums
  17011. * @module packet/userid
  17012. */
  17013. 'use strict';
  17014. Object.defineProperty(exports, "__esModule", {
  17015. value: true
  17016. });
  17017. exports.default = Userid;
  17018. var _util = _dereq_('../util.js');
  17019. var _util2 = _interopRequireDefault(_util);
  17020. var _enums = _dereq_('../enums.js');
  17021. var _enums2 = _interopRequireDefault(_enums);
  17022. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  17023. /**
  17024. * @constructor
  17025. */
  17026. function Userid() {
  17027. this.tag = _enums2.default.packet.userid;
  17028. /** A string containing the user id. Usually in the form
  17029. * John Doe <john@example.com>
  17030. * @type {String}
  17031. */
  17032. this.userid = '';
  17033. }
  17034. /**
  17035. * Parsing function for a user id packet (tag 13).
  17036. * @param {Uint8Array} input payload of a tag 13 packet
  17037. */
  17038. Userid.prototype.read = function (bytes) {
  17039. this.userid = _util2.default.decode_utf8(_util2.default.Uint8Array2str(bytes));
  17040. };
  17041. /**
  17042. * Creates a binary representation of the user id packet
  17043. * @return {Uint8Array} binary representation
  17044. */
  17045. Userid.prototype.write = function () {
  17046. return _util2.default.str2Uint8Array(_util2.default.encode_utf8(this.userid));
  17047. };
  17048. },{"../enums.js":35,"../util.js":70}],66:[function(_dereq_,module,exports){
  17049. // GPG4Browsers - An OpenPGP implementation in javascript
  17050. // Copyright (C) 2011 Recurity Labs GmbH
  17051. //
  17052. // This library is free software; you can redistribute it and/or
  17053. // modify it under the terms of the GNU Lesser General Public
  17054. // License as published by the Free Software Foundation; either
  17055. // version 3.0 of the License, or (at your option) any later version.
  17056. //
  17057. // This library is distributed in the hope that it will be useful,
  17058. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17059. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17060. // Lesser General Public License for more details.
  17061. //
  17062. // You should have received a copy of the GNU Lesser General Public
  17063. // License along with this library; if not, write to the Free Software
  17064. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17065. /**
  17066. * @requires config
  17067. * @requires crypto
  17068. * @requires encoding/armor
  17069. * @requires enums
  17070. * @requires packet
  17071. * @module signature
  17072. */
  17073. 'use strict';
  17074. Object.defineProperty(exports, "__esModule", {
  17075. value: true
  17076. });
  17077. exports.Signature = Signature;
  17078. exports.readArmored = readArmored;
  17079. exports.read = read;
  17080. var _packet = _dereq_('./packet');
  17081. var _packet2 = _interopRequireDefault(_packet);
  17082. var _enums = _dereq_('./enums.js');
  17083. var _enums2 = _interopRequireDefault(_enums);
  17084. var _armor = _dereq_('./encoding/armor.js');
  17085. var _armor2 = _interopRequireDefault(_armor);
  17086. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  17087. /**
  17088. * @class
  17089. * @classdesc Class that represents an OpenPGP signature.
  17090. * @param {module:packet/packetlist} packetlist The signature packets
  17091. */
  17092. function Signature(packetlist) {
  17093. if (!(this instanceof Signature)) {
  17094. return new Signature(packetlist);
  17095. }
  17096. this.packets = packetlist || new _packet2.default.List();
  17097. }
  17098. /**
  17099. * Returns ASCII armored text of signature
  17100. * @return {String} ASCII armor
  17101. */
  17102. Signature.prototype.armor = function () {
  17103. return _armor2.default.encode(_enums2.default.armor.signature, this.packets.write());
  17104. };
  17105. /**
  17106. * reads an OpenPGP armored signature and returns a signature object
  17107. * @param {String} armoredText text to be parsed
  17108. * @return {Signature} new signature object
  17109. * @static
  17110. */
  17111. function readArmored(armoredText) {
  17112. var input = _armor2.default.decode(armoredText).data;
  17113. return read(input);
  17114. }
  17115. /**
  17116. * reads an OpenPGP signature as byte array and returns a signature object
  17117. * @param {Uint8Array} input binary signature
  17118. * @return {Signature} new signature object
  17119. * @static
  17120. */
  17121. function read(input) {
  17122. var packetlist = new _packet2.default.List();
  17123. packetlist.read(input);
  17124. return new Signature(packetlist);
  17125. }
  17126. },{"./encoding/armor.js":33,"./enums.js":35,"./packet":47}],67:[function(_dereq_,module,exports){
  17127. // GPG4Browsers - An OpenPGP implementation in javascript
  17128. // Copyright (C) 2011 Recurity Labs GmbH
  17129. //
  17130. // This library is free software; you can redistribute it and/or
  17131. // modify it under the terms of the GNU Lesser General Public
  17132. // License as published by the Free Software Foundation; either
  17133. // version 3.0 of the License, or (at your option) any later version.
  17134. //
  17135. // This library is distributed in the hope that it will be useful,
  17136. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17137. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17138. // Lesser General Public License for more details.
  17139. //
  17140. // You should have received a copy of the GNU Lesser General Public
  17141. // License along with this library; if not, write to the Free Software
  17142. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17143. /**
  17144. * Implementation of type key id ({@link http://tools.ietf.org/html/rfc4880#section-3.3|RFC4880 3.3})<br/>
  17145. * <br/>
  17146. * A Key ID is an eight-octet scalar that identifies a key.
  17147. * Implementations SHOULD NOT assume that Key IDs are unique. The
  17148. * section "Enhanced Key Formats" below describes how Key IDs are
  17149. * formed.
  17150. * @requires util
  17151. * @module type/keyid
  17152. */
  17153. 'use strict';
  17154. Object.defineProperty(exports, "__esModule", {
  17155. value: true
  17156. });
  17157. exports.default = Keyid;
  17158. var _util = _dereq_('../util.js');
  17159. var _util2 = _interopRequireDefault(_util);
  17160. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  17161. /**
  17162. * @constructor
  17163. */
  17164. function Keyid() {
  17165. this.bytes = '';
  17166. }
  17167. /**
  17168. * Parsing method for a key id
  17169. * @param {Uint8Array} input Input to read the key id from
  17170. */
  17171. Keyid.prototype.read = function (bytes) {
  17172. this.bytes = _util2.default.Uint8Array2str(bytes.subarray(0, 8));
  17173. };
  17174. Keyid.prototype.write = function () {
  17175. return _util2.default.str2Uint8Array(this.bytes);
  17176. };
  17177. Keyid.prototype.toHex = function () {
  17178. return _util2.default.hexstrdump(this.bytes);
  17179. };
  17180. Keyid.prototype.equals = function (keyid) {
  17181. return this.bytes === keyid.bytes;
  17182. };
  17183. Keyid.prototype.isNull = function () {
  17184. return this.bytes === '';
  17185. };
  17186. Keyid.mapToHex = function (keyId) {
  17187. return keyId.toHex();
  17188. };
  17189. Keyid.fromClone = function (clone) {
  17190. var keyid = new Keyid();
  17191. keyid.bytes = clone.bytes;
  17192. return keyid;
  17193. };
  17194. Keyid.fromId = function (hex) {
  17195. var keyid = new Keyid();
  17196. keyid.read(_util2.default.str2Uint8Array(_util2.default.hex2bin(hex)));
  17197. return keyid;
  17198. };
  17199. },{"../util.js":70}],68:[function(_dereq_,module,exports){
  17200. // GPG4Browsers - An OpenPGP implementation in javascript
  17201. // Copyright (C) 2011 Recurity Labs GmbH
  17202. //
  17203. // This library is free software; you can redistribute it and/or
  17204. // modify it under the terms of the GNU Lesser General Public
  17205. // License as published by the Free Software Foundation; either
  17206. // version 3.0 of the License, or (at your option) any later version.
  17207. //
  17208. // This library is distributed in the hope that it will be useful,
  17209. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17210. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17211. // Lesser General Public License for more details.
  17212. //
  17213. // You should have received a copy of the GNU Lesser General Public
  17214. // License along with this library; if not, write to the Free Software
  17215. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17216. // Hint: We hold our MPIs as an array of octets in big endian format preceeding a two
  17217. // octet scalar: MPI: [a,b,c,d,e,f]
  17218. // - MPI size: (a << 8) | b
  17219. // - MPI = c | d << 8 | e << ((MPI.length -2)*8) | f ((MPI.length -2)*8)
  17220. /**
  17221. * Implementation of type MPI ({@link http://tools.ietf.org/html/rfc4880#section-3.2|RFC4880 3.2})<br/>
  17222. * <br/>
  17223. * Multiprecision integers (also called MPIs) are unsigned integers used
  17224. * to hold large integers such as the ones used in cryptographic
  17225. * calculations.
  17226. * An MPI consists of two pieces: a two-octet scalar that is the length
  17227. * of the MPI in bits followed by a string of octets that contain the
  17228. * actual integer.
  17229. * @requires crypto/public_key/jsbn
  17230. * @requires util
  17231. * @module type/mpi
  17232. */
  17233. 'use strict';
  17234. Object.defineProperty(exports, "__esModule", {
  17235. value: true
  17236. });
  17237. exports.default = MPI;
  17238. var _jsbn = _dereq_('../crypto/public_key/jsbn.js');
  17239. var _jsbn2 = _interopRequireDefault(_jsbn);
  17240. var _util = _dereq_('../util.js');
  17241. var _util2 = _interopRequireDefault(_util);
  17242. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  17243. /**
  17244. * @constructor
  17245. */
  17246. function MPI() {
  17247. /** An implementation dependent integer */
  17248. this.data = null;
  17249. }
  17250. /**
  17251. * Parsing function for a mpi ({@link http://tools.ietf.org/html/rfc4880#section3.2|RFC 4880 3.2}).
  17252. * @param {String} input Payload of mpi data
  17253. * @return {Integer} Length of data read
  17254. */
  17255. MPI.prototype.read = function (bytes) {
  17256. if (typeof bytes === 'string' || String.prototype.isPrototypeOf(bytes)) {
  17257. bytes = _util2.default.str2Uint8Array(bytes);
  17258. }
  17259. var bits = bytes[0] << 8 | bytes[1];
  17260. // Additional rules:
  17261. //
  17262. // The size of an MPI is ((MPI.length + 7) / 8) + 2 octets.
  17263. //
  17264. // The length field of an MPI describes the length starting from its
  17265. // most significant non-zero bit. Thus, the MPI [00 02 01] is not
  17266. // formed correctly. It should be [00 01 01].
  17267. // TODO: Verification of this size method! This size calculation as
  17268. // specified above is not applicable in JavaScript
  17269. var bytelen = Math.ceil(bits / 8);
  17270. var raw = _util2.default.Uint8Array2str(bytes.subarray(2, 2 + bytelen));
  17271. this.fromBytes(raw);
  17272. return 2 + bytelen;
  17273. };
  17274. MPI.prototype.fromBytes = function (bytes) {
  17275. this.data = new _jsbn2.default(_util2.default.hexstrdump(bytes), 16);
  17276. };
  17277. MPI.prototype.toBytes = function () {
  17278. var bytes = _util2.default.Uint8Array2str(this.write());
  17279. return bytes.substr(2);
  17280. };
  17281. MPI.prototype.byteLength = function () {
  17282. return this.toBytes().length;
  17283. };
  17284. /**
  17285. * Converts the mpi object to a bytes as specified in {@link http://tools.ietf.org/html/rfc4880#section-3.2|RFC4880 3.2}
  17286. * @return {Uint8Aray} mpi Byte representation
  17287. */
  17288. MPI.prototype.write = function () {
  17289. return _util2.default.str2Uint8Array(this.data.toMPI());
  17290. };
  17291. MPI.prototype.toBigInteger = function () {
  17292. return this.data.clone();
  17293. };
  17294. MPI.prototype.fromBigInteger = function (bn) {
  17295. this.data = bn.clone();
  17296. };
  17297. MPI.fromClone = function (clone) {
  17298. clone.data.copyTo = _jsbn2.default.prototype.copyTo;
  17299. var bn = new _jsbn2.default();
  17300. clone.data.copyTo(bn);
  17301. var mpi = new MPI();
  17302. mpi.data = bn;
  17303. return mpi;
  17304. };
  17305. },{"../crypto/public_key/jsbn.js":29,"../util.js":70}],69:[function(_dereq_,module,exports){
  17306. // GPG4Browsers - An OpenPGP implementation in javascript
  17307. // Copyright (C) 2011 Recurity Labs GmbH
  17308. //
  17309. // This library is free software; you can redistribute it and/or
  17310. // modify it under the terms of the GNU Lesser General Public
  17311. // License as published by the Free Software Foundation; either
  17312. // version 3.0 of the License, or (at your option) any later version.
  17313. //
  17314. // This library is distributed in the hope that it will be useful,
  17315. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17316. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17317. // Lesser General Public License for more details.
  17318. //
  17319. // You should have received a copy of the GNU Lesser General Public
  17320. // License along with this library; if not, write to the Free Software
  17321. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17322. /**
  17323. * Implementation of the String-to-key specifier ({@link http://tools.ietf.org/html/rfc4880#section-3.7|RFC4880 3.7})<br/>
  17324. * <br/>
  17325. * String-to-key (S2K) specifiers are used to convert passphrase strings
  17326. * into symmetric-key encryption/decryption keys. They are used in two
  17327. * places, currently: to encrypt the secret part of private keys in the
  17328. * private keyring, and to convert passphrases to encryption keys for
  17329. * symmetrically encrypted messages.
  17330. * @requires crypto
  17331. * @requires enums
  17332. * @requires util
  17333. * @module type/s2k
  17334. */
  17335. 'use strict';
  17336. Object.defineProperty(exports, "__esModule", {
  17337. value: true
  17338. });
  17339. exports.default = S2K;
  17340. var _enums = _dereq_('../enums.js');
  17341. var _enums2 = _interopRequireDefault(_enums);
  17342. var _util = _dereq_('../util.js');
  17343. var _util2 = _interopRequireDefault(_util);
  17344. var _crypto = _dereq_('../crypto');
  17345. var _crypto2 = _interopRequireDefault(_crypto);
  17346. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  17347. /**
  17348. * @constructor
  17349. */
  17350. function S2K() {
  17351. /** @type {module:enums.hash} */
  17352. this.algorithm = 'sha256';
  17353. /** @type {module:enums.s2k} */
  17354. this.type = 'iterated';
  17355. this.c = 96;
  17356. /** Eight bytes of salt in a binary string.
  17357. * @type {String}
  17358. */
  17359. this.salt = _crypto2.default.random.getRandomBytes(8);
  17360. }
  17361. S2K.prototype.get_count = function () {
  17362. // Exponent bias, defined in RFC4880
  17363. var expbias = 6;
  17364. return 16 + (this.c & 15) << (this.c >> 4) + expbias;
  17365. };
  17366. /**
  17367. * Parsing function for a string-to-key specifier ({@link http://tools.ietf.org/html/rfc4880#section-3.7|RFC 4880 3.7}).
  17368. * @param {String} input Payload of string-to-key specifier
  17369. * @return {Integer} Actual length of the object
  17370. */
  17371. S2K.prototype.read = function (bytes) {
  17372. var i = 0;
  17373. this.type = _enums2.default.read(_enums2.default.s2k, bytes[i++]);
  17374. this.algorithm = _enums2.default.read(_enums2.default.hash, bytes[i++]);
  17375. switch (this.type) {
  17376. case 'simple':
  17377. break;
  17378. case 'salted':
  17379. this.salt = bytes.subarray(i, i + 8);
  17380. i += 8;
  17381. break;
  17382. case 'iterated':
  17383. this.salt = bytes.subarray(i, i + 8);
  17384. i += 8;
  17385. // Octet 10: count, a one-octet, coded value
  17386. this.c = bytes[i++];
  17387. break;
  17388. case 'gnu':
  17389. if (_util2.default.Uint8Array2str(bytes.subarray(i, 3)) === "GNU") {
  17390. i += 3; // GNU
  17391. var gnuExtType = 1000 + bytes[i++];
  17392. if (gnuExtType === 1001) {
  17393. this.type = gnuExtType;
  17394. // GnuPG extension mode 1001 -- don't write secret key at all
  17395. } else {
  17396. throw new Error("Unknown s2k gnu protection mode.");
  17397. }
  17398. } else {
  17399. throw new Error("Unknown s2k type.");
  17400. }
  17401. break;
  17402. default:
  17403. throw new Error("Unknown s2k type.");
  17404. }
  17405. return i;
  17406. };
  17407. /**
  17408. * Serializes s2k information
  17409. * @return {Uint8Array} binary representation of s2k
  17410. */
  17411. S2K.prototype.write = function () {
  17412. var arr = [new Uint8Array([_enums2.default.write(_enums2.default.s2k, this.type), _enums2.default.write(_enums2.default.hash, this.algorithm)])];
  17413. switch (this.type) {
  17414. case 'simple':
  17415. break;
  17416. case 'salted':
  17417. arr.push(this.salt);
  17418. break;
  17419. case 'iterated':
  17420. arr.push(this.salt);
  17421. arr.push(new Uint8Array([this.c]));
  17422. break;
  17423. case 'gnu':
  17424. throw new Error("GNU s2k type not supported.");
  17425. default:
  17426. throw new Error("Unknown s2k type.");
  17427. }
  17428. return _util2.default.concatUint8Array(arr);
  17429. };
  17430. /**
  17431. * Produces a key using the specified passphrase and the defined
  17432. * hashAlgorithm
  17433. * @param {String} passphrase Passphrase containing user input
  17434. * @return {Uint8Array} Produced key with a length corresponding to
  17435. * hashAlgorithm hash length
  17436. */
  17437. S2K.prototype.produce_key = function (passphrase, numBytes) {
  17438. passphrase = _util2.default.str2Uint8Array(_util2.default.encode_utf8(passphrase));
  17439. function round(prefix, s2k) {
  17440. var algorithm = _enums2.default.write(_enums2.default.hash, s2k.algorithm);
  17441. switch (s2k.type) {
  17442. case 'simple':
  17443. return _crypto2.default.hash.digest(algorithm, _util2.default.concatUint8Array([prefix, passphrase]));
  17444. case 'salted':
  17445. return _crypto2.default.hash.digest(algorithm, _util2.default.concatUint8Array([prefix, s2k.salt, passphrase]));
  17446. case 'iterated':
  17447. var isp = [],
  17448. count = s2k.get_count(),
  17449. data = _util2.default.concatUint8Array([s2k.salt, passphrase]);
  17450. while (isp.length * data.length < count) {
  17451. isp.push(data);
  17452. }
  17453. isp = _util2.default.concatUint8Array(isp);
  17454. if (isp.length > count) {
  17455. isp = isp.subarray(0, count);
  17456. }
  17457. return _crypto2.default.hash.digest(algorithm, _util2.default.concatUint8Array([prefix, isp]));
  17458. case 'gnu':
  17459. throw new Error("GNU s2k type not supported.");
  17460. default:
  17461. throw new Error("Unknown s2k type.");
  17462. }
  17463. }
  17464. var arr = [],
  17465. rlength = 0,
  17466. prefix = new Uint8Array(numBytes);
  17467. for (var i = 0; i < numBytes; i++) {
  17468. prefix[i] = 0;
  17469. }
  17470. i = 0;
  17471. while (rlength < numBytes) {
  17472. var result = round(prefix.subarray(0, i), this);
  17473. arr.push(result);
  17474. rlength += result.length;
  17475. i++;
  17476. }
  17477. return _util2.default.concatUint8Array(arr).subarray(0, numBytes);
  17478. };
  17479. S2K.fromClone = function (clone) {
  17480. var s2k = new S2K();
  17481. s2k.algorithm = clone.algorithm;
  17482. s2k.type = clone.type;
  17483. s2k.c = clone.c;
  17484. s2k.salt = clone.salt;
  17485. return s2k;
  17486. };
  17487. },{"../crypto":24,"../enums.js":35,"../util.js":70}],70:[function(_dereq_,module,exports){
  17488. // GPG4Browsers - An OpenPGP implementation in javascript
  17489. // Copyright (C) 2011 Recurity Labs GmbH
  17490. //
  17491. // This library is free software; you can redistribute it and/or
  17492. // modify it under the terms of the GNU Lesser General Public
  17493. // License as published by the Free Software Foundation; either
  17494. // version 3.0 of the License, or (at your option) any later version.
  17495. //
  17496. // This library is distributed in the hope that it will be useful,
  17497. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  17498. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  17499. // Lesser General Public License for more details.
  17500. //
  17501. // You should have received a copy of the GNU Lesser General Public
  17502. // License along with this library; if not, write to the Free Software
  17503. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  17504. /**
  17505. * This object contains utility functions
  17506. * @requires config
  17507. * @module util
  17508. */
  17509. 'use strict';
  17510. Object.defineProperty(exports, "__esModule", {
  17511. value: true
  17512. });
  17513. var _config = _dereq_('./config');
  17514. var _config2 = _interopRequireDefault(_config);
  17515. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  17516. exports.default = {
  17517. isString: function isString(data) {
  17518. return typeof data === 'string' || String.prototype.isPrototypeOf(data);
  17519. },
  17520. isArray: function isArray(data) {
  17521. return Array.prototype.isPrototypeOf(data);
  17522. },
  17523. isUint8Array: function isUint8Array(data) {
  17524. return Uint8Array.prototype.isPrototypeOf(data);
  17525. },
  17526. isEmailAddress: function isEmailAddress(data) {
  17527. if (!this.isString(data)) {
  17528. return false;
  17529. }
  17530. var re = /^(([^<>()[\]\\.,;:\s@\"]+(\.[^<>()[\]\\.,;:\s@\"]+)*)|(\".+\"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/;
  17531. return re.test(data);
  17532. },
  17533. isUserId: function isUserId(data) {
  17534. if (!this.isString(data)) {
  17535. return false;
  17536. }
  17537. return (/</.test(data) && />$/.test(data)
  17538. );
  17539. },
  17540. /**
  17541. * Get transferable objects to pass buffers with zero copy (similar to "pass by reference" in C++)
  17542. * See: https://developer.mozilla.org/en-US/docs/Web/API/Worker/postMessage
  17543. * @param {Object} obj the options object to be passed to the web worker
  17544. * @return {Array<ArrayBuffer>} an array of binary data to be passed
  17545. */
  17546. getTransferables: function getTransferables(obj) {
  17547. if (_config2.default.zero_copy && Object.prototype.isPrototypeOf(obj)) {
  17548. var transferables = [];
  17549. this.collectBuffers(obj, transferables);
  17550. return transferables.length ? transferables : undefined;
  17551. }
  17552. },
  17553. collectBuffers: function collectBuffers(obj, collection) {
  17554. if (!obj) {
  17555. return;
  17556. }
  17557. if (this.isUint8Array(obj) && collection.indexOf(obj.buffer) === -1) {
  17558. collection.push(obj.buffer);
  17559. return;
  17560. }
  17561. if (Object.prototype.isPrototypeOf(obj)) {
  17562. for (var key in obj) {
  17563. // recursively search all children
  17564. this.collectBuffers(obj[key], collection);
  17565. }
  17566. }
  17567. },
  17568. readNumber: function readNumber(bytes) {
  17569. var n = 0;
  17570. for (var i = 0; i < bytes.length; i++) {
  17571. n <<= 8;
  17572. n += bytes[i];
  17573. }
  17574. return n;
  17575. },
  17576. writeNumber: function writeNumber(n, bytes) {
  17577. var b = new Uint8Array(bytes);
  17578. for (var i = 0; i < bytes; i++) {
  17579. b[i] = n >> 8 * (bytes - i - 1) & 0xFF;
  17580. }
  17581. return b;
  17582. },
  17583. readDate: function readDate(bytes) {
  17584. var n = this.readNumber(bytes);
  17585. var d = new Date();
  17586. d.setTime(n * 1000);
  17587. return d;
  17588. },
  17589. writeDate: function writeDate(time) {
  17590. var numeric = Math.round(time.getTime() / 1000);
  17591. return this.writeNumber(numeric, 4);
  17592. },
  17593. hexdump: function hexdump(str) {
  17594. var r = [];
  17595. var e = str.length;
  17596. var c = 0;
  17597. var h;
  17598. var i = 0;
  17599. while (c < e) {
  17600. h = str.charCodeAt(c++).toString(16);
  17601. while (h.length < 2) {
  17602. h = "0" + h;
  17603. }
  17604. r.push(" " + h);
  17605. i++;
  17606. if (i % 32 === 0) {
  17607. r.push("\n ");
  17608. }
  17609. }
  17610. return r.join('');
  17611. },
  17612. /**
  17613. * Create hexstring from a binary
  17614. * @param {String} str String to convert
  17615. * @return {String} String containing the hexadecimal values
  17616. */
  17617. hexstrdump: function hexstrdump(str) {
  17618. if (str === null) {
  17619. return "";
  17620. }
  17621. var r = [];
  17622. var e = str.length;
  17623. var c = 0;
  17624. var h;
  17625. while (c < e) {
  17626. h = str.charCodeAt(c++).toString(16);
  17627. while (h.length < 2) {
  17628. h = "0" + h;
  17629. }
  17630. r.push("" + h);
  17631. }
  17632. return r.join('');
  17633. },
  17634. /**
  17635. * Create binary string from a hex encoded string
  17636. * @param {String} str Hex string to convert
  17637. * @return {String} String containing the binary values
  17638. */
  17639. hex2bin: function hex2bin(hex) {
  17640. var str = '';
  17641. for (var i = 0; i < hex.length; i += 2) {
  17642. str += String.fromCharCode(parseInt(hex.substr(i, 2), 16));
  17643. }
  17644. return str;
  17645. },
  17646. /**
  17647. * Creating a hex string from an binary array of integers (0..255)
  17648. * @param {String} str Array of bytes to convert
  17649. * @return {String} Hexadecimal representation of the array
  17650. */
  17651. hexidump: function hexidump(str) {
  17652. var r = [];
  17653. var e = str.length;
  17654. var c = 0;
  17655. var h;
  17656. while (c < e) {
  17657. h = str[c++].toString(16);
  17658. while (h.length < 2) {
  17659. h = "0" + h;
  17660. }
  17661. r.push("" + h);
  17662. }
  17663. return r.join('');
  17664. },
  17665. /**
  17666. * Convert a native javascript string to a string of utf8 bytes
  17667. * @param {String} str The string to convert
  17668. * @return {String} A valid squence of utf8 bytes
  17669. */
  17670. encode_utf8: function encode_utf8(str) {
  17671. return unescape(encodeURIComponent(str));
  17672. },
  17673. /**
  17674. * Convert a string of utf8 bytes to a native javascript string
  17675. * @param {String} utf8 A valid squence of utf8 bytes
  17676. * @return {String} A native javascript string
  17677. */
  17678. decode_utf8: function decode_utf8(utf8) {
  17679. if (typeof utf8 !== 'string') {
  17680. throw new Error('Parameter "utf8" is not of type string');
  17681. }
  17682. try {
  17683. return decodeURIComponent(escape(utf8));
  17684. } catch (e) {
  17685. return utf8;
  17686. }
  17687. },
  17688. /**
  17689. * Convert an array of integers(0.255) to a string
  17690. * @param {Array<Integer>} bin An array of (binary) integers to convert
  17691. * @return {String} The string representation of the array
  17692. */
  17693. bin2str: function bin2str(bin) {
  17694. var result = [];
  17695. for (var i = 0; i < bin.length; i++) {
  17696. result[i] = String.fromCharCode(bin[i]);
  17697. }
  17698. return result.join('');
  17699. },
  17700. /**
  17701. * Convert a string to an array of integers(0.255)
  17702. * @param {String} str String to convert
  17703. * @return {Array<Integer>} An array of (binary) integers
  17704. */
  17705. str2bin: function str2bin(str) {
  17706. var result = [];
  17707. for (var i = 0; i < str.length; i++) {
  17708. result[i] = str.charCodeAt(i);
  17709. }
  17710. return result;
  17711. },
  17712. /**
  17713. * Convert a string to a Uint8Array
  17714. * @param {String} str String to convert
  17715. * @return {Uint8Array} The array of (binary) integers
  17716. */
  17717. str2Uint8Array: function str2Uint8Array(str) {
  17718. if (typeof str !== 'string' && !String.prototype.isPrototypeOf(str)) {
  17719. throw new Error('str2Uint8Array: Data must be in the form of a string');
  17720. }
  17721. var result = new Uint8Array(str.length);
  17722. for (var i = 0; i < str.length; i++) {
  17723. result[i] = str.charCodeAt(i);
  17724. }
  17725. return result;
  17726. },
  17727. /**
  17728. * Convert a Uint8Array to a string. This currently functions
  17729. * the same as bin2str.
  17730. * @function module:util.Uint8Array2str
  17731. * @param {Uint8Array} bin An array of (binary) integers to convert
  17732. * @return {String} String representation of the array
  17733. */
  17734. Uint8Array2str: function Uint8Array2str(bin) {
  17735. if (!Uint8Array.prototype.isPrototypeOf(bin)) {
  17736. throw new Error('Uint8Array2str: Data must be in the form of a Uint8Array');
  17737. }
  17738. var result = [],
  17739. bs = 16384,
  17740. j = bin.length;
  17741. for (var i = 0; i < j; i += bs) {
  17742. result.push(String.fromCharCode.apply(String, bin.subarray(i, i + bs < j ? i + bs : j)));
  17743. }
  17744. return result.join('');
  17745. },
  17746. /**
  17747. * Concat Uint8arrays
  17748. * @function module:util.concatUint8Array
  17749. * @param {Array<Uint8array>} Array of Uint8Arrays to concatenate
  17750. * @return {Uint8array} Concatenated array
  17751. */
  17752. concatUint8Array: function concatUint8Array(arrays) {
  17753. var totalLength = 0;
  17754. arrays.forEach(function (element) {
  17755. if (!Uint8Array.prototype.isPrototypeOf(element)) {
  17756. throw new Error('concatUint8Array: Data must be in the form of a Uint8Array');
  17757. }
  17758. totalLength += element.length;
  17759. });
  17760. var result = new Uint8Array(totalLength);
  17761. var pos = 0;
  17762. arrays.forEach(function (element) {
  17763. result.set(element, pos);
  17764. pos += element.length;
  17765. });
  17766. return result;
  17767. },
  17768. /**
  17769. * Deep copy Uint8Array
  17770. * @function module:util.copyUint8Array
  17771. * @param {Uint8Array} Array to copy
  17772. * @return {Uint8Array} new Uint8Array
  17773. */
  17774. copyUint8Array: function copyUint8Array(array) {
  17775. if (!Uint8Array.prototype.isPrototypeOf(array)) {
  17776. throw new Error('Data must be in the form of a Uint8Array');
  17777. }
  17778. var copy = new Uint8Array(array.length);
  17779. copy.set(array);
  17780. return copy;
  17781. },
  17782. /**
  17783. * Check Uint8Array equality
  17784. * @function module:util.equalsUint8Array
  17785. * @param {Uint8Array} first array
  17786. * @param {Uint8Array} second array
  17787. * @return {Boolean} equality
  17788. */
  17789. equalsUint8Array: function equalsUint8Array(array1, array2) {
  17790. if (!Uint8Array.prototype.isPrototypeOf(array1) || !Uint8Array.prototype.isPrototypeOf(array2)) {
  17791. throw new Error('Data must be in the form of a Uint8Array');
  17792. }
  17793. if (array1.length !== array2.length) {
  17794. return false;
  17795. }
  17796. for (var i = 0; i < array1.length; i++) {
  17797. if (array1[i] !== array2[i]) {
  17798. return false;
  17799. }
  17800. }
  17801. return true;
  17802. },
  17803. /**
  17804. * Calculates a 16bit sum of a Uint8Array by adding each character
  17805. * codes modulus 65535
  17806. * @param {Uint8Array} Uint8Array to create a sum of
  17807. * @return {Integer} An integer containing the sum of all character
  17808. * codes % 65535
  17809. */
  17810. calc_checksum: function calc_checksum(text) {
  17811. var checksum = {
  17812. s: 0,
  17813. add: function add(sadd) {
  17814. this.s = (this.s + sadd) % 65536;
  17815. }
  17816. };
  17817. for (var i = 0; i < text.length; i++) {
  17818. checksum.add(text[i]);
  17819. }
  17820. return checksum.s;
  17821. },
  17822. /**
  17823. * Helper function to print a debug message. Debug
  17824. * messages are only printed if
  17825. * @link module:config/config.debug is set to true.
  17826. * @param {String} str String of the debug message
  17827. */
  17828. print_debug: function print_debug(str) {
  17829. if (_config2.default.debug) {
  17830. console.log(str);
  17831. }
  17832. },
  17833. /**
  17834. * Helper function to print a debug message. Debug
  17835. * messages are only printed if
  17836. * @link module:config/config.debug is set to true.
  17837. * Different than print_debug because will call hexstrdump iff necessary.
  17838. * @param {String} str String of the debug message
  17839. */
  17840. print_debug_hexstr_dump: function print_debug_hexstr_dump(str, strToHex) {
  17841. if (_config2.default.debug) {
  17842. str = str + this.hexstrdump(strToHex);
  17843. console.log(str);
  17844. }
  17845. },
  17846. getLeftNBits: function getLeftNBits(string, bitcount) {
  17847. var rest = bitcount % 8;
  17848. if (rest === 0) {
  17849. return string.substring(0, bitcount / 8);
  17850. }
  17851. var bytes = (bitcount - rest) / 8 + 1;
  17852. var result = string.substring(0, bytes);
  17853. return this.shiftRight(result, 8 - rest); // +String.fromCharCode(string.charCodeAt(bytes -1) << (8-rest) & 0xFF);
  17854. },
  17855. /**
  17856. * Shifting a string to n bits right
  17857. * @param {String} value The string to shift
  17858. * @param {Integer} bitcount Amount of bits to shift (MUST be smaller
  17859. * than 9)
  17860. * @return {String} Resulting string.
  17861. */
  17862. shiftRight: function shiftRight(value, bitcount) {
  17863. var temp = this.str2bin(value);
  17864. if (bitcount % 8 !== 0) {
  17865. for (var i = temp.length - 1; i >= 0; i--) {
  17866. temp[i] >>= bitcount % 8;
  17867. if (i > 0) {
  17868. temp[i] |= temp[i - 1] << 8 - bitcount % 8 & 0xFF;
  17869. }
  17870. }
  17871. } else {
  17872. return value;
  17873. }
  17874. return this.bin2str(temp);
  17875. },
  17876. /**
  17877. * Return the algorithm type as string
  17878. * @return {String} String representing the message type
  17879. */
  17880. get_hashAlgorithmString: function get_hashAlgorithmString(algo) {
  17881. switch (algo) {
  17882. case 1:
  17883. return "MD5";
  17884. case 2:
  17885. return "SHA1";
  17886. case 3:
  17887. return "RIPEMD160";
  17888. case 8:
  17889. return "SHA256";
  17890. case 9:
  17891. return "SHA384";
  17892. case 10:
  17893. return "SHA512";
  17894. case 11:
  17895. return "SHA224";
  17896. }
  17897. return "unknown";
  17898. },
  17899. /**
  17900. * Get native Web Cryptography api, only the current version of the spec.
  17901. * The default configuration is to use the api when available. But it can
  17902. * be deactivated with config.use_native
  17903. * @return {Object} The SubtleCrypto api or 'undefined'
  17904. */
  17905. getWebCrypto: function getWebCrypto() {
  17906. if (!_config2.default.use_native) {
  17907. return;
  17908. }
  17909. return typeof window !== 'undefined' && window.crypto && window.crypto.subtle;
  17910. },
  17911. /**
  17912. * Get native Web Cryptography api for all browsers, including legacy
  17913. * implementations of the spec e.g IE11 and Safari 8/9. The default
  17914. * configuration is to use the api when available. But it can be deactivated
  17915. * with config.use_native
  17916. * @return {Object} The SubtleCrypto api or 'undefined'
  17917. */
  17918. getWebCryptoAll: function getWebCryptoAll() {
  17919. if (!_config2.default.use_native) {
  17920. return;
  17921. }
  17922. if (typeof window !== 'undefined') {
  17923. if (window.crypto) {
  17924. return window.crypto.subtle || window.crypto.webkitSubtle;
  17925. }
  17926. if (window.msCrypto) {
  17927. return window.msCrypto.subtle;
  17928. }
  17929. }
  17930. },
  17931. /**
  17932. * Wraps a generic synchronous function in an ES6 Promise.
  17933. * @param {Function} fn The function to be wrapped
  17934. * @return {Function} The function wrapped in a Promise
  17935. */
  17936. promisify: function promisify(fn) {
  17937. return function () {
  17938. var args = arguments;
  17939. return new Promise(function (resolve) {
  17940. var result = fn.apply(null, args);
  17941. resolve(result);
  17942. });
  17943. };
  17944. },
  17945. /**
  17946. * Converts an IE11 web crypro api result to a promise.
  17947. * This is required since IE11 implements an old version of the
  17948. * Web Crypto specification that does not use promises.
  17949. * @param {Object} cryptoOp The return value of an IE11 web cryptro api call
  17950. * @param {String} errmsg An error message for a specific operation
  17951. * @return {Promise} The resulting Promise
  17952. */
  17953. promisifyIE11Op: function promisifyIE11Op(cryptoOp, errmsg) {
  17954. return new Promise(function (resolve, reject) {
  17955. cryptoOp.onerror = function () {
  17956. reject(new Error(errmsg));
  17957. };
  17958. cryptoOp.oncomplete = function (e) {
  17959. resolve(e.target.result);
  17960. };
  17961. });
  17962. },
  17963. /**
  17964. * Detect Node.js runtime.
  17965. */
  17966. detectNode: function detectNode() {
  17967. return typeof window === 'undefined';
  17968. },
  17969. /**
  17970. * Get native Node.js crypto api. The default configuration is to use
  17971. * the api when available. But it can also be deactivated with config.use_native
  17972. * @return {Object} The crypto module or 'undefined'
  17973. */
  17974. getNodeCrypto: function getNodeCrypto() {
  17975. if (!this.detectNode() || !_config2.default.use_native) {
  17976. return;
  17977. }
  17978. return _dereq_('crypto');
  17979. },
  17980. /**
  17981. * Get native Node.js Buffer constructor. This should be used since
  17982. * Buffer is not available under browserify.
  17983. * @return {Function} The Buffer constructor or 'undefined'
  17984. */
  17985. getNodeBuffer: function getNodeBuffer() {
  17986. if (!this.detectNode()) {
  17987. return;
  17988. }
  17989. return _dereq_('buffer').Buffer;
  17990. }
  17991. };
  17992. },{"./config":10,"buffer":"buffer","crypto":"crypto"}],71:[function(_dereq_,module,exports){
  17993. // GPG4Browsers - An OpenPGP implementation in javascript
  17994. // Copyright (C) 2011 Recurity Labs GmbH
  17995. //
  17996. // This library is free software; you can redistribute it and/or
  17997. // modify it under the terms of the GNU Lesser General Public
  17998. // License as published by the Free Software Foundation; either
  17999. // version 3.0 of the License, or (at your option) any later version.
  18000. //
  18001. // This library is distributed in the hope that it will be useful,
  18002. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  18003. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  18004. // Lesser General Public License for more details.
  18005. //
  18006. // You should have received a copy of the GNU Lesser General Public
  18007. // License along with this library; if not, write to the Free Software
  18008. // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  18009. 'use strict';
  18010. Object.defineProperty(exports, "__esModule", {
  18011. value: true
  18012. });
  18013. exports.default = AsyncProxy;
  18014. var _util = _dereq_('../util.js');
  18015. var _util2 = _interopRequireDefault(_util);
  18016. var _crypto = _dereq_('../crypto');
  18017. var _crypto2 = _interopRequireDefault(_crypto);
  18018. var _packet = _dereq_('../packet');
  18019. var _packet2 = _interopRequireDefault(_packet);
  18020. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  18021. var INITIAL_RANDOM_SEED = 50000,
  18022. // random bytes seeded to worker
  18023. RANDOM_SEED_REQUEST = 20000; // random bytes seeded after worker request
  18024. /**
  18025. * Initializes a new proxy and loads the web worker
  18026. * @constructor
  18027. * @param {String} path The path to the worker or 'openpgp.worker.js' by default
  18028. * @param {Object} config config The worker configuration
  18029. * @param {Object} worker alternative to path parameter: web worker initialized with 'openpgp.worker.js'
  18030. * @return {Promise}
  18031. */
  18032. function AsyncProxy() {
  18033. var _ref = arguments.length <= 0 || arguments[0] === undefined ? {} : arguments[0];
  18034. var _ref$path = _ref.path;
  18035. var path = _ref$path === undefined ? 'openpgp.worker.js' : _ref$path;
  18036. var worker = _ref.worker;
  18037. var config = _ref.config;
  18038. this.worker = worker || new Worker(path);
  18039. this.worker.onmessage = this.onMessage.bind(this);
  18040. this.worker.onerror = function (e) {
  18041. throw new Error('Unhandled error in openpgp worker: ' + e.message + ' (' + e.filename + ':' + e.lineno + ')');
  18042. };
  18043. this.seedRandom(INITIAL_RANDOM_SEED);
  18044. if (config) {
  18045. this.worker.postMessage({ event: 'configure', config: config });
  18046. }
  18047. // Cannot rely on task order being maintained, use object keyed by request ID to track tasks
  18048. this.tasks = {};
  18049. this.currentID = 0;
  18050. }
  18051. /**
  18052. * Get new request ID
  18053. * @return {integer} New unique request ID
  18054. */
  18055. AsyncProxy.prototype.getID = function () {
  18056. return this.currentID++;
  18057. };
  18058. /**
  18059. * Message handling
  18060. */
  18061. AsyncProxy.prototype.onMessage = function (event) {
  18062. var msg = event.data;
  18063. switch (msg.event) {
  18064. case 'method-return':
  18065. if (msg.err) {
  18066. // fail
  18067. this.tasks[msg.id].reject(new Error(msg.err));
  18068. } else {
  18069. // success
  18070. this.tasks[msg.id].resolve(msg.data);
  18071. }
  18072. delete this.tasks[msg.id];
  18073. break;
  18074. case 'request-seed':
  18075. this.seedRandom(RANDOM_SEED_REQUEST);
  18076. break;
  18077. default:
  18078. throw new Error('Unknown Worker Event.');
  18079. }
  18080. };
  18081. /**
  18082. * Send message to worker with random data
  18083. * @param {Integer} size Number of bytes to send
  18084. */
  18085. AsyncProxy.prototype.seedRandom = function (size) {
  18086. var buf = this.getRandomBuffer(size);
  18087. this.worker.postMessage({ event: 'seed-random', buf: buf }, _util2.default.getTransferables.call(_util2.default, buf));
  18088. };
  18089. /**
  18090. * Get Uint8Array with random numbers
  18091. * @param {Integer} size Length of buffer
  18092. * @return {Uint8Array}
  18093. */
  18094. AsyncProxy.prototype.getRandomBuffer = function (size) {
  18095. if (!size) {
  18096. return null;
  18097. }
  18098. var buf = new Uint8Array(size);
  18099. _crypto2.default.random.getRandomValues(buf);
  18100. return buf;
  18101. };
  18102. /**
  18103. * Terminates the worker
  18104. */
  18105. AsyncProxy.prototype.terminate = function () {
  18106. this.worker.terminate();
  18107. };
  18108. /**
  18109. * Generic proxy function that handles all commands from the public api.
  18110. * @param {String} method the public api function to be delegated to the worker thread
  18111. * @param {Object} options the api function's options
  18112. * @return {Promise} see the corresponding public api functions for their return types
  18113. */
  18114. AsyncProxy.prototype.delegate = function (method, options) {
  18115. var _this = this;
  18116. var id = this.getID();
  18117. return new Promise(function (_resolve, reject) {
  18118. // clone packets (for web worker structured cloning algorithm)
  18119. _this.worker.postMessage({ id: id, event: method, options: _packet2.default.clone.clonePackets(options) }, _util2.default.getTransferables.call(_util2.default, options));
  18120. // remember to handle parsing cloned packets from worker
  18121. _this.tasks[id] = { resolve: function resolve(data) {
  18122. return _resolve(_packet2.default.clone.parseClonedPackets(data, method));
  18123. }, reject: reject };
  18124. });
  18125. };
  18126. },{"../crypto":24,"../packet":47,"../util.js":70}]},{},[37])(37)
  18127. });