crypto-hiker.js 183 KB

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