crypto-hiker.js 187 KB

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