crypto-js.js 203 KB

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