crypto-js.js 146 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149415041514152415341544155415641574158415941604161416241634164416541664167416841694170417141724173417441754176417741784179418041814182418341844185418641874188418941904191419241934194419541964197419841994200420142024203420442054206420742084209421042114212421342144215421642174218421942204221422242234224422542264227422842294230423142324233423442354236423742384239424042414242424342444245424642474248424942504251425242534254425542564257425842594260426142624263426442654266426742684269427042714272427342744275427642774278427942804281428242834284428542864287428842894290429142924293429442954296429742984299430043014302430343044305430643074308430943104311431243134314431543164317431843194320432143224323432443254326432743284329433043314332433343344335433643374338433943404341434243434344434543464347434843494350435143524353435443554356435743584359436043614362436343644365436643674368436943704371437243734374437543764377437843794380438143824383438443854386438743884389439043914392439343944395439643974398439944004401440244034404440544064407440844094410441144124413441444154416441744184419442044214422442344244425442644274428442944304431443244334434443544364437443844394440444144424443444444454446444744484449445044514452445344544455445644574458445944604461446244634464446544664467446844694470447144724473447444754476447744784479448044814482448344844485448644874488448944904491449244934494449544964497449844994500450145024503450445054506450745084509451045114512451345144515451645174518451945204521452245234524452545264527452845294530453145324533453445354536453745384539454045414542454345444545454645474548454945504551455245534554455545564557455845594560456145624563456445654566456745684569457045714572457345744575457645774578457945804581458245834584458545864587458845894590459145924593459445954596459745984599460046014602460346044605460646074608460946104611461246134614461546164617461846194620462146224623462446254626462746284629463046314632463346344635463646374638463946404641464246434644464546464647464846494650465146524653465446554656465746584659466046614662466346644665466646674668466946704671467246734674467546764677467846794680468146824683468446854686468746884689469046914692469346944695469646974698469947004701470247034704470547064707470847094710471147124713471447154716471747184719472047214722472347244725472647274728472947304731473247334734473547364737473847394740474147424743474447454746474747484749475047514752475347544755475647574758475947604761476247634764476547664767476847694770477147724773477447754776477747784779478047814782478347844785478647874788478947904791479247934794479547964797479847994800480148024803480448054806480748084809481048114812481348144815481648174818481948204821482248234824482548264827482848294830483148324833483448354836483748384839484048414842484348444845484648474848484948504851485248534854485548564857485848594860486148624863486448654866486748684869487048714872487348744875487648774878487948804881488248834884488548864887488848894890489148924893489448954896489748984899490049014902490349044905490649074908490949104911491249134914491549164917491849194920492149224923492449254926492749284929493049314932493349344935493649374938493949404941494249434944494549464947494849494950495149524953495449554956495749584959496049614962496349644965496649674968496949704971497249734974497549764977497849794980498149824983498449854986498749884989499049914992499349944995499649974998499950005001500250035004500550065007500850095010501150125013501450155016501750185019502050215022502350245025502650275028502950305031503250335034503550365037503850395040504150425043504450455046504750485049505050515052505350545055505650575058505950605061506250635064506550665067506850695070507150725073507450755076507750785079508050815082508350845085508650875088508950905091509250935094509550965097509850995100510151025103510451055106510751085109511051115112511351145115511651175118511951205121512251235124512551265127512851295130513151325133513451355136513751385139514051415142514351445145514651475148514951505151515251535154515551565157515851595160516151625163516451655166516751685169517051715172517351745175517651775178517951805181518251835184518551865187518851895190519151925193519451955196519751985199520052015202520352045205520652075208520952105211521252135214521552165217521852195220522152225223522452255226522752285229523052315232523352345235523652375238523952405241524252435244524552465247524852495250525152525253525452555256525752585259526052615262526352645265526652675268526952705271527252735274527552765277527852795280528152825283528452855286528752885289529052915292529352945295529652975298529953005301530253035304530553065307530853095310531153125313531453155316531753185319532053215322532353245325532653275328532953305331533253335334533553365337533853395340534153425343534453455346534753485349535053515352535353545355535653575358535953605361536253635364536553665367536853695370537153725373537453755376537753785379538053815382538353845385538653875388538953905391539253935394539553965397539853995400540154025403540454055406540754085409541054115412541354145415541654175418541954205421542254235424542554265427542854295430543154325433543454355436543754385439544054415442544354445445544654475448544954505451545254535454545554565457545854595460546154625463546454655466546754685469547054715472547354745475547654775478547954805481548254835484548554865487548854895490549154925493549454955496549754985499550055015502550355045505550655075508550955105511551255135514551555165517551855195520552155225523552455255526552755285529553055315532553355345535553655375538553955405541554255435544554555465547554855495550555155525553555455555556555755585559556055615562556355645565556655675568556955705571557255735574557555765577557855795580558155825583558455855586558755885589559055915592559355945595559655975598559956005601560256035604560556065607560856095610561156125613561456155616561756185619562056215622562356245625562656275628562956305631563256335634563556365637563856395640564156425643564456455646564756485649565056515652565356545655565656575658565956605661566256635664566556665667566856695670567156725673567456755676567756785679568056815682568356845685568656875688568956905691569256935694569556965697569856995700570157025703570457055706570757085709571057115712571357145715571657175718571957205721572257235724572557265727572857295730573157325733573457355736573757385739574057415742574357445745574657475748574957505751575257535754575557565757575857595760576157625763576457655766576757685769577057715772577357745775577657775778577957805781578257835784578557865787578857895790579157925793579457955796579757985799580058015802580358045805580658075808580958105811581258135814581558165817581858195820582158225823582458255826582758285829583058315832583358345835583658375838583958405841584258435844584558465847584858495850585158525853585458555856585758585859586058615862586358645865586658675868586958705871587258735874587558765877587858795880588158825883588458855886588758885889589058915892589358945895589658975898589959005901590259035904590559065907590859095910591159125913591459155916591759185919592059215922592359245925592659275928592959305931593259335934593559365937593859395940594159425943594459455946594759485949595059515952595359545955595659575958595959605961596259635964596559665967596859695970597159725973597459755976597759785979598059815982598359845985598659875988598959905991599259935994599559965997599859996000600160026003600460056006600760086009601060116012601360146015601660176018601960206021602260236024602560266027602860296030
  1. /*globals window, global, require*/
  2. /**
  3. * CryptoJS core components.
  4. */
  5. window.CryptoJS = window.CryptoJS || (function (Math, undefined) {
  6. var crypto;
  7. // Native crypto from window (Browser)
  8. if (typeof window !== 'undefined' && window.crypto) {
  9. crypto = window.crypto;
  10. }
  11. // Native (experimental IE 11) crypto from window (Browser)
  12. if (!crypto && typeof window !== 'undefined' && window.msCrypto) {
  13. crypto = window.msCrypto;
  14. }
  15. // Native crypto from global (NodeJS)
  16. if (!crypto && typeof global !== 'undefined' && global.crypto) {
  17. crypto = global.crypto;
  18. }
  19. // Native crypto import via require (NodeJS)
  20. // if (!crypto && typeof require === 'function') {
  21. // try {
  22. // crypto = require('crypto');
  23. // } catch (err) { }
  24. // }
  25. /*
  26. * Cryptographically secure pseudorandom number generator
  27. *
  28. * As Math.random() is cryptographically not safe to use
  29. */
  30. var cryptoSecureRandomInt = function () {
  31. if (crypto) {
  32. // Use getRandomValues method (Browser)
  33. if (typeof crypto.getRandomValues === 'function') {
  34. try {
  35. return crypto.getRandomValues(new Uint32Array(1))[0];
  36. } catch (err) { }
  37. }
  38. // Use randomBytes method (NodeJS)
  39. if (typeof crypto.randomBytes === 'function') {
  40. try {
  41. return crypto.randomBytes(4).readInt32LE();
  42. } catch (err) { }
  43. }
  44. }
  45. throw new Error('Native crypto module could not be used to get secure random number.');
  46. };
  47. /*
  48. * Local polyfill of Object.create
  49. */
  50. var create = Object.create || (function () {
  51. function F() { }
  52. return function (obj) {
  53. var subtype;
  54. F.prototype = obj;
  55. subtype = new F();
  56. F.prototype = null;
  57. return subtype;
  58. };
  59. }())
  60. /**
  61. * CryptoJS namespace.
  62. */
  63. var C = {};
  64. /**
  65. * Library namespace.
  66. */
  67. var C_lib = C.lib = {};
  68. /**
  69. * Base object for prototypal inheritance.
  70. */
  71. var Base = C_lib.Base = (function () {
  72. return {
  73. /**
  74. * Creates a new object that inherits from this object.
  75. *
  76. * @param {Object} overrides Properties to copy into the new object.
  77. *
  78. * @return {Object} The new object.
  79. *
  80. * @static
  81. *
  82. * @example
  83. *
  84. * var MyType = CryptoJS.lib.Base.extend({
  85. * field: 'value',
  86. *
  87. * method: function () {
  88. * }
  89. * });
  90. */
  91. extend: function (overrides) {
  92. // Spawn
  93. var subtype = create(this);
  94. // Augment
  95. if (overrides) {
  96. subtype.mixIn(overrides);
  97. }
  98. // Create default initializer
  99. if (!subtype.hasOwnProperty('init') || this.init === subtype.init) {
  100. subtype.init = function () {
  101. subtype.$super.init.apply(this, arguments);
  102. };
  103. }
  104. // Initializer's prototype is the subtype object
  105. subtype.init.prototype = subtype;
  106. // Reference supertype
  107. subtype.$super = this;
  108. return subtype;
  109. },
  110. /**
  111. * Extends this object and runs the init method.
  112. * Arguments to create() will be passed to init().
  113. *
  114. * @return {Object} The new object.
  115. *
  116. * @static
  117. *
  118. * @example
  119. *
  120. * var instance = MyType.create();
  121. */
  122. create: function () {
  123. var instance = this.extend();
  124. instance.init.apply(instance, arguments);
  125. return instance;
  126. },
  127. /**
  128. * Initializes a newly created object.
  129. * Override this method to add some logic when your objects are created.
  130. *
  131. * @example
  132. *
  133. * var MyType = CryptoJS.lib.Base.extend({
  134. * init: function () {
  135. * // ...
  136. * }
  137. * });
  138. */
  139. init: function () {
  140. },
  141. /**
  142. * Copies properties into this object.
  143. *
  144. * @param {Object} properties The properties to mix in.
  145. *
  146. * @example
  147. *
  148. * MyType.mixIn({
  149. * field: 'value'
  150. * });
  151. */
  152. mixIn: function (properties) {
  153. for (var propertyName in properties) {
  154. if (properties.hasOwnProperty(propertyName)) {
  155. this[propertyName] = properties[propertyName];
  156. }
  157. }
  158. // IE won't copy toString using the loop above
  159. if (properties.hasOwnProperty('toString')) {
  160. this.toString = properties.toString;
  161. }
  162. },
  163. /**
  164. * Creates a copy of this object.
  165. *
  166. * @return {Object} The clone.
  167. *
  168. * @example
  169. *
  170. * var clone = instance.clone();
  171. */
  172. clone: function () {
  173. return this.init.prototype.extend(this);
  174. }
  175. };
  176. }());
  177. /**
  178. * An array of 32-bit words.
  179. *
  180. * @property {Array} words The array of 32-bit words.
  181. * @property {number} sigBytes The number of significant bytes in this word array.
  182. */
  183. var WordArray = C_lib.WordArray = Base.extend({
  184. /**
  185. * Initializes a newly created word array.
  186. *
  187. * @param {Array} words (Optional) An array of 32-bit words.
  188. * @param {number} sigBytes (Optional) The number of significant bytes in the words.
  189. *
  190. * @example
  191. *
  192. * var wordArray = CryptoJS.lib.WordArray.create();
  193. * var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607]);
  194. * var wordArray = CryptoJS.lib.WordArray.create([0x00010203, 0x04050607], 6);
  195. */
  196. init: function (words, sigBytes) {
  197. words = this.words = words || [];
  198. if (sigBytes != undefined) {
  199. this.sigBytes = sigBytes;
  200. } else {
  201. this.sigBytes = words.length * 4;
  202. }
  203. },
  204. /**
  205. * Converts this word array to a string.
  206. *
  207. * @param {Encoder} encoder (Optional) The encoding strategy to use. Default: CryptoJS.enc.Hex
  208. *
  209. * @return {string} The stringified word array.
  210. *
  211. * @example
  212. *
  213. * var string = wordArray + '';
  214. * var string = wordArray.toString();
  215. * var string = wordArray.toString(CryptoJS.enc.Utf8);
  216. */
  217. toString: function (encoder) {
  218. return (encoder || Hex).stringify(this);
  219. },
  220. /**
  221. * Concatenates a word array to this word array.
  222. *
  223. * @param {WordArray} wordArray The word array to append.
  224. *
  225. * @return {WordArray} This word array.
  226. *
  227. * @example
  228. *
  229. * wordArray1.concat(wordArray2);
  230. */
  231. concat: function (wordArray) {
  232. // Shortcuts
  233. var thisWords = this.words;
  234. var thatWords = wordArray.words;
  235. var thisSigBytes = this.sigBytes;
  236. var thatSigBytes = wordArray.sigBytes;
  237. // Clamp excess bits
  238. this.clamp();
  239. // Concat
  240. if (thisSigBytes % 4) {
  241. // Copy one byte at a time
  242. for (var i = 0; i < thatSigBytes; i++) {
  243. var thatByte = (thatWords[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
  244. thisWords[(thisSigBytes + i) >>> 2] |= thatByte << (24 - ((thisSigBytes + i) % 4) * 8);
  245. }
  246. } else {
  247. // Copy one word at a time
  248. for (var i = 0; i < thatSigBytes; i += 4) {
  249. thisWords[(thisSigBytes + i) >>> 2] = thatWords[i >>> 2];
  250. }
  251. }
  252. this.sigBytes += thatSigBytes;
  253. // Chainable
  254. return this;
  255. },
  256. /**
  257. * Removes insignificant bits.
  258. *
  259. * @example
  260. *
  261. * wordArray.clamp();
  262. */
  263. clamp: function () {
  264. // Shortcuts
  265. var words = this.words;
  266. var sigBytes = this.sigBytes;
  267. // Clamp
  268. words[sigBytes >>> 2] &= 0xffffffff << (32 - (sigBytes % 4) * 8);
  269. words.length = Math.ceil(sigBytes / 4);
  270. },
  271. /**
  272. * Creates a copy of this word array.
  273. *
  274. * @return {WordArray} The clone.
  275. *
  276. * @example
  277. *
  278. * var clone = wordArray.clone();
  279. */
  280. clone: function () {
  281. var clone = Base.clone.call(this);
  282. clone.words = this.words.slice(0);
  283. return clone;
  284. },
  285. /**
  286. * Creates a word array filled with random bytes.
  287. *
  288. * @param {number} nBytes The number of random bytes to generate.
  289. *
  290. * @return {WordArray} The random word array.
  291. *
  292. * @static
  293. *
  294. * @example
  295. *
  296. * var wordArray = CryptoJS.lib.WordArray.random(16);
  297. */
  298. random: function (nBytes) {
  299. var words = [];
  300. for (var i = 0; i < nBytes; i += 4) {
  301. words.push(cryptoSecureRandomInt());
  302. }
  303. return new WordArray.init(words, nBytes);
  304. }
  305. });
  306. /**
  307. * Encoder namespace.
  308. */
  309. var C_enc = C.enc = {};
  310. /**
  311. * Hex encoding strategy.
  312. */
  313. var Hex = C_enc.Hex = {
  314. /**
  315. * Converts a word array to a hex string.
  316. *
  317. * @param {WordArray} wordArray The word array.
  318. *
  319. * @return {string} The hex string.
  320. *
  321. * @static
  322. *
  323. * @example
  324. *
  325. * var hexString = CryptoJS.enc.Hex.stringify(wordArray);
  326. */
  327. stringify: function (wordArray) {
  328. // Shortcuts
  329. var words = wordArray.words;
  330. var sigBytes = wordArray.sigBytes;
  331. // Convert
  332. var hexChars = [];
  333. for (var i = 0; i < sigBytes; i++) {
  334. var bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
  335. hexChars.push((bite >>> 4).toString(16));
  336. hexChars.push((bite & 0x0f).toString(16));
  337. }
  338. return hexChars.join('');
  339. },
  340. /**
  341. * Converts a hex string to a word array.
  342. *
  343. * @param {string} hexStr The hex string.
  344. *
  345. * @return {WordArray} The word array.
  346. *
  347. * @static
  348. *
  349. * @example
  350. *
  351. * var wordArray = CryptoJS.enc.Hex.parse(hexString);
  352. */
  353. parse: function (hexStr) {
  354. // Shortcut
  355. var hexStrLength = hexStr.length;
  356. // Convert
  357. var words = [];
  358. for (var i = 0; i < hexStrLength; i += 2) {
  359. words[i >>> 3] |= parseInt(hexStr.substr(i, 2), 16) << (24 - (i % 8) * 4);
  360. }
  361. return new WordArray.init(words, hexStrLength / 2);
  362. }
  363. };
  364. /**
  365. * Latin1 encoding strategy.
  366. */
  367. var Latin1 = C_enc.Latin1 = {
  368. /**
  369. * Converts a word array to a Latin1 string.
  370. *
  371. * @param {WordArray} wordArray The word array.
  372. *
  373. * @return {string} The Latin1 string.
  374. *
  375. * @static
  376. *
  377. * @example
  378. *
  379. * var latin1String = CryptoJS.enc.Latin1.stringify(wordArray);
  380. */
  381. stringify: function (wordArray) {
  382. // Shortcuts
  383. var words = wordArray.words;
  384. var sigBytes = wordArray.sigBytes;
  385. // Convert
  386. var latin1Chars = [];
  387. for (var i = 0; i < sigBytes; i++) {
  388. var bite = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
  389. latin1Chars.push(String.fromCharCode(bite));
  390. }
  391. return latin1Chars.join('');
  392. },
  393. /**
  394. * Converts a Latin1 string to a word array.
  395. *
  396. * @param {string} latin1Str The Latin1 string.
  397. *
  398. * @return {WordArray} The word array.
  399. *
  400. * @static
  401. *
  402. * @example
  403. *
  404. * var wordArray = CryptoJS.enc.Latin1.parse(latin1String);
  405. */
  406. parse: function (latin1Str) {
  407. // Shortcut
  408. var latin1StrLength = latin1Str.length;
  409. // Convert
  410. var words = [];
  411. for (var i = 0; i < latin1StrLength; i++) {
  412. words[i >>> 2] |= (latin1Str.charCodeAt(i) & 0xff) << (24 - (i % 4) * 8);
  413. }
  414. return new WordArray.init(words, latin1StrLength);
  415. }
  416. };
  417. /**
  418. * UTF-8 encoding strategy.
  419. */
  420. var Utf8 = C_enc.Utf8 = {
  421. /**
  422. * Converts a word array to a UTF-8 string.
  423. *
  424. * @param {WordArray} wordArray The word array.
  425. *
  426. * @return {string} The UTF-8 string.
  427. *
  428. * @static
  429. *
  430. * @example
  431. *
  432. * var utf8String = CryptoJS.enc.Utf8.stringify(wordArray);
  433. */
  434. stringify: function (wordArray) {
  435. try {
  436. return decodeURIComponent(escape(Latin1.stringify(wordArray)));
  437. } catch (e) {
  438. throw new Error('Malformed UTF-8 data');
  439. }
  440. },
  441. /**
  442. * Converts a UTF-8 string to a word array.
  443. *
  444. * @param {string} utf8Str The UTF-8 string.
  445. *
  446. * @return {WordArray} The word array.
  447. *
  448. * @static
  449. *
  450. * @example
  451. *
  452. * var wordArray = CryptoJS.enc.Utf8.parse(utf8String);
  453. */
  454. parse: function (utf8Str) {
  455. return Latin1.parse(unescape(encodeURIComponent(utf8Str)));
  456. }
  457. };
  458. /**
  459. * Abstract buffered block algorithm template.
  460. *
  461. * The property blockSize must be implemented in a concrete subtype.
  462. *
  463. * @property {number} _minBufferSize The number of blocks that should be kept unprocessed in the buffer. Default: 0
  464. */
  465. var BufferedBlockAlgorithm = C_lib.BufferedBlockAlgorithm = Base.extend({
  466. /**
  467. * Resets this block algorithm's data buffer to its initial state.
  468. *
  469. * @example
  470. *
  471. * bufferedBlockAlgorithm.reset();
  472. */
  473. reset: function () {
  474. // Initial values
  475. this._data = new WordArray.init();
  476. this._nDataBytes = 0;
  477. },
  478. /**
  479. * Adds new data to this block algorithm's buffer.
  480. *
  481. * @param {WordArray|string} data The data to append. Strings are converted to a WordArray using UTF-8.
  482. *
  483. * @example
  484. *
  485. * bufferedBlockAlgorithm._append('data');
  486. * bufferedBlockAlgorithm._append(wordArray);
  487. */
  488. _append: function (data) {
  489. // Convert string to WordArray, else assume WordArray already
  490. if (typeof data == 'string') {
  491. data = Utf8.parse(data);
  492. }
  493. // Append
  494. this._data.concat(data);
  495. this._nDataBytes += data.sigBytes;
  496. },
  497. /**
  498. * Processes available data blocks.
  499. *
  500. * This method invokes _doProcessBlock(offset), which must be implemented by a concrete subtype.
  501. *
  502. * @param {boolean} doFlush Whether all blocks and partial blocks should be processed.
  503. *
  504. * @return {WordArray} The processed data.
  505. *
  506. * @example
  507. *
  508. * var processedData = bufferedBlockAlgorithm._process();
  509. * var processedData = bufferedBlockAlgorithm._process(!!'flush');
  510. */
  511. _process: function (doFlush) {
  512. var processedWords;
  513. // Shortcuts
  514. var data = this._data;
  515. var dataWords = data.words;
  516. var dataSigBytes = data.sigBytes;
  517. var blockSize = this.blockSize;
  518. var blockSizeBytes = blockSize * 4;
  519. // Count blocks ready
  520. var nBlocksReady = dataSigBytes / blockSizeBytes;
  521. if (doFlush) {
  522. // Round up to include partial blocks
  523. nBlocksReady = Math.ceil(nBlocksReady);
  524. } else {
  525. // Round down to include only full blocks,
  526. // less the number of blocks that must remain in the buffer
  527. nBlocksReady = Math.max((nBlocksReady | 0) - this._minBufferSize, 0);
  528. }
  529. // Count words ready
  530. var nWordsReady = nBlocksReady * blockSize;
  531. // Count bytes ready
  532. var nBytesReady = Math.min(nWordsReady * 4, dataSigBytes);
  533. // Process blocks
  534. if (nWordsReady) {
  535. for (var offset = 0; offset < nWordsReady; offset += blockSize) {
  536. // Perform concrete-algorithm logic
  537. this._doProcessBlock(dataWords, offset);
  538. }
  539. // Remove processed words
  540. processedWords = dataWords.splice(0, nWordsReady);
  541. data.sigBytes -= nBytesReady;
  542. }
  543. // Return processed words
  544. return new WordArray.init(processedWords, nBytesReady);
  545. },
  546. /**
  547. * Creates a copy of this object.
  548. *
  549. * @return {Object} The clone.
  550. *
  551. * @example
  552. *
  553. * var clone = bufferedBlockAlgorithm.clone();
  554. */
  555. clone: function () {
  556. var clone = Base.clone.call(this);
  557. clone._data = this._data.clone();
  558. return clone;
  559. },
  560. _minBufferSize: 0
  561. });
  562. /**
  563. * Abstract hasher template.
  564. *
  565. * @property {number} blockSize The number of 32-bit words this hasher operates on. Default: 16 (512 bits)
  566. */
  567. var Hasher = C_lib.Hasher = BufferedBlockAlgorithm.extend({
  568. /**
  569. * Configuration options.
  570. */
  571. cfg: Base.extend(),
  572. /**
  573. * Initializes a newly created hasher.
  574. *
  575. * @param {Object} cfg (Optional) The configuration options to use for this hash computation.
  576. *
  577. * @example
  578. *
  579. * var hasher = CryptoJS.algo.SHA256.create();
  580. */
  581. init: function (cfg) {
  582. // Apply config defaults
  583. this.cfg = this.cfg.extend(cfg);
  584. // Set initial values
  585. this.reset();
  586. },
  587. /**
  588. * Resets this hasher to its initial state.
  589. *
  590. * @example
  591. *
  592. * hasher.reset();
  593. */
  594. reset: function () {
  595. // Reset data buffer
  596. BufferedBlockAlgorithm.reset.call(this);
  597. // Perform concrete-hasher logic
  598. this._doReset();
  599. },
  600. /**
  601. * Updates this hasher with a message.
  602. *
  603. * @param {WordArray|string} messageUpdate The message to append.
  604. *
  605. * @return {Hasher} This hasher.
  606. *
  607. * @example
  608. *
  609. * hasher.update('message');
  610. * hasher.update(wordArray);
  611. */
  612. update: function (messageUpdate) {
  613. // Append
  614. this._append(messageUpdate);
  615. // Update the hash
  616. this._process();
  617. // Chainable
  618. return this;
  619. },
  620. /**
  621. * Finalizes the hash computation.
  622. * Note that the finalize operation is effectively a destructive, read-once operation.
  623. *
  624. * @param {WordArray|string} messageUpdate (Optional) A final message update.
  625. *
  626. * @return {WordArray} The hash.
  627. *
  628. * @example
  629. *
  630. * var hash = hasher.finalize();
  631. * var hash = hasher.finalize('message');
  632. * var hash = hasher.finalize(wordArray);
  633. */
  634. finalize: function (messageUpdate) {
  635. // Final message update
  636. if (messageUpdate) {
  637. this._append(messageUpdate);
  638. }
  639. // Perform concrete-hasher logic
  640. var hash = this._doFinalize();
  641. return hash;
  642. },
  643. blockSize: 512 / 32,
  644. /**
  645. * Creates a shortcut function to a hasher's object interface.
  646. *
  647. * @param {Hasher} hasher The hasher to create a helper for.
  648. *
  649. * @return {Function} The shortcut function.
  650. *
  651. * @static
  652. *
  653. * @example
  654. *
  655. * var SHA256 = CryptoJS.lib.Hasher._createHelper(CryptoJS.algo.SHA256);
  656. */
  657. _createHelper: function (hasher) {
  658. return function (message, cfg) {
  659. return new hasher.init(cfg).finalize(message);
  660. };
  661. },
  662. /**
  663. * Creates a shortcut function to the HMAC's object interface.
  664. *
  665. * @param {Hasher} hasher The hasher to use in this HMAC helper.
  666. *
  667. * @return {Function} The shortcut function.
  668. *
  669. * @static
  670. *
  671. * @example
  672. *
  673. * var HmacSHA256 = CryptoJS.lib.Hasher._createHmacHelper(CryptoJS.algo.SHA256);
  674. */
  675. _createHmacHelper: function (hasher) {
  676. return function (message, key) {
  677. return new C_algo.HMAC.init(hasher, key).finalize(message);
  678. };
  679. }
  680. });
  681. /**
  682. * Algorithm namespace.
  683. */
  684. var C_algo = C.algo = {};
  685. return C;
  686. }(Math));
  687. (function () {
  688. // Shortcuts
  689. var C = CryptoJS;
  690. var C_lib = C.lib;
  691. var WordArray = C_lib.WordArray;
  692. var C_enc = C.enc;
  693. /**
  694. * Base64 encoding strategy.
  695. */
  696. var Base64 = C_enc.Base64 = {
  697. /**
  698. * Converts a word array to a Base64 string.
  699. *
  700. * @param {WordArray} wordArray The word array.
  701. *
  702. * @return {string} The Base64 string.
  703. *
  704. * @static
  705. *
  706. * @example
  707. *
  708. * var base64String = CryptoJS.enc.Base64.stringify(wordArray);
  709. */
  710. stringify: function (wordArray) {
  711. // Shortcuts
  712. var words = wordArray.words;
  713. var sigBytes = wordArray.sigBytes;
  714. var map = this._map;
  715. // Clamp excess bits
  716. wordArray.clamp();
  717. // Convert
  718. var base64Chars = [];
  719. for (var i = 0; i < sigBytes; i += 3) {
  720. var byte1 = (words[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff;
  721. var byte2 = (words[(i + 1) >>> 2] >>> (24 - ((i + 1) % 4) * 8)) & 0xff;
  722. var byte3 = (words[(i + 2) >>> 2] >>> (24 - ((i + 2) % 4) * 8)) & 0xff;
  723. var triplet = (byte1 << 16) | (byte2 << 8) | byte3;
  724. for (var j = 0; (j < 4) && (i + j * 0.75 < sigBytes); j++) {
  725. base64Chars.push(map.charAt((triplet >>> (6 * (3 - j))) & 0x3f));
  726. }
  727. }
  728. // Add padding
  729. var paddingChar = map.charAt(64);
  730. if (paddingChar) {
  731. while (base64Chars.length % 4) {
  732. base64Chars.push(paddingChar);
  733. }
  734. }
  735. return base64Chars.join('');
  736. },
  737. /**
  738. * Converts a Base64 string to a word array.
  739. *
  740. * @param {string} base64Str The Base64 string.
  741. *
  742. * @return {WordArray} The word array.
  743. *
  744. * @static
  745. *
  746. * @example
  747. *
  748. * var wordArray = CryptoJS.enc.Base64.parse(base64String);
  749. */
  750. parse: function (base64Str) {
  751. // Shortcuts
  752. var base64StrLength = base64Str.length;
  753. var map = this._map;
  754. var reverseMap = this._reverseMap;
  755. if (!reverseMap) {
  756. reverseMap = this._reverseMap = [];
  757. for (var j = 0; j < map.length; j++) {
  758. reverseMap[map.charCodeAt(j)] = j;
  759. }
  760. }
  761. // Ignore padding
  762. var paddingChar = map.charAt(64);
  763. if (paddingChar) {
  764. var paddingIndex = base64Str.indexOf(paddingChar);
  765. if (paddingIndex !== -1) {
  766. base64StrLength = paddingIndex;
  767. }
  768. }
  769. // Convert
  770. return parseLoop(base64Str, base64StrLength, reverseMap);
  771. },
  772. _map: 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/='
  773. };
  774. function parseLoop(base64Str, base64StrLength, reverseMap) {
  775. var words = [];
  776. var nBytes = 0;
  777. for (var i = 0; i < base64StrLength; i++) {
  778. if (i % 4) {
  779. var bits1 = reverseMap[base64Str.charCodeAt(i - 1)] << ((i % 4) * 2);
  780. var bits2 = reverseMap[base64Str.charCodeAt(i)] >>> (6 - (i % 4) * 2);
  781. var bitsCombined = bits1 | bits2;
  782. words[nBytes >>> 2] |= bitsCombined << (24 - (nBytes % 4) * 8);
  783. nBytes++;
  784. }
  785. }
  786. return WordArray.create(words, nBytes);
  787. }
  788. }());
  789. (function (Math) {
  790. // Shortcuts
  791. var C = CryptoJS;
  792. var C_lib = C.lib;
  793. var WordArray = C_lib.WordArray;
  794. var Hasher = C_lib.Hasher;
  795. var C_algo = C.algo;
  796. // Constants table
  797. var T = [];
  798. // Compute constants
  799. (function () {
  800. for (var i = 0; i < 64; i++) {
  801. T[i] = (Math.abs(Math.sin(i + 1)) * 0x100000000) | 0;
  802. }
  803. }());
  804. /**
  805. * MD5 hash algorithm.
  806. */
  807. var MD5 = C_algo.MD5 = Hasher.extend({
  808. _doReset: function () {
  809. this._hash = new WordArray.init([
  810. 0x67452301, 0xefcdab89,
  811. 0x98badcfe, 0x10325476
  812. ]);
  813. },
  814. _doProcessBlock: function (M, offset) {
  815. // Swap endian
  816. for (var i = 0; i < 16; i++) {
  817. // Shortcuts
  818. var offset_i = offset + i;
  819. var M_offset_i = M[offset_i];
  820. M[offset_i] = (
  821. (((M_offset_i << 8) | (M_offset_i >>> 24)) & 0x00ff00ff) |
  822. (((M_offset_i << 24) | (M_offset_i >>> 8)) & 0xff00ff00)
  823. );
  824. }
  825. // Shortcuts
  826. var H = this._hash.words;
  827. var M_offset_0 = M[offset + 0];
  828. var M_offset_1 = M[offset + 1];
  829. var M_offset_2 = M[offset + 2];
  830. var M_offset_3 = M[offset + 3];
  831. var M_offset_4 = M[offset + 4];
  832. var M_offset_5 = M[offset + 5];
  833. var M_offset_6 = M[offset + 6];
  834. var M_offset_7 = M[offset + 7];
  835. var M_offset_8 = M[offset + 8];
  836. var M_offset_9 = M[offset + 9];
  837. var M_offset_10 = M[offset + 10];
  838. var M_offset_11 = M[offset + 11];
  839. var M_offset_12 = M[offset + 12];
  840. var M_offset_13 = M[offset + 13];
  841. var M_offset_14 = M[offset + 14];
  842. var M_offset_15 = M[offset + 15];
  843. // Working varialbes
  844. var a = H[0];
  845. var b = H[1];
  846. var c = H[2];
  847. var d = H[3];
  848. // Computation
  849. a = FF(a, b, c, d, M_offset_0, 7, T[0]);
  850. d = FF(d, a, b, c, M_offset_1, 12, T[1]);
  851. c = FF(c, d, a, b, M_offset_2, 17, T[2]);
  852. b = FF(b, c, d, a, M_offset_3, 22, T[3]);
  853. a = FF(a, b, c, d, M_offset_4, 7, T[4]);
  854. d = FF(d, a, b, c, M_offset_5, 12, T[5]);
  855. c = FF(c, d, a, b, M_offset_6, 17, T[6]);
  856. b = FF(b, c, d, a, M_offset_7, 22, T[7]);
  857. a = FF(a, b, c, d, M_offset_8, 7, T[8]);
  858. d = FF(d, a, b, c, M_offset_9, 12, T[9]);
  859. c = FF(c, d, a, b, M_offset_10, 17, T[10]);
  860. b = FF(b, c, d, a, M_offset_11, 22, T[11]);
  861. a = FF(a, b, c, d, M_offset_12, 7, T[12]);
  862. d = FF(d, a, b, c, M_offset_13, 12, T[13]);
  863. c = FF(c, d, a, b, M_offset_14, 17, T[14]);
  864. b = FF(b, c, d, a, M_offset_15, 22, T[15]);
  865. a = GG(a, b, c, d, M_offset_1, 5, T[16]);
  866. d = GG(d, a, b, c, M_offset_6, 9, T[17]);
  867. c = GG(c, d, a, b, M_offset_11, 14, T[18]);
  868. b = GG(b, c, d, a, M_offset_0, 20, T[19]);
  869. a = GG(a, b, c, d, M_offset_5, 5, T[20]);
  870. d = GG(d, a, b, c, M_offset_10, 9, T[21]);
  871. c = GG(c, d, a, b, M_offset_15, 14, T[22]);
  872. b = GG(b, c, d, a, M_offset_4, 20, T[23]);
  873. a = GG(a, b, c, d, M_offset_9, 5, T[24]);
  874. d = GG(d, a, b, c, M_offset_14, 9, T[25]);
  875. c = GG(c, d, a, b, M_offset_3, 14, T[26]);
  876. b = GG(b, c, d, a, M_offset_8, 20, T[27]);
  877. a = GG(a, b, c, d, M_offset_13, 5, T[28]);
  878. d = GG(d, a, b, c, M_offset_2, 9, T[29]);
  879. c = GG(c, d, a, b, M_offset_7, 14, T[30]);
  880. b = GG(b, c, d, a, M_offset_12, 20, T[31]);
  881. a = HH(a, b, c, d, M_offset_5, 4, T[32]);
  882. d = HH(d, a, b, c, M_offset_8, 11, T[33]);
  883. c = HH(c, d, a, b, M_offset_11, 16, T[34]);
  884. b = HH(b, c, d, a, M_offset_14, 23, T[35]);
  885. a = HH(a, b, c, d, M_offset_1, 4, T[36]);
  886. d = HH(d, a, b, c, M_offset_4, 11, T[37]);
  887. c = HH(c, d, a, b, M_offset_7, 16, T[38]);
  888. b = HH(b, c, d, a, M_offset_10, 23, T[39]);
  889. a = HH(a, b, c, d, M_offset_13, 4, T[40]);
  890. d = HH(d, a, b, c, M_offset_0, 11, T[41]);
  891. c = HH(c, d, a, b, M_offset_3, 16, T[42]);
  892. b = HH(b, c, d, a, M_offset_6, 23, T[43]);
  893. a = HH(a, b, c, d, M_offset_9, 4, T[44]);
  894. d = HH(d, a, b, c, M_offset_12, 11, T[45]);
  895. c = HH(c, d, a, b, M_offset_15, 16, T[46]);
  896. b = HH(b, c, d, a, M_offset_2, 23, T[47]);
  897. a = II(a, b, c, d, M_offset_0, 6, T[48]);
  898. d = II(d, a, b, c, M_offset_7, 10, T[49]);
  899. c = II(c, d, a, b, M_offset_14, 15, T[50]);
  900. b = II(b, c, d, a, M_offset_5, 21, T[51]);
  901. a = II(a, b, c, d, M_offset_12, 6, T[52]);
  902. d = II(d, a, b, c, M_offset_3, 10, T[53]);
  903. c = II(c, d, a, b, M_offset_10, 15, T[54]);
  904. b = II(b, c, d, a, M_offset_1, 21, T[55]);
  905. a = II(a, b, c, d, M_offset_8, 6, T[56]);
  906. d = II(d, a, b, c, M_offset_15, 10, T[57]);
  907. c = II(c, d, a, b, M_offset_6, 15, T[58]);
  908. b = II(b, c, d, a, M_offset_13, 21, T[59]);
  909. a = II(a, b, c, d, M_offset_4, 6, T[60]);
  910. d = II(d, a, b, c, M_offset_11, 10, T[61]);
  911. c = II(c, d, a, b, M_offset_2, 15, T[62]);
  912. b = II(b, c, d, a, M_offset_9, 21, T[63]);
  913. // Intermediate hash value
  914. H[0] = (H[0] + a) | 0;
  915. H[1] = (H[1] + b) | 0;
  916. H[2] = (H[2] + c) | 0;
  917. H[3] = (H[3] + d) | 0;
  918. },
  919. _doFinalize: function () {
  920. // Shortcuts
  921. var data = this._data;
  922. var dataWords = data.words;
  923. var nBitsTotal = this._nDataBytes * 8;
  924. var nBitsLeft = data.sigBytes * 8;
  925. // Add padding
  926. dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
  927. var nBitsTotalH = Math.floor(nBitsTotal / 0x100000000);
  928. var nBitsTotalL = nBitsTotal;
  929. dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = (
  930. (((nBitsTotalH << 8) | (nBitsTotalH >>> 24)) & 0x00ff00ff) |
  931. (((nBitsTotalH << 24) | (nBitsTotalH >>> 8)) & 0xff00ff00)
  932. );
  933. dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = (
  934. (((nBitsTotalL << 8) | (nBitsTotalL >>> 24)) & 0x00ff00ff) |
  935. (((nBitsTotalL << 24) | (nBitsTotalL >>> 8)) & 0xff00ff00)
  936. );
  937. data.sigBytes = (dataWords.length + 1) * 4;
  938. // Hash final blocks
  939. this._process();
  940. // Shortcuts
  941. var hash = this._hash;
  942. var H = hash.words;
  943. // Swap endian
  944. for (var i = 0; i < 4; i++) {
  945. // Shortcut
  946. var H_i = H[i];
  947. H[i] = (((H_i << 8) | (H_i >>> 24)) & 0x00ff00ff) |
  948. (((H_i << 24) | (H_i >>> 8)) & 0xff00ff00);
  949. }
  950. // Return final computed hash
  951. return hash;
  952. },
  953. clone: function () {
  954. var clone = Hasher.clone.call(this);
  955. clone._hash = this._hash.clone();
  956. return clone;
  957. }
  958. });
  959. function FF(a, b, c, d, x, s, t) {
  960. var n = a + ((b & c) | (~b & d)) + x + t;
  961. return ((n << s) | (n >>> (32 - s))) + b;
  962. }
  963. function GG(a, b, c, d, x, s, t) {
  964. var n = a + ((b & d) | (c & ~d)) + x + t;
  965. return ((n << s) | (n >>> (32 - s))) + b;
  966. }
  967. function HH(a, b, c, d, x, s, t) {
  968. var n = a + (b ^ c ^ d) + x + t;
  969. return ((n << s) | (n >>> (32 - s))) + b;
  970. }
  971. function II(a, b, c, d, x, s, t) {
  972. var n = a + (c ^ (b | ~d)) + x + t;
  973. return ((n << s) | (n >>> (32 - s))) + b;
  974. }
  975. /**
  976. * Shortcut function to the hasher's object interface.
  977. *
  978. * @param {WordArray|string} message The message to hash.
  979. *
  980. * @return {WordArray} The hash.
  981. *
  982. * @static
  983. *
  984. * @example
  985. *
  986. * var hash = CryptoJS.MD5('message');
  987. * var hash = CryptoJS.MD5(wordArray);
  988. */
  989. C.MD5 = Hasher._createHelper(MD5);
  990. /**
  991. * Shortcut function to the HMAC's object interface.
  992. *
  993. * @param {WordArray|string} message The message to hash.
  994. * @param {WordArray|string} key The secret key.
  995. *
  996. * @return {WordArray} The HMAC.
  997. *
  998. * @static
  999. *
  1000. * @example
  1001. *
  1002. * var hmac = CryptoJS.HmacMD5(message, key);
  1003. */
  1004. C.HmacMD5 = Hasher._createHmacHelper(MD5);
  1005. }(Math));
  1006. (function () {
  1007. // Shortcuts
  1008. var C = CryptoJS;
  1009. var C_lib = C.lib;
  1010. var WordArray = C_lib.WordArray;
  1011. var Hasher = C_lib.Hasher;
  1012. var C_algo = C.algo;
  1013. // Reusable object
  1014. var W = [];
  1015. /**
  1016. * SHA-1 hash algorithm.
  1017. */
  1018. var SHA1 = C_algo.SHA1 = Hasher.extend({
  1019. _doReset: function () {
  1020. this._hash = new WordArray.init([
  1021. 0x67452301, 0xefcdab89,
  1022. 0x98badcfe, 0x10325476,
  1023. 0xc3d2e1f0
  1024. ]);
  1025. },
  1026. _doProcessBlock: function (M, offset) {
  1027. // Shortcut
  1028. var H = this._hash.words;
  1029. // Working variables
  1030. var a = H[0];
  1031. var b = H[1];
  1032. var c = H[2];
  1033. var d = H[3];
  1034. var e = H[4];
  1035. // Computation
  1036. for (var i = 0; i < 80; i++) {
  1037. if (i < 16) {
  1038. W[i] = M[offset + i] | 0;
  1039. } else {
  1040. var n = W[i - 3] ^ W[i - 8] ^ W[i - 14] ^ W[i - 16];
  1041. W[i] = (n << 1) | (n >>> 31);
  1042. }
  1043. var t = ((a << 5) | (a >>> 27)) + e + W[i];
  1044. if (i < 20) {
  1045. t += ((b & c) | (~b & d)) + 0x5a827999;
  1046. } else if (i < 40) {
  1047. t += (b ^ c ^ d) + 0x6ed9eba1;
  1048. } else if (i < 60) {
  1049. t += ((b & c) | (b & d) | (c & d)) - 0x70e44324;
  1050. } else /* if (i < 80) */ {
  1051. t += (b ^ c ^ d) - 0x359d3e2a;
  1052. }
  1053. e = d;
  1054. d = c;
  1055. c = (b << 30) | (b >>> 2);
  1056. b = a;
  1057. a = t;
  1058. }
  1059. // Intermediate hash value
  1060. H[0] = (H[0] + a) | 0;
  1061. H[1] = (H[1] + b) | 0;
  1062. H[2] = (H[2] + c) | 0;
  1063. H[3] = (H[3] + d) | 0;
  1064. H[4] = (H[4] + e) | 0;
  1065. },
  1066. _doFinalize: function () {
  1067. // Shortcuts
  1068. var data = this._data;
  1069. var dataWords = data.words;
  1070. var nBitsTotal = this._nDataBytes * 8;
  1071. var nBitsLeft = data.sigBytes * 8;
  1072. // Add padding
  1073. dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
  1074. dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = Math.floor(nBitsTotal / 0x100000000);
  1075. dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = nBitsTotal;
  1076. data.sigBytes = dataWords.length * 4;
  1077. // Hash final blocks
  1078. this._process();
  1079. // Return final computed hash
  1080. return this._hash;
  1081. },
  1082. clone: function () {
  1083. var clone = Hasher.clone.call(this);
  1084. clone._hash = this._hash.clone();
  1085. return clone;
  1086. }
  1087. });
  1088. /**
  1089. * Shortcut function to the hasher's object interface.
  1090. *
  1091. * @param {WordArray|string} message The message to hash.
  1092. *
  1093. * @return {WordArray} The hash.
  1094. *
  1095. * @static
  1096. *
  1097. * @example
  1098. *
  1099. * var hash = CryptoJS.SHA1('message');
  1100. * var hash = CryptoJS.SHA1(wordArray);
  1101. */
  1102. C.SHA1 = Hasher._createHelper(SHA1);
  1103. /**
  1104. * Shortcut function to the HMAC's object interface.
  1105. *
  1106. * @param {WordArray|string} message The message to hash.
  1107. * @param {WordArray|string} key The secret key.
  1108. *
  1109. * @return {WordArray} The HMAC.
  1110. *
  1111. * @static
  1112. *
  1113. * @example
  1114. *
  1115. * var hmac = CryptoJS.HmacSHA1(message, key);
  1116. */
  1117. C.HmacSHA1 = Hasher._createHmacHelper(SHA1);
  1118. }());
  1119. (function (Math) {
  1120. // Shortcuts
  1121. var C = CryptoJS;
  1122. var C_lib = C.lib;
  1123. var WordArray = C_lib.WordArray;
  1124. var Hasher = C_lib.Hasher;
  1125. var C_algo = C.algo;
  1126. // Initialization and round constants tables
  1127. var H = [];
  1128. var K = [];
  1129. // Compute constants
  1130. (function () {
  1131. function isPrime(n) {
  1132. var sqrtN = Math.sqrt(n);
  1133. for (var factor = 2; factor <= sqrtN; factor++) {
  1134. if (!(n % factor)) {
  1135. return false;
  1136. }
  1137. }
  1138. return true;
  1139. }
  1140. function getFractionalBits(n) {
  1141. return ((n - (n | 0)) * 0x100000000) | 0;
  1142. }
  1143. var n = 2;
  1144. var nPrime = 0;
  1145. while (nPrime < 64) {
  1146. if (isPrime(n)) {
  1147. if (nPrime < 8) {
  1148. H[nPrime] = getFractionalBits(Math.pow(n, 1 / 2));
  1149. }
  1150. K[nPrime] = getFractionalBits(Math.pow(n, 1 / 3));
  1151. nPrime++;
  1152. }
  1153. n++;
  1154. }
  1155. }());
  1156. // Reusable object
  1157. var W = [];
  1158. /**
  1159. * SHA-256 hash algorithm.
  1160. */
  1161. var SHA256 = C_algo.SHA256 = Hasher.extend({
  1162. _doReset: function () {
  1163. this._hash = new WordArray.init(H.slice(0));
  1164. },
  1165. _doProcessBlock: function (M, offset) {
  1166. // Shortcut
  1167. var H = this._hash.words;
  1168. // Working variables
  1169. var a = H[0];
  1170. var b = H[1];
  1171. var c = H[2];
  1172. var d = H[3];
  1173. var e = H[4];
  1174. var f = H[5];
  1175. var g = H[6];
  1176. var h = H[7];
  1177. // Computation
  1178. for (var i = 0; i < 64; i++) {
  1179. if (i < 16) {
  1180. W[i] = M[offset + i] | 0;
  1181. } else {
  1182. var gamma0x = W[i - 15];
  1183. var gamma0 = ((gamma0x << 25) | (gamma0x >>> 7)) ^
  1184. ((gamma0x << 14) | (gamma0x >>> 18)) ^
  1185. (gamma0x >>> 3);
  1186. var gamma1x = W[i - 2];
  1187. var gamma1 = ((gamma1x << 15) | (gamma1x >>> 17)) ^
  1188. ((gamma1x << 13) | (gamma1x >>> 19)) ^
  1189. (gamma1x >>> 10);
  1190. W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16];
  1191. }
  1192. var ch = (e & f) ^ (~e & g);
  1193. var maj = (a & b) ^ (a & c) ^ (b & c);
  1194. var sigma0 = ((a << 30) | (a >>> 2)) ^ ((a << 19) | (a >>> 13)) ^ ((a << 10) | (a >>> 22));
  1195. var sigma1 = ((e << 26) | (e >>> 6)) ^ ((e << 21) | (e >>> 11)) ^ ((e << 7) | (e >>> 25));
  1196. var t1 = h + sigma1 + ch + K[i] + W[i];
  1197. var t2 = sigma0 + maj;
  1198. h = g;
  1199. g = f;
  1200. f = e;
  1201. e = (d + t1) | 0;
  1202. d = c;
  1203. c = b;
  1204. b = a;
  1205. a = (t1 + t2) | 0;
  1206. }
  1207. // Intermediate hash value
  1208. H[0] = (H[0] + a) | 0;
  1209. H[1] = (H[1] + b) | 0;
  1210. H[2] = (H[2] + c) | 0;
  1211. H[3] = (H[3] + d) | 0;
  1212. H[4] = (H[4] + e) | 0;
  1213. H[5] = (H[5] + f) | 0;
  1214. H[6] = (H[6] + g) | 0;
  1215. H[7] = (H[7] + h) | 0;
  1216. },
  1217. _doFinalize: function () {
  1218. // Shortcuts
  1219. var data = this._data;
  1220. var dataWords = data.words;
  1221. var nBitsTotal = this._nDataBytes * 8;
  1222. var nBitsLeft = data.sigBytes * 8;
  1223. // Add padding
  1224. dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
  1225. dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = Math.floor(nBitsTotal / 0x100000000);
  1226. dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 15] = nBitsTotal;
  1227. data.sigBytes = dataWords.length * 4;
  1228. // Hash final blocks
  1229. this._process();
  1230. // Return final computed hash
  1231. return this._hash;
  1232. },
  1233. clone: function () {
  1234. var clone = Hasher.clone.call(this);
  1235. clone._hash = this._hash.clone();
  1236. return clone;
  1237. }
  1238. });
  1239. /**
  1240. * Shortcut function to the hasher's object interface.
  1241. *
  1242. * @param {WordArray|string} message The message to hash.
  1243. *
  1244. * @return {WordArray} The hash.
  1245. *
  1246. * @static
  1247. *
  1248. * @example
  1249. *
  1250. * var hash = CryptoJS.SHA256('message');
  1251. * var hash = CryptoJS.SHA256(wordArray);
  1252. */
  1253. C.SHA256 = Hasher._createHelper(SHA256);
  1254. /**
  1255. * Shortcut function to the HMAC's object interface.
  1256. *
  1257. * @param {WordArray|string} message The message to hash.
  1258. * @param {WordArray|string} key The secret key.
  1259. *
  1260. * @return {WordArray} The HMAC.
  1261. *
  1262. * @static
  1263. *
  1264. * @example
  1265. *
  1266. * var hmac = CryptoJS.HmacSHA256(message, key);
  1267. */
  1268. C.HmacSHA256 = Hasher._createHmacHelper(SHA256);
  1269. }(Math));
  1270. (function () {
  1271. // Shortcuts
  1272. var C = CryptoJS;
  1273. var C_lib = C.lib;
  1274. var WordArray = C_lib.WordArray;
  1275. var C_enc = C.enc;
  1276. /**
  1277. * UTF-16 BE encoding strategy.
  1278. */
  1279. var Utf16BE = C_enc.Utf16 = C_enc.Utf16BE = {
  1280. /**
  1281. * Converts a word array to a UTF-16 BE string.
  1282. *
  1283. * @param {WordArray} wordArray The word array.
  1284. *
  1285. * @return {string} The UTF-16 BE string.
  1286. *
  1287. * @static
  1288. *
  1289. * @example
  1290. *
  1291. * var utf16String = CryptoJS.enc.Utf16.stringify(wordArray);
  1292. */
  1293. stringify: function (wordArray) {
  1294. // Shortcuts
  1295. var words = wordArray.words;
  1296. var sigBytes = wordArray.sigBytes;
  1297. // Convert
  1298. var utf16Chars = [];
  1299. for (var i = 0; i < sigBytes; i += 2) {
  1300. var codePoint = (words[i >>> 2] >>> (16 - (i % 4) * 8)) & 0xffff;
  1301. utf16Chars.push(String.fromCharCode(codePoint));
  1302. }
  1303. return utf16Chars.join('');
  1304. },
  1305. /**
  1306. * Converts a UTF-16 BE string to a word array.
  1307. *
  1308. * @param {string} utf16Str The UTF-16 BE string.
  1309. *
  1310. * @return {WordArray} The word array.
  1311. *
  1312. * @static
  1313. *
  1314. * @example
  1315. *
  1316. * var wordArray = CryptoJS.enc.Utf16.parse(utf16String);
  1317. */
  1318. parse: function (utf16Str) {
  1319. // Shortcut
  1320. var utf16StrLength = utf16Str.length;
  1321. // Convert
  1322. var words = [];
  1323. for (var i = 0; i < utf16StrLength; i++) {
  1324. words[i >>> 1] |= utf16Str.charCodeAt(i) << (16 - (i % 2) * 16);
  1325. }
  1326. return WordArray.create(words, utf16StrLength * 2);
  1327. }
  1328. };
  1329. /**
  1330. * UTF-16 LE encoding strategy.
  1331. */
  1332. C_enc.Utf16LE = {
  1333. /**
  1334. * Converts a word array to a UTF-16 LE string.
  1335. *
  1336. * @param {WordArray} wordArray The word array.
  1337. *
  1338. * @return {string} The UTF-16 LE string.
  1339. *
  1340. * @static
  1341. *
  1342. * @example
  1343. *
  1344. * var utf16Str = CryptoJS.enc.Utf16LE.stringify(wordArray);
  1345. */
  1346. stringify: function (wordArray) {
  1347. // Shortcuts
  1348. var words = wordArray.words;
  1349. var sigBytes = wordArray.sigBytes;
  1350. // Convert
  1351. var utf16Chars = [];
  1352. for (var i = 0; i < sigBytes; i += 2) {
  1353. var codePoint = swapEndian((words[i >>> 2] >>> (16 - (i % 4) * 8)) & 0xffff);
  1354. utf16Chars.push(String.fromCharCode(codePoint));
  1355. }
  1356. return utf16Chars.join('');
  1357. },
  1358. /**
  1359. * Converts a UTF-16 LE string to a word array.
  1360. *
  1361. * @param {string} utf16Str The UTF-16 LE string.
  1362. *
  1363. * @return {WordArray} The word array.
  1364. *
  1365. * @static
  1366. *
  1367. * @example
  1368. *
  1369. * var wordArray = CryptoJS.enc.Utf16LE.parse(utf16Str);
  1370. */
  1371. parse: function (utf16Str) {
  1372. // Shortcut
  1373. var utf16StrLength = utf16Str.length;
  1374. // Convert
  1375. var words = [];
  1376. for (var i = 0; i < utf16StrLength; i++) {
  1377. words[i >>> 1] |= swapEndian(utf16Str.charCodeAt(i) << (16 - (i % 2) * 16));
  1378. }
  1379. return WordArray.create(words, utf16StrLength * 2);
  1380. }
  1381. };
  1382. function swapEndian(word) {
  1383. return ((word << 8) & 0xff00ff00) | ((word >>> 8) & 0x00ff00ff);
  1384. }
  1385. }());
  1386. (function () {
  1387. // Check if typed arrays are supported
  1388. if (typeof ArrayBuffer != 'function') {
  1389. return;
  1390. }
  1391. // Shortcuts
  1392. var C = CryptoJS;
  1393. var C_lib = C.lib;
  1394. var WordArray = C_lib.WordArray;
  1395. // Reference original init
  1396. var superInit = WordArray.init;
  1397. // Augment WordArray.init to handle typed arrays
  1398. var subInit = WordArray.init = function (typedArray) {
  1399. // Convert buffers to uint8
  1400. if (typedArray instanceof ArrayBuffer) {
  1401. typedArray = new Uint8Array(typedArray);
  1402. }
  1403. // Convert other array views to uint8
  1404. if (
  1405. typedArray instanceof Int8Array ||
  1406. (typeof Uint8ClampedArray !== "undefined" && typedArray instanceof Uint8ClampedArray) ||
  1407. typedArray instanceof Int16Array ||
  1408. typedArray instanceof Uint16Array ||
  1409. typedArray instanceof Int32Array ||
  1410. typedArray instanceof Uint32Array ||
  1411. typedArray instanceof Float32Array ||
  1412. typedArray instanceof Float64Array
  1413. ) {
  1414. typedArray = new Uint8Array(typedArray.buffer, typedArray.byteOffset, typedArray.byteLength);
  1415. }
  1416. // Handle Uint8Array
  1417. if (typedArray instanceof Uint8Array) {
  1418. // Shortcut
  1419. var typedArrayByteLength = typedArray.byteLength;
  1420. // Extract bytes
  1421. var words = [];
  1422. for (var i = 0; i < typedArrayByteLength; i++) {
  1423. words[i >>> 2] |= typedArray[i] << (24 - (i % 4) * 8);
  1424. }
  1425. // Initialize this word array
  1426. superInit.call(this, words, typedArrayByteLength);
  1427. } else {
  1428. // Else call normal init
  1429. superInit.apply(this, arguments);
  1430. }
  1431. };
  1432. subInit.prototype = WordArray;
  1433. }());
  1434. /** @preserve
  1435. (c) 2012 by Cédric Mesnil. All rights reserved.
  1436. Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
  1437. - Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
  1438. - Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
  1439. THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  1440. */
  1441. (function (Math) {
  1442. // Shortcuts
  1443. var C = CryptoJS;
  1444. var C_lib = C.lib;
  1445. var WordArray = C_lib.WordArray;
  1446. var Hasher = C_lib.Hasher;
  1447. var C_algo = C.algo;
  1448. // Constants table
  1449. var _zl = WordArray.create([
  1450. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  1451. 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
  1452. 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
  1453. 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
  1454. 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13]);
  1455. var _zr = WordArray.create([
  1456. 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
  1457. 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
  1458. 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
  1459. 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
  1460. 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11]);
  1461. var _sl = WordArray.create([
  1462. 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
  1463. 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
  1464. 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
  1465. 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
  1466. 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6]);
  1467. var _sr = WordArray.create([
  1468. 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
  1469. 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
  1470. 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
  1471. 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
  1472. 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11]);
  1473. var _hl = WordArray.create([0x00000000, 0x5A827999, 0x6ED9EBA1, 0x8F1BBCDC, 0xA953FD4E]);
  1474. var _hr = WordArray.create([0x50A28BE6, 0x5C4DD124, 0x6D703EF3, 0x7A6D76E9, 0x00000000]);
  1475. /**
  1476. * RIPEMD160 hash algorithm.
  1477. */
  1478. var RIPEMD160 = C_algo.RIPEMD160 = Hasher.extend({
  1479. _doReset: function () {
  1480. this._hash = WordArray.create([0x67452301, 0xEFCDAB89, 0x98BADCFE, 0x10325476, 0xC3D2E1F0]);
  1481. },
  1482. _doProcessBlock: function (M, offset) {
  1483. // Swap endian
  1484. for (var i = 0; i < 16; i++) {
  1485. // Shortcuts
  1486. var offset_i = offset + i;
  1487. var M_offset_i = M[offset_i];
  1488. // Swap
  1489. M[offset_i] = (
  1490. (((M_offset_i << 8) | (M_offset_i >>> 24)) & 0x00ff00ff) |
  1491. (((M_offset_i << 24) | (M_offset_i >>> 8)) & 0xff00ff00)
  1492. );
  1493. }
  1494. // Shortcut
  1495. var H = this._hash.words;
  1496. var hl = _hl.words;
  1497. var hr = _hr.words;
  1498. var zl = _zl.words;
  1499. var zr = _zr.words;
  1500. var sl = _sl.words;
  1501. var sr = _sr.words;
  1502. // Working variables
  1503. var al, bl, cl, dl, el;
  1504. var ar, br, cr, dr, er;
  1505. ar = al = H[0];
  1506. br = bl = H[1];
  1507. cr = cl = H[2];
  1508. dr = dl = H[3];
  1509. er = el = H[4];
  1510. // Computation
  1511. var t;
  1512. for (var i = 0; i < 80; i += 1) {
  1513. t = (al + M[offset + zl[i]]) | 0;
  1514. if (i < 16) {
  1515. t += f1(bl, cl, dl) + hl[0];
  1516. } else if (i < 32) {
  1517. t += f2(bl, cl, dl) + hl[1];
  1518. } else if (i < 48) {
  1519. t += f3(bl, cl, dl) + hl[2];
  1520. } else if (i < 64) {
  1521. t += f4(bl, cl, dl) + hl[3];
  1522. } else {// if (i<80) {
  1523. t += f5(bl, cl, dl) + hl[4];
  1524. }
  1525. t = t | 0;
  1526. t = rotl(t, sl[i]);
  1527. t = (t + el) | 0;
  1528. al = el;
  1529. el = dl;
  1530. dl = rotl(cl, 10);
  1531. cl = bl;
  1532. bl = t;
  1533. t = (ar + M[offset + zr[i]]) | 0;
  1534. if (i < 16) {
  1535. t += f5(br, cr, dr) + hr[0];
  1536. } else if (i < 32) {
  1537. t += f4(br, cr, dr) + hr[1];
  1538. } else if (i < 48) {
  1539. t += f3(br, cr, dr) + hr[2];
  1540. } else if (i < 64) {
  1541. t += f2(br, cr, dr) + hr[3];
  1542. } else {// if (i<80) {
  1543. t += f1(br, cr, dr) + hr[4];
  1544. }
  1545. t = t | 0;
  1546. t = rotl(t, sr[i]);
  1547. t = (t + er) | 0;
  1548. ar = er;
  1549. er = dr;
  1550. dr = rotl(cr, 10);
  1551. cr = br;
  1552. br = t;
  1553. }
  1554. // Intermediate hash value
  1555. t = (H[1] + cl + dr) | 0;
  1556. H[1] = (H[2] + dl + er) | 0;
  1557. H[2] = (H[3] + el + ar) | 0;
  1558. H[3] = (H[4] + al + br) | 0;
  1559. H[4] = (H[0] + bl + cr) | 0;
  1560. H[0] = t;
  1561. },
  1562. _doFinalize: function () {
  1563. // Shortcuts
  1564. var data = this._data;
  1565. var dataWords = data.words;
  1566. var nBitsTotal = this._nDataBytes * 8;
  1567. var nBitsLeft = data.sigBytes * 8;
  1568. // Add padding
  1569. dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
  1570. dataWords[(((nBitsLeft + 64) >>> 9) << 4) + 14] = (
  1571. (((nBitsTotal << 8) | (nBitsTotal >>> 24)) & 0x00ff00ff) |
  1572. (((nBitsTotal << 24) | (nBitsTotal >>> 8)) & 0xff00ff00)
  1573. );
  1574. data.sigBytes = (dataWords.length + 1) * 4;
  1575. // Hash final blocks
  1576. this._process();
  1577. // Shortcuts
  1578. var hash = this._hash;
  1579. var H = hash.words;
  1580. // Swap endian
  1581. for (var i = 0; i < 5; i++) {
  1582. // Shortcut
  1583. var H_i = H[i];
  1584. // Swap
  1585. H[i] = (((H_i << 8) | (H_i >>> 24)) & 0x00ff00ff) |
  1586. (((H_i << 24) | (H_i >>> 8)) & 0xff00ff00);
  1587. }
  1588. // Return final computed hash
  1589. return hash;
  1590. },
  1591. clone: function () {
  1592. var clone = Hasher.clone.call(this);
  1593. clone._hash = this._hash.clone();
  1594. return clone;
  1595. }
  1596. });
  1597. function f1(x, y, z) {
  1598. return ((x) ^ (y) ^ (z));
  1599. }
  1600. function f2(x, y, z) {
  1601. return (((x) & (y)) | ((~x) & (z)));
  1602. }
  1603. function f3(x, y, z) {
  1604. return (((x) | (~(y))) ^ (z));
  1605. }
  1606. function f4(x, y, z) {
  1607. return (((x) & (z)) | ((y) & (~(z))));
  1608. }
  1609. function f5(x, y, z) {
  1610. return ((x) ^ ((y) | (~(z))));
  1611. }
  1612. function rotl(x, n) {
  1613. return (x << n) | (x >>> (32 - n));
  1614. }
  1615. /**
  1616. * Shortcut function to the hasher's object interface.
  1617. *
  1618. * @param {WordArray|string} message The message to hash.
  1619. *
  1620. * @return {WordArray} The hash.
  1621. *
  1622. * @static
  1623. *
  1624. * @example
  1625. *
  1626. * var hash = CryptoJS.RIPEMD160('message');
  1627. * var hash = CryptoJS.RIPEMD160(wordArray);
  1628. */
  1629. C.RIPEMD160 = Hasher._createHelper(RIPEMD160);
  1630. /**
  1631. * Shortcut function to the HMAC's object interface.
  1632. *
  1633. * @param {WordArray|string} message The message to hash.
  1634. * @param {WordArray|string} key The secret key.
  1635. *
  1636. * @return {WordArray} The HMAC.
  1637. *
  1638. * @static
  1639. *
  1640. * @example
  1641. *
  1642. * var hmac = CryptoJS.HmacRIPEMD160(message, key);
  1643. */
  1644. C.HmacRIPEMD160 = Hasher._createHmacHelper(RIPEMD160);
  1645. }(Math));
  1646. (function () {
  1647. // Shortcuts
  1648. var C = CryptoJS;
  1649. var C_lib = C.lib;
  1650. var Base = C_lib.Base;
  1651. var C_enc = C.enc;
  1652. var Utf8 = C_enc.Utf8;
  1653. var C_algo = C.algo;
  1654. /**
  1655. * HMAC algorithm.
  1656. */
  1657. var HMAC = C_algo.HMAC = Base.extend({
  1658. /**
  1659. * Initializes a newly created HMAC.
  1660. *
  1661. * @param {Hasher} hasher The hash algorithm to use.
  1662. * @param {WordArray|string} key The secret key.
  1663. *
  1664. * @example
  1665. *
  1666. * var hmacHasher = CryptoJS.algo.HMAC.create(CryptoJS.algo.SHA256, key);
  1667. */
  1668. init: function (hasher, key) {
  1669. // Init hasher
  1670. hasher = this._hasher = new hasher.init();
  1671. // Convert string to WordArray, else assume WordArray already
  1672. if (typeof key == 'string') {
  1673. key = Utf8.parse(key);
  1674. }
  1675. // Shortcuts
  1676. var hasherBlockSize = hasher.blockSize;
  1677. var hasherBlockSizeBytes = hasherBlockSize * 4;
  1678. // Allow arbitrary length keys
  1679. if (key.sigBytes > hasherBlockSizeBytes) {
  1680. key = hasher.finalize(key);
  1681. }
  1682. // Clamp excess bits
  1683. key.clamp();
  1684. // Clone key for inner and outer pads
  1685. var oKey = this._oKey = key.clone();
  1686. var iKey = this._iKey = key.clone();
  1687. // Shortcuts
  1688. var oKeyWords = oKey.words;
  1689. var iKeyWords = iKey.words;
  1690. // XOR keys with pad constants
  1691. for (var i = 0; i < hasherBlockSize; i++) {
  1692. oKeyWords[i] ^= 0x5c5c5c5c;
  1693. iKeyWords[i] ^= 0x36363636;
  1694. }
  1695. oKey.sigBytes = iKey.sigBytes = hasherBlockSizeBytes;
  1696. // Set initial values
  1697. this.reset();
  1698. },
  1699. /**
  1700. * Resets this HMAC to its initial state.
  1701. *
  1702. * @example
  1703. *
  1704. * hmacHasher.reset();
  1705. */
  1706. reset: function () {
  1707. // Shortcut
  1708. var hasher = this._hasher;
  1709. // Reset
  1710. hasher.reset();
  1711. hasher.update(this._iKey);
  1712. },
  1713. /**
  1714. * Updates this HMAC with a message.
  1715. *
  1716. * @param {WordArray|string} messageUpdate The message to append.
  1717. *
  1718. * @return {HMAC} This HMAC instance.
  1719. *
  1720. * @example
  1721. *
  1722. * hmacHasher.update('message');
  1723. * hmacHasher.update(wordArray);
  1724. */
  1725. update: function (messageUpdate) {
  1726. this._hasher.update(messageUpdate);
  1727. // Chainable
  1728. return this;
  1729. },
  1730. /**
  1731. * Finalizes the HMAC computation.
  1732. * Note that the finalize operation is effectively a destructive, read-once operation.
  1733. *
  1734. * @param {WordArray|string} messageUpdate (Optional) A final message update.
  1735. *
  1736. * @return {WordArray} The HMAC.
  1737. *
  1738. * @example
  1739. *
  1740. * var hmac = hmacHasher.finalize();
  1741. * var hmac = hmacHasher.finalize('message');
  1742. * var hmac = hmacHasher.finalize(wordArray);
  1743. */
  1744. finalize: function (messageUpdate) {
  1745. // Shortcut
  1746. var hasher = this._hasher;
  1747. // Compute HMAC
  1748. var innerHash = hasher.finalize(messageUpdate);
  1749. hasher.reset();
  1750. var hmac = hasher.finalize(this._oKey.clone().concat(innerHash));
  1751. return hmac;
  1752. }
  1753. });
  1754. }());
  1755. (function () {
  1756. // Shortcuts
  1757. var C = CryptoJS;
  1758. var C_lib = C.lib;
  1759. var Base = C_lib.Base;
  1760. var WordArray = C_lib.WordArray;
  1761. var C_algo = C.algo;
  1762. var SHA1 = C_algo.SHA1;
  1763. var HMAC = C_algo.HMAC;
  1764. /**
  1765. * Password-Based Key Derivation Function 2 algorithm.
  1766. */
  1767. var PBKDF2 = C_algo.PBKDF2 = Base.extend({
  1768. /**
  1769. * Configuration options.
  1770. *
  1771. * @property {number} keySize The key size in words to generate. Default: 4 (128 bits)
  1772. * @property {Hasher} hasher The hasher to use. Default: SHA1
  1773. * @property {number} iterations The number of iterations to perform. Default: 1
  1774. */
  1775. cfg: Base.extend({
  1776. keySize: 128 / 32,
  1777. hasher: SHA1,
  1778. iterations: 1
  1779. }),
  1780. /**
  1781. * Initializes a newly created key derivation function.
  1782. *
  1783. * @param {Object} cfg (Optional) The configuration options to use for the derivation.
  1784. *
  1785. * @example
  1786. *
  1787. * var kdf = CryptoJS.algo.PBKDF2.create();
  1788. * var kdf = CryptoJS.algo.PBKDF2.create({ keySize: 8 });
  1789. * var kdf = CryptoJS.algo.PBKDF2.create({ keySize: 8, iterations: 1000 });
  1790. */
  1791. init: function (cfg) {
  1792. this.cfg = this.cfg.extend(cfg);
  1793. },
  1794. /**
  1795. * Computes the Password-Based Key Derivation Function 2.
  1796. *
  1797. * @param {WordArray|string} password The password.
  1798. * @param {WordArray|string} salt A salt.
  1799. *
  1800. * @return {WordArray} The derived key.
  1801. *
  1802. * @example
  1803. *
  1804. * var key = kdf.compute(password, salt);
  1805. */
  1806. compute: function (password, salt) {
  1807. // Shortcut
  1808. var cfg = this.cfg;
  1809. // Init HMAC
  1810. var hmac = HMAC.create(cfg.hasher, password);
  1811. // Initial values
  1812. var derivedKey = WordArray.create();
  1813. var blockIndex = WordArray.create([0x00000001]);
  1814. // Shortcuts
  1815. var derivedKeyWords = derivedKey.words;
  1816. var blockIndexWords = blockIndex.words;
  1817. var keySize = cfg.keySize;
  1818. var iterations = cfg.iterations;
  1819. // Generate key
  1820. while (derivedKeyWords.length < keySize) {
  1821. var block = hmac.update(salt).finalize(blockIndex);
  1822. hmac.reset();
  1823. // Shortcuts
  1824. var blockWords = block.words;
  1825. var blockWordsLength = blockWords.length;
  1826. // Iterations
  1827. var intermediate = block;
  1828. for (var i = 1; i < iterations; i++) {
  1829. intermediate = hmac.finalize(intermediate);
  1830. hmac.reset();
  1831. // Shortcut
  1832. var intermediateWords = intermediate.words;
  1833. // XOR intermediate with block
  1834. for (var j = 0; j < blockWordsLength; j++) {
  1835. blockWords[j] ^= intermediateWords[j];
  1836. }
  1837. }
  1838. derivedKey.concat(block);
  1839. blockIndexWords[0]++;
  1840. }
  1841. derivedKey.sigBytes = keySize * 4;
  1842. return derivedKey;
  1843. }
  1844. });
  1845. /**
  1846. * Computes the Password-Based Key Derivation Function 2.
  1847. *
  1848. * @param {WordArray|string} password The password.
  1849. * @param {WordArray|string} salt A salt.
  1850. * @param {Object} cfg (Optional) The configuration options to use for this computation.
  1851. *
  1852. * @return {WordArray} The derived key.
  1853. *
  1854. * @static
  1855. *
  1856. * @example
  1857. *
  1858. * var key = CryptoJS.PBKDF2(password, salt);
  1859. * var key = CryptoJS.PBKDF2(password, salt, { keySize: 8 });
  1860. * var key = CryptoJS.PBKDF2(password, salt, { keySize: 8, iterations: 1000 });
  1861. */
  1862. C.PBKDF2 = function (password, salt, cfg) {
  1863. return PBKDF2.create(cfg).compute(password, salt);
  1864. };
  1865. }());
  1866. (function () {
  1867. // Shortcuts
  1868. var C = CryptoJS;
  1869. var C_lib = C.lib;
  1870. var Base = C_lib.Base;
  1871. var WordArray = C_lib.WordArray;
  1872. var C_algo = C.algo;
  1873. var MD5 = C_algo.MD5;
  1874. /**
  1875. * This key derivation function is meant to conform with EVP_BytesToKey.
  1876. * www.openssl.org/docs/crypto/EVP_BytesToKey.html
  1877. */
  1878. var EvpKDF = C_algo.EvpKDF = Base.extend({
  1879. /**
  1880. * Configuration options.
  1881. *
  1882. * @property {number} keySize The key size in words to generate. Default: 4 (128 bits)
  1883. * @property {Hasher} hasher The hash algorithm to use. Default: MD5
  1884. * @property {number} iterations The number of iterations to perform. Default: 1
  1885. */
  1886. cfg: Base.extend({
  1887. keySize: 128 / 32,
  1888. hasher: MD5,
  1889. iterations: 1
  1890. }),
  1891. /**
  1892. * Initializes a newly created key derivation function.
  1893. *
  1894. * @param {Object} cfg (Optional) The configuration options to use for the derivation.
  1895. *
  1896. * @example
  1897. *
  1898. * var kdf = CryptoJS.algo.EvpKDF.create();
  1899. * var kdf = CryptoJS.algo.EvpKDF.create({ keySize: 8 });
  1900. * var kdf = CryptoJS.algo.EvpKDF.create({ keySize: 8, iterations: 1000 });
  1901. */
  1902. init: function (cfg) {
  1903. this.cfg = this.cfg.extend(cfg);
  1904. },
  1905. /**
  1906. * Derives a key from a password.
  1907. *
  1908. * @param {WordArray|string} password The password.
  1909. * @param {WordArray|string} salt A salt.
  1910. *
  1911. * @return {WordArray} The derived key.
  1912. *
  1913. * @example
  1914. *
  1915. * var key = kdf.compute(password, salt);
  1916. */
  1917. compute: function (password, salt) {
  1918. var block;
  1919. // Shortcut
  1920. var cfg = this.cfg;
  1921. // Init hasher
  1922. var hasher = cfg.hasher.create();
  1923. // Initial values
  1924. var derivedKey = WordArray.create();
  1925. // Shortcuts
  1926. var derivedKeyWords = derivedKey.words;
  1927. var keySize = cfg.keySize;
  1928. var iterations = cfg.iterations;
  1929. // Generate key
  1930. while (derivedKeyWords.length < keySize) {
  1931. if (block) {
  1932. hasher.update(block);
  1933. }
  1934. block = hasher.update(password).finalize(salt);
  1935. hasher.reset();
  1936. // Iterations
  1937. for (var i = 1; i < iterations; i++) {
  1938. block = hasher.finalize(block);
  1939. hasher.reset();
  1940. }
  1941. derivedKey.concat(block);
  1942. }
  1943. derivedKey.sigBytes = keySize * 4;
  1944. return derivedKey;
  1945. }
  1946. });
  1947. /**
  1948. * Derives a key from a password.
  1949. *
  1950. * @param {WordArray|string} password The password.
  1951. * @param {WordArray|string} salt A salt.
  1952. * @param {Object} cfg (Optional) The configuration options to use for this computation.
  1953. *
  1954. * @return {WordArray} The derived key.
  1955. *
  1956. * @static
  1957. *
  1958. * @example
  1959. *
  1960. * var key = CryptoJS.EvpKDF(password, salt);
  1961. * var key = CryptoJS.EvpKDF(password, salt, { keySize: 8 });
  1962. * var key = CryptoJS.EvpKDF(password, salt, { keySize: 8, iterations: 1000 });
  1963. */
  1964. C.EvpKDF = function (password, salt, cfg) {
  1965. return EvpKDF.create(cfg).compute(password, salt);
  1966. };
  1967. }());
  1968. (function () {
  1969. // Shortcuts
  1970. var C = CryptoJS;
  1971. var C_lib = C.lib;
  1972. var WordArray = C_lib.WordArray;
  1973. var C_algo = C.algo;
  1974. var SHA256 = C_algo.SHA256;
  1975. /**
  1976. * SHA-224 hash algorithm.
  1977. */
  1978. var SHA224 = C_algo.SHA224 = SHA256.extend({
  1979. _doReset: function () {
  1980. this._hash = new WordArray.init([
  1981. 0xc1059ed8, 0x367cd507, 0x3070dd17, 0xf70e5939,
  1982. 0xffc00b31, 0x68581511, 0x64f98fa7, 0xbefa4fa4
  1983. ]);
  1984. },
  1985. _doFinalize: function () {
  1986. var hash = SHA256._doFinalize.call(this);
  1987. hash.sigBytes -= 4;
  1988. return hash;
  1989. }
  1990. });
  1991. /**
  1992. * Shortcut function to the hasher's object interface.
  1993. *
  1994. * @param {WordArray|string} message The message to hash.
  1995. *
  1996. * @return {WordArray} The hash.
  1997. *
  1998. * @static
  1999. *
  2000. * @example
  2001. *
  2002. * var hash = CryptoJS.SHA224('message');
  2003. * var hash = CryptoJS.SHA224(wordArray);
  2004. */
  2005. C.SHA224 = SHA256._createHelper(SHA224);
  2006. /**
  2007. * Shortcut function to the HMAC's object interface.
  2008. *
  2009. * @param {WordArray|string} message The message to hash.
  2010. * @param {WordArray|string} key The secret key.
  2011. *
  2012. * @return {WordArray} The HMAC.
  2013. *
  2014. * @static
  2015. *
  2016. * @example
  2017. *
  2018. * var hmac = CryptoJS.HmacSHA224(message, key);
  2019. */
  2020. C.HmacSHA224 = SHA256._createHmacHelper(SHA224);
  2021. }());
  2022. (function (undefined) {
  2023. // Shortcuts
  2024. var C = CryptoJS;
  2025. var C_lib = C.lib;
  2026. var Base = C_lib.Base;
  2027. var X32WordArray = C_lib.WordArray;
  2028. /**
  2029. * x64 namespace.
  2030. */
  2031. var C_x64 = C.x64 = {};
  2032. /**
  2033. * A 64-bit word.
  2034. */
  2035. var X64Word = C_x64.Word = Base.extend({
  2036. /**
  2037. * Initializes a newly created 64-bit word.
  2038. *
  2039. * @param {number} high The high 32 bits.
  2040. * @param {number} low The low 32 bits.
  2041. *
  2042. * @example
  2043. *
  2044. * var x64Word = CryptoJS.x64.Word.create(0x00010203, 0x04050607);
  2045. */
  2046. init: function (high, low) {
  2047. this.high = high;
  2048. this.low = low;
  2049. }
  2050. /**
  2051. * Bitwise NOTs this word.
  2052. *
  2053. * @return {X64Word} A new x64-Word object after negating.
  2054. *
  2055. * @example
  2056. *
  2057. * var negated = x64Word.not();
  2058. */
  2059. // not: function () {
  2060. // var high = ~this.high;
  2061. // var low = ~this.low;
  2062. // return X64Word.create(high, low);
  2063. // },
  2064. /**
  2065. * Bitwise ANDs this word with the passed word.
  2066. *
  2067. * @param {X64Word} word The x64-Word to AND with this word.
  2068. *
  2069. * @return {X64Word} A new x64-Word object after ANDing.
  2070. *
  2071. * @example
  2072. *
  2073. * var anded = x64Word.and(anotherX64Word);
  2074. */
  2075. // and: function (word) {
  2076. // var high = this.high & word.high;
  2077. // var low = this.low & word.low;
  2078. // return X64Word.create(high, low);
  2079. // },
  2080. /**
  2081. * Bitwise ORs this word with the passed word.
  2082. *
  2083. * @param {X64Word} word The x64-Word to OR with this word.
  2084. *
  2085. * @return {X64Word} A new x64-Word object after ORing.
  2086. *
  2087. * @example
  2088. *
  2089. * var ored = x64Word.or(anotherX64Word);
  2090. */
  2091. // or: function (word) {
  2092. // var high = this.high | word.high;
  2093. // var low = this.low | word.low;
  2094. // return X64Word.create(high, low);
  2095. // },
  2096. /**
  2097. * Bitwise XORs this word with the passed word.
  2098. *
  2099. * @param {X64Word} word The x64-Word to XOR with this word.
  2100. *
  2101. * @return {X64Word} A new x64-Word object after XORing.
  2102. *
  2103. * @example
  2104. *
  2105. * var xored = x64Word.xor(anotherX64Word);
  2106. */
  2107. // xor: function (word) {
  2108. // var high = this.high ^ word.high;
  2109. // var low = this.low ^ word.low;
  2110. // return X64Word.create(high, low);
  2111. // },
  2112. /**
  2113. * Shifts this word n bits to the left.
  2114. *
  2115. * @param {number} n The number of bits to shift.
  2116. *
  2117. * @return {X64Word} A new x64-Word object after shifting.
  2118. *
  2119. * @example
  2120. *
  2121. * var shifted = x64Word.shiftL(25);
  2122. */
  2123. // shiftL: function (n) {
  2124. // if (n < 32) {
  2125. // var high = (this.high << n) | (this.low >>> (32 - n));
  2126. // var low = this.low << n;
  2127. // } else {
  2128. // var high = this.low << (n - 32);
  2129. // var low = 0;
  2130. // }
  2131. // return X64Word.create(high, low);
  2132. // },
  2133. /**
  2134. * Shifts this word n bits to the right.
  2135. *
  2136. * @param {number} n The number of bits to shift.
  2137. *
  2138. * @return {X64Word} A new x64-Word object after shifting.
  2139. *
  2140. * @example
  2141. *
  2142. * var shifted = x64Word.shiftR(7);
  2143. */
  2144. // shiftR: function (n) {
  2145. // if (n < 32) {
  2146. // var low = (this.low >>> n) | (this.high << (32 - n));
  2147. // var high = this.high >>> n;
  2148. // } else {
  2149. // var low = this.high >>> (n - 32);
  2150. // var high = 0;
  2151. // }
  2152. // return X64Word.create(high, low);
  2153. // },
  2154. /**
  2155. * Rotates this word n bits to the left.
  2156. *
  2157. * @param {number} n The number of bits to rotate.
  2158. *
  2159. * @return {X64Word} A new x64-Word object after rotating.
  2160. *
  2161. * @example
  2162. *
  2163. * var rotated = x64Word.rotL(25);
  2164. */
  2165. // rotL: function (n) {
  2166. // return this.shiftL(n).or(this.shiftR(64 - n));
  2167. // },
  2168. /**
  2169. * Rotates this word n bits to the right.
  2170. *
  2171. * @param {number} n The number of bits to rotate.
  2172. *
  2173. * @return {X64Word} A new x64-Word object after rotating.
  2174. *
  2175. * @example
  2176. *
  2177. * var rotated = x64Word.rotR(7);
  2178. */
  2179. // rotR: function (n) {
  2180. // return this.shiftR(n).or(this.shiftL(64 - n));
  2181. // },
  2182. /**
  2183. * Adds this word with the passed word.
  2184. *
  2185. * @param {X64Word} word The x64-Word to add with this word.
  2186. *
  2187. * @return {X64Word} A new x64-Word object after adding.
  2188. *
  2189. * @example
  2190. *
  2191. * var added = x64Word.add(anotherX64Word);
  2192. */
  2193. // add: function (word) {
  2194. // var low = (this.low + word.low) | 0;
  2195. // var carry = (low >>> 0) < (this.low >>> 0) ? 1 : 0;
  2196. // var high = (this.high + word.high + carry) | 0;
  2197. // return X64Word.create(high, low);
  2198. // }
  2199. });
  2200. /**
  2201. * An array of 64-bit words.
  2202. *
  2203. * @property {Array} words The array of CryptoJS.x64.Word objects.
  2204. * @property {number} sigBytes The number of significant bytes in this word array.
  2205. */
  2206. var X64WordArray = C_x64.WordArray = Base.extend({
  2207. /**
  2208. * Initializes a newly created word array.
  2209. *
  2210. * @param {Array} words (Optional) An array of CryptoJS.x64.Word objects.
  2211. * @param {number} sigBytes (Optional) The number of significant bytes in the words.
  2212. *
  2213. * @example
  2214. *
  2215. * var wordArray = CryptoJS.x64.WordArray.create();
  2216. *
  2217. * var wordArray = CryptoJS.x64.WordArray.create([
  2218. * CryptoJS.x64.Word.create(0x00010203, 0x04050607),
  2219. * CryptoJS.x64.Word.create(0x18191a1b, 0x1c1d1e1f)
  2220. * ]);
  2221. *
  2222. * var wordArray = CryptoJS.x64.WordArray.create([
  2223. * CryptoJS.x64.Word.create(0x00010203, 0x04050607),
  2224. * CryptoJS.x64.Word.create(0x18191a1b, 0x1c1d1e1f)
  2225. * ], 10);
  2226. */
  2227. init: function (words, sigBytes) {
  2228. words = this.words = words || [];
  2229. if (sigBytes != undefined) {
  2230. this.sigBytes = sigBytes;
  2231. } else {
  2232. this.sigBytes = words.length * 8;
  2233. }
  2234. },
  2235. /**
  2236. * Converts this 64-bit word array to a 32-bit word array.
  2237. *
  2238. * @return {CryptoJS.lib.WordArray} This word array's data as a 32-bit word array.
  2239. *
  2240. * @example
  2241. *
  2242. * var x32WordArray = x64WordArray.toX32();
  2243. */
  2244. toX32: function () {
  2245. // Shortcuts
  2246. var x64Words = this.words;
  2247. var x64WordsLength = x64Words.length;
  2248. // Convert
  2249. var x32Words = [];
  2250. for (var i = 0; i < x64WordsLength; i++) {
  2251. var x64Word = x64Words[i];
  2252. x32Words.push(x64Word.high);
  2253. x32Words.push(x64Word.low);
  2254. }
  2255. return X32WordArray.create(x32Words, this.sigBytes);
  2256. },
  2257. /**
  2258. * Creates a copy of this word array.
  2259. *
  2260. * @return {X64WordArray} The clone.
  2261. *
  2262. * @example
  2263. *
  2264. * var clone = x64WordArray.clone();
  2265. */
  2266. clone: function () {
  2267. var clone = Base.clone.call(this);
  2268. // Clone "words" array
  2269. var words = clone.words = this.words.slice(0);
  2270. // Clone each X64Word object
  2271. var wordsLength = words.length;
  2272. for (var i = 0; i < wordsLength; i++) {
  2273. words[i] = words[i].clone();
  2274. }
  2275. return clone;
  2276. }
  2277. });
  2278. }());
  2279. (function (Math) {
  2280. // Shortcuts
  2281. var C = CryptoJS;
  2282. var C_lib = C.lib;
  2283. var WordArray = C_lib.WordArray;
  2284. var Hasher = C_lib.Hasher;
  2285. var C_x64 = C.x64;
  2286. var X64Word = C_x64.Word;
  2287. var C_algo = C.algo;
  2288. // Constants tables
  2289. var RHO_OFFSETS = [];
  2290. var PI_INDEXES = [];
  2291. var ROUND_CONSTANTS = [];
  2292. // Compute Constants
  2293. (function () {
  2294. // Compute rho offset constants
  2295. var x = 1, y = 0;
  2296. for (var t = 0; t < 24; t++) {
  2297. RHO_OFFSETS[x + 5 * y] = ((t + 1) * (t + 2) / 2) % 64;
  2298. var newX = y % 5;
  2299. var newY = (2 * x + 3 * y) % 5;
  2300. x = newX;
  2301. y = newY;
  2302. }
  2303. // Compute pi index constants
  2304. for (var x = 0; x < 5; x++) {
  2305. for (var y = 0; y < 5; y++) {
  2306. PI_INDEXES[x + 5 * y] = y + ((2 * x + 3 * y) % 5) * 5;
  2307. }
  2308. }
  2309. // Compute round constants
  2310. var LFSR = 0x01;
  2311. for (var i = 0; i < 24; i++) {
  2312. var roundConstantMsw = 0;
  2313. var roundConstantLsw = 0;
  2314. for (var j = 0; j < 7; j++) {
  2315. if (LFSR & 0x01) {
  2316. var bitPosition = (1 << j) - 1;
  2317. if (bitPosition < 32) {
  2318. roundConstantLsw ^= 1 << bitPosition;
  2319. } else /* if (bitPosition >= 32) */ {
  2320. roundConstantMsw ^= 1 << (bitPosition - 32);
  2321. }
  2322. }
  2323. // Compute next LFSR
  2324. if (LFSR & 0x80) {
  2325. // Primitive polynomial over GF(2): x^8 + x^6 + x^5 + x^4 + 1
  2326. LFSR = (LFSR << 1) ^ 0x71;
  2327. } else {
  2328. LFSR <<= 1;
  2329. }
  2330. }
  2331. ROUND_CONSTANTS[i] = X64Word.create(roundConstantMsw, roundConstantLsw);
  2332. }
  2333. }());
  2334. // Reusable objects for temporary values
  2335. var T = [];
  2336. (function () {
  2337. for (var i = 0; i < 25; i++) {
  2338. T[i] = X64Word.create();
  2339. }
  2340. }());
  2341. /**
  2342. * SHA-3 hash algorithm.
  2343. */
  2344. var SHA3 = C_algo.SHA3 = Hasher.extend({
  2345. /**
  2346. * Configuration options.
  2347. *
  2348. * @property {number} outputLength
  2349. * The desired number of bits in the output hash.
  2350. * Only values permitted are: 224, 256, 384, 512.
  2351. * Default: 512
  2352. */
  2353. cfg: Hasher.cfg.extend({
  2354. outputLength: 512
  2355. }),
  2356. _doReset: function () {
  2357. var state = this._state = []
  2358. for (var i = 0; i < 25; i++) {
  2359. state[i] = new X64Word.init();
  2360. }
  2361. this.blockSize = (1600 - 2 * this.cfg.outputLength) / 32;
  2362. },
  2363. _doProcessBlock: function (M, offset) {
  2364. // Shortcuts
  2365. var state = this._state;
  2366. var nBlockSizeLanes = this.blockSize / 2;
  2367. // Absorb
  2368. for (var i = 0; i < nBlockSizeLanes; i++) {
  2369. // Shortcuts
  2370. var M2i = M[offset + 2 * i];
  2371. var M2i1 = M[offset + 2 * i + 1];
  2372. // Swap endian
  2373. M2i = (
  2374. (((M2i << 8) | (M2i >>> 24)) & 0x00ff00ff) |
  2375. (((M2i << 24) | (M2i >>> 8)) & 0xff00ff00)
  2376. );
  2377. M2i1 = (
  2378. (((M2i1 << 8) | (M2i1 >>> 24)) & 0x00ff00ff) |
  2379. (((M2i1 << 24) | (M2i1 >>> 8)) & 0xff00ff00)
  2380. );
  2381. // Absorb message into state
  2382. var lane = state[i];
  2383. lane.high ^= M2i1;
  2384. lane.low ^= M2i;
  2385. }
  2386. // Rounds
  2387. for (var round = 0; round < 24; round++) {
  2388. // Theta
  2389. for (var x = 0; x < 5; x++) {
  2390. // Mix column lanes
  2391. var tMsw = 0, tLsw = 0;
  2392. for (var y = 0; y < 5; y++) {
  2393. var lane = state[x + 5 * y];
  2394. tMsw ^= lane.high;
  2395. tLsw ^= lane.low;
  2396. }
  2397. // Temporary values
  2398. var Tx = T[x];
  2399. Tx.high = tMsw;
  2400. Tx.low = tLsw;
  2401. }
  2402. for (var x = 0; x < 5; x++) {
  2403. // Shortcuts
  2404. var Tx4 = T[(x + 4) % 5];
  2405. var Tx1 = T[(x + 1) % 5];
  2406. var Tx1Msw = Tx1.high;
  2407. var Tx1Lsw = Tx1.low;
  2408. // Mix surrounding columns
  2409. var tMsw = Tx4.high ^ ((Tx1Msw << 1) | (Tx1Lsw >>> 31));
  2410. var tLsw = Tx4.low ^ ((Tx1Lsw << 1) | (Tx1Msw >>> 31));
  2411. for (var y = 0; y < 5; y++) {
  2412. var lane = state[x + 5 * y];
  2413. lane.high ^= tMsw;
  2414. lane.low ^= tLsw;
  2415. }
  2416. }
  2417. // Rho Pi
  2418. for (var laneIndex = 1; laneIndex < 25; laneIndex++) {
  2419. var tMsw;
  2420. var tLsw;
  2421. // Shortcuts
  2422. var lane = state[laneIndex];
  2423. var laneMsw = lane.high;
  2424. var laneLsw = lane.low;
  2425. var rhoOffset = RHO_OFFSETS[laneIndex];
  2426. // Rotate lanes
  2427. if (rhoOffset < 32) {
  2428. tMsw = (laneMsw << rhoOffset) | (laneLsw >>> (32 - rhoOffset));
  2429. tLsw = (laneLsw << rhoOffset) | (laneMsw >>> (32 - rhoOffset));
  2430. } else /* if (rhoOffset >= 32) */ {
  2431. tMsw = (laneLsw << (rhoOffset - 32)) | (laneMsw >>> (64 - rhoOffset));
  2432. tLsw = (laneMsw << (rhoOffset - 32)) | (laneLsw >>> (64 - rhoOffset));
  2433. }
  2434. // Transpose lanes
  2435. var TPiLane = T[PI_INDEXES[laneIndex]];
  2436. TPiLane.high = tMsw;
  2437. TPiLane.low = tLsw;
  2438. }
  2439. // Rho pi at x = y = 0
  2440. var T0 = T[0];
  2441. var state0 = state[0];
  2442. T0.high = state0.high;
  2443. T0.low = state0.low;
  2444. // Chi
  2445. for (var x = 0; x < 5; x++) {
  2446. for (var y = 0; y < 5; y++) {
  2447. // Shortcuts
  2448. var laneIndex = x + 5 * y;
  2449. var lane = state[laneIndex];
  2450. var TLane = T[laneIndex];
  2451. var Tx1Lane = T[((x + 1) % 5) + 5 * y];
  2452. var Tx2Lane = T[((x + 2) % 5) + 5 * y];
  2453. // Mix rows
  2454. lane.high = TLane.high ^ (~Tx1Lane.high & Tx2Lane.high);
  2455. lane.low = TLane.low ^ (~Tx1Lane.low & Tx2Lane.low);
  2456. }
  2457. }
  2458. // Iota
  2459. var lane = state[0];
  2460. var roundConstant = ROUND_CONSTANTS[round];
  2461. lane.high ^= roundConstant.high;
  2462. lane.low ^= roundConstant.low;
  2463. }
  2464. },
  2465. _doFinalize: function () {
  2466. // Shortcuts
  2467. var data = this._data;
  2468. var dataWords = data.words;
  2469. var nBitsTotal = this._nDataBytes * 8;
  2470. var nBitsLeft = data.sigBytes * 8;
  2471. var blockSizeBits = this.blockSize * 32;
  2472. // Add padding
  2473. dataWords[nBitsLeft >>> 5] |= 0x1 << (24 - nBitsLeft % 32);
  2474. dataWords[((Math.ceil((nBitsLeft + 1) / blockSizeBits) * blockSizeBits) >>> 5) - 1] |= 0x80;
  2475. data.sigBytes = dataWords.length * 4;
  2476. // Hash final blocks
  2477. this._process();
  2478. // Shortcuts
  2479. var state = this._state;
  2480. var outputLengthBytes = this.cfg.outputLength / 8;
  2481. var outputLengthLanes = outputLengthBytes / 8;
  2482. // Squeeze
  2483. var hashWords = [];
  2484. for (var i = 0; i < outputLengthLanes; i++) {
  2485. // Shortcuts
  2486. var lane = state[i];
  2487. var laneMsw = lane.high;
  2488. var laneLsw = lane.low;
  2489. // Swap endian
  2490. laneMsw = (
  2491. (((laneMsw << 8) | (laneMsw >>> 24)) & 0x00ff00ff) |
  2492. (((laneMsw << 24) | (laneMsw >>> 8)) & 0xff00ff00)
  2493. );
  2494. laneLsw = (
  2495. (((laneLsw << 8) | (laneLsw >>> 24)) & 0x00ff00ff) |
  2496. (((laneLsw << 24) | (laneLsw >>> 8)) & 0xff00ff00)
  2497. );
  2498. // Squeeze state to retrieve hash
  2499. hashWords.push(laneLsw);
  2500. hashWords.push(laneMsw);
  2501. }
  2502. // Return final computed hash
  2503. return new WordArray.init(hashWords, outputLengthBytes);
  2504. },
  2505. clone: function () {
  2506. var clone = Hasher.clone.call(this);
  2507. var state = clone._state = this._state.slice(0);
  2508. for (var i = 0; i < 25; i++) {
  2509. state[i] = state[i].clone();
  2510. }
  2511. return clone;
  2512. }
  2513. });
  2514. /**
  2515. * Shortcut function to the hasher's object interface.
  2516. *
  2517. * @param {WordArray|string} message The message to hash.
  2518. *
  2519. * @return {WordArray} The hash.
  2520. *
  2521. * @static
  2522. *
  2523. * @example
  2524. *
  2525. * var hash = CryptoJS.SHA3('message');
  2526. * var hash = CryptoJS.SHA3(wordArray);
  2527. */
  2528. C.SHA3 = Hasher._createHelper(SHA3);
  2529. /**
  2530. * Shortcut function to the HMAC's object interface.
  2531. *
  2532. * @param {WordArray|string} message The message to hash.
  2533. * @param {WordArray|string} key The secret key.
  2534. *
  2535. * @return {WordArray} The HMAC.
  2536. *
  2537. * @static
  2538. *
  2539. * @example
  2540. *
  2541. * var hmac = CryptoJS.HmacSHA3(message, key);
  2542. */
  2543. C.HmacSHA3 = Hasher._createHmacHelper(SHA3);
  2544. }(Math));
  2545. (function () {
  2546. // Shortcuts
  2547. var C = CryptoJS;
  2548. var C_lib = C.lib;
  2549. var Hasher = C_lib.Hasher;
  2550. var C_x64 = C.x64;
  2551. var X64Word = C_x64.Word;
  2552. var X64WordArray = C_x64.WordArray;
  2553. var C_algo = C.algo;
  2554. function X64Word_create() {
  2555. return X64Word.create.apply(X64Word, arguments);
  2556. }
  2557. // Constants
  2558. var K = [
  2559. X64Word_create(0x428a2f98, 0xd728ae22), X64Word_create(0x71374491, 0x23ef65cd),
  2560. X64Word_create(0xb5c0fbcf, 0xec4d3b2f), X64Word_create(0xe9b5dba5, 0x8189dbbc),
  2561. X64Word_create(0x3956c25b, 0xf348b538), X64Word_create(0x59f111f1, 0xb605d019),
  2562. X64Word_create(0x923f82a4, 0xaf194f9b), X64Word_create(0xab1c5ed5, 0xda6d8118),
  2563. X64Word_create(0xd807aa98, 0xa3030242), X64Word_create(0x12835b01, 0x45706fbe),
  2564. X64Word_create(0x243185be, 0x4ee4b28c), X64Word_create(0x550c7dc3, 0xd5ffb4e2),
  2565. X64Word_create(0x72be5d74, 0xf27b896f), X64Word_create(0x80deb1fe, 0x3b1696b1),
  2566. X64Word_create(0x9bdc06a7, 0x25c71235), X64Word_create(0xc19bf174, 0xcf692694),
  2567. X64Word_create(0xe49b69c1, 0x9ef14ad2), X64Word_create(0xefbe4786, 0x384f25e3),
  2568. X64Word_create(0x0fc19dc6, 0x8b8cd5b5), X64Word_create(0x240ca1cc, 0x77ac9c65),
  2569. X64Word_create(0x2de92c6f, 0x592b0275), X64Word_create(0x4a7484aa, 0x6ea6e483),
  2570. X64Word_create(0x5cb0a9dc, 0xbd41fbd4), X64Word_create(0x76f988da, 0x831153b5),
  2571. X64Word_create(0x983e5152, 0xee66dfab), X64Word_create(0xa831c66d, 0x2db43210),
  2572. X64Word_create(0xb00327c8, 0x98fb213f), X64Word_create(0xbf597fc7, 0xbeef0ee4),
  2573. X64Word_create(0xc6e00bf3, 0x3da88fc2), X64Word_create(0xd5a79147, 0x930aa725),
  2574. X64Word_create(0x06ca6351, 0xe003826f), X64Word_create(0x14292967, 0x0a0e6e70),
  2575. X64Word_create(0x27b70a85, 0x46d22ffc), X64Word_create(0x2e1b2138, 0x5c26c926),
  2576. X64Word_create(0x4d2c6dfc, 0x5ac42aed), X64Word_create(0x53380d13, 0x9d95b3df),
  2577. X64Word_create(0x650a7354, 0x8baf63de), X64Word_create(0x766a0abb, 0x3c77b2a8),
  2578. X64Word_create(0x81c2c92e, 0x47edaee6), X64Word_create(0x92722c85, 0x1482353b),
  2579. X64Word_create(0xa2bfe8a1, 0x4cf10364), X64Word_create(0xa81a664b, 0xbc423001),
  2580. X64Word_create(0xc24b8b70, 0xd0f89791), X64Word_create(0xc76c51a3, 0x0654be30),
  2581. X64Word_create(0xd192e819, 0xd6ef5218), X64Word_create(0xd6990624, 0x5565a910),
  2582. X64Word_create(0xf40e3585, 0x5771202a), X64Word_create(0x106aa070, 0x32bbd1b8),
  2583. X64Word_create(0x19a4c116, 0xb8d2d0c8), X64Word_create(0x1e376c08, 0x5141ab53),
  2584. X64Word_create(0x2748774c, 0xdf8eeb99), X64Word_create(0x34b0bcb5, 0xe19b48a8),
  2585. X64Word_create(0x391c0cb3, 0xc5c95a63), X64Word_create(0x4ed8aa4a, 0xe3418acb),
  2586. X64Word_create(0x5b9cca4f, 0x7763e373), X64Word_create(0x682e6ff3, 0xd6b2b8a3),
  2587. X64Word_create(0x748f82ee, 0x5defb2fc), X64Word_create(0x78a5636f, 0x43172f60),
  2588. X64Word_create(0x84c87814, 0xa1f0ab72), X64Word_create(0x8cc70208, 0x1a6439ec),
  2589. X64Word_create(0x90befffa, 0x23631e28), X64Word_create(0xa4506ceb, 0xde82bde9),
  2590. X64Word_create(0xbef9a3f7, 0xb2c67915), X64Word_create(0xc67178f2, 0xe372532b),
  2591. X64Word_create(0xca273ece, 0xea26619c), X64Word_create(0xd186b8c7, 0x21c0c207),
  2592. X64Word_create(0xeada7dd6, 0xcde0eb1e), X64Word_create(0xf57d4f7f, 0xee6ed178),
  2593. X64Word_create(0x06f067aa, 0x72176fba), X64Word_create(0x0a637dc5, 0xa2c898a6),
  2594. X64Word_create(0x113f9804, 0xbef90dae), X64Word_create(0x1b710b35, 0x131c471b),
  2595. X64Word_create(0x28db77f5, 0x23047d84), X64Word_create(0x32caab7b, 0x40c72493),
  2596. X64Word_create(0x3c9ebe0a, 0x15c9bebc), X64Word_create(0x431d67c4, 0x9c100d4c),
  2597. X64Word_create(0x4cc5d4be, 0xcb3e42b6), X64Word_create(0x597f299c, 0xfc657e2a),
  2598. X64Word_create(0x5fcb6fab, 0x3ad6faec), X64Word_create(0x6c44198c, 0x4a475817)
  2599. ];
  2600. // Reusable objects
  2601. var W = [];
  2602. (function () {
  2603. for (var i = 0; i < 80; i++) {
  2604. W[i] = X64Word_create();
  2605. }
  2606. }());
  2607. /**
  2608. * SHA-512 hash algorithm.
  2609. */
  2610. var SHA512 = C_algo.SHA512 = Hasher.extend({
  2611. _doReset: function () {
  2612. this._hash = new X64WordArray.init([
  2613. new X64Word.init(0x6a09e667, 0xf3bcc908), new X64Word.init(0xbb67ae85, 0x84caa73b),
  2614. new X64Word.init(0x3c6ef372, 0xfe94f82b), new X64Word.init(0xa54ff53a, 0x5f1d36f1),
  2615. new X64Word.init(0x510e527f, 0xade682d1), new X64Word.init(0x9b05688c, 0x2b3e6c1f),
  2616. new X64Word.init(0x1f83d9ab, 0xfb41bd6b), new X64Word.init(0x5be0cd19, 0x137e2179)
  2617. ]);
  2618. },
  2619. _doProcessBlock: function (M, offset) {
  2620. // Shortcuts
  2621. var H = this._hash.words;
  2622. var H0 = H[0];
  2623. var H1 = H[1];
  2624. var H2 = H[2];
  2625. var H3 = H[3];
  2626. var H4 = H[4];
  2627. var H5 = H[5];
  2628. var H6 = H[6];
  2629. var H7 = H[7];
  2630. var H0h = H0.high;
  2631. var H0l = H0.low;
  2632. var H1h = H1.high;
  2633. var H1l = H1.low;
  2634. var H2h = H2.high;
  2635. var H2l = H2.low;
  2636. var H3h = H3.high;
  2637. var H3l = H3.low;
  2638. var H4h = H4.high;
  2639. var H4l = H4.low;
  2640. var H5h = H5.high;
  2641. var H5l = H5.low;
  2642. var H6h = H6.high;
  2643. var H6l = H6.low;
  2644. var H7h = H7.high;
  2645. var H7l = H7.low;
  2646. // Working variables
  2647. var ah = H0h;
  2648. var al = H0l;
  2649. var bh = H1h;
  2650. var bl = H1l;
  2651. var ch = H2h;
  2652. var cl = H2l;
  2653. var dh = H3h;
  2654. var dl = H3l;
  2655. var eh = H4h;
  2656. var el = H4l;
  2657. var fh = H5h;
  2658. var fl = H5l;
  2659. var gh = H6h;
  2660. var gl = H6l;
  2661. var hh = H7h;
  2662. var hl = H7l;
  2663. // Rounds
  2664. for (var i = 0; i < 80; i++) {
  2665. var Wil;
  2666. var Wih;
  2667. // Shortcut
  2668. var Wi = W[i];
  2669. // Extend message
  2670. if (i < 16) {
  2671. Wih = Wi.high = M[offset + i * 2] | 0;
  2672. Wil = Wi.low = M[offset + i * 2 + 1] | 0;
  2673. } else {
  2674. // Gamma0
  2675. var gamma0x = W[i - 15];
  2676. var gamma0xh = gamma0x.high;
  2677. var gamma0xl = gamma0x.low;
  2678. var gamma0h = ((gamma0xh >>> 1) | (gamma0xl << 31)) ^ ((gamma0xh >>> 8) | (gamma0xl << 24)) ^ (gamma0xh >>> 7);
  2679. var gamma0l = ((gamma0xl >>> 1) | (gamma0xh << 31)) ^ ((gamma0xl >>> 8) | (gamma0xh << 24)) ^ ((gamma0xl >>> 7) | (gamma0xh << 25));
  2680. // Gamma1
  2681. var gamma1x = W[i - 2];
  2682. var gamma1xh = gamma1x.high;
  2683. var gamma1xl = gamma1x.low;
  2684. var gamma1h = ((gamma1xh >>> 19) | (gamma1xl << 13)) ^ ((gamma1xh << 3) | (gamma1xl >>> 29)) ^ (gamma1xh >>> 6);
  2685. var gamma1l = ((gamma1xl >>> 19) | (gamma1xh << 13)) ^ ((gamma1xl << 3) | (gamma1xh >>> 29)) ^ ((gamma1xl >>> 6) | (gamma1xh << 26));
  2686. // W[i] = gamma0 + W[i - 7] + gamma1 + W[i - 16]
  2687. var Wi7 = W[i - 7];
  2688. var Wi7h = Wi7.high;
  2689. var Wi7l = Wi7.low;
  2690. var Wi16 = W[i - 16];
  2691. var Wi16h = Wi16.high;
  2692. var Wi16l = Wi16.low;
  2693. Wil = gamma0l + Wi7l;
  2694. Wih = gamma0h + Wi7h + ((Wil >>> 0) < (gamma0l >>> 0) ? 1 : 0);
  2695. Wil = Wil + gamma1l;
  2696. Wih = Wih + gamma1h + ((Wil >>> 0) < (gamma1l >>> 0) ? 1 : 0);
  2697. Wil = Wil + Wi16l;
  2698. Wih = Wih + Wi16h + ((Wil >>> 0) < (Wi16l >>> 0) ? 1 : 0);
  2699. Wi.high = Wih;
  2700. Wi.low = Wil;
  2701. }
  2702. var chh = (eh & fh) ^ (~eh & gh);
  2703. var chl = (el & fl) ^ (~el & gl);
  2704. var majh = (ah & bh) ^ (ah & ch) ^ (bh & ch);
  2705. var majl = (al & bl) ^ (al & cl) ^ (bl & cl);
  2706. var sigma0h = ((ah >>> 28) | (al << 4)) ^ ((ah << 30) | (al >>> 2)) ^ ((ah << 25) | (al >>> 7));
  2707. var sigma0l = ((al >>> 28) | (ah << 4)) ^ ((al << 30) | (ah >>> 2)) ^ ((al << 25) | (ah >>> 7));
  2708. var sigma1h = ((eh >>> 14) | (el << 18)) ^ ((eh >>> 18) | (el << 14)) ^ ((eh << 23) | (el >>> 9));
  2709. var sigma1l = ((el >>> 14) | (eh << 18)) ^ ((el >>> 18) | (eh << 14)) ^ ((el << 23) | (eh >>> 9));
  2710. // t1 = h + sigma1 + ch + K[i] + W[i]
  2711. var Ki = K[i];
  2712. var Kih = Ki.high;
  2713. var Kil = Ki.low;
  2714. var t1l = hl + sigma1l;
  2715. var t1h = hh + sigma1h + ((t1l >>> 0) < (hl >>> 0) ? 1 : 0);
  2716. var t1l = t1l + chl;
  2717. var t1h = t1h + chh + ((t1l >>> 0) < (chl >>> 0) ? 1 : 0);
  2718. var t1l = t1l + Kil;
  2719. var t1h = t1h + Kih + ((t1l >>> 0) < (Kil >>> 0) ? 1 : 0);
  2720. var t1l = t1l + Wil;
  2721. var t1h = t1h + Wih + ((t1l >>> 0) < (Wil >>> 0) ? 1 : 0);
  2722. // t2 = sigma0 + maj
  2723. var t2l = sigma0l + majl;
  2724. var t2h = sigma0h + majh + ((t2l >>> 0) < (sigma0l >>> 0) ? 1 : 0);
  2725. // Update working variables
  2726. hh = gh;
  2727. hl = gl;
  2728. gh = fh;
  2729. gl = fl;
  2730. fh = eh;
  2731. fl = el;
  2732. el = (dl + t1l) | 0;
  2733. eh = (dh + t1h + ((el >>> 0) < (dl >>> 0) ? 1 : 0)) | 0;
  2734. dh = ch;
  2735. dl = cl;
  2736. ch = bh;
  2737. cl = bl;
  2738. bh = ah;
  2739. bl = al;
  2740. al = (t1l + t2l) | 0;
  2741. ah = (t1h + t2h + ((al >>> 0) < (t1l >>> 0) ? 1 : 0)) | 0;
  2742. }
  2743. // Intermediate hash value
  2744. H0l = H0.low = (H0l + al);
  2745. H0.high = (H0h + ah + ((H0l >>> 0) < (al >>> 0) ? 1 : 0));
  2746. H1l = H1.low = (H1l + bl);
  2747. H1.high = (H1h + bh + ((H1l >>> 0) < (bl >>> 0) ? 1 : 0));
  2748. H2l = H2.low = (H2l + cl);
  2749. H2.high = (H2h + ch + ((H2l >>> 0) < (cl >>> 0) ? 1 : 0));
  2750. H3l = H3.low = (H3l + dl);
  2751. H3.high = (H3h + dh + ((H3l >>> 0) < (dl >>> 0) ? 1 : 0));
  2752. H4l = H4.low = (H4l + el);
  2753. H4.high = (H4h + eh + ((H4l >>> 0) < (el >>> 0) ? 1 : 0));
  2754. H5l = H5.low = (H5l + fl);
  2755. H5.high = (H5h + fh + ((H5l >>> 0) < (fl >>> 0) ? 1 : 0));
  2756. H6l = H6.low = (H6l + gl);
  2757. H6.high = (H6h + gh + ((H6l >>> 0) < (gl >>> 0) ? 1 : 0));
  2758. H7l = H7.low = (H7l + hl);
  2759. H7.high = (H7h + hh + ((H7l >>> 0) < (hl >>> 0) ? 1 : 0));
  2760. },
  2761. _doFinalize: function () {
  2762. // Shortcuts
  2763. var data = this._data;
  2764. var dataWords = data.words;
  2765. var nBitsTotal = this._nDataBytes * 8;
  2766. var nBitsLeft = data.sigBytes * 8;
  2767. // Add padding
  2768. dataWords[nBitsLeft >>> 5] |= 0x80 << (24 - nBitsLeft % 32);
  2769. dataWords[(((nBitsLeft + 128) >>> 10) << 5) + 30] = Math.floor(nBitsTotal / 0x100000000);
  2770. dataWords[(((nBitsLeft + 128) >>> 10) << 5) + 31] = nBitsTotal;
  2771. data.sigBytes = dataWords.length * 4;
  2772. // Hash final blocks
  2773. this._process();
  2774. // Convert hash to 32-bit word array before returning
  2775. var hash = this._hash.toX32();
  2776. // Return final computed hash
  2777. return hash;
  2778. },
  2779. clone: function () {
  2780. var clone = Hasher.clone.call(this);
  2781. clone._hash = this._hash.clone();
  2782. return clone;
  2783. },
  2784. blockSize: 1024 / 32
  2785. });
  2786. /**
  2787. * Shortcut function to the hasher's object interface.
  2788. *
  2789. * @param {WordArray|string} message The message to hash.
  2790. *
  2791. * @return {WordArray} The hash.
  2792. *
  2793. * @static
  2794. *
  2795. * @example
  2796. *
  2797. * var hash = CryptoJS.SHA512('message');
  2798. * var hash = CryptoJS.SHA512(wordArray);
  2799. */
  2800. C.SHA512 = Hasher._createHelper(SHA512);
  2801. /**
  2802. * Shortcut function to the HMAC's object interface.
  2803. *
  2804. * @param {WordArray|string} message The message to hash.
  2805. * @param {WordArray|string} key The secret key.
  2806. *
  2807. * @return {WordArray} The HMAC.
  2808. *
  2809. * @static
  2810. *
  2811. * @example
  2812. *
  2813. * var hmac = CryptoJS.HmacSHA512(message, key);
  2814. */
  2815. C.HmacSHA512 = Hasher._createHmacHelper(SHA512);
  2816. }());
  2817. (function () {
  2818. // Shortcuts
  2819. var C = CryptoJS;
  2820. var C_x64 = C.x64;
  2821. var X64Word = C_x64.Word;
  2822. var X64WordArray = C_x64.WordArray;
  2823. var C_algo = C.algo;
  2824. var SHA512 = C_algo.SHA512;
  2825. /**
  2826. * SHA-384 hash algorithm.
  2827. */
  2828. var SHA384 = C_algo.SHA384 = SHA512.extend({
  2829. _doReset: function () {
  2830. this._hash = new X64WordArray.init([
  2831. new X64Word.init(0xcbbb9d5d, 0xc1059ed8), new X64Word.init(0x629a292a, 0x367cd507),
  2832. new X64Word.init(0x9159015a, 0x3070dd17), new X64Word.init(0x152fecd8, 0xf70e5939),
  2833. new X64Word.init(0x67332667, 0xffc00b31), new X64Word.init(0x8eb44a87, 0x68581511),
  2834. new X64Word.init(0xdb0c2e0d, 0x64f98fa7), new X64Word.init(0x47b5481d, 0xbefa4fa4)
  2835. ]);
  2836. },
  2837. _doFinalize: function () {
  2838. var hash = SHA512._doFinalize.call(this);
  2839. hash.sigBytes -= 16;
  2840. return hash;
  2841. }
  2842. });
  2843. /**
  2844. * Shortcut function to the hasher's object interface.
  2845. *
  2846. * @param {WordArray|string} message The message to hash.
  2847. *
  2848. * @return {WordArray} The hash.
  2849. *
  2850. * @static
  2851. *
  2852. * @example
  2853. *
  2854. * var hash = CryptoJS.SHA384('message');
  2855. * var hash = CryptoJS.SHA384(wordArray);
  2856. */
  2857. C.SHA384 = SHA512._createHelper(SHA384);
  2858. /**
  2859. * Shortcut function to the HMAC's object interface.
  2860. *
  2861. * @param {WordArray|string} message The message to hash.
  2862. * @param {WordArray|string} key The secret key.
  2863. *
  2864. * @return {WordArray} The HMAC.
  2865. *
  2866. * @static
  2867. *
  2868. * @example
  2869. *
  2870. * var hmac = CryptoJS.HmacSHA384(message, key);
  2871. */
  2872. C.HmacSHA384 = SHA512._createHmacHelper(SHA384);
  2873. }());
  2874. /**
  2875. * Cipher core components.
  2876. */
  2877. CryptoJS.lib.Cipher || (function (undefined) {
  2878. // Shortcuts
  2879. var C = CryptoJS;
  2880. var C_lib = C.lib;
  2881. var Base = C_lib.Base;
  2882. var WordArray = C_lib.WordArray;
  2883. var BufferedBlockAlgorithm = C_lib.BufferedBlockAlgorithm;
  2884. var C_enc = C.enc;
  2885. var Utf8 = C_enc.Utf8;
  2886. var Base64 = C_enc.Base64;
  2887. var C_algo = C.algo;
  2888. var EvpKDF = C_algo.EvpKDF;
  2889. /**
  2890. * Abstract base cipher template.
  2891. *
  2892. * @property {number} keySize This cipher's key size. Default: 4 (128 bits)
  2893. * @property {number} ivSize This cipher's IV size. Default: 4 (128 bits)
  2894. * @property {number} _ENC_XFORM_MODE A constant representing encryption mode.
  2895. * @property {number} _DEC_XFORM_MODE A constant representing decryption mode.
  2896. */
  2897. var Cipher = C_lib.Cipher = BufferedBlockAlgorithm.extend({
  2898. /**
  2899. * Configuration options.
  2900. *
  2901. * @property {WordArray} iv The IV to use for this operation.
  2902. */
  2903. cfg: Base.extend(),
  2904. /**
  2905. * Creates this cipher in encryption mode.
  2906. *
  2907. * @param {WordArray} key The key.
  2908. * @param {Object} cfg (Optional) The configuration options to use for this operation.
  2909. *
  2910. * @return {Cipher} A cipher instance.
  2911. *
  2912. * @static
  2913. *
  2914. * @example
  2915. *
  2916. * var cipher = CryptoJS.algo.AES.createEncryptor(keyWordArray, { iv: ivWordArray });
  2917. */
  2918. createEncryptor: function (key, cfg) {
  2919. return this.create(this._ENC_XFORM_MODE, key, cfg);
  2920. },
  2921. /**
  2922. * Creates this cipher in decryption mode.
  2923. *
  2924. * @param {WordArray} key The key.
  2925. * @param {Object} cfg (Optional) The configuration options to use for this operation.
  2926. *
  2927. * @return {Cipher} A cipher instance.
  2928. *
  2929. * @static
  2930. *
  2931. * @example
  2932. *
  2933. * var cipher = CryptoJS.algo.AES.createDecryptor(keyWordArray, { iv: ivWordArray });
  2934. */
  2935. createDecryptor: function (key, cfg) {
  2936. return this.create(this._DEC_XFORM_MODE, key, cfg);
  2937. },
  2938. /**
  2939. * Initializes a newly created cipher.
  2940. *
  2941. * @param {number} xformMode Either the encryption or decryption transormation mode constant.
  2942. * @param {WordArray} key The key.
  2943. * @param {Object} cfg (Optional) The configuration options to use for this operation.
  2944. *
  2945. * @example
  2946. *
  2947. * var cipher = CryptoJS.algo.AES.create(CryptoJS.algo.AES._ENC_XFORM_MODE, keyWordArray, { iv: ivWordArray });
  2948. */
  2949. init: function (xformMode, key, cfg) {
  2950. // Apply config defaults
  2951. this.cfg = this.cfg.extend(cfg);
  2952. // Store transform mode and key
  2953. this._xformMode = xformMode;
  2954. this._key = key;
  2955. // Set initial values
  2956. this.reset();
  2957. },
  2958. /**
  2959. * Resets this cipher to its initial state.
  2960. *
  2961. * @example
  2962. *
  2963. * cipher.reset();
  2964. */
  2965. reset: function () {
  2966. // Reset data buffer
  2967. BufferedBlockAlgorithm.reset.call(this);
  2968. // Perform concrete-cipher logic
  2969. this._doReset();
  2970. },
  2971. /**
  2972. * Adds data to be encrypted or decrypted.
  2973. *
  2974. * @param {WordArray|string} dataUpdate The data to encrypt or decrypt.
  2975. *
  2976. * @return {WordArray} The data after processing.
  2977. *
  2978. * @example
  2979. *
  2980. * var encrypted = cipher.process('data');
  2981. * var encrypted = cipher.process(wordArray);
  2982. */
  2983. process: function (dataUpdate) {
  2984. // Append
  2985. this._append(dataUpdate);
  2986. // Process available blocks
  2987. return this._process();
  2988. },
  2989. /**
  2990. * Finalizes the encryption or decryption process.
  2991. * Note that the finalize operation is effectively a destructive, read-once operation.
  2992. *
  2993. * @param {WordArray|string} dataUpdate The final data to encrypt or decrypt.
  2994. *
  2995. * @return {WordArray} The data after final processing.
  2996. *
  2997. * @example
  2998. *
  2999. * var encrypted = cipher.finalize();
  3000. * var encrypted = cipher.finalize('data');
  3001. * var encrypted = cipher.finalize(wordArray);
  3002. */
  3003. finalize: function (dataUpdate) {
  3004. // Final data update
  3005. if (dataUpdate) {
  3006. this._append(dataUpdate);
  3007. }
  3008. // Perform concrete-cipher logic
  3009. var finalProcessedData = this._doFinalize();
  3010. return finalProcessedData;
  3011. },
  3012. keySize: 128 / 32,
  3013. ivSize: 128 / 32,
  3014. _ENC_XFORM_MODE: 1,
  3015. _DEC_XFORM_MODE: 2,
  3016. /**
  3017. * Creates shortcut functions to a cipher's object interface.
  3018. *
  3019. * @param {Cipher} cipher The cipher to create a helper for.
  3020. *
  3021. * @return {Object} An object with encrypt and decrypt shortcut functions.
  3022. *
  3023. * @static
  3024. *
  3025. * @example
  3026. *
  3027. * var AES = CryptoJS.lib.Cipher._createHelper(CryptoJS.algo.AES);
  3028. */
  3029. _createHelper: (function () {
  3030. function selectCipherStrategy(key) {
  3031. if (typeof key == 'string') {
  3032. return PasswordBasedCipher;
  3033. } else {
  3034. return SerializableCipher;
  3035. }
  3036. }
  3037. return function (cipher) {
  3038. return {
  3039. encrypt: function (message, key, cfg) {
  3040. return selectCipherStrategy(key).encrypt(cipher, message, key, cfg);
  3041. },
  3042. decrypt: function (ciphertext, key, cfg) {
  3043. return selectCipherStrategy(key).decrypt(cipher, ciphertext, key, cfg);
  3044. }
  3045. };
  3046. };
  3047. }())
  3048. });
  3049. /**
  3050. * Abstract base stream cipher template.
  3051. *
  3052. * @property {number} blockSize The number of 32-bit words this cipher operates on. Default: 1 (32 bits)
  3053. */
  3054. var StreamCipher = C_lib.StreamCipher = Cipher.extend({
  3055. _doFinalize: function () {
  3056. // Process partial blocks
  3057. var finalProcessedBlocks = this._process(!!'flush');
  3058. return finalProcessedBlocks;
  3059. },
  3060. blockSize: 1
  3061. });
  3062. /**
  3063. * Mode namespace.
  3064. */
  3065. var C_mode = C.mode = {};
  3066. /**
  3067. * Abstract base block cipher mode template.
  3068. */
  3069. var BlockCipherMode = C_lib.BlockCipherMode = Base.extend({
  3070. /**
  3071. * Creates this mode for encryption.
  3072. *
  3073. * @param {Cipher} cipher A block cipher instance.
  3074. * @param {Array} iv The IV words.
  3075. *
  3076. * @static
  3077. *
  3078. * @example
  3079. *
  3080. * var mode = CryptoJS.mode.CBC.createEncryptor(cipher, iv.words);
  3081. */
  3082. createEncryptor: function (cipher, iv) {
  3083. return this.Encryptor.create(cipher, iv);
  3084. },
  3085. /**
  3086. * Creates this mode for decryption.
  3087. *
  3088. * @param {Cipher} cipher A block cipher instance.
  3089. * @param {Array} iv The IV words.
  3090. *
  3091. * @static
  3092. *
  3093. * @example
  3094. *
  3095. * var mode = CryptoJS.mode.CBC.createDecryptor(cipher, iv.words);
  3096. */
  3097. createDecryptor: function (cipher, iv) {
  3098. return this.Decryptor.create(cipher, iv);
  3099. },
  3100. /**
  3101. * Initializes a newly created mode.
  3102. *
  3103. * @param {Cipher} cipher A block cipher instance.
  3104. * @param {Array} iv The IV words.
  3105. *
  3106. * @example
  3107. *
  3108. * var mode = CryptoJS.mode.CBC.Encryptor.create(cipher, iv.words);
  3109. */
  3110. init: function (cipher, iv) {
  3111. this._cipher = cipher;
  3112. this._iv = iv;
  3113. }
  3114. });
  3115. /**
  3116. * Cipher Block Chaining mode.
  3117. */
  3118. var CBC = C_mode.CBC = (function () {
  3119. /**
  3120. * Abstract base CBC mode.
  3121. */
  3122. var CBC = BlockCipherMode.extend();
  3123. /**
  3124. * CBC encryptor.
  3125. */
  3126. CBC.Encryptor = CBC.extend({
  3127. /**
  3128. * Processes the data block at offset.
  3129. *
  3130. * @param {Array} words The data words to operate on.
  3131. * @param {number} offset The offset where the block starts.
  3132. *
  3133. * @example
  3134. *
  3135. * mode.processBlock(data.words, offset);
  3136. */
  3137. processBlock: function (words, offset) {
  3138. // Shortcuts
  3139. var cipher = this._cipher;
  3140. var blockSize = cipher.blockSize;
  3141. // XOR and encrypt
  3142. xorBlock.call(this, words, offset, blockSize);
  3143. cipher.encryptBlock(words, offset);
  3144. // Remember this block to use with next block
  3145. this._prevBlock = words.slice(offset, offset + blockSize);
  3146. }
  3147. });
  3148. /**
  3149. * CBC decryptor.
  3150. */
  3151. CBC.Decryptor = CBC.extend({
  3152. /**
  3153. * Processes the data block at offset.
  3154. *
  3155. * @param {Array} words The data words to operate on.
  3156. * @param {number} offset The offset where the block starts.
  3157. *
  3158. * @example
  3159. *
  3160. * mode.processBlock(data.words, offset);
  3161. */
  3162. processBlock: function (words, offset) {
  3163. // Shortcuts
  3164. var cipher = this._cipher;
  3165. var blockSize = cipher.blockSize;
  3166. // Remember this block to use with next block
  3167. var thisBlock = words.slice(offset, offset + blockSize);
  3168. // Decrypt and XOR
  3169. cipher.decryptBlock(words, offset);
  3170. xorBlock.call(this, words, offset, blockSize);
  3171. // This block becomes the previous block
  3172. this._prevBlock = thisBlock;
  3173. }
  3174. });
  3175. function xorBlock(words, offset, blockSize) {
  3176. var block;
  3177. // Shortcut
  3178. var iv = this._iv;
  3179. // Choose mixing block
  3180. if (iv) {
  3181. block = iv;
  3182. // Remove IV for subsequent blocks
  3183. this._iv = undefined;
  3184. } else {
  3185. block = this._prevBlock;
  3186. }
  3187. // XOR blocks
  3188. for (var i = 0; i < blockSize; i++) {
  3189. words[offset + i] ^= block[i];
  3190. }
  3191. }
  3192. return CBC;
  3193. }());
  3194. /**
  3195. * Padding namespace.
  3196. */
  3197. var C_pad = C.pad = {};
  3198. /**
  3199. * PKCS #5/7 padding strategy.
  3200. */
  3201. var Pkcs7 = C_pad.Pkcs7 = {
  3202. /**
  3203. * Pads data using the algorithm defined in PKCS #5/7.
  3204. *
  3205. * @param {WordArray} data The data to pad.
  3206. * @param {number} blockSize The multiple that the data should be padded to.
  3207. *
  3208. * @static
  3209. *
  3210. * @example
  3211. *
  3212. * CryptoJS.pad.Pkcs7.pad(wordArray, 4);
  3213. */
  3214. pad: function (data, blockSize) {
  3215. // Shortcut
  3216. var blockSizeBytes = blockSize * 4;
  3217. // Count padding bytes
  3218. var nPaddingBytes = blockSizeBytes - data.sigBytes % blockSizeBytes;
  3219. // Create padding word
  3220. var paddingWord = (nPaddingBytes << 24) | (nPaddingBytes << 16) | (nPaddingBytes << 8) | nPaddingBytes;
  3221. // Create padding
  3222. var paddingWords = [];
  3223. for (var i = 0; i < nPaddingBytes; i += 4) {
  3224. paddingWords.push(paddingWord);
  3225. }
  3226. var padding = WordArray.create(paddingWords, nPaddingBytes);
  3227. // Add padding
  3228. data.concat(padding);
  3229. },
  3230. /**
  3231. * Unpads data that had been padded using the algorithm defined in PKCS #5/7.
  3232. *
  3233. * @param {WordArray} data The data to unpad.
  3234. *
  3235. * @static
  3236. *
  3237. * @example
  3238. *
  3239. * CryptoJS.pad.Pkcs7.unpad(wordArray);
  3240. */
  3241. unpad: function (data) {
  3242. // Get number of padding bytes from last byte
  3243. var nPaddingBytes = data.words[(data.sigBytes - 1) >>> 2] & 0xff;
  3244. // Remove padding
  3245. data.sigBytes -= nPaddingBytes;
  3246. }
  3247. };
  3248. /**
  3249. * Abstract base block cipher template.
  3250. *
  3251. * @property {number} blockSize The number of 32-bit words this cipher operates on. Default: 4 (128 bits)
  3252. */
  3253. var BlockCipher = C_lib.BlockCipher = Cipher.extend({
  3254. /**
  3255. * Configuration options.
  3256. *
  3257. * @property {Mode} mode The block mode to use. Default: CBC
  3258. * @property {Padding} padding The padding strategy to use. Default: Pkcs7
  3259. */
  3260. cfg: Cipher.cfg.extend({
  3261. mode: CBC,
  3262. padding: Pkcs7
  3263. }),
  3264. reset: function () {
  3265. var modeCreator;
  3266. // Reset cipher
  3267. Cipher.reset.call(this);
  3268. // Shortcuts
  3269. var cfg = this.cfg;
  3270. var iv = cfg.iv;
  3271. var mode = cfg.mode;
  3272. // Reset block mode
  3273. if (this._xformMode == this._ENC_XFORM_MODE) {
  3274. modeCreator = mode.createEncryptor;
  3275. } else /* if (this._xformMode == this._DEC_XFORM_MODE) */ {
  3276. modeCreator = mode.createDecryptor;
  3277. // Keep at least one block in the buffer for unpadding
  3278. this._minBufferSize = 1;
  3279. }
  3280. if (this._mode && this._mode.__creator == modeCreator) {
  3281. this._mode.init(this, iv && iv.words);
  3282. } else {
  3283. this._mode = modeCreator.call(mode, this, iv && iv.words);
  3284. this._mode.__creator = modeCreator;
  3285. }
  3286. },
  3287. _doProcessBlock: function (words, offset) {
  3288. this._mode.processBlock(words, offset);
  3289. },
  3290. _doFinalize: function () {
  3291. var finalProcessedBlocks;
  3292. // Shortcut
  3293. var padding = this.cfg.padding;
  3294. // Finalize
  3295. if (this._xformMode == this._ENC_XFORM_MODE) {
  3296. // Pad data
  3297. padding.pad(this._data, this.blockSize);
  3298. // Process final blocks
  3299. finalProcessedBlocks = this._process(!!'flush');
  3300. } else /* if (this._xformMode == this._DEC_XFORM_MODE) */ {
  3301. // Process final blocks
  3302. finalProcessedBlocks = this._process(!!'flush');
  3303. // Unpad data
  3304. padding.unpad(finalProcessedBlocks);
  3305. }
  3306. return finalProcessedBlocks;
  3307. },
  3308. blockSize: 128 / 32
  3309. });
  3310. /**
  3311. * A collection of cipher parameters.
  3312. *
  3313. * @property {WordArray} ciphertext The raw ciphertext.
  3314. * @property {WordArray} key The key to this ciphertext.
  3315. * @property {WordArray} iv The IV used in the ciphering operation.
  3316. * @property {WordArray} salt The salt used with a key derivation function.
  3317. * @property {Cipher} algorithm The cipher algorithm.
  3318. * @property {Mode} mode The block mode used in the ciphering operation.
  3319. * @property {Padding} padding The padding scheme used in the ciphering operation.
  3320. * @property {number} blockSize The block size of the cipher.
  3321. * @property {Format} formatter The default formatting strategy to convert this cipher params object to a string.
  3322. */
  3323. var CipherParams = C_lib.CipherParams = Base.extend({
  3324. /**
  3325. * Initializes a newly created cipher params object.
  3326. *
  3327. * @param {Object} cipherParams An object with any of the possible cipher parameters.
  3328. *
  3329. * @example
  3330. *
  3331. * var cipherParams = CryptoJS.lib.CipherParams.create({
  3332. * ciphertext: ciphertextWordArray,
  3333. * key: keyWordArray,
  3334. * iv: ivWordArray,
  3335. * salt: saltWordArray,
  3336. * algorithm: CryptoJS.algo.AES,
  3337. * mode: CryptoJS.mode.CBC,
  3338. * padding: CryptoJS.pad.PKCS7,
  3339. * blockSize: 4,
  3340. * formatter: CryptoJS.format.OpenSSL
  3341. * });
  3342. */
  3343. init: function (cipherParams) {
  3344. this.mixIn(cipherParams);
  3345. },
  3346. /**
  3347. * Converts this cipher params object to a string.
  3348. *
  3349. * @param {Format} formatter (Optional) The formatting strategy to use.
  3350. *
  3351. * @return {string} The stringified cipher params.
  3352. *
  3353. * @throws Error If neither the formatter nor the default formatter is set.
  3354. *
  3355. * @example
  3356. *
  3357. * var string = cipherParams + '';
  3358. * var string = cipherParams.toString();
  3359. * var string = cipherParams.toString(CryptoJS.format.OpenSSL);
  3360. */
  3361. toString: function (formatter) {
  3362. return (formatter || this.formatter).stringify(this);
  3363. }
  3364. });
  3365. /**
  3366. * Format namespace.
  3367. */
  3368. var C_format = C.format = {};
  3369. /**
  3370. * OpenSSL formatting strategy.
  3371. */
  3372. var OpenSSLFormatter = C_format.OpenSSL = {
  3373. /**
  3374. * Converts a cipher params object to an OpenSSL-compatible string.
  3375. *
  3376. * @param {CipherParams} cipherParams The cipher params object.
  3377. *
  3378. * @return {string} The OpenSSL-compatible string.
  3379. *
  3380. * @static
  3381. *
  3382. * @example
  3383. *
  3384. * var openSSLString = CryptoJS.format.OpenSSL.stringify(cipherParams);
  3385. */
  3386. stringify: function (cipherParams) {
  3387. var wordArray;
  3388. // Shortcuts
  3389. var ciphertext = cipherParams.ciphertext;
  3390. var salt = cipherParams.salt;
  3391. // Format
  3392. if (salt) {
  3393. wordArray = WordArray.create([0x53616c74, 0x65645f5f]).concat(salt).concat(ciphertext);
  3394. } else {
  3395. wordArray = ciphertext;
  3396. }
  3397. return wordArray.toString(Base64);
  3398. },
  3399. /**
  3400. * Converts an OpenSSL-compatible string to a cipher params object.
  3401. *
  3402. * @param {string} openSSLStr The OpenSSL-compatible string.
  3403. *
  3404. * @return {CipherParams} The cipher params object.
  3405. *
  3406. * @static
  3407. *
  3408. * @example
  3409. *
  3410. * var cipherParams = CryptoJS.format.OpenSSL.parse(openSSLString);
  3411. */
  3412. parse: function (openSSLStr) {
  3413. var salt;
  3414. // Parse base64
  3415. var ciphertext = Base64.parse(openSSLStr);
  3416. // Shortcut
  3417. var ciphertextWords = ciphertext.words;
  3418. // Test for salt
  3419. if (ciphertextWords[0] == 0x53616c74 && ciphertextWords[1] == 0x65645f5f) {
  3420. // Extract salt
  3421. salt = WordArray.create(ciphertextWords.slice(2, 4));
  3422. // Remove salt from ciphertext
  3423. ciphertextWords.splice(0, 4);
  3424. ciphertext.sigBytes -= 16;
  3425. }
  3426. return CipherParams.create({ ciphertext: ciphertext, salt: salt });
  3427. }
  3428. };
  3429. /**
  3430. * A cipher wrapper that returns ciphertext as a serializable cipher params object.
  3431. */
  3432. var SerializableCipher = C_lib.SerializableCipher = Base.extend({
  3433. /**
  3434. * Configuration options.
  3435. *
  3436. * @property {Formatter} format The formatting strategy to convert cipher param objects to and from a string. Default: OpenSSL
  3437. */
  3438. cfg: Base.extend({
  3439. format: OpenSSLFormatter
  3440. }),
  3441. /**
  3442. * Encrypts a message.
  3443. *
  3444. * @param {Cipher} cipher The cipher algorithm to use.
  3445. * @param {WordArray|string} message The message to encrypt.
  3446. * @param {WordArray} key The key.
  3447. * @param {Object} cfg (Optional) The configuration options to use for this operation.
  3448. *
  3449. * @return {CipherParams} A cipher params object.
  3450. *
  3451. * @static
  3452. *
  3453. * @example
  3454. *
  3455. * var ciphertextParams = CryptoJS.lib.SerializableCipher.encrypt(CryptoJS.algo.AES, message, key);
  3456. * var ciphertextParams = CryptoJS.lib.SerializableCipher.encrypt(CryptoJS.algo.AES, message, key, { iv: iv });
  3457. * var ciphertextParams = CryptoJS.lib.SerializableCipher.encrypt(CryptoJS.algo.AES, message, key, { iv: iv, format: CryptoJS.format.OpenSSL });
  3458. */
  3459. encrypt: function (cipher, message, key, cfg) {
  3460. // Apply config defaults
  3461. cfg = this.cfg.extend(cfg);
  3462. // Encrypt
  3463. var encryptor = cipher.createEncryptor(key, cfg);
  3464. var ciphertext = encryptor.finalize(message);
  3465. // Shortcut
  3466. var cipherCfg = encryptor.cfg;
  3467. // Create and return serializable cipher params
  3468. return CipherParams.create({
  3469. ciphertext: ciphertext,
  3470. key: key,
  3471. iv: cipherCfg.iv,
  3472. algorithm: cipher,
  3473. mode: cipherCfg.mode,
  3474. padding: cipherCfg.padding,
  3475. blockSize: cipher.blockSize,
  3476. formatter: cfg.format
  3477. });
  3478. },
  3479. /**
  3480. * Decrypts serialized ciphertext.
  3481. *
  3482. * @param {Cipher} cipher The cipher algorithm to use.
  3483. * @param {CipherParams|string} ciphertext The ciphertext to decrypt.
  3484. * @param {WordArray} key The key.
  3485. * @param {Object} cfg (Optional) The configuration options to use for this operation.
  3486. *
  3487. * @return {WordArray} The plaintext.
  3488. *
  3489. * @static
  3490. *
  3491. * @example
  3492. *
  3493. * var plaintext = CryptoJS.lib.SerializableCipher.decrypt(CryptoJS.algo.AES, formattedCiphertext, key, { iv: iv, format: CryptoJS.format.OpenSSL });
  3494. * var plaintext = CryptoJS.lib.SerializableCipher.decrypt(CryptoJS.algo.AES, ciphertextParams, key, { iv: iv, format: CryptoJS.format.OpenSSL });
  3495. */
  3496. decrypt: function (cipher, ciphertext, key, cfg) {
  3497. // Apply config defaults
  3498. cfg = this.cfg.extend(cfg);
  3499. // Convert string to CipherParams
  3500. ciphertext = this._parse(ciphertext, cfg.format);
  3501. // Decrypt
  3502. var plaintext = cipher.createDecryptor(key, cfg).finalize(ciphertext.ciphertext);
  3503. return plaintext;
  3504. },
  3505. /**
  3506. * Converts serialized ciphertext to CipherParams,
  3507. * else assumed CipherParams already and returns ciphertext unchanged.
  3508. *
  3509. * @param {CipherParams|string} ciphertext The ciphertext.
  3510. * @param {Formatter} format The formatting strategy to use to parse serialized ciphertext.
  3511. *
  3512. * @return {CipherParams} The unserialized ciphertext.
  3513. *
  3514. * @static
  3515. *
  3516. * @example
  3517. *
  3518. * var ciphertextParams = CryptoJS.lib.SerializableCipher._parse(ciphertextStringOrParams, format);
  3519. */
  3520. _parse: function (ciphertext, format) {
  3521. if (typeof ciphertext == 'string') {
  3522. return format.parse(ciphertext, this);
  3523. } else {
  3524. return ciphertext;
  3525. }
  3526. }
  3527. });
  3528. /**
  3529. * Key derivation function namespace.
  3530. */
  3531. var C_kdf = C.kdf = {};
  3532. /**
  3533. * OpenSSL key derivation function.
  3534. */
  3535. var OpenSSLKdf = C_kdf.OpenSSL = {
  3536. /**
  3537. * Derives a key and IV from a password.
  3538. *
  3539. * @param {string} password The password to derive from.
  3540. * @param {number} keySize The size in words of the key to generate.
  3541. * @param {number} ivSize The size in words of the IV to generate.
  3542. * @param {WordArray|string} salt (Optional) A 64-bit salt to use. If omitted, a salt will be generated randomly.
  3543. *
  3544. * @return {CipherParams} A cipher params object with the key, IV, and salt.
  3545. *
  3546. * @static
  3547. *
  3548. * @example
  3549. *
  3550. * var derivedParams = CryptoJS.kdf.OpenSSL.execute('Password', 256/32, 128/32);
  3551. * var derivedParams = CryptoJS.kdf.OpenSSL.execute('Password', 256/32, 128/32, 'saltsalt');
  3552. */
  3553. execute: function (password, keySize, ivSize, salt) {
  3554. // Generate random salt
  3555. if (!salt) {
  3556. salt = WordArray.random(64 / 8);
  3557. }
  3558. // Derive key and IV
  3559. var key = EvpKDF.create({ keySize: keySize + ivSize }).compute(password, salt);
  3560. // Separate key and IV
  3561. var iv = WordArray.create(key.words.slice(keySize), ivSize * 4);
  3562. key.sigBytes = keySize * 4;
  3563. // Return params
  3564. return CipherParams.create({ key: key, iv: iv, salt: salt });
  3565. }
  3566. };
  3567. /**
  3568. * A serializable cipher wrapper that derives the key from a password,
  3569. * and returns ciphertext as a serializable cipher params object.
  3570. */
  3571. var PasswordBasedCipher = C_lib.PasswordBasedCipher = SerializableCipher.extend({
  3572. /**
  3573. * Configuration options.
  3574. *
  3575. * @property {KDF} kdf The key derivation function to use to generate a key and IV from a password. Default: OpenSSL
  3576. */
  3577. cfg: SerializableCipher.cfg.extend({
  3578. kdf: OpenSSLKdf
  3579. }),
  3580. /**
  3581. * Encrypts a message using a password.
  3582. *
  3583. * @param {Cipher} cipher The cipher algorithm to use.
  3584. * @param {WordArray|string} message The message to encrypt.
  3585. * @param {string} password The password.
  3586. * @param {Object} cfg (Optional) The configuration options to use for this operation.
  3587. *
  3588. * @return {CipherParams} A cipher params object.
  3589. *
  3590. * @static
  3591. *
  3592. * @example
  3593. *
  3594. * var ciphertextParams = CryptoJS.lib.PasswordBasedCipher.encrypt(CryptoJS.algo.AES, message, 'password');
  3595. * var ciphertextParams = CryptoJS.lib.PasswordBasedCipher.encrypt(CryptoJS.algo.AES, message, 'password', { format: CryptoJS.format.OpenSSL });
  3596. */
  3597. encrypt: function (cipher, message, password, cfg) {
  3598. // Apply config defaults
  3599. cfg = this.cfg.extend(cfg);
  3600. // Derive key and other params
  3601. var derivedParams = cfg.kdf.execute(password, cipher.keySize, cipher.ivSize);
  3602. // Add IV to config
  3603. cfg.iv = derivedParams.iv;
  3604. // Encrypt
  3605. var ciphertext = SerializableCipher.encrypt.call(this, cipher, message, derivedParams.key, cfg);
  3606. // Mix in derived params
  3607. ciphertext.mixIn(derivedParams);
  3608. return ciphertext;
  3609. },
  3610. /**
  3611. * Decrypts serialized ciphertext using a password.
  3612. *
  3613. * @param {Cipher} cipher The cipher algorithm to use.
  3614. * @param {CipherParams|string} ciphertext The ciphertext to decrypt.
  3615. * @param {string} password The password.
  3616. * @param {Object} cfg (Optional) The configuration options to use for this operation.
  3617. *
  3618. * @return {WordArray} The plaintext.
  3619. *
  3620. * @static
  3621. *
  3622. * @example
  3623. *
  3624. * var plaintext = CryptoJS.lib.PasswordBasedCipher.decrypt(CryptoJS.algo.AES, formattedCiphertext, 'password', { format: CryptoJS.format.OpenSSL });
  3625. * var plaintext = CryptoJS.lib.PasswordBasedCipher.decrypt(CryptoJS.algo.AES, ciphertextParams, 'password', { format: CryptoJS.format.OpenSSL });
  3626. */
  3627. decrypt: function (cipher, ciphertext, password, cfg) {
  3628. // Apply config defaults
  3629. cfg = this.cfg.extend(cfg);
  3630. // Convert string to CipherParams
  3631. ciphertext = this._parse(ciphertext, cfg.format);
  3632. // Derive key and other params
  3633. var derivedParams = cfg.kdf.execute(password, cipher.keySize, cipher.ivSize, ciphertext.salt);
  3634. // Add IV to config
  3635. cfg.iv = derivedParams.iv;
  3636. // Decrypt
  3637. var plaintext = SerializableCipher.decrypt.call(this, cipher, ciphertext, derivedParams.key, cfg);
  3638. return plaintext;
  3639. }
  3640. });
  3641. }());
  3642. /**
  3643. * Cipher Feedback block mode.
  3644. */
  3645. CryptoJS.mode.CFB = (function () {
  3646. var CFB = CryptoJS.lib.BlockCipherMode.extend();
  3647. CFB.Encryptor = CFB.extend({
  3648. processBlock: function (words, offset) {
  3649. // Shortcuts
  3650. var cipher = this._cipher;
  3651. var blockSize = cipher.blockSize;
  3652. generateKeystreamAndEncrypt.call(this, words, offset, blockSize, cipher);
  3653. // Remember this block to use with next block
  3654. this._prevBlock = words.slice(offset, offset + blockSize);
  3655. }
  3656. });
  3657. CFB.Decryptor = CFB.extend({
  3658. processBlock: function (words, offset) {
  3659. // Shortcuts
  3660. var cipher = this._cipher;
  3661. var blockSize = cipher.blockSize;
  3662. // Remember this block to use with next block
  3663. var thisBlock = words.slice(offset, offset + blockSize);
  3664. generateKeystreamAndEncrypt.call(this, words, offset, blockSize, cipher);
  3665. // This block becomes the previous block
  3666. this._prevBlock = thisBlock;
  3667. }
  3668. });
  3669. function generateKeystreamAndEncrypt(words, offset, blockSize, cipher) {
  3670. var keystream;
  3671. // Shortcut
  3672. var iv = this._iv;
  3673. // Generate keystream
  3674. if (iv) {
  3675. keystream = iv.slice(0);
  3676. // Remove IV for subsequent blocks
  3677. this._iv = undefined;
  3678. } else {
  3679. keystream = this._prevBlock;
  3680. }
  3681. cipher.encryptBlock(keystream, 0);
  3682. // Encrypt
  3683. for (var i = 0; i < blockSize; i++) {
  3684. words[offset + i] ^= keystream[i];
  3685. }
  3686. }
  3687. return CFB;
  3688. }());
  3689. /**
  3690. * Electronic Codebook block mode.
  3691. */
  3692. CryptoJS.mode.ECB = (function () {
  3693. var ECB = CryptoJS.lib.BlockCipherMode.extend();
  3694. ECB.Encryptor = ECB.extend({
  3695. processBlock: function (words, offset) {
  3696. this._cipher.encryptBlock(words, offset);
  3697. }
  3698. });
  3699. ECB.Decryptor = ECB.extend({
  3700. processBlock: function (words, offset) {
  3701. this._cipher.decryptBlock(words, offset);
  3702. }
  3703. });
  3704. return ECB;
  3705. }());
  3706. /**
  3707. * ANSI X.923 padding strategy.
  3708. */
  3709. CryptoJS.pad.AnsiX923 = {
  3710. pad: function (data, blockSize) {
  3711. // Shortcuts
  3712. var dataSigBytes = data.sigBytes;
  3713. var blockSizeBytes = blockSize * 4;
  3714. // Count padding bytes
  3715. var nPaddingBytes = blockSizeBytes - dataSigBytes % blockSizeBytes;
  3716. // Compute last byte position
  3717. var lastBytePos = dataSigBytes + nPaddingBytes - 1;
  3718. // Pad
  3719. data.clamp();
  3720. data.words[lastBytePos >>> 2] |= nPaddingBytes << (24 - (lastBytePos % 4) * 8);
  3721. data.sigBytes += nPaddingBytes;
  3722. },
  3723. unpad: function (data) {
  3724. // Get number of padding bytes from last byte
  3725. var nPaddingBytes = data.words[(data.sigBytes - 1) >>> 2] & 0xff;
  3726. // Remove padding
  3727. data.sigBytes -= nPaddingBytes;
  3728. }
  3729. };
  3730. /**
  3731. * ISO 10126 padding strategy.
  3732. */
  3733. CryptoJS.pad.Iso10126 = {
  3734. pad: function (data, blockSize) {
  3735. // Shortcut
  3736. var blockSizeBytes = blockSize * 4;
  3737. // Count padding bytes
  3738. var nPaddingBytes = blockSizeBytes - data.sigBytes % blockSizeBytes;
  3739. // Pad
  3740. data.concat(CryptoJS.lib.WordArray.random(nPaddingBytes - 1)).
  3741. concat(CryptoJS.lib.WordArray.create([nPaddingBytes << 24], 1));
  3742. },
  3743. unpad: function (data) {
  3744. // Get number of padding bytes from last byte
  3745. var nPaddingBytes = data.words[(data.sigBytes - 1) >>> 2] & 0xff;
  3746. // Remove padding
  3747. data.sigBytes -= nPaddingBytes;
  3748. }
  3749. };
  3750. /**
  3751. * ISO/IEC 9797-1 Padding Method 2.
  3752. */
  3753. CryptoJS.pad.Iso97971 = {
  3754. pad: function (data, blockSize) {
  3755. // Add 0x80 byte
  3756. data.concat(CryptoJS.lib.WordArray.create([0x80000000], 1));
  3757. // Zero pad the rest
  3758. CryptoJS.pad.ZeroPadding.pad(data, blockSize);
  3759. },
  3760. unpad: function (data) {
  3761. // Remove zero padding
  3762. CryptoJS.pad.ZeroPadding.unpad(data);
  3763. // Remove one more byte -- the 0x80 byte
  3764. data.sigBytes--;
  3765. }
  3766. };
  3767. /**
  3768. * Output Feedback block mode.
  3769. */
  3770. CryptoJS.mode.OFB = (function () {
  3771. var OFB = CryptoJS.lib.BlockCipherMode.extend();
  3772. var Encryptor = OFB.Encryptor = OFB.extend({
  3773. processBlock: function (words, offset) {
  3774. // Shortcuts
  3775. var cipher = this._cipher
  3776. var blockSize = cipher.blockSize;
  3777. var iv = this._iv;
  3778. var keystream = this._keystream;
  3779. // Generate keystream
  3780. if (iv) {
  3781. keystream = this._keystream = iv.slice(0);
  3782. // Remove IV for subsequent blocks
  3783. this._iv = undefined;
  3784. }
  3785. cipher.encryptBlock(keystream, 0);
  3786. // Encrypt
  3787. for (var i = 0; i < blockSize; i++) {
  3788. words[offset + i] ^= keystream[i];
  3789. }
  3790. }
  3791. });
  3792. OFB.Decryptor = Encryptor;
  3793. return OFB;
  3794. }());
  3795. /**
  3796. * A noop padding strategy.
  3797. */
  3798. CryptoJS.pad.NoPadding = {
  3799. pad: function () {
  3800. },
  3801. unpad: function () {
  3802. }
  3803. };
  3804. (function (undefined) {
  3805. // Shortcuts
  3806. var C = CryptoJS;
  3807. var C_lib = C.lib;
  3808. var CipherParams = C_lib.CipherParams;
  3809. var C_enc = C.enc;
  3810. var Hex = C_enc.Hex;
  3811. var C_format = C.format;
  3812. var HexFormatter = C_format.Hex = {
  3813. /**
  3814. * Converts the ciphertext of a cipher params object to a hexadecimally encoded string.
  3815. *
  3816. * @param {CipherParams} cipherParams The cipher params object.
  3817. *
  3818. * @return {string} The hexadecimally encoded string.
  3819. *
  3820. * @static
  3821. *
  3822. * @example
  3823. *
  3824. * var hexString = CryptoJS.format.Hex.stringify(cipherParams);
  3825. */
  3826. stringify: function (cipherParams) {
  3827. return cipherParams.ciphertext.toString(Hex);
  3828. },
  3829. /**
  3830. * Converts a hexadecimally encoded ciphertext string to a cipher params object.
  3831. *
  3832. * @param {string} input The hexadecimally encoded string.
  3833. *
  3834. * @return {CipherParams} The cipher params object.
  3835. *
  3836. * @static
  3837. *
  3838. * @example
  3839. *
  3840. * var cipherParams = CryptoJS.format.Hex.parse(hexString);
  3841. */
  3842. parse: function (input) {
  3843. var ciphertext = Hex.parse(input);
  3844. return CipherParams.create({ ciphertext: ciphertext });
  3845. }
  3846. };
  3847. }());
  3848. (function () {
  3849. // Shortcuts
  3850. var C = CryptoJS;
  3851. var C_lib = C.lib;
  3852. var BlockCipher = C_lib.BlockCipher;
  3853. var C_algo = C.algo;
  3854. // Lookup tables
  3855. var SBOX = [];
  3856. var INV_SBOX = [];
  3857. var SUB_MIX_0 = [];
  3858. var SUB_MIX_1 = [];
  3859. var SUB_MIX_2 = [];
  3860. var SUB_MIX_3 = [];
  3861. var INV_SUB_MIX_0 = [];
  3862. var INV_SUB_MIX_1 = [];
  3863. var INV_SUB_MIX_2 = [];
  3864. var INV_SUB_MIX_3 = [];
  3865. // Compute lookup tables
  3866. (function () {
  3867. // Compute double table
  3868. var d = [];
  3869. for (var i = 0; i < 256; i++) {
  3870. if (i < 128) {
  3871. d[i] = i << 1;
  3872. } else {
  3873. d[i] = (i << 1) ^ 0x11b;
  3874. }
  3875. }
  3876. // Walk GF(2^8)
  3877. var x = 0;
  3878. var xi = 0;
  3879. for (var i = 0; i < 256; i++) {
  3880. // Compute sbox
  3881. var sx = xi ^ (xi << 1) ^ (xi << 2) ^ (xi << 3) ^ (xi << 4);
  3882. sx = (sx >>> 8) ^ (sx & 0xff) ^ 0x63;
  3883. SBOX[x] = sx;
  3884. INV_SBOX[sx] = x;
  3885. // Compute multiplication
  3886. var x2 = d[x];
  3887. var x4 = d[x2];
  3888. var x8 = d[x4];
  3889. // Compute sub bytes, mix columns tables
  3890. var t = (d[sx] * 0x101) ^ (sx * 0x1010100);
  3891. SUB_MIX_0[x] = (t << 24) | (t >>> 8);
  3892. SUB_MIX_1[x] = (t << 16) | (t >>> 16);
  3893. SUB_MIX_2[x] = (t << 8) | (t >>> 24);
  3894. SUB_MIX_3[x] = t;
  3895. // Compute inv sub bytes, inv mix columns tables
  3896. var t = (x8 * 0x1010101) ^ (x4 * 0x10001) ^ (x2 * 0x101) ^ (x * 0x1010100);
  3897. INV_SUB_MIX_0[sx] = (t << 24) | (t >>> 8);
  3898. INV_SUB_MIX_1[sx] = (t << 16) | (t >>> 16);
  3899. INV_SUB_MIX_2[sx] = (t << 8) | (t >>> 24);
  3900. INV_SUB_MIX_3[sx] = t;
  3901. // Compute next counter
  3902. if (!x) {
  3903. x = xi = 1;
  3904. } else {
  3905. x = x2 ^ d[d[d[x8 ^ x2]]];
  3906. xi ^= d[d[xi]];
  3907. }
  3908. }
  3909. }());
  3910. // Precomputed Rcon lookup
  3911. var RCON = [0x00, 0x01, 0x02, 0x04, 0x08, 0x10, 0x20, 0x40, 0x80, 0x1b, 0x36];
  3912. /**
  3913. * AES block cipher algorithm.
  3914. */
  3915. var AES = C_algo.AES = BlockCipher.extend({
  3916. _doReset: function () {
  3917. var t;
  3918. // Skip reset of nRounds has been set before and key did not change
  3919. if (this._nRounds && this._keyPriorReset === this._key) {
  3920. return;
  3921. }
  3922. // Shortcuts
  3923. var key = this._keyPriorReset = this._key;
  3924. var keyWords = key.words;
  3925. var keySize = key.sigBytes / 4;
  3926. // Compute number of rounds
  3927. var nRounds = this._nRounds = keySize + 6;
  3928. // Compute number of key schedule rows
  3929. var ksRows = (nRounds + 1) * 4;
  3930. // Compute key schedule
  3931. var keySchedule = this._keySchedule = [];
  3932. for (var ksRow = 0; ksRow < ksRows; ksRow++) {
  3933. if (ksRow < keySize) {
  3934. keySchedule[ksRow] = keyWords[ksRow];
  3935. } else {
  3936. t = keySchedule[ksRow - 1];
  3937. if (!(ksRow % keySize)) {
  3938. // Rot word
  3939. t = (t << 8) | (t >>> 24);
  3940. // Sub word
  3941. t = (SBOX[t >>> 24] << 24) | (SBOX[(t >>> 16) & 0xff] << 16) | (SBOX[(t >>> 8) & 0xff] << 8) | SBOX[t & 0xff];
  3942. // Mix Rcon
  3943. t ^= RCON[(ksRow / keySize) | 0] << 24;
  3944. } else if (keySize > 6 && ksRow % keySize == 4) {
  3945. // Sub word
  3946. t = (SBOX[t >>> 24] << 24) | (SBOX[(t >>> 16) & 0xff] << 16) | (SBOX[(t >>> 8) & 0xff] << 8) | SBOX[t & 0xff];
  3947. }
  3948. keySchedule[ksRow] = keySchedule[ksRow - keySize] ^ t;
  3949. }
  3950. }
  3951. // Compute inv key schedule
  3952. var invKeySchedule = this._invKeySchedule = [];
  3953. for (var invKsRow = 0; invKsRow < ksRows; invKsRow++) {
  3954. var ksRow = ksRows - invKsRow;
  3955. if (invKsRow % 4) {
  3956. var t = keySchedule[ksRow];
  3957. } else {
  3958. var t = keySchedule[ksRow - 4];
  3959. }
  3960. if (invKsRow < 4 || ksRow <= 4) {
  3961. invKeySchedule[invKsRow] = t;
  3962. } else {
  3963. invKeySchedule[invKsRow] = INV_SUB_MIX_0[SBOX[t >>> 24]] ^ INV_SUB_MIX_1[SBOX[(t >>> 16) & 0xff]] ^
  3964. INV_SUB_MIX_2[SBOX[(t >>> 8) & 0xff]] ^ INV_SUB_MIX_3[SBOX[t & 0xff]];
  3965. }
  3966. }
  3967. },
  3968. encryptBlock: function (M, offset) {
  3969. this._doCryptBlock(M, offset, this._keySchedule, SUB_MIX_0, SUB_MIX_1, SUB_MIX_2, SUB_MIX_3, SBOX);
  3970. },
  3971. decryptBlock: function (M, offset) {
  3972. // Swap 2nd and 4th rows
  3973. var t = M[offset + 1];
  3974. M[offset + 1] = M[offset + 3];
  3975. M[offset + 3] = t;
  3976. this._doCryptBlock(M, offset, this._invKeySchedule, INV_SUB_MIX_0, INV_SUB_MIX_1, INV_SUB_MIX_2, INV_SUB_MIX_3, INV_SBOX);
  3977. // Inv swap 2nd and 4th rows
  3978. var t = M[offset + 1];
  3979. M[offset + 1] = M[offset + 3];
  3980. M[offset + 3] = t;
  3981. },
  3982. _doCryptBlock: function (M, offset, keySchedule, SUB_MIX_0, SUB_MIX_1, SUB_MIX_2, SUB_MIX_3, SBOX) {
  3983. // Shortcut
  3984. var nRounds = this._nRounds;
  3985. // Get input, add round key
  3986. var s0 = M[offset] ^ keySchedule[0];
  3987. var s1 = M[offset + 1] ^ keySchedule[1];
  3988. var s2 = M[offset + 2] ^ keySchedule[2];
  3989. var s3 = M[offset + 3] ^ keySchedule[3];
  3990. // Key schedule row counter
  3991. var ksRow = 4;
  3992. // Rounds
  3993. for (var round = 1; round < nRounds; round++) {
  3994. // Shift rows, sub bytes, mix columns, add round key
  3995. var t0 = SUB_MIX_0[s0 >>> 24] ^ SUB_MIX_1[(s1 >>> 16) & 0xff] ^ SUB_MIX_2[(s2 >>> 8) & 0xff] ^ SUB_MIX_3[s3 & 0xff] ^ keySchedule[ksRow++];
  3996. var t1 = SUB_MIX_0[s1 >>> 24] ^ SUB_MIX_1[(s2 >>> 16) & 0xff] ^ SUB_MIX_2[(s3 >>> 8) & 0xff] ^ SUB_MIX_3[s0 & 0xff] ^ keySchedule[ksRow++];
  3997. var t2 = SUB_MIX_0[s2 >>> 24] ^ SUB_MIX_1[(s3 >>> 16) & 0xff] ^ SUB_MIX_2[(s0 >>> 8) & 0xff] ^ SUB_MIX_3[s1 & 0xff] ^ keySchedule[ksRow++];
  3998. var t3 = SUB_MIX_0[s3 >>> 24] ^ SUB_MIX_1[(s0 >>> 16) & 0xff] ^ SUB_MIX_2[(s1 >>> 8) & 0xff] ^ SUB_MIX_3[s2 & 0xff] ^ keySchedule[ksRow++];
  3999. // Update state
  4000. s0 = t0;
  4001. s1 = t1;
  4002. s2 = t2;
  4003. s3 = t3;
  4004. }
  4005. // Shift rows, sub bytes, add round key
  4006. var t0 = ((SBOX[s0 >>> 24] << 24) | (SBOX[(s1 >>> 16) & 0xff] << 16) | (SBOX[(s2 >>> 8) & 0xff] << 8) | SBOX[s3 & 0xff]) ^ keySchedule[ksRow++];
  4007. var t1 = ((SBOX[s1 >>> 24] << 24) | (SBOX[(s2 >>> 16) & 0xff] << 16) | (SBOX[(s3 >>> 8) & 0xff] << 8) | SBOX[s0 & 0xff]) ^ keySchedule[ksRow++];
  4008. var t2 = ((SBOX[s2 >>> 24] << 24) | (SBOX[(s3 >>> 16) & 0xff] << 16) | (SBOX[(s0 >>> 8) & 0xff] << 8) | SBOX[s1 & 0xff]) ^ keySchedule[ksRow++];
  4009. var t3 = ((SBOX[s3 >>> 24] << 24) | (SBOX[(s0 >>> 16) & 0xff] << 16) | (SBOX[(s1 >>> 8) & 0xff] << 8) | SBOX[s2 & 0xff]) ^ keySchedule[ksRow++];
  4010. // Set output
  4011. M[offset] = t0;
  4012. M[offset + 1] = t1;
  4013. M[offset + 2] = t2;
  4014. M[offset + 3] = t3;
  4015. },
  4016. keySize: 256 / 32
  4017. });
  4018. /**
  4019. * Shortcut functions to the cipher's object interface.
  4020. *
  4021. * @example
  4022. *
  4023. * var ciphertext = CryptoJS.AES.encrypt(message, key, cfg);
  4024. * var plaintext = CryptoJS.AES.decrypt(ciphertext, key, cfg);
  4025. */
  4026. C.AES = BlockCipher._createHelper(AES);
  4027. }());
  4028. (function () {
  4029. // Shortcuts
  4030. var C = CryptoJS;
  4031. var C_lib = C.lib;
  4032. var WordArray = C_lib.WordArray;
  4033. var BlockCipher = C_lib.BlockCipher;
  4034. var C_algo = C.algo;
  4035. // Permuted Choice 1 constants
  4036. var PC1 = [
  4037. 57, 49, 41, 33, 25, 17, 9, 1,
  4038. 58, 50, 42, 34, 26, 18, 10, 2,
  4039. 59, 51, 43, 35, 27, 19, 11, 3,
  4040. 60, 52, 44, 36, 63, 55, 47, 39,
  4041. 31, 23, 15, 7, 62, 54, 46, 38,
  4042. 30, 22, 14, 6, 61, 53, 45, 37,
  4043. 29, 21, 13, 5, 28, 20, 12, 4
  4044. ];
  4045. // Permuted Choice 2 constants
  4046. var PC2 = [
  4047. 14, 17, 11, 24, 1, 5,
  4048. 3, 28, 15, 6, 21, 10,
  4049. 23, 19, 12, 4, 26, 8,
  4050. 16, 7, 27, 20, 13, 2,
  4051. 41, 52, 31, 37, 47, 55,
  4052. 30, 40, 51, 45, 33, 48,
  4053. 44, 49, 39, 56, 34, 53,
  4054. 46, 42, 50, 36, 29, 32
  4055. ];
  4056. // Cumulative bit shift constants
  4057. var BIT_SHIFTS = [1, 2, 4, 6, 8, 10, 12, 14, 15, 17, 19, 21, 23, 25, 27, 28];
  4058. // SBOXes and round permutation constants
  4059. var SBOX_P = [
  4060. {
  4061. 0x0: 0x808200,
  4062. 0x10000000: 0x8000,
  4063. 0x20000000: 0x808002,
  4064. 0x30000000: 0x2,
  4065. 0x40000000: 0x200,
  4066. 0x50000000: 0x808202,
  4067. 0x60000000: 0x800202,
  4068. 0x70000000: 0x800000,
  4069. 0x80000000: 0x202,
  4070. 0x90000000: 0x800200,
  4071. 0xa0000000: 0x8200,
  4072. 0xb0000000: 0x808000,
  4073. 0xc0000000: 0x8002,
  4074. 0xd0000000: 0x800002,
  4075. 0xe0000000: 0x0,
  4076. 0xf0000000: 0x8202,
  4077. 0x8000000: 0x0,
  4078. 0x18000000: 0x808202,
  4079. 0x28000000: 0x8202,
  4080. 0x38000000: 0x8000,
  4081. 0x48000000: 0x808200,
  4082. 0x58000000: 0x200,
  4083. 0x68000000: 0x808002,
  4084. 0x78000000: 0x2,
  4085. 0x88000000: 0x800200,
  4086. 0x98000000: 0x8200,
  4087. 0xa8000000: 0x808000,
  4088. 0xb8000000: 0x800202,
  4089. 0xc8000000: 0x800002,
  4090. 0xd8000000: 0x8002,
  4091. 0xe8000000: 0x202,
  4092. 0xf8000000: 0x800000,
  4093. 0x1: 0x8000,
  4094. 0x10000001: 0x2,
  4095. 0x20000001: 0x808200,
  4096. 0x30000001: 0x800000,
  4097. 0x40000001: 0x808002,
  4098. 0x50000001: 0x8200,
  4099. 0x60000001: 0x200,
  4100. 0x70000001: 0x800202,
  4101. 0x80000001: 0x808202,
  4102. 0x90000001: 0x808000,
  4103. 0xa0000001: 0x800002,
  4104. 0xb0000001: 0x8202,
  4105. 0xc0000001: 0x202,
  4106. 0xd0000001: 0x800200,
  4107. 0xe0000001: 0x8002,
  4108. 0xf0000001: 0x0,
  4109. 0x8000001: 0x808202,
  4110. 0x18000001: 0x808000,
  4111. 0x28000001: 0x800000,
  4112. 0x38000001: 0x200,
  4113. 0x48000001: 0x8000,
  4114. 0x58000001: 0x800002,
  4115. 0x68000001: 0x2,
  4116. 0x78000001: 0x8202,
  4117. 0x88000001: 0x8002,
  4118. 0x98000001: 0x800202,
  4119. 0xa8000001: 0x202,
  4120. 0xb8000001: 0x808200,
  4121. 0xc8000001: 0x800200,
  4122. 0xd8000001: 0x0,
  4123. 0xe8000001: 0x8200,
  4124. 0xf8000001: 0x808002
  4125. },
  4126. {
  4127. 0x0: 0x40084010,
  4128. 0x1000000: 0x4000,
  4129. 0x2000000: 0x80000,
  4130. 0x3000000: 0x40080010,
  4131. 0x4000000: 0x40000010,
  4132. 0x5000000: 0x40084000,
  4133. 0x6000000: 0x40004000,
  4134. 0x7000000: 0x10,
  4135. 0x8000000: 0x84000,
  4136. 0x9000000: 0x40004010,
  4137. 0xa000000: 0x40000000,
  4138. 0xb000000: 0x84010,
  4139. 0xc000000: 0x80010,
  4140. 0xd000000: 0x0,
  4141. 0xe000000: 0x4010,
  4142. 0xf000000: 0x40080000,
  4143. 0x800000: 0x40004000,
  4144. 0x1800000: 0x84010,
  4145. 0x2800000: 0x10,
  4146. 0x3800000: 0x40004010,
  4147. 0x4800000: 0x40084010,
  4148. 0x5800000: 0x40000000,
  4149. 0x6800000: 0x80000,
  4150. 0x7800000: 0x40080010,
  4151. 0x8800000: 0x80010,
  4152. 0x9800000: 0x0,
  4153. 0xa800000: 0x4000,
  4154. 0xb800000: 0x40080000,
  4155. 0xc800000: 0x40000010,
  4156. 0xd800000: 0x84000,
  4157. 0xe800000: 0x40084000,
  4158. 0xf800000: 0x4010,
  4159. 0x10000000: 0x0,
  4160. 0x11000000: 0x40080010,
  4161. 0x12000000: 0x40004010,
  4162. 0x13000000: 0x40084000,
  4163. 0x14000000: 0x40080000,
  4164. 0x15000000: 0x10,
  4165. 0x16000000: 0x84010,
  4166. 0x17000000: 0x4000,
  4167. 0x18000000: 0x4010,
  4168. 0x19000000: 0x80000,
  4169. 0x1a000000: 0x80010,
  4170. 0x1b000000: 0x40000010,
  4171. 0x1c000000: 0x84000,
  4172. 0x1d000000: 0x40004000,
  4173. 0x1e000000: 0x40000000,
  4174. 0x1f000000: 0x40084010,
  4175. 0x10800000: 0x84010,
  4176. 0x11800000: 0x80000,
  4177. 0x12800000: 0x40080000,
  4178. 0x13800000: 0x4000,
  4179. 0x14800000: 0x40004000,
  4180. 0x15800000: 0x40084010,
  4181. 0x16800000: 0x10,
  4182. 0x17800000: 0x40000000,
  4183. 0x18800000: 0x40084000,
  4184. 0x19800000: 0x40000010,
  4185. 0x1a800000: 0x40004010,
  4186. 0x1b800000: 0x80010,
  4187. 0x1c800000: 0x0,
  4188. 0x1d800000: 0x4010,
  4189. 0x1e800000: 0x40080010,
  4190. 0x1f800000: 0x84000
  4191. },
  4192. {
  4193. 0x0: 0x104,
  4194. 0x100000: 0x0,
  4195. 0x200000: 0x4000100,
  4196. 0x300000: 0x10104,
  4197. 0x400000: 0x10004,
  4198. 0x500000: 0x4000004,
  4199. 0x600000: 0x4010104,
  4200. 0x700000: 0x4010000,
  4201. 0x800000: 0x4000000,
  4202. 0x900000: 0x4010100,
  4203. 0xa00000: 0x10100,
  4204. 0xb00000: 0x4010004,
  4205. 0xc00000: 0x4000104,
  4206. 0xd00000: 0x10000,
  4207. 0xe00000: 0x4,
  4208. 0xf00000: 0x100,
  4209. 0x80000: 0x4010100,
  4210. 0x180000: 0x4010004,
  4211. 0x280000: 0x0,
  4212. 0x380000: 0x4000100,
  4213. 0x480000: 0x4000004,
  4214. 0x580000: 0x10000,
  4215. 0x680000: 0x10004,
  4216. 0x780000: 0x104,
  4217. 0x880000: 0x4,
  4218. 0x980000: 0x100,
  4219. 0xa80000: 0x4010000,
  4220. 0xb80000: 0x10104,
  4221. 0xc80000: 0x10100,
  4222. 0xd80000: 0x4000104,
  4223. 0xe80000: 0x4010104,
  4224. 0xf80000: 0x4000000,
  4225. 0x1000000: 0x4010100,
  4226. 0x1100000: 0x10004,
  4227. 0x1200000: 0x10000,
  4228. 0x1300000: 0x4000100,
  4229. 0x1400000: 0x100,
  4230. 0x1500000: 0x4010104,
  4231. 0x1600000: 0x4000004,
  4232. 0x1700000: 0x0,
  4233. 0x1800000: 0x4000104,
  4234. 0x1900000: 0x4000000,
  4235. 0x1a00000: 0x4,
  4236. 0x1b00000: 0x10100,
  4237. 0x1c00000: 0x4010000,
  4238. 0x1d00000: 0x104,
  4239. 0x1e00000: 0x10104,
  4240. 0x1f00000: 0x4010004,
  4241. 0x1080000: 0x4000000,
  4242. 0x1180000: 0x104,
  4243. 0x1280000: 0x4010100,
  4244. 0x1380000: 0x0,
  4245. 0x1480000: 0x10004,
  4246. 0x1580000: 0x4000100,
  4247. 0x1680000: 0x100,
  4248. 0x1780000: 0x4010004,
  4249. 0x1880000: 0x10000,
  4250. 0x1980000: 0x4010104,
  4251. 0x1a80000: 0x10104,
  4252. 0x1b80000: 0x4000004,
  4253. 0x1c80000: 0x4000104,
  4254. 0x1d80000: 0x4010000,
  4255. 0x1e80000: 0x4,
  4256. 0x1f80000: 0x10100
  4257. },
  4258. {
  4259. 0x0: 0x80401000,
  4260. 0x10000: 0x80001040,
  4261. 0x20000: 0x401040,
  4262. 0x30000: 0x80400000,
  4263. 0x40000: 0x0,
  4264. 0x50000: 0x401000,
  4265. 0x60000: 0x80000040,
  4266. 0x70000: 0x400040,
  4267. 0x80000: 0x80000000,
  4268. 0x90000: 0x400000,
  4269. 0xa0000: 0x40,
  4270. 0xb0000: 0x80001000,
  4271. 0xc0000: 0x80400040,
  4272. 0xd0000: 0x1040,
  4273. 0xe0000: 0x1000,
  4274. 0xf0000: 0x80401040,
  4275. 0x8000: 0x80001040,
  4276. 0x18000: 0x40,
  4277. 0x28000: 0x80400040,
  4278. 0x38000: 0x80001000,
  4279. 0x48000: 0x401000,
  4280. 0x58000: 0x80401040,
  4281. 0x68000: 0x0,
  4282. 0x78000: 0x80400000,
  4283. 0x88000: 0x1000,
  4284. 0x98000: 0x80401000,
  4285. 0xa8000: 0x400000,
  4286. 0xb8000: 0x1040,
  4287. 0xc8000: 0x80000000,
  4288. 0xd8000: 0x400040,
  4289. 0xe8000: 0x401040,
  4290. 0xf8000: 0x80000040,
  4291. 0x100000: 0x400040,
  4292. 0x110000: 0x401000,
  4293. 0x120000: 0x80000040,
  4294. 0x130000: 0x0,
  4295. 0x140000: 0x1040,
  4296. 0x150000: 0x80400040,
  4297. 0x160000: 0x80401000,
  4298. 0x170000: 0x80001040,
  4299. 0x180000: 0x80401040,
  4300. 0x190000: 0x80000000,
  4301. 0x1a0000: 0x80400000,
  4302. 0x1b0000: 0x401040,
  4303. 0x1c0000: 0x80001000,
  4304. 0x1d0000: 0x400000,
  4305. 0x1e0000: 0x40,
  4306. 0x1f0000: 0x1000,
  4307. 0x108000: 0x80400000,
  4308. 0x118000: 0x80401040,
  4309. 0x128000: 0x0,
  4310. 0x138000: 0x401000,
  4311. 0x148000: 0x400040,
  4312. 0x158000: 0x80000000,
  4313. 0x168000: 0x80001040,
  4314. 0x178000: 0x40,
  4315. 0x188000: 0x80000040,
  4316. 0x198000: 0x1000,
  4317. 0x1a8000: 0x80001000,
  4318. 0x1b8000: 0x80400040,
  4319. 0x1c8000: 0x1040,
  4320. 0x1d8000: 0x80401000,
  4321. 0x1e8000: 0x400000,
  4322. 0x1f8000: 0x401040
  4323. },
  4324. {
  4325. 0x0: 0x80,
  4326. 0x1000: 0x1040000,
  4327. 0x2000: 0x40000,
  4328. 0x3000: 0x20000000,
  4329. 0x4000: 0x20040080,
  4330. 0x5000: 0x1000080,
  4331. 0x6000: 0x21000080,
  4332. 0x7000: 0x40080,
  4333. 0x8000: 0x1000000,
  4334. 0x9000: 0x20040000,
  4335. 0xa000: 0x20000080,
  4336. 0xb000: 0x21040080,
  4337. 0xc000: 0x21040000,
  4338. 0xd000: 0x0,
  4339. 0xe000: 0x1040080,
  4340. 0xf000: 0x21000000,
  4341. 0x800: 0x1040080,
  4342. 0x1800: 0x21000080,
  4343. 0x2800: 0x80,
  4344. 0x3800: 0x1040000,
  4345. 0x4800: 0x40000,
  4346. 0x5800: 0x20040080,
  4347. 0x6800: 0x21040000,
  4348. 0x7800: 0x20000000,
  4349. 0x8800: 0x20040000,
  4350. 0x9800: 0x0,
  4351. 0xa800: 0x21040080,
  4352. 0xb800: 0x1000080,
  4353. 0xc800: 0x20000080,
  4354. 0xd800: 0x21000000,
  4355. 0xe800: 0x1000000,
  4356. 0xf800: 0x40080,
  4357. 0x10000: 0x40000,
  4358. 0x11000: 0x80,
  4359. 0x12000: 0x20000000,
  4360. 0x13000: 0x21000080,
  4361. 0x14000: 0x1000080,
  4362. 0x15000: 0x21040000,
  4363. 0x16000: 0x20040080,
  4364. 0x17000: 0x1000000,
  4365. 0x18000: 0x21040080,
  4366. 0x19000: 0x21000000,
  4367. 0x1a000: 0x1040000,
  4368. 0x1b000: 0x20040000,
  4369. 0x1c000: 0x40080,
  4370. 0x1d000: 0x20000080,
  4371. 0x1e000: 0x0,
  4372. 0x1f000: 0x1040080,
  4373. 0x10800: 0x21000080,
  4374. 0x11800: 0x1000000,
  4375. 0x12800: 0x1040000,
  4376. 0x13800: 0x20040080,
  4377. 0x14800: 0x20000000,
  4378. 0x15800: 0x1040080,
  4379. 0x16800: 0x80,
  4380. 0x17800: 0x21040000,
  4381. 0x18800: 0x40080,
  4382. 0x19800: 0x21040080,
  4383. 0x1a800: 0x0,
  4384. 0x1b800: 0x21000000,
  4385. 0x1c800: 0x1000080,
  4386. 0x1d800: 0x40000,
  4387. 0x1e800: 0x20040000,
  4388. 0x1f800: 0x20000080
  4389. },
  4390. {
  4391. 0x0: 0x10000008,
  4392. 0x100: 0x2000,
  4393. 0x200: 0x10200000,
  4394. 0x300: 0x10202008,
  4395. 0x400: 0x10002000,
  4396. 0x500: 0x200000,
  4397. 0x600: 0x200008,
  4398. 0x700: 0x10000000,
  4399. 0x800: 0x0,
  4400. 0x900: 0x10002008,
  4401. 0xa00: 0x202000,
  4402. 0xb00: 0x8,
  4403. 0xc00: 0x10200008,
  4404. 0xd00: 0x202008,
  4405. 0xe00: 0x2008,
  4406. 0xf00: 0x10202000,
  4407. 0x80: 0x10200000,
  4408. 0x180: 0x10202008,
  4409. 0x280: 0x8,
  4410. 0x380: 0x200000,
  4411. 0x480: 0x202008,
  4412. 0x580: 0x10000008,
  4413. 0x680: 0x10002000,
  4414. 0x780: 0x2008,
  4415. 0x880: 0x200008,
  4416. 0x980: 0x2000,
  4417. 0xa80: 0x10002008,
  4418. 0xb80: 0x10200008,
  4419. 0xc80: 0x0,
  4420. 0xd80: 0x10202000,
  4421. 0xe80: 0x202000,
  4422. 0xf80: 0x10000000,
  4423. 0x1000: 0x10002000,
  4424. 0x1100: 0x10200008,
  4425. 0x1200: 0x10202008,
  4426. 0x1300: 0x2008,
  4427. 0x1400: 0x200000,
  4428. 0x1500: 0x10000000,
  4429. 0x1600: 0x10000008,
  4430. 0x1700: 0x202000,
  4431. 0x1800: 0x202008,
  4432. 0x1900: 0x0,
  4433. 0x1a00: 0x8,
  4434. 0x1b00: 0x10200000,
  4435. 0x1c00: 0x2000,
  4436. 0x1d00: 0x10002008,
  4437. 0x1e00: 0x10202000,
  4438. 0x1f00: 0x200008,
  4439. 0x1080: 0x8,
  4440. 0x1180: 0x202000,
  4441. 0x1280: 0x200000,
  4442. 0x1380: 0x10000008,
  4443. 0x1480: 0x10002000,
  4444. 0x1580: 0x2008,
  4445. 0x1680: 0x10202008,
  4446. 0x1780: 0x10200000,
  4447. 0x1880: 0x10202000,
  4448. 0x1980: 0x10200008,
  4449. 0x1a80: 0x2000,
  4450. 0x1b80: 0x202008,
  4451. 0x1c80: 0x200008,
  4452. 0x1d80: 0x0,
  4453. 0x1e80: 0x10000000,
  4454. 0x1f80: 0x10002008
  4455. },
  4456. {
  4457. 0x0: 0x100000,
  4458. 0x10: 0x2000401,
  4459. 0x20: 0x400,
  4460. 0x30: 0x100401,
  4461. 0x40: 0x2100401,
  4462. 0x50: 0x0,
  4463. 0x60: 0x1,
  4464. 0x70: 0x2100001,
  4465. 0x80: 0x2000400,
  4466. 0x90: 0x100001,
  4467. 0xa0: 0x2000001,
  4468. 0xb0: 0x2100400,
  4469. 0xc0: 0x2100000,
  4470. 0xd0: 0x401,
  4471. 0xe0: 0x100400,
  4472. 0xf0: 0x2000000,
  4473. 0x8: 0x2100001,
  4474. 0x18: 0x0,
  4475. 0x28: 0x2000401,
  4476. 0x38: 0x2100400,
  4477. 0x48: 0x100000,
  4478. 0x58: 0x2000001,
  4479. 0x68: 0x2000000,
  4480. 0x78: 0x401,
  4481. 0x88: 0x100401,
  4482. 0x98: 0x2000400,
  4483. 0xa8: 0x2100000,
  4484. 0xb8: 0x100001,
  4485. 0xc8: 0x400,
  4486. 0xd8: 0x2100401,
  4487. 0xe8: 0x1,
  4488. 0xf8: 0x100400,
  4489. 0x100: 0x2000000,
  4490. 0x110: 0x100000,
  4491. 0x120: 0x2000401,
  4492. 0x130: 0x2100001,
  4493. 0x140: 0x100001,
  4494. 0x150: 0x2000400,
  4495. 0x160: 0x2100400,
  4496. 0x170: 0x100401,
  4497. 0x180: 0x401,
  4498. 0x190: 0x2100401,
  4499. 0x1a0: 0x100400,
  4500. 0x1b0: 0x1,
  4501. 0x1c0: 0x0,
  4502. 0x1d0: 0x2100000,
  4503. 0x1e0: 0x2000001,
  4504. 0x1f0: 0x400,
  4505. 0x108: 0x100400,
  4506. 0x118: 0x2000401,
  4507. 0x128: 0x2100001,
  4508. 0x138: 0x1,
  4509. 0x148: 0x2000000,
  4510. 0x158: 0x100000,
  4511. 0x168: 0x401,
  4512. 0x178: 0x2100400,
  4513. 0x188: 0x2000001,
  4514. 0x198: 0x2100000,
  4515. 0x1a8: 0x0,
  4516. 0x1b8: 0x2100401,
  4517. 0x1c8: 0x100401,
  4518. 0x1d8: 0x400,
  4519. 0x1e8: 0x2000400,
  4520. 0x1f8: 0x100001
  4521. },
  4522. {
  4523. 0x0: 0x8000820,
  4524. 0x1: 0x20000,
  4525. 0x2: 0x8000000,
  4526. 0x3: 0x20,
  4527. 0x4: 0x20020,
  4528. 0x5: 0x8020820,
  4529. 0x6: 0x8020800,
  4530. 0x7: 0x800,
  4531. 0x8: 0x8020000,
  4532. 0x9: 0x8000800,
  4533. 0xa: 0x20800,
  4534. 0xb: 0x8020020,
  4535. 0xc: 0x820,
  4536. 0xd: 0x0,
  4537. 0xe: 0x8000020,
  4538. 0xf: 0x20820,
  4539. 0x80000000: 0x800,
  4540. 0x80000001: 0x8020820,
  4541. 0x80000002: 0x8000820,
  4542. 0x80000003: 0x8000000,
  4543. 0x80000004: 0x8020000,
  4544. 0x80000005: 0x20800,
  4545. 0x80000006: 0x20820,
  4546. 0x80000007: 0x20,
  4547. 0x80000008: 0x8000020,
  4548. 0x80000009: 0x820,
  4549. 0x8000000a: 0x20020,
  4550. 0x8000000b: 0x8020800,
  4551. 0x8000000c: 0x0,
  4552. 0x8000000d: 0x8020020,
  4553. 0x8000000e: 0x8000800,
  4554. 0x8000000f: 0x20000,
  4555. 0x10: 0x20820,
  4556. 0x11: 0x8020800,
  4557. 0x12: 0x20,
  4558. 0x13: 0x800,
  4559. 0x14: 0x8000800,
  4560. 0x15: 0x8000020,
  4561. 0x16: 0x8020020,
  4562. 0x17: 0x20000,
  4563. 0x18: 0x0,
  4564. 0x19: 0x20020,
  4565. 0x1a: 0x8020000,
  4566. 0x1b: 0x8000820,
  4567. 0x1c: 0x8020820,
  4568. 0x1d: 0x20800,
  4569. 0x1e: 0x820,
  4570. 0x1f: 0x8000000,
  4571. 0x80000010: 0x20000,
  4572. 0x80000011: 0x800,
  4573. 0x80000012: 0x8020020,
  4574. 0x80000013: 0x20820,
  4575. 0x80000014: 0x20,
  4576. 0x80000015: 0x8020000,
  4577. 0x80000016: 0x8000000,
  4578. 0x80000017: 0x8000820,
  4579. 0x80000018: 0x8020820,
  4580. 0x80000019: 0x8000020,
  4581. 0x8000001a: 0x8000800,
  4582. 0x8000001b: 0x0,
  4583. 0x8000001c: 0x20800,
  4584. 0x8000001d: 0x820,
  4585. 0x8000001e: 0x20020,
  4586. 0x8000001f: 0x8020800
  4587. }
  4588. ];
  4589. // Masks that select the SBOX input
  4590. var SBOX_MASK = [
  4591. 0xf8000001, 0x1f800000, 0x01f80000, 0x001f8000,
  4592. 0x0001f800, 0x00001f80, 0x000001f8, 0x8000001f
  4593. ];
  4594. /**
  4595. * DES block cipher algorithm.
  4596. */
  4597. var DES = C_algo.DES = BlockCipher.extend({
  4598. _doReset: function () {
  4599. // Shortcuts
  4600. var key = this._key;
  4601. var keyWords = key.words;
  4602. // Select 56 bits according to PC1
  4603. var keyBits = [];
  4604. for (var i = 0; i < 56; i++) {
  4605. var keyBitPos = PC1[i] - 1;
  4606. keyBits[i] = (keyWords[keyBitPos >>> 5] >>> (31 - keyBitPos % 32)) & 1;
  4607. }
  4608. // Assemble 16 subkeys
  4609. var subKeys = this._subKeys = [];
  4610. for (var nSubKey = 0; nSubKey < 16; nSubKey++) {
  4611. // Create subkey
  4612. var subKey = subKeys[nSubKey] = [];
  4613. // Shortcut
  4614. var bitShift = BIT_SHIFTS[nSubKey];
  4615. // Select 48 bits according to PC2
  4616. for (var i = 0; i < 24; i++) {
  4617. // Select from the left 28 key bits
  4618. subKey[(i / 6) | 0] |= keyBits[((PC2[i] - 1) + bitShift) % 28] << (31 - i % 6);
  4619. // Select from the right 28 key bits
  4620. subKey[4 + ((i / 6) | 0)] |= keyBits[28 + (((PC2[i + 24] - 1) + bitShift) % 28)] << (31 - i % 6);
  4621. }
  4622. // Since each subkey is applied to an expanded 32-bit input,
  4623. // the subkey can be broken into 8 values scaled to 32-bits,
  4624. // which allows the key to be used without expansion
  4625. subKey[0] = (subKey[0] << 1) | (subKey[0] >>> 31);
  4626. for (var i = 1; i < 7; i++) {
  4627. subKey[i] = subKey[i] >>> ((i - 1) * 4 + 3);
  4628. }
  4629. subKey[7] = (subKey[7] << 5) | (subKey[7] >>> 27);
  4630. }
  4631. // Compute inverse subkeys
  4632. var invSubKeys = this._invSubKeys = [];
  4633. for (var i = 0; i < 16; i++) {
  4634. invSubKeys[i] = subKeys[15 - i];
  4635. }
  4636. },
  4637. encryptBlock: function (M, offset) {
  4638. this._doCryptBlock(M, offset, this._subKeys);
  4639. },
  4640. decryptBlock: function (M, offset) {
  4641. this._doCryptBlock(M, offset, this._invSubKeys);
  4642. },
  4643. _doCryptBlock: function (M, offset, subKeys) {
  4644. // Get input
  4645. this._lBlock = M[offset];
  4646. this._rBlock = M[offset + 1];
  4647. // Initial permutation
  4648. exchangeLR.call(this, 4, 0x0f0f0f0f);
  4649. exchangeLR.call(this, 16, 0x0000ffff);
  4650. exchangeRL.call(this, 2, 0x33333333);
  4651. exchangeRL.call(this, 8, 0x00ff00ff);
  4652. exchangeLR.call(this, 1, 0x55555555);
  4653. // Rounds
  4654. for (var round = 0; round < 16; round++) {
  4655. // Shortcuts
  4656. var subKey = subKeys[round];
  4657. var lBlock = this._lBlock;
  4658. var rBlock = this._rBlock;
  4659. // Feistel function
  4660. var f = 0;
  4661. for (var i = 0; i < 8; i++) {
  4662. f |= SBOX_P[i][((rBlock ^ subKey[i]) & SBOX_MASK[i]) >>> 0];
  4663. }
  4664. this._lBlock = rBlock;
  4665. this._rBlock = lBlock ^ f;
  4666. }
  4667. // Undo swap from last round
  4668. var t = this._lBlock;
  4669. this._lBlock = this._rBlock;
  4670. this._rBlock = t;
  4671. // Final permutation
  4672. exchangeLR.call(this, 1, 0x55555555);
  4673. exchangeRL.call(this, 8, 0x00ff00ff);
  4674. exchangeRL.call(this, 2, 0x33333333);
  4675. exchangeLR.call(this, 16, 0x0000ffff);
  4676. exchangeLR.call(this, 4, 0x0f0f0f0f);
  4677. // Set output
  4678. M[offset] = this._lBlock;
  4679. M[offset + 1] = this._rBlock;
  4680. },
  4681. keySize: 64 / 32,
  4682. ivSize: 64 / 32,
  4683. blockSize: 64 / 32
  4684. });
  4685. // Swap bits across the left and right words
  4686. function exchangeLR(offset, mask) {
  4687. var t = ((this._lBlock >>> offset) ^ this._rBlock) & mask;
  4688. this._rBlock ^= t;
  4689. this._lBlock ^= t << offset;
  4690. }
  4691. function exchangeRL(offset, mask) {
  4692. var t = ((this._rBlock >>> offset) ^ this._lBlock) & mask;
  4693. this._lBlock ^= t;
  4694. this._rBlock ^= t << offset;
  4695. }
  4696. /**
  4697. * Shortcut functions to the cipher's object interface.
  4698. *
  4699. * @example
  4700. *
  4701. * var ciphertext = CryptoJS.DES.encrypt(message, key, cfg);
  4702. * var plaintext = CryptoJS.DES.decrypt(ciphertext, key, cfg);
  4703. */
  4704. C.DES = BlockCipher._createHelper(DES);
  4705. /**
  4706. * Triple-DES block cipher algorithm.
  4707. */
  4708. var TripleDES = C_algo.TripleDES = BlockCipher.extend({
  4709. _doReset: function () {
  4710. // Shortcuts
  4711. var key = this._key;
  4712. var keyWords = key.words;
  4713. // Make sure the key length is valid (64, 128 or >= 192 bit)
  4714. if (keyWords.length !== 2 && keyWords.length !== 4 && keyWords.length < 6) {
  4715. throw new Error('Invalid key length - 3DES requires the key length to be 64, 128, 192 or >192.');
  4716. }
  4717. // Extend the key according to the keying options defined in 3DES standard
  4718. var key1 = keyWords.slice(0, 2);
  4719. var key2 = keyWords.length < 4 ? keyWords.slice(0, 2) : keyWords.slice(2, 4);
  4720. var key3 = keyWords.length < 6 ? keyWords.slice(0, 2) : keyWords.slice(4, 6);
  4721. // Create DES instances
  4722. this._des1 = DES.createEncryptor(WordArray.create(key1));
  4723. this._des2 = DES.createEncryptor(WordArray.create(key2));
  4724. this._des3 = DES.createEncryptor(WordArray.create(key3));
  4725. },
  4726. encryptBlock: function (M, offset) {
  4727. this._des1.encryptBlock(M, offset);
  4728. this._des2.decryptBlock(M, offset);
  4729. this._des3.encryptBlock(M, offset);
  4730. },
  4731. decryptBlock: function (M, offset) {
  4732. this._des3.decryptBlock(M, offset);
  4733. this._des2.encryptBlock(M, offset);
  4734. this._des1.decryptBlock(M, offset);
  4735. },
  4736. keySize: 192 / 32,
  4737. ivSize: 64 / 32,
  4738. blockSize: 64 / 32
  4739. });
  4740. /**
  4741. * Shortcut functions to the cipher's object interface.
  4742. *
  4743. * @example
  4744. *
  4745. * var ciphertext = CryptoJS.TripleDES.encrypt(message, key, cfg);
  4746. * var plaintext = CryptoJS.TripleDES.decrypt(ciphertext, key, cfg);
  4747. */
  4748. C.TripleDES = BlockCipher._createHelper(TripleDES);
  4749. }());
  4750. (function () {
  4751. // Shortcuts
  4752. var C = CryptoJS;
  4753. var C_lib = C.lib;
  4754. var StreamCipher = C_lib.StreamCipher;
  4755. var C_algo = C.algo;
  4756. /**
  4757. * RC4 stream cipher algorithm.
  4758. */
  4759. var RC4 = C_algo.RC4 = StreamCipher.extend({
  4760. _doReset: function () {
  4761. // Shortcuts
  4762. var key = this._key;
  4763. var keyWords = key.words;
  4764. var keySigBytes = key.sigBytes;
  4765. // Init sbox
  4766. var S = this._S = [];
  4767. for (var i = 0; i < 256; i++) {
  4768. S[i] = i;
  4769. }
  4770. // Key setup
  4771. for (var i = 0, j = 0; i < 256; i++) {
  4772. var keyByteIndex = i % keySigBytes;
  4773. var keyByte = (keyWords[keyByteIndex >>> 2] >>> (24 - (keyByteIndex % 4) * 8)) & 0xff;
  4774. j = (j + S[i] + keyByte) % 256;
  4775. // Swap
  4776. var t = S[i];
  4777. S[i] = S[j];
  4778. S[j] = t;
  4779. }
  4780. // Counters
  4781. this._i = this._j = 0;
  4782. },
  4783. _doProcessBlock: function (M, offset) {
  4784. M[offset] ^= generateKeystreamWord.call(this);
  4785. },
  4786. keySize: 256 / 32,
  4787. ivSize: 0
  4788. });
  4789. function generateKeystreamWord() {
  4790. // Shortcuts
  4791. var S = this._S;
  4792. var i = this._i;
  4793. var j = this._j;
  4794. // Generate keystream word
  4795. var keystreamWord = 0;
  4796. for (var n = 0; n < 4; n++) {
  4797. i = (i + 1) % 256;
  4798. j = (j + S[i]) % 256;
  4799. // Swap
  4800. var t = S[i];
  4801. S[i] = S[j];
  4802. S[j] = t;
  4803. keystreamWord |= S[(S[i] + S[j]) % 256] << (24 - n * 8);
  4804. }
  4805. // Update counters
  4806. this._i = i;
  4807. this._j = j;
  4808. return keystreamWord;
  4809. }
  4810. /**
  4811. * Shortcut functions to the cipher's object interface.
  4812. *
  4813. * @example
  4814. *
  4815. * var ciphertext = CryptoJS.RC4.encrypt(message, key, cfg);
  4816. * var plaintext = CryptoJS.RC4.decrypt(ciphertext, key, cfg);
  4817. */
  4818. C.RC4 = StreamCipher._createHelper(RC4);
  4819. /**
  4820. * Modified RC4 stream cipher algorithm.
  4821. */
  4822. var RC4Drop = C_algo.RC4Drop = RC4.extend({
  4823. /**
  4824. * Configuration options.
  4825. *
  4826. * @property {number} drop The number of keystream words to drop. Default 192
  4827. */
  4828. cfg: RC4.cfg.extend({
  4829. drop: 192
  4830. }),
  4831. _doReset: function () {
  4832. RC4._doReset.call(this);
  4833. // Drop
  4834. for (var i = this.cfg.drop; i > 0; i--) {
  4835. generateKeystreamWord.call(this);
  4836. }
  4837. }
  4838. });
  4839. /**
  4840. * Shortcut functions to the cipher's object interface.
  4841. *
  4842. * @example
  4843. *
  4844. * var ciphertext = CryptoJS.RC4Drop.encrypt(message, key, cfg);
  4845. * var plaintext = CryptoJS.RC4Drop.decrypt(ciphertext, key, cfg);
  4846. */
  4847. C.RC4Drop = StreamCipher._createHelper(RC4Drop);
  4848. }());
  4849. /** @preserve
  4850. * Counter block mode compatible with Dr Brian Gladman fileenc.c
  4851. * derived from CryptoJS.mode.CTR
  4852. * Jan Hruby jhruby.web@gmail.com
  4853. */
  4854. CryptoJS.mode.CTRGladman = (function () {
  4855. var CTRGladman = CryptoJS.lib.BlockCipherMode.extend();
  4856. function incWord(word) {
  4857. if (((word >> 24) & 0xff) === 0xff) { //overflow
  4858. var b1 = (word >> 16) & 0xff;
  4859. var b2 = (word >> 8) & 0xff;
  4860. var b3 = word & 0xff;
  4861. if (b1 === 0xff) // overflow b1
  4862. {
  4863. b1 = 0;
  4864. if (b2 === 0xff) {
  4865. b2 = 0;
  4866. if (b3 === 0xff) {
  4867. b3 = 0;
  4868. }
  4869. else {
  4870. ++b3;
  4871. }
  4872. }
  4873. else {
  4874. ++b2;
  4875. }
  4876. }
  4877. else {
  4878. ++b1;
  4879. }
  4880. word = 0;
  4881. word += (b1 << 16);
  4882. word += (b2 << 8);
  4883. word += b3;
  4884. }
  4885. else {
  4886. word += (0x01 << 24);
  4887. }
  4888. return word;
  4889. }
  4890. function incCounter(counter) {
  4891. if ((counter[0] = incWord(counter[0])) === 0) {
  4892. // encr_data in fileenc.c from Dr Brian Gladman's counts only with DWORD j < 8
  4893. counter[1] = incWord(counter[1]);
  4894. }
  4895. return counter;
  4896. }
  4897. var Encryptor = CTRGladman.Encryptor = CTRGladman.extend({
  4898. processBlock: function (words, offset) {
  4899. // Shortcuts
  4900. var cipher = this._cipher
  4901. var blockSize = cipher.blockSize;
  4902. var iv = this._iv;
  4903. var counter = this._counter;
  4904. // Generate keystream
  4905. if (iv) {
  4906. counter = this._counter = iv.slice(0);
  4907. // Remove IV for subsequent blocks
  4908. this._iv = undefined;
  4909. }
  4910. incCounter(counter);
  4911. var keystream = counter.slice(0);
  4912. cipher.encryptBlock(keystream, 0);
  4913. // Encrypt
  4914. for (var i = 0; i < blockSize; i++) {
  4915. words[offset + i] ^= keystream[i];
  4916. }
  4917. }
  4918. });
  4919. CTRGladman.Decryptor = Encryptor;
  4920. return CTRGladman;
  4921. }());
  4922. (function () {
  4923. // Shortcuts
  4924. var C = CryptoJS;
  4925. var C_lib = C.lib;
  4926. var StreamCipher = C_lib.StreamCipher;
  4927. var C_algo = C.algo;
  4928. // Reusable objects
  4929. var S = [];
  4930. var C_ = [];
  4931. var G = [];
  4932. /**
  4933. * Rabbit stream cipher algorithm
  4934. */
  4935. var Rabbit = C_algo.Rabbit = StreamCipher.extend({
  4936. _doReset: function () {
  4937. // Shortcuts
  4938. var K = this._key.words;
  4939. var iv = this.cfg.iv;
  4940. // Swap endian
  4941. for (var i = 0; i < 4; i++) {
  4942. K[i] = (((K[i] << 8) | (K[i] >>> 24)) & 0x00ff00ff) |
  4943. (((K[i] << 24) | (K[i] >>> 8)) & 0xff00ff00);
  4944. }
  4945. // Generate initial state values
  4946. var X = this._X = [
  4947. K[0], (K[3] << 16) | (K[2] >>> 16),
  4948. K[1], (K[0] << 16) | (K[3] >>> 16),
  4949. K[2], (K[1] << 16) | (K[0] >>> 16),
  4950. K[3], (K[2] << 16) | (K[1] >>> 16)
  4951. ];
  4952. // Generate initial counter values
  4953. var C = this._C = [
  4954. (K[2] << 16) | (K[2] >>> 16), (K[0] & 0xffff0000) | (K[1] & 0x0000ffff),
  4955. (K[3] << 16) | (K[3] >>> 16), (K[1] & 0xffff0000) | (K[2] & 0x0000ffff),
  4956. (K[0] << 16) | (K[0] >>> 16), (K[2] & 0xffff0000) | (K[3] & 0x0000ffff),
  4957. (K[1] << 16) | (K[1] >>> 16), (K[3] & 0xffff0000) | (K[0] & 0x0000ffff)
  4958. ];
  4959. // Carry bit
  4960. this._b = 0;
  4961. // Iterate the system four times
  4962. for (var i = 0; i < 4; i++) {
  4963. nextState.call(this);
  4964. }
  4965. // Modify the counters
  4966. for (var i = 0; i < 8; i++) {
  4967. C[i] ^= X[(i + 4) & 7];
  4968. }
  4969. // IV setup
  4970. if (iv) {
  4971. // Shortcuts
  4972. var IV = iv.words;
  4973. var IV_0 = IV[0];
  4974. var IV_1 = IV[1];
  4975. // Generate four subvectors
  4976. var i0 = (((IV_0 << 8) | (IV_0 >>> 24)) & 0x00ff00ff) | (((IV_0 << 24) | (IV_0 >>> 8)) & 0xff00ff00);
  4977. var i2 = (((IV_1 << 8) | (IV_1 >>> 24)) & 0x00ff00ff) | (((IV_1 << 24) | (IV_1 >>> 8)) & 0xff00ff00);
  4978. var i1 = (i0 >>> 16) | (i2 & 0xffff0000);
  4979. var i3 = (i2 << 16) | (i0 & 0x0000ffff);
  4980. // Modify counter values
  4981. C[0] ^= i0;
  4982. C[1] ^= i1;
  4983. C[2] ^= i2;
  4984. C[3] ^= i3;
  4985. C[4] ^= i0;
  4986. C[5] ^= i1;
  4987. C[6] ^= i2;
  4988. C[7] ^= i3;
  4989. // Iterate the system four times
  4990. for (var i = 0; i < 4; i++) {
  4991. nextState.call(this);
  4992. }
  4993. }
  4994. },
  4995. _doProcessBlock: function (M, offset) {
  4996. // Shortcut
  4997. var X = this._X;
  4998. // Iterate the system
  4999. nextState.call(this);
  5000. // Generate four keystream words
  5001. S[0] = X[0] ^ (X[5] >>> 16) ^ (X[3] << 16);
  5002. S[1] = X[2] ^ (X[7] >>> 16) ^ (X[5] << 16);
  5003. S[2] = X[4] ^ (X[1] >>> 16) ^ (X[7] << 16);
  5004. S[3] = X[6] ^ (X[3] >>> 16) ^ (X[1] << 16);
  5005. for (var i = 0; i < 4; i++) {
  5006. // Swap endian
  5007. S[i] = (((S[i] << 8) | (S[i] >>> 24)) & 0x00ff00ff) |
  5008. (((S[i] << 24) | (S[i] >>> 8)) & 0xff00ff00);
  5009. // Encrypt
  5010. M[offset + i] ^= S[i];
  5011. }
  5012. },
  5013. blockSize: 128 / 32,
  5014. ivSize: 64 / 32
  5015. });
  5016. function nextState() {
  5017. // Shortcuts
  5018. var X = this._X;
  5019. var C = this._C;
  5020. // Save old counter values
  5021. for (var i = 0; i < 8; i++) {
  5022. C_[i] = C[i];
  5023. }
  5024. // Calculate new counter values
  5025. C[0] = (C[0] + 0x4d34d34d + this._b) | 0;
  5026. C[1] = (C[1] + 0xd34d34d3 + ((C[0] >>> 0) < (C_[0] >>> 0) ? 1 : 0)) | 0;
  5027. C[2] = (C[2] + 0x34d34d34 + ((C[1] >>> 0) < (C_[1] >>> 0) ? 1 : 0)) | 0;
  5028. C[3] = (C[3] + 0x4d34d34d + ((C[2] >>> 0) < (C_[2] >>> 0) ? 1 : 0)) | 0;
  5029. C[4] = (C[4] + 0xd34d34d3 + ((C[3] >>> 0) < (C_[3] >>> 0) ? 1 : 0)) | 0;
  5030. C[5] = (C[5] + 0x34d34d34 + ((C[4] >>> 0) < (C_[4] >>> 0) ? 1 : 0)) | 0;
  5031. C[6] = (C[6] + 0x4d34d34d + ((C[5] >>> 0) < (C_[5] >>> 0) ? 1 : 0)) | 0;
  5032. C[7] = (C[7] + 0xd34d34d3 + ((C[6] >>> 0) < (C_[6] >>> 0) ? 1 : 0)) | 0;
  5033. this._b = (C[7] >>> 0) < (C_[7] >>> 0) ? 1 : 0;
  5034. // Calculate the g-values
  5035. for (var i = 0; i < 8; i++) {
  5036. var gx = X[i] + C[i];
  5037. // Construct high and low argument for squaring
  5038. var ga = gx & 0xffff;
  5039. var gb = gx >>> 16;
  5040. // Calculate high and low result of squaring
  5041. var gh = ((((ga * ga) >>> 17) + ga * gb) >>> 15) + gb * gb;
  5042. var gl = (((gx & 0xffff0000) * gx) | 0) + (((gx & 0x0000ffff) * gx) | 0);
  5043. // High XOR low
  5044. G[i] = gh ^ gl;
  5045. }
  5046. // Calculate new state values
  5047. X[0] = (G[0] + ((G[7] << 16) | (G[7] >>> 16)) + ((G[6] << 16) | (G[6] >>> 16))) | 0;
  5048. X[1] = (G[1] + ((G[0] << 8) | (G[0] >>> 24)) + G[7]) | 0;
  5049. X[2] = (G[2] + ((G[1] << 16) | (G[1] >>> 16)) + ((G[0] << 16) | (G[0] >>> 16))) | 0;
  5050. X[3] = (G[3] + ((G[2] << 8) | (G[2] >>> 24)) + G[1]) | 0;
  5051. X[4] = (G[4] + ((G[3] << 16) | (G[3] >>> 16)) + ((G[2] << 16) | (G[2] >>> 16))) | 0;
  5052. X[5] = (G[5] + ((G[4] << 8) | (G[4] >>> 24)) + G[3]) | 0;
  5053. X[6] = (G[6] + ((G[5] << 16) | (G[5] >>> 16)) + ((G[4] << 16) | (G[4] >>> 16))) | 0;
  5054. X[7] = (G[7] + ((G[6] << 8) | (G[6] >>> 24)) + G[5]) | 0;
  5055. }
  5056. /**
  5057. * Shortcut functions to the cipher's object interface.
  5058. *
  5059. * @example
  5060. *
  5061. * var ciphertext = CryptoJS.Rabbit.encrypt(message, key, cfg);
  5062. * var plaintext = CryptoJS.Rabbit.decrypt(ciphertext, key, cfg);
  5063. */
  5064. C.Rabbit = StreamCipher._createHelper(Rabbit);
  5065. }());
  5066. /**
  5067. * Counter block mode.
  5068. */
  5069. CryptoJS.mode.CTR = (function () {
  5070. var CTR = CryptoJS.lib.BlockCipherMode.extend();
  5071. var Encryptor = CTR.Encryptor = CTR.extend({
  5072. processBlock: function (words, offset) {
  5073. // Shortcuts
  5074. var cipher = this._cipher
  5075. var blockSize = cipher.blockSize;
  5076. var iv = this._iv;
  5077. var counter = this._counter;
  5078. // Generate keystream
  5079. if (iv) {
  5080. counter = this._counter = iv.slice(0);
  5081. // Remove IV for subsequent blocks
  5082. this._iv = undefined;
  5083. }
  5084. var keystream = counter.slice(0);
  5085. cipher.encryptBlock(keystream, 0);
  5086. // Increment counter
  5087. counter[blockSize - 1] = (counter[blockSize - 1] + 1) | 0
  5088. // Encrypt
  5089. for (var i = 0; i < blockSize; i++) {
  5090. words[offset + i] ^= keystream[i];
  5091. }
  5092. }
  5093. });
  5094. CTR.Decryptor = Encryptor;
  5095. return CTR;
  5096. }());
  5097. (function () {
  5098. // Shortcuts
  5099. var C = CryptoJS;
  5100. var C_lib = C.lib;
  5101. var StreamCipher = C_lib.StreamCipher;
  5102. var C_algo = C.algo;
  5103. // Reusable objects
  5104. var S = [];
  5105. var C_ = [];
  5106. var G = [];
  5107. /**
  5108. * Rabbit stream cipher algorithm.
  5109. *
  5110. * This is a legacy version that neglected to convert the key to little-endian.
  5111. * This error doesn't affect the cipher's security,
  5112. * but it does affect its compatibility with other implementations.
  5113. */
  5114. var RabbitLegacy = C_algo.RabbitLegacy = StreamCipher.extend({
  5115. _doReset: function () {
  5116. // Shortcuts
  5117. var K = this._key.words;
  5118. var iv = this.cfg.iv;
  5119. // Generate initial state values
  5120. var X = this._X = [
  5121. K[0], (K[3] << 16) | (K[2] >>> 16),
  5122. K[1], (K[0] << 16) | (K[3] >>> 16),
  5123. K[2], (K[1] << 16) | (K[0] >>> 16),
  5124. K[3], (K[2] << 16) | (K[1] >>> 16)
  5125. ];
  5126. // Generate initial counter values
  5127. var C = this._C = [
  5128. (K[2] << 16) | (K[2] >>> 16), (K[0] & 0xffff0000) | (K[1] & 0x0000ffff),
  5129. (K[3] << 16) | (K[3] >>> 16), (K[1] & 0xffff0000) | (K[2] & 0x0000ffff),
  5130. (K[0] << 16) | (K[0] >>> 16), (K[2] & 0xffff0000) | (K[3] & 0x0000ffff),
  5131. (K[1] << 16) | (K[1] >>> 16), (K[3] & 0xffff0000) | (K[0] & 0x0000ffff)
  5132. ];
  5133. // Carry bit
  5134. this._b = 0;
  5135. // Iterate the system four times
  5136. for (var i = 0; i < 4; i++) {
  5137. nextState.call(this);
  5138. }
  5139. // Modify the counters
  5140. for (var i = 0; i < 8; i++) {
  5141. C[i] ^= X[(i + 4) & 7];
  5142. }
  5143. // IV setup
  5144. if (iv) {
  5145. // Shortcuts
  5146. var IV = iv.words;
  5147. var IV_0 = IV[0];
  5148. var IV_1 = IV[1];
  5149. // Generate four subvectors
  5150. var i0 = (((IV_0 << 8) | (IV_0 >>> 24)) & 0x00ff00ff) | (((IV_0 << 24) | (IV_0 >>> 8)) & 0xff00ff00);
  5151. var i2 = (((IV_1 << 8) | (IV_1 >>> 24)) & 0x00ff00ff) | (((IV_1 << 24) | (IV_1 >>> 8)) & 0xff00ff00);
  5152. var i1 = (i0 >>> 16) | (i2 & 0xffff0000);
  5153. var i3 = (i2 << 16) | (i0 & 0x0000ffff);
  5154. // Modify counter values
  5155. C[0] ^= i0;
  5156. C[1] ^= i1;
  5157. C[2] ^= i2;
  5158. C[3] ^= i3;
  5159. C[4] ^= i0;
  5160. C[5] ^= i1;
  5161. C[6] ^= i2;
  5162. C[7] ^= i3;
  5163. // Iterate the system four times
  5164. for (var i = 0; i < 4; i++) {
  5165. nextState.call(this);
  5166. }
  5167. }
  5168. },
  5169. _doProcessBlock: function (M, offset) {
  5170. // Shortcut
  5171. var X = this._X;
  5172. // Iterate the system
  5173. nextState.call(this);
  5174. // Generate four keystream words
  5175. S[0] = X[0] ^ (X[5] >>> 16) ^ (X[3] << 16);
  5176. S[1] = X[2] ^ (X[7] >>> 16) ^ (X[5] << 16);
  5177. S[2] = X[4] ^ (X[1] >>> 16) ^ (X[7] << 16);
  5178. S[3] = X[6] ^ (X[3] >>> 16) ^ (X[1] << 16);
  5179. for (var i = 0; i < 4; i++) {
  5180. // Swap endian
  5181. S[i] = (((S[i] << 8) | (S[i] >>> 24)) & 0x00ff00ff) |
  5182. (((S[i] << 24) | (S[i] >>> 8)) & 0xff00ff00);
  5183. // Encrypt
  5184. M[offset + i] ^= S[i];
  5185. }
  5186. },
  5187. blockSize: 128 / 32,
  5188. ivSize: 64 / 32
  5189. });
  5190. function nextState() {
  5191. // Shortcuts
  5192. var X = this._X;
  5193. var C = this._C;
  5194. // Save old counter values
  5195. for (var i = 0; i < 8; i++) {
  5196. C_[i] = C[i];
  5197. }
  5198. // Calculate new counter values
  5199. C[0] = (C[0] + 0x4d34d34d + this._b) | 0;
  5200. C[1] = (C[1] + 0xd34d34d3 + ((C[0] >>> 0) < (C_[0] >>> 0) ? 1 : 0)) | 0;
  5201. C[2] = (C[2] + 0x34d34d34 + ((C[1] >>> 0) < (C_[1] >>> 0) ? 1 : 0)) | 0;
  5202. C[3] = (C[3] + 0x4d34d34d + ((C[2] >>> 0) < (C_[2] >>> 0) ? 1 : 0)) | 0;
  5203. C[4] = (C[4] + 0xd34d34d3 + ((C[3] >>> 0) < (C_[3] >>> 0) ? 1 : 0)) | 0;
  5204. C[5] = (C[5] + 0x34d34d34 + ((C[4] >>> 0) < (C_[4] >>> 0) ? 1 : 0)) | 0;
  5205. C[6] = (C[6] + 0x4d34d34d + ((C[5] >>> 0) < (C_[5] >>> 0) ? 1 : 0)) | 0;
  5206. C[7] = (C[7] + 0xd34d34d3 + ((C[6] >>> 0) < (C_[6] >>> 0) ? 1 : 0)) | 0;
  5207. this._b = (C[7] >>> 0) < (C_[7] >>> 0) ? 1 : 0;
  5208. // Calculate the g-values
  5209. for (var i = 0; i < 8; i++) {
  5210. var gx = X[i] + C[i];
  5211. // Construct high and low argument for squaring
  5212. var ga = gx & 0xffff;
  5213. var gb = gx >>> 16;
  5214. // Calculate high and low result of squaring
  5215. var gh = ((((ga * ga) >>> 17) + ga * gb) >>> 15) + gb * gb;
  5216. var gl = (((gx & 0xffff0000) * gx) | 0) + (((gx & 0x0000ffff) * gx) | 0);
  5217. // High XOR low
  5218. G[i] = gh ^ gl;
  5219. }
  5220. // Calculate new state values
  5221. X[0] = (G[0] + ((G[7] << 16) | (G[7] >>> 16)) + ((G[6] << 16) | (G[6] >>> 16))) | 0;
  5222. X[1] = (G[1] + ((G[0] << 8) | (G[0] >>> 24)) + G[7]) | 0;
  5223. X[2] = (G[2] + ((G[1] << 16) | (G[1] >>> 16)) + ((G[0] << 16) | (G[0] >>> 16))) | 0;
  5224. X[3] = (G[3] + ((G[2] << 8) | (G[2] >>> 24)) + G[1]) | 0;
  5225. X[4] = (G[4] + ((G[3] << 16) | (G[3] >>> 16)) + ((G[2] << 16) | (G[2] >>> 16))) | 0;
  5226. X[5] = (G[5] + ((G[4] << 8) | (G[4] >>> 24)) + G[3]) | 0;
  5227. X[6] = (G[6] + ((G[5] << 16) | (G[5] >>> 16)) + ((G[4] << 16) | (G[4] >>> 16))) | 0;
  5228. X[7] = (G[7] + ((G[6] << 8) | (G[6] >>> 24)) + G[5]) | 0;
  5229. }
  5230. /**
  5231. * Shortcut functions to the cipher's object interface.
  5232. *
  5233. * @example
  5234. *
  5235. * var ciphertext = CryptoJS.RabbitLegacy.encrypt(message, key, cfg);
  5236. * var plaintext = CryptoJS.RabbitLegacy.decrypt(ciphertext, key, cfg);
  5237. */
  5238. C.RabbitLegacy = StreamCipher._createHelper(RabbitLegacy);
  5239. }());
  5240. /**
  5241. * Zero padding strategy.
  5242. */
  5243. CryptoJS.pad.ZeroPadding = {
  5244. pad: function (data, blockSize) {
  5245. // Shortcut
  5246. var blockSizeBytes = blockSize * 4;
  5247. // Pad
  5248. data.clamp();
  5249. data.sigBytes += blockSizeBytes - ((data.sigBytes % blockSizeBytes) || blockSizeBytes);
  5250. },
  5251. unpad: function (data) {
  5252. // Shortcut
  5253. var dataWords = data.words;
  5254. // Unpad
  5255. var i = data.sigBytes - 1;
  5256. for (var i = data.sigBytes - 1; i >= 0; i--) {
  5257. if (((dataWords[i >>> 2] >>> (24 - (i % 4) * 8)) & 0xff)) {
  5258. data.sigBytes = i + 1;
  5259. break;
  5260. }
  5261. }
  5262. }
  5263. };