crypto-js.js 187 KB

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