crypto-js.js 186 KB

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