auth.js 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051
  1. (function(e){if("function"==typeof bootstrap)bootstrap("wpapiauth",e);else if("object"==typeof exports)module.exports=e();else if("function"==typeof define&&define.amd)define(e);else if("undefined"!=typeof ses){if(!ses.ok())return;ses.makeWpApiAuth=e}else"undefined"!=typeof window?window.wpApiAuth=e():global.wpApiAuth=e()})(function(){var define,ses,bootstrap,module,exports;
  2. return (function(e,t,n){function i(n,s){if(!t[n]){if(!e[n]){var o=typeof require=="function"&&require;if(!s&&o)return o(n,!0);if(r)return r(n,!0);throw new Error("Cannot find module '"+n+"'")}var u=t[n]={exports:{}};e[n][0].call(u.exports,function(t){var r=e[n][1][t];return i(r?r:t)},u,u.exports)}return t[n].exports}var r=typeof require=="function"&&require;for(var s=0;s<n.length;s++)i(n[s]);return i})({1:[function(require,module,exports){
  3. 'use strict';
  4. var ohauth = require('ohauth'),
  5. xtend = require('xtend'),
  6. store = require('store');
  7. // # osm-auth
  8. //
  9. // This code is only compatible with IE10+ because the [XDomainRequest](http://bit.ly/LfO7xo)
  10. // object, IE<10's idea of [CORS](http://en.wikipedia.org/wiki/Cross-origin_resource_sharing),
  11. // does not support custom headers, which this uses everywhere.
  12. module.exports = function(o) {
  13. var oauth = {};
  14. // authenticated users will also have a request token secret, but it's
  15. // not used in transactions with the server
  16. oauth.authenticated = function() {
  17. return !!(token('oauth_token') && token('oauth_token_secret'));
  18. };
  19. oauth.logout = function() {
  20. token('oauth_token', '');
  21. token('oauth_token_secret', '');
  22. token('oauth_request_token_secret', '');
  23. return oauth;
  24. };
  25. // TODO: detect lack of click event
  26. oauth.authenticate = function(callback) {
  27. if (oauth.authenticated()) return callback();
  28. oauth.logout();
  29. // ## Getting a request token
  30. var params = timenonce(getAuth(o)),
  31. url = o.urls.request;
  32. params.oauth_signature = ohauth.signature(
  33. o.oauth_secret, '',
  34. ohauth.baseString('POST', url, params));
  35. if (!o.singlepage) {
  36. // Create a 600x550 popup window in the center of the screen
  37. var w = 600, h = 550,
  38. settings = [
  39. ['width', w], ['height', h],
  40. ['left', screen.width / 2 - w / 2],
  41. ['top', screen.height / 2 - h / 2]].map(function(x) {
  42. return x.join('=');
  43. }).join(','),
  44. popup = window.open('about:blank', 'oauth_window', settings);
  45. }
  46. // Request a request token. When this is complete, the popup
  47. // window is redirected to OSM's authorization page.
  48. ohauth.xhr('POST', url, params, null, {}, reqTokenDone);
  49. o.loading();
  50. function reqTokenDone(err, xhr) {
  51. o.done();
  52. if (err) return callback(err);
  53. var resp = ohauth.stringQs(xhr.response);
  54. token('oauth_request_token_secret', resp.oauth_token_secret);
  55. var authorize_url = o.urls.authorize + '?' + ohauth.qsString({
  56. oauth_token: resp.oauth_token,
  57. oauth_callback: location.href.replace('index.html', '')
  58. .replace(/#.*/, '') + o.landing
  59. });
  60. if (o.singlepage) {
  61. location.href = authorize_url;
  62. } else {
  63. popup.location = authorize_url;
  64. }
  65. }
  66. // Called by a function in a landing page, in the popup window. The
  67. // window closes itself.
  68. window.authComplete = function(token) {
  69. var oauth_token = ohauth.stringQs(token.split('?')[1]);
  70. get_access_token(oauth_token.oauth_token, oauth_token.oauth_verifier);
  71. delete window.authComplete;
  72. };
  73. // ## Getting an request token
  74. //
  75. // At this point we have an `oauth_token`, brought in from a function
  76. // call on a landing page popup.
  77. function get_access_token(oauth_token, oauth_verifier) {
  78. var url = o.urls.access,
  79. params = timenonce(getAuth(o)),
  80. request_token_secret = token('oauth_request_token_secret');
  81. params.oauth_verifier = oauth_verifier;
  82. params.oauth_token = oauth_token;
  83. params.oauth_signature = ohauth.signature(
  84. o.oauth_secret,
  85. request_token_secret,
  86. ohauth.baseString('POST', url, params));
  87. // ## Getting an access token
  88. //
  89. // The final token required for authentication. At this point
  90. // we have a `request token secret`
  91. ohauth.xhr('POST', url, params, null, {}, accessTokenDone);
  92. o.loading();
  93. }
  94. function accessTokenDone(err, xhr) {
  95. o.done();
  96. if (err) return callback(err);
  97. var access_token = ohauth.stringQs(xhr.response);
  98. token('oauth_token', access_token.oauth_token);
  99. token('oauth_token_secret', access_token.oauth_token_secret);
  100. callback(null, oauth);
  101. }
  102. };
  103. oauth.bootstrapToken = function(oauth_token, callback) {
  104. // ## Getting an request token
  105. // At this point we have an `oauth_token`, brought in from a function
  106. // call on a landing page popup.
  107. function get_access_token(oauth_token) {
  108. var url = o.urls.access,
  109. params = timenonce(getAuth(o)),
  110. request_token_secret = token('oauth_request_token_secret');
  111. params.oauth_token = oauth_token;
  112. params.oauth_signature = ohauth.signature(
  113. o.oauth_secret,
  114. request_token_secret,
  115. ohauth.baseString('POST', url, params));
  116. // ## Getting an access token
  117. // The final token required for authentication. At this point
  118. // we have a `request token secret`
  119. ohauth.xhr('POST', url, params, null, {}, accessTokenDone);
  120. o.loading();
  121. }
  122. function accessTokenDone(err, xhr) {
  123. o.done();
  124. if (err) return callback(err);
  125. var access_token = ohauth.stringQs(xhr.response);
  126. token('oauth_token', access_token.oauth_token);
  127. token('oauth_token_secret', access_token.oauth_token_secret);
  128. callback(null, oauth);
  129. }
  130. get_access_token(oauth_token);
  131. };
  132. // # xhr
  133. //
  134. // A single XMLHttpRequest wrapper that does authenticated calls if the
  135. // user has logged in.
  136. oauth.xhr = function(options, callback) {
  137. if (!oauth.authenticated()) {
  138. if (o.auto) return oauth.authenticate(run);
  139. else return callback('not authenticated', null);
  140. } else return run();
  141. function run() {
  142. var params = timenonce(getAuth(o)),
  143. url = o.url + options.path,
  144. oauth_token_secret = token('oauth_token_secret');
  145. // https://tools.ietf.org/html/rfc5849#section-3.4.1.3.1
  146. if ((!options.options || !options.options.header ||
  147. options.options.header['Content-Type'] === 'application/x-www-form-urlencoded') &&
  148. options.content) {
  149. params = xtend(params, ohauth.stringQs(options.content));
  150. }
  151. params.oauth_token = token('oauth_token');
  152. params.oauth_signature = ohauth.signature(
  153. o.oauth_secret,
  154. oauth_token_secret,
  155. ohauth.baseString(options.method, url, params));
  156. ohauth.xhr(options.method,
  157. url, params, options.content, options.options, done);
  158. }
  159. function done(err, xhr) {
  160. if (err) return callback(err);
  161. else if (xhr.responseXML) return callback(err, xhr.responseXML);
  162. else return callback(err, xhr.response);
  163. }
  164. };
  165. oauth.ajaxBeforeSend = function (xhr, options) {
  166. if (!oauth.authenticated()) {
  167. return;
  168. }
  169. var params = timenonce(getAuth(o)),
  170. oauth_token_secret = token('oauth_token_secret'),
  171. method = options.method || 'GET',
  172. url = options.url;
  173. // Canonicalize the URL
  174. var parser = document.createElement('a');
  175. parser.href = url;
  176. var canonical_url = parser.origin + parser.pathname;
  177. if (parser.search) {
  178. params = xtend(params, ohauth.stringQs(parser.search.substring(1)));
  179. }
  180. // https://tools.ietf.org/html/rfc5849#section-3.4.1.3.1
  181. if ((!options || !options.headers ||
  182. options.headers['Content-Type'] === 'application/x-www-form-urlencoded') &&
  183. options.data) {
  184. params = xtend(params, ohauth.stringQs(options.data));
  185. }
  186. params.oauth_token = token('oauth_token');
  187. params.oauth_signature = ohauth.signature(
  188. o.oauth_secret,
  189. oauth_token_secret,
  190. ohauth.baseString(method, canonical_url, params));
  191. xhr.setRequestHeader( "Authorization", 'OAuth ' + ohauth.authHeader(params) );
  192. }
  193. // pre-authorize this object, if we can just get a token and token_secret
  194. // from the start
  195. oauth.preauth = function(c) {
  196. if (!c) return;
  197. if (c.oauth_token) token('oauth_token', c.oauth_token);
  198. if (c.oauth_token_secret) token('oauth_token_secret', c.oauth_token_secret);
  199. return oauth;
  200. };
  201. oauth.options = function(_) {
  202. if (!arguments.length) return o;
  203. o = _;
  204. o.url = o.url || false;
  205. o.landing = o.landing || 'land.html';
  206. o.singlepage = o.singlepage || false;
  207. // Optional loading and loading-done functions for nice UI feedback.
  208. // by default, no-ops
  209. o.loading = o.loading || function() {};
  210. o.done = o.done || function() {};
  211. return oauth.preauth(o);
  212. };
  213. // 'stamp' an authentication object from `getAuth()`
  214. // with a [nonce](http://en.wikipedia.org/wiki/Cryptographic_nonce)
  215. // and timestamp
  216. function timenonce(o) {
  217. o.oauth_timestamp = ohauth.timestamp();
  218. o.oauth_nonce = ohauth.nonce();
  219. return o;
  220. }
  221. // get/set tokens. These are prefixed with the base URL so that `osm-auth`
  222. // can be used with multiple APIs and the keys in `localStorage`
  223. // will not clash
  224. var token;
  225. if (store.enabled) {
  226. token = function (x, y) {
  227. if (arguments.length === 1) return store.get(o.url + x);
  228. else if (arguments.length === 2) return store.set(o.url + x, y);
  229. };
  230. } else {
  231. var storage = {};
  232. token = function (x, y) {
  233. if (arguments.length === 1) return storage[o.url + x];
  234. else if (arguments.length === 2) return storage[o.url + x] = y;
  235. };
  236. }
  237. // Get an authentication object. If you just add and remove properties
  238. // from a single object, you'll need to use `delete` to make sure that
  239. // it doesn't contain undesired properties for authentication
  240. function getAuth(o) {
  241. return {
  242. oauth_consumer_key: o.oauth_consumer_key,
  243. oauth_signature_method: "HMAC-SHA1"
  244. };
  245. }
  246. // potentially pre-authorize
  247. oauth.options(o);
  248. return oauth;
  249. };
  250. },{"ohauth":2,"store":3,"xtend":4}],3:[function(require,module,exports){
  251. (function(global){;(function(win){
  252. var store = {},
  253. doc = win.document,
  254. localStorageName = 'localStorage',
  255. storage
  256. store.disabled = false
  257. store.set = function(key, value) {}
  258. store.get = function(key) {}
  259. store.remove = function(key) {}
  260. store.clear = function() {}
  261. store.transact = function(key, defaultVal, transactionFn) {
  262. var val = store.get(key)
  263. if (transactionFn == null) {
  264. transactionFn = defaultVal
  265. defaultVal = null
  266. }
  267. if (typeof val == 'undefined') { val = defaultVal || {} }
  268. transactionFn(val)
  269. store.set(key, val)
  270. }
  271. store.getAll = function() {}
  272. store.forEach = function() {}
  273. store.serialize = function(value) {
  274. return JSON.stringify(value)
  275. }
  276. store.deserialize = function(value) {
  277. if (typeof value != 'string') { return undefined }
  278. try { return JSON.parse(value) }
  279. catch(e) { return value || undefined }
  280. }
  281. // Functions to encapsulate questionable FireFox 3.6.13 behavior
  282. // when about.config::dom.storage.enabled === false
  283. // See https://github.com/marcuswestin/store.js/issues#issue/13
  284. function isLocalStorageNameSupported() {
  285. try { return (localStorageName in win && win[localStorageName]) }
  286. catch(err) { return false }
  287. }
  288. if (isLocalStorageNameSupported()) {
  289. storage = win[localStorageName]
  290. store.set = function(key, val) {
  291. if (val === undefined) { return store.remove(key) }
  292. storage.setItem(key, store.serialize(val))
  293. return val
  294. }
  295. store.get = function(key) { return store.deserialize(storage.getItem(key)) }
  296. store.remove = function(key) { storage.removeItem(key) }
  297. store.clear = function() { storage.clear() }
  298. store.getAll = function() {
  299. var ret = {}
  300. store.forEach(function(key, val) {
  301. ret[key] = val
  302. })
  303. return ret
  304. }
  305. store.forEach = function(callback) {
  306. for (var i=0; i<storage.length; i++) {
  307. var key = storage.key(i)
  308. callback(key, store.get(key))
  309. }
  310. }
  311. } else if (doc.documentElement.addBehavior) {
  312. var storageOwner,
  313. storageContainer
  314. // Since #userData storage applies only to specific paths, we need to
  315. // somehow link our data to a specific path. We choose /favicon.ico
  316. // as a pretty safe option, since all browsers already make a request to
  317. // this URL anyway and being a 404 will not hurt us here. We wrap an
  318. // iframe pointing to the favicon in an ActiveXObject(htmlfile) object
  319. // (see: http://msdn.microsoft.com/en-us/library/aa752574(v=VS.85).aspx)
  320. // since the iframe access rules appear to allow direct access and
  321. // manipulation of the document element, even for a 404 page. This
  322. // document can be used instead of the current document (which would
  323. // have been limited to the current path) to perform #userData storage.
  324. try {
  325. storageContainer = new ActiveXObject('htmlfile')
  326. storageContainer.open()
  327. storageContainer.write('<s' + 'cript>document.w=window</s' + 'cript><iframe src="/favicon.ico"></iframe>')
  328. storageContainer.close()
  329. storageOwner = storageContainer.w.frames[0].document
  330. storage = storageOwner.createElement('div')
  331. } catch(e) {
  332. // somehow ActiveXObject instantiation failed (perhaps some special
  333. // security settings or otherwse), fall back to per-path storage
  334. storage = doc.createElement('div')
  335. storageOwner = doc.body
  336. }
  337. function withIEStorage(storeFunction) {
  338. return function() {
  339. var args = Array.prototype.slice.call(arguments, 0)
  340. args.unshift(storage)
  341. // See http://msdn.microsoft.com/en-us/library/ms531081(v=VS.85).aspx
  342. // and http://msdn.microsoft.com/en-us/library/ms531424(v=VS.85).aspx
  343. storageOwner.appendChild(storage)
  344. storage.addBehavior('#default#userData')
  345. storage.load(localStorageName)
  346. var result = storeFunction.apply(store, args)
  347. storageOwner.removeChild(storage)
  348. return result
  349. }
  350. }
  351. // In IE7, keys may not contain special chars. See all of https://github.com/marcuswestin/store.js/issues/40
  352. var forbiddenCharsRegex = new RegExp("[!\"#$%&'()*+,/\\\\:;<=>?@[\\]^`{|}~]", "g")
  353. function ieKeyFix(key) {
  354. return key.replace(forbiddenCharsRegex, '___')
  355. }
  356. store.set = withIEStorage(function(storage, key, val) {
  357. key = ieKeyFix(key)
  358. if (val === undefined) { return store.remove(key) }
  359. storage.setAttribute(key, store.serialize(val))
  360. storage.save(localStorageName)
  361. return val
  362. })
  363. store.get = withIEStorage(function(storage, key) {
  364. key = ieKeyFix(key)
  365. return store.deserialize(storage.getAttribute(key))
  366. })
  367. store.remove = withIEStorage(function(storage, key) {
  368. key = ieKeyFix(key)
  369. storage.removeAttribute(key)
  370. storage.save(localStorageName)
  371. })
  372. store.clear = withIEStorage(function(storage) {
  373. var attributes = storage.XMLDocument.documentElement.attributes
  374. storage.load(localStorageName)
  375. for (var i=0, attr; attr=attributes[i]; i++) {
  376. storage.removeAttribute(attr.name)
  377. }
  378. storage.save(localStorageName)
  379. })
  380. store.getAll = function(storage) {
  381. var ret = {}
  382. store.forEach(function(key, val) {
  383. ret[key] = val
  384. })
  385. return ret
  386. }
  387. store.forEach = withIEStorage(function(storage, callback) {
  388. var attributes = storage.XMLDocument.documentElement.attributes
  389. for (var i=0, attr; attr=attributes[i]; ++i) {
  390. callback(attr.name, store.deserialize(storage.getAttribute(attr.name)))
  391. }
  392. })
  393. }
  394. try {
  395. var testKey = '__storejs__'
  396. store.set(testKey, testKey)
  397. if (store.get(testKey) != testKey) { store.disabled = true }
  398. store.remove(testKey)
  399. } catch(e) {
  400. store.disabled = true
  401. }
  402. store.enabled = !store.disabled
  403. if (typeof module != 'undefined' && module.exports) { module.exports = store }
  404. else if (typeof define === 'function' && define.amd) { define(store) }
  405. else { win.store = store }
  406. })(this.window || global);
  407. })(window)
  408. },{}],5:[function(require,module,exports){
  409. module.exports = hasKeys
  410. function hasKeys(source) {
  411. return source !== null &&
  412. (typeof source === "object" ||
  413. typeof source === "function")
  414. }
  415. },{}],4:[function(require,module,exports){
  416. var Keys = require("object-keys")
  417. var hasKeys = require("./has-keys")
  418. module.exports = extend
  419. function extend() {
  420. var target = {}
  421. for (var i = 0; i < arguments.length; i++) {
  422. var source = arguments[i]
  423. if (!hasKeys(source)) {
  424. continue
  425. }
  426. var keys = Keys(source)
  427. for (var j = 0; j < keys.length; j++) {
  428. var name = keys[j]
  429. target[name] = source[name]
  430. }
  431. }
  432. return target
  433. }
  434. },{"./has-keys":5,"object-keys":6}],7:[function(require,module,exports){
  435. (function(global){/**
  436. * jsHashes - A fast and independent hashing library pure JavaScript implemented (ES3 compliant) for both server and client side
  437. *
  438. * @class Hashes
  439. * @author Tomas Aparicio <tomas@rijndael-project.com>
  440. * @license New BSD (see LICENSE file)
  441. * @version 1.0.4
  442. *
  443. * Algorithms specification:
  444. *
  445. * MD5 <http://www.ietf.org/rfc/rfc1321.txt>
  446. * RIPEMD-160 <http://homes.esat.kuleuven.be/~bosselae/ripemd160.html>
  447. * SHA1 <http://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf>
  448. * SHA256 <http://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf>
  449. * SHA512 <http://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf>
  450. * HMAC <http://www.ietf.org/rfc/rfc2104.txt>
  451. *
  452. */
  453. (function(){
  454. var Hashes;
  455. // private helper methods
  456. function utf8Encode(str) {
  457. var x, y, output = '', i = -1, l;
  458. if (str && str.length) {
  459. l = str.length;
  460. while ((i+=1) < l) {
  461. /* Decode utf-16 surrogate pairs */
  462. x = str.charCodeAt(i);
  463. y = i + 1 < l ? str.charCodeAt(i + 1) : 0;
  464. if (0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF) {
  465. x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);
  466. i += 1;
  467. }
  468. /* Encode output as utf-8 */
  469. if (x <= 0x7F) {
  470. output += String.fromCharCode(x);
  471. } else if (x <= 0x7FF) {
  472. output += String.fromCharCode(0xC0 | ((x >>> 6 ) & 0x1F),
  473. 0x80 | ( x & 0x3F));
  474. } else if (x <= 0xFFFF) {
  475. output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F),
  476. 0x80 | ((x >>> 6 ) & 0x3F),
  477. 0x80 | ( x & 0x3F));
  478. } else if (x <= 0x1FFFFF) {
  479. output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07),
  480. 0x80 | ((x >>> 12) & 0x3F),
  481. 0x80 | ((x >>> 6 ) & 0x3F),
  482. 0x80 | ( x & 0x3F));
  483. }
  484. }
  485. }
  486. return output;
  487. }
  488. function utf8Decode(str) {
  489. var i, ac, c1, c2, c3, arr = [], l;
  490. i = ac = c1 = c2 = c3 = 0;
  491. if (str && str.length) {
  492. l = str.length;
  493. str += '';
  494. while (i < l) {
  495. c1 = str.charCodeAt(i);
  496. ac += 1;
  497. if (c1 < 128) {
  498. arr[ac] = String.fromCharCode(c1);
  499. i+=1;
  500. } else if (c1 > 191 && c1 < 224) {
  501. c2 = str.charCodeAt(i + 1);
  502. arr[ac] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
  503. i += 2;
  504. } else {
  505. c2 = str.charCodeAt(i + 1);
  506. c3 = str.charCodeAt(i + 2);
  507. arr[ac] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  508. i += 3;
  509. }
  510. }
  511. }
  512. return arr.join('');
  513. }
  514. /**
  515. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  516. * to work around bugs in some JS interpreters.
  517. */
  518. function safe_add(x, y) {
  519. var lsw = (x & 0xFFFF) + (y & 0xFFFF),
  520. msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  521. return (msw << 16) | (lsw & 0xFFFF);
  522. }
  523. /**
  524. * Bitwise rotate a 32-bit number to the left.
  525. */
  526. function bit_rol(num, cnt) {
  527. return (num << cnt) | (num >>> (32 - cnt));
  528. }
  529. /**
  530. * Convert a raw string to a hex string
  531. */
  532. function rstr2hex(input, hexcase) {
  533. var hex_tab = hexcase ? '0123456789ABCDEF' : '0123456789abcdef',
  534. output = '', x, i = 0, l = input.length;
  535. for (; i < l; i+=1) {
  536. x = input.charCodeAt(i);
  537. output += hex_tab.charAt((x >>> 4) & 0x0F) + hex_tab.charAt(x & 0x0F);
  538. }
  539. return output;
  540. }
  541. /**
  542. * Encode a string as utf-16
  543. */
  544. function str2rstr_utf16le(input) {
  545. var i, l = input.length, output = '';
  546. for (i = 0; i < l; i+=1) {
  547. output += String.fromCharCode( input.charCodeAt(i) & 0xFF, (input.charCodeAt(i) >>> 8) & 0xFF);
  548. }
  549. return output;
  550. }
  551. function str2rstr_utf16be(input) {
  552. var i, l = input.length, output = '';
  553. for (i = 0; i < l; i+=1) {
  554. output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF, input.charCodeAt(i) & 0xFF);
  555. }
  556. return output;
  557. }
  558. /**
  559. * Convert an array of big-endian words to a string
  560. */
  561. function binb2rstr(input) {
  562. var i, l = input.length * 32, output = '';
  563. for (i = 0; i < l; i += 8) {
  564. output += String.fromCharCode((input[i>>5] >>> (24 - i % 32)) & 0xFF);
  565. }
  566. return output;
  567. }
  568. /**
  569. * Convert an array of little-endian words to a string
  570. */
  571. function binl2rstr(input) {
  572. var i, l = input.length * 32, output = '';
  573. for (i = 0;i < l; i += 8) {
  574. output += String.fromCharCode((input[i>>5] >>> (i % 32)) & 0xFF);
  575. }
  576. return output;
  577. }
  578. /**
  579. * Convert a raw string to an array of little-endian words
  580. * Characters >255 have their high-byte silently ignored.
  581. */
  582. function rstr2binl(input) {
  583. var i, l = input.length * 8, output = Array(input.length >> 2), lo = output.length;
  584. for (i = 0; i < lo; i+=1) {
  585. output[i] = 0;
  586. }
  587. for (i = 0; i < l; i += 8) {
  588. output[i>>5] |= (input.charCodeAt(i / 8) & 0xFF) << (i%32);
  589. }
  590. return output;
  591. }
  592. /**
  593. * Convert a raw string to an array of big-endian words
  594. * Characters >255 have their high-byte silently ignored.
  595. */
  596. function rstr2binb(input) {
  597. var i, l = input.length * 8, output = Array(input.length >> 2), lo = output.length;
  598. for (i = 0; i < lo; i+=1) {
  599. output[i] = 0;
  600. }
  601. for (i = 0; i < l; i += 8) {
  602. output[i>>5] |= (input.charCodeAt(i / 8) & 0xFF) << (24 - i % 32);
  603. }
  604. return output;
  605. }
  606. /**
  607. * Convert a raw string to an arbitrary string encoding
  608. */
  609. function rstr2any(input, encoding) {
  610. var divisor = encoding.length,
  611. remainders = Array(),
  612. i, q, x, ld, quotient, dividend, output, full_length;
  613. /* Convert to an array of 16-bit big-endian values, forming the dividend */
  614. dividend = Array(Math.ceil(input.length / 2));
  615. ld = dividend.length;
  616. for (i = 0; i < ld; i+=1) {
  617. dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
  618. }
  619. /**
  620. * Repeatedly perform a long division. The binary array forms the dividend,
  621. * the length of the encoding is the divisor. Once computed, the quotient
  622. * forms the dividend for the next step. We stop when the dividend is zerHashes.
  623. * All remainders are stored for later use.
  624. */
  625. while(dividend.length > 0) {
  626. quotient = Array();
  627. x = 0;
  628. for (i = 0; i < dividend.length; i+=1) {
  629. x = (x << 16) + dividend[i];
  630. q = Math.floor(x / divisor);
  631. x -= q * divisor;
  632. if (quotient.length > 0 || q > 0) {
  633. quotient[quotient.length] = q;
  634. }
  635. }
  636. remainders[remainders.length] = x;
  637. dividend = quotient;
  638. }
  639. /* Convert the remainders to the output string */
  640. output = '';
  641. for (i = remainders.length - 1; i >= 0; i--) {
  642. output += encoding.charAt(remainders[i]);
  643. }
  644. /* Append leading zero equivalents */
  645. full_length = Math.ceil(input.length * 8 / (Math.log(encoding.length) / Math.log(2)));
  646. for (i = output.length; i < full_length; i+=1) {
  647. output = encoding[0] + output;
  648. }
  649. return output;
  650. }
  651. /**
  652. * Convert a raw string to a base-64 string
  653. */
  654. function rstr2b64(input, b64pad) {
  655. var tab = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
  656. output = '',
  657. len = input.length, i, j, triplet;
  658. b64pad= b64pad || '=';
  659. for (i = 0; i < len; i += 3) {
  660. triplet = (input.charCodeAt(i) << 16)
  661. | (i + 1 < len ? input.charCodeAt(i+1) << 8 : 0)
  662. | (i + 2 < len ? input.charCodeAt(i+2) : 0);
  663. for (j = 0; j < 4; j+=1) {
  664. if (i * 8 + j * 6 > input.length * 8) {
  665. output += b64pad;
  666. } else {
  667. output += tab.charAt((triplet >>> 6*(3-j)) & 0x3F);
  668. }
  669. }
  670. }
  671. return output;
  672. }
  673. Hashes = {
  674. /**
  675. * @property {String} version
  676. * @readonly
  677. */
  678. VERSION : '1.0.3',
  679. /**
  680. * @member Hashes
  681. * @class Base64
  682. * @constructor
  683. */
  684. Base64 : function () {
  685. // private properties
  686. var tab = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
  687. pad = '=', // default pad according with the RFC standard
  688. url = false, // URL encoding support @todo
  689. utf8 = true; // by default enable UTF-8 support encoding
  690. // public method for encoding
  691. this.encode = function (input) {
  692. var i, j, triplet,
  693. output = '',
  694. len = input.length;
  695. pad = pad || '=';
  696. input = (utf8) ? utf8Encode(input) : input;
  697. for (i = 0; i < len; i += 3) {
  698. triplet = (input.charCodeAt(i) << 16)
  699. | (i + 1 < len ? input.charCodeAt(i+1) << 8 : 0)
  700. | (i + 2 < len ? input.charCodeAt(i+2) : 0);
  701. for (j = 0; j < 4; j+=1) {
  702. if (i * 8 + j * 6 > len * 8) {
  703. output += pad;
  704. } else {
  705. output += tab.charAt((triplet >>> 6*(3-j)) & 0x3F);
  706. }
  707. }
  708. }
  709. return output;
  710. };
  711. // public method for decoding
  712. this.decode = function (input) {
  713. // var b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  714. var i, o1, o2, o3, h1, h2, h3, h4, bits, ac,
  715. dec = '',
  716. arr = [];
  717. if (!input) { return input; }
  718. i = ac = 0;
  719. input = input.replace(new RegExp('\\'+pad,'gi'),''); // use '='
  720. //input += '';
  721. do { // unpack four hexets into three octets using index points in b64
  722. h1 = tab.indexOf(input.charAt(i+=1));
  723. h2 = tab.indexOf(input.charAt(i+=1));
  724. h3 = tab.indexOf(input.charAt(i+=1));
  725. h4 = tab.indexOf(input.charAt(i+=1));
  726. bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
  727. o1 = bits >> 16 & 0xff;
  728. o2 = bits >> 8 & 0xff;
  729. o3 = bits & 0xff;
  730. ac += 1;
  731. if (h3 === 64) {
  732. arr[ac] = String.fromCharCode(o1);
  733. } else if (h4 === 64) {
  734. arr[ac] = String.fromCharCode(o1, o2);
  735. } else {
  736. arr[ac] = String.fromCharCode(o1, o2, o3);
  737. }
  738. } while (i < input.length);
  739. dec = arr.join('');
  740. dec = (utf8) ? utf8Decode(dec) : dec;
  741. return dec;
  742. };
  743. // set custom pad string
  744. this.setPad = function (str) {
  745. pad = str || pad;
  746. return this;
  747. };
  748. // set custom tab string characters
  749. this.setTab = function (str) {
  750. tab = str || tab;
  751. return this;
  752. };
  753. this.setUTF8 = function (bool) {
  754. if (typeof bool === 'boolean') {
  755. utf8 = bool;
  756. }
  757. return this;
  758. };
  759. },
  760. /**
  761. * CRC-32 calculation
  762. * @member Hashes
  763. * @method CRC32
  764. * @static
  765. * @param {String} str Input String
  766. * @return {String}
  767. */
  768. CRC32 : function (str) {
  769. var crc = 0, x = 0, y = 0, table, i, iTop;
  770. str = utf8Encode(str);
  771. table = [
  772. '00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 ',
  773. '79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 ',
  774. '84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F ',
  775. '63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD ',
  776. 'A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC ',
  777. '51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 ',
  778. 'B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 ',
  779. '06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 ',
  780. 'E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 ',
  781. '12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 ',
  782. 'D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 ',
  783. '33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 ',
  784. 'CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 ',
  785. '9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E ',
  786. '7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D ',
  787. '806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 ',
  788. '60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA ',
  789. 'AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 ',
  790. '5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 ',
  791. 'B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 ',
  792. '05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 ',
  793. 'F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA ',
  794. '11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 ',
  795. 'D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F ',
  796. '30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E ',
  797. 'C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D'
  798. ].join('');
  799. crc = crc ^ (-1);
  800. for (i = 0, iTop = str.length; i < iTop; i+=1 ) {
  801. y = ( crc ^ str.charCodeAt( i ) ) & 0xFF;
  802. x = '0x' + table.substr( y * 9, 8 );
  803. crc = ( crc >>> 8 ) ^ x;
  804. }
  805. // always return a positive number (that's what >>> 0 does)
  806. return (crc ^ (-1)) >>> 0;
  807. },
  808. /**
  809. * @member Hashes
  810. * @class MD5
  811. * @constructor
  812. * @param {Object} [config]
  813. *
  814. * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
  815. * Digest Algorithm, as defined in RFC 1321.
  816. * Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
  817. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  818. * See <http://pajhome.org.uk/crypt/md5> for more infHashes.
  819. */
  820. MD5 : function (options) {
  821. /**
  822. * Private config properties. You may need to tweak these to be compatible with
  823. * the server-side, but the defaults work in most cases.
  824. * See {@link Hashes.MD5#method-setUpperCase} and {@link Hashes.SHA1#method-setUpperCase}
  825. */
  826. var hexcase = (options && typeof options.uppercase === 'boolean') ? options.uppercase : false, // hexadecimal output case format. false - lowercase; true - uppercase
  827. b64pad = (options && typeof options.pad === 'string') ? options.pda : '=', // base-64 pad character. Defaults to '=' for strict RFC compliance
  828. utf8 = (options && typeof options.utf8 === 'boolean') ? options.utf8 : true; // enable/disable utf8 encoding
  829. // privileged (public) methods
  830. this.hex = function (s) {
  831. return rstr2hex(rstr(s, utf8), hexcase);
  832. };
  833. this.b64 = function (s) {
  834. return rstr2b64(rstr(s), b64pad);
  835. };
  836. this.any = function(s, e) {
  837. return rstr2any(rstr(s, utf8), e);
  838. };
  839. this.hex_hmac = function (k, d) {
  840. return rstr2hex(rstr_hmac(k, d), hexcase);
  841. };
  842. this.b64_hmac = function (k, d) {
  843. return rstr2b64(rstr_hmac(k,d), b64pad);
  844. };
  845. this.any_hmac = function (k, d, e) {
  846. return rstr2any(rstr_hmac(k, d), e);
  847. };
  848. /**
  849. * Perform a simple self-test to see if the VM is working
  850. * @return {String} Hexadecimal hash sample
  851. */
  852. this.vm_test = function () {
  853. return hex('abc').toLowerCase() === '900150983cd24fb0d6963f7d28e17f72';
  854. };
  855. /**
  856. * Enable/disable uppercase hexadecimal returned string
  857. * @param {Boolean}
  858. * @return {Object} this
  859. */
  860. this.setUpperCase = function (a) {
  861. if (typeof a === 'boolean' ) {
  862. hexcase = a;
  863. }
  864. return this;
  865. };
  866. /**
  867. * Defines a base64 pad string
  868. * @param {String} Pad
  869. * @return {Object} this
  870. */
  871. this.setPad = function (a) {
  872. b64pad = a || b64pad;
  873. return this;
  874. };
  875. /**
  876. * Defines a base64 pad string
  877. * @param {Boolean}
  878. * @return {Object} [this]
  879. */
  880. this.setUTF8 = function (a) {
  881. if (typeof a === 'boolean') {
  882. utf8 = a;
  883. }
  884. return this;
  885. };
  886. // private methods
  887. /**
  888. * Calculate the MD5 of a raw string
  889. */
  890. function rstr(s) {
  891. s = (utf8) ? utf8Encode(s): s;
  892. return binl2rstr(binl(rstr2binl(s), s.length * 8));
  893. }
  894. /**
  895. * Calculate the HMAC-MD5, of a key and some data (raw strings)
  896. */
  897. function rstr_hmac(key, data) {
  898. var bkey, ipad, opad, hash, i;
  899. key = (utf8) ? utf8Encode(key) : key;
  900. data = (utf8) ? utf8Encode(data) : data;
  901. bkey = rstr2binl(key);
  902. if (bkey.length > 16) {
  903. bkey = binl(bkey, key.length * 8);
  904. }
  905. ipad = Array(16), opad = Array(16);
  906. for (i = 0; i < 16; i+=1) {
  907. ipad[i] = bkey[i] ^ 0x36363636;
  908. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  909. }
  910. hash = binl(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
  911. return binl2rstr(binl(opad.concat(hash), 512 + 128));
  912. }
  913. /**
  914. * Calculate the MD5 of an array of little-endian words, and a bit length.
  915. */
  916. function binl(x, len) {
  917. var i, olda, oldb, oldc, oldd,
  918. a = 1732584193,
  919. b = -271733879,
  920. c = -1732584194,
  921. d = 271733878;
  922. /* append padding */
  923. x[len >> 5] |= 0x80 << ((len) % 32);
  924. x[(((len + 64) >>> 9) << 4) + 14] = len;
  925. for (i = 0; i < x.length; i += 16) {
  926. olda = a;
  927. oldb = b;
  928. oldc = c;
  929. oldd = d;
  930. a = md5_ff(a, b, c, d, x[i+ 0], 7 , -680876936);
  931. d = md5_ff(d, a, b, c, x[i+ 1], 12, -389564586);
  932. c = md5_ff(c, d, a, b, x[i+ 2], 17, 606105819);
  933. b = md5_ff(b, c, d, a, x[i+ 3], 22, -1044525330);
  934. a = md5_ff(a, b, c, d, x[i+ 4], 7 , -176418897);
  935. d = md5_ff(d, a, b, c, x[i+ 5], 12, 1200080426);
  936. c = md5_ff(c, d, a, b, x[i+ 6], 17, -1473231341);
  937. b = md5_ff(b, c, d, a, x[i+ 7], 22, -45705983);
  938. a = md5_ff(a, b, c, d, x[i+ 8], 7 , 1770035416);
  939. d = md5_ff(d, a, b, c, x[i+ 9], 12, -1958414417);
  940. c = md5_ff(c, d, a, b, x[i+10], 17, -42063);
  941. b = md5_ff(b, c, d, a, x[i+11], 22, -1990404162);
  942. a = md5_ff(a, b, c, d, x[i+12], 7 , 1804603682);
  943. d = md5_ff(d, a, b, c, x[i+13], 12, -40341101);
  944. c = md5_ff(c, d, a, b, x[i+14], 17, -1502002290);
  945. b = md5_ff(b, c, d, a, x[i+15], 22, 1236535329);
  946. a = md5_gg(a, b, c, d, x[i+ 1], 5 , -165796510);
  947. d = md5_gg(d, a, b, c, x[i+ 6], 9 , -1069501632);
  948. c = md5_gg(c, d, a, b, x[i+11], 14, 643717713);
  949. b = md5_gg(b, c, d, a, x[i+ 0], 20, -373897302);
  950. a = md5_gg(a, b, c, d, x[i+ 5], 5 , -701558691);
  951. d = md5_gg(d, a, b, c, x[i+10], 9 , 38016083);
  952. c = md5_gg(c, d, a, b, x[i+15], 14, -660478335);
  953. b = md5_gg(b, c, d, a, x[i+ 4], 20, -405537848);
  954. a = md5_gg(a, b, c, d, x[i+ 9], 5 , 568446438);
  955. d = md5_gg(d, a, b, c, x[i+14], 9 , -1019803690);
  956. c = md5_gg(c, d, a, b, x[i+ 3], 14, -187363961);
  957. b = md5_gg(b, c, d, a, x[i+ 8], 20, 1163531501);
  958. a = md5_gg(a, b, c, d, x[i+13], 5 , -1444681467);
  959. d = md5_gg(d, a, b, c, x[i+ 2], 9 , -51403784);
  960. c = md5_gg(c, d, a, b, x[i+ 7], 14, 1735328473);
  961. b = md5_gg(b, c, d, a, x[i+12], 20, -1926607734);
  962. a = md5_hh(a, b, c, d, x[i+ 5], 4 , -378558);
  963. d = md5_hh(d, a, b, c, x[i+ 8], 11, -2022574463);
  964. c = md5_hh(c, d, a, b, x[i+11], 16, 1839030562);
  965. b = md5_hh(b, c, d, a, x[i+14], 23, -35309556);
  966. a = md5_hh(a, b, c, d, x[i+ 1], 4 , -1530992060);
  967. d = md5_hh(d, a, b, c, x[i+ 4], 11, 1272893353);
  968. c = md5_hh(c, d, a, b, x[i+ 7], 16, -155497632);
  969. b = md5_hh(b, c, d, a, x[i+10], 23, -1094730640);
  970. a = md5_hh(a, b, c, d, x[i+13], 4 , 681279174);
  971. d = md5_hh(d, a, b, c, x[i+ 0], 11, -358537222);
  972. c = md5_hh(c, d, a, b, x[i+ 3], 16, -722521979);
  973. b = md5_hh(b, c, d, a, x[i+ 6], 23, 76029189);
  974. a = md5_hh(a, b, c, d, x[i+ 9], 4 , -640364487);
  975. d = md5_hh(d, a, b, c, x[i+12], 11, -421815835);
  976. c = md5_hh(c, d, a, b, x[i+15], 16, 530742520);
  977. b = md5_hh(b, c, d, a, x[i+ 2], 23, -995338651);
  978. a = md5_ii(a, b, c, d, x[i+ 0], 6 , -198630844);
  979. d = md5_ii(d, a, b, c, x[i+ 7], 10, 1126891415);
  980. c = md5_ii(c, d, a, b, x[i+14], 15, -1416354905);
  981. b = md5_ii(b, c, d, a, x[i+ 5], 21, -57434055);
  982. a = md5_ii(a, b, c, d, x[i+12], 6 , 1700485571);
  983. d = md5_ii(d, a, b, c, x[i+ 3], 10, -1894986606);
  984. c = md5_ii(c, d, a, b, x[i+10], 15, -1051523);
  985. b = md5_ii(b, c, d, a, x[i+ 1], 21, -2054922799);
  986. a = md5_ii(a, b, c, d, x[i+ 8], 6 , 1873313359);
  987. d = md5_ii(d, a, b, c, x[i+15], 10, -30611744);
  988. c = md5_ii(c, d, a, b, x[i+ 6], 15, -1560198380);
  989. b = md5_ii(b, c, d, a, x[i+13], 21, 1309151649);
  990. a = md5_ii(a, b, c, d, x[i+ 4], 6 , -145523070);
  991. d = md5_ii(d, a, b, c, x[i+11], 10, -1120210379);
  992. c = md5_ii(c, d, a, b, x[i+ 2], 15, 718787259);
  993. b = md5_ii(b, c, d, a, x[i+ 9], 21, -343485551);
  994. a = safe_add(a, olda);
  995. b = safe_add(b, oldb);
  996. c = safe_add(c, oldc);
  997. d = safe_add(d, oldd);
  998. }
  999. return Array(a, b, c, d);
  1000. }
  1001. /**
  1002. * These functions implement the four basic operations the algorithm uses.
  1003. */
  1004. function md5_cmn(q, a, b, x, s, t) {
  1005. return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s),b);
  1006. }
  1007. function md5_ff(a, b, c, d, x, s, t) {
  1008. return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
  1009. }
  1010. function md5_gg(a, b, c, d, x, s, t) {
  1011. return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
  1012. }
  1013. function md5_hh(a, b, c, d, x, s, t) {
  1014. return md5_cmn(b ^ c ^ d, a, b, x, s, t);
  1015. }
  1016. function md5_ii(a, b, c, d, x, s, t) {
  1017. return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
  1018. }
  1019. },
  1020. /**
  1021. * @member Hashes
  1022. * @class Hashes.SHA1
  1023. * @param {Object} [config]
  1024. * @constructor
  1025. *
  1026. * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined in FIPS 180-1
  1027. * Version 2.2 Copyright Paul Johnston 2000 - 2009.
  1028. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  1029. * See http://pajhome.org.uk/crypt/md5 for details.
  1030. */
  1031. SHA1 : function (options) {
  1032. /**
  1033. * Private config properties. You may need to tweak these to be compatible with
  1034. * the server-side, but the defaults work in most cases.
  1035. * See {@link Hashes.MD5#method-setUpperCase} and {@link Hashes.SHA1#method-setUpperCase}
  1036. */
  1037. var hexcase = (options && typeof options.uppercase === 'boolean') ? options.uppercase : false, // hexadecimal output case format. false - lowercase; true - uppercase
  1038. b64pad = (options && typeof options.pad === 'string') ? options.pda : '=', // base-64 pad character. Defaults to '=' for strict RFC compliance
  1039. utf8 = (options && typeof options.utf8 === 'boolean') ? options.utf8 : true; // enable/disable utf8 encoding
  1040. // public methods
  1041. this.hex = function (s) {
  1042. return rstr2hex(rstr(s, utf8), hexcase);
  1043. };
  1044. this.b64 = function (s) {
  1045. return rstr2b64(rstr(s, utf8), b64pad);
  1046. };
  1047. this.any = function (s, e) {
  1048. return rstr2any(rstr(s, utf8), e);
  1049. };
  1050. this.hex_hmac = function (k, d) {
  1051. return rstr2hex(rstr_hmac(k, d));
  1052. };
  1053. this.b64_hmac = function (k, d) {
  1054. return rstr2b64(rstr_hmac(k, d), b64pad);
  1055. };
  1056. this.any_hmac = function (k, d, e) {
  1057. return rstr2any(rstr_hmac(k, d), e);
  1058. };
  1059. /**
  1060. * Perform a simple self-test to see if the VM is working
  1061. * @return {String} Hexadecimal hash sample
  1062. * @public
  1063. */
  1064. this.vm_test = function () {
  1065. return hex('abc').toLowerCase() === '900150983cd24fb0d6963f7d28e17f72';
  1066. };
  1067. /**
  1068. * @description Enable/disable uppercase hexadecimal returned string
  1069. * @param {boolean}
  1070. * @return {Object} this
  1071. * @public
  1072. */
  1073. this.setUpperCase = function (a) {
  1074. if (typeof a === 'boolean') {
  1075. hexcase = a;
  1076. }
  1077. return this;
  1078. };
  1079. /**
  1080. * @description Defines a base64 pad string
  1081. * @param {string} Pad
  1082. * @return {Object} this
  1083. * @public
  1084. */
  1085. this.setPad = function (a) {
  1086. b64pad = a || b64pad;
  1087. return this;
  1088. };
  1089. /**
  1090. * @description Defines a base64 pad string
  1091. * @param {boolean}
  1092. * @return {Object} this
  1093. * @public
  1094. */
  1095. this.setUTF8 = function (a) {
  1096. if (typeof a === 'boolean') {
  1097. utf8 = a;
  1098. }
  1099. return this;
  1100. };
  1101. // private methods
  1102. /**
  1103. * Calculate the SHA-512 of a raw string
  1104. */
  1105. function rstr(s) {
  1106. s = (utf8) ? utf8Encode(s) : s;
  1107. return binb2rstr(binb(rstr2binb(s), s.length * 8));
  1108. }
  1109. /**
  1110. * Calculate the HMAC-SHA1 of a key and some data (raw strings)
  1111. */
  1112. function rstr_hmac(key, data) {
  1113. var bkey, ipad, opad, i, hash;
  1114. key = (utf8) ? utf8Encode(key) : key;
  1115. data = (utf8) ? utf8Encode(data) : data;
  1116. bkey = rstr2binb(key);
  1117. if (bkey.length > 16) {
  1118. bkey = binb(bkey, key.length * 8);
  1119. }
  1120. ipad = Array(16), opad = Array(16);
  1121. for (i = 0; i < 16; i+=1) {
  1122. ipad[i] = bkey[i] ^ 0x36363636;
  1123. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  1124. }
  1125. hash = binb(ipad.concat(rstr2binb(data)), 512 + data.length * 8);
  1126. return binb2rstr(binb(opad.concat(hash), 512 + 160));
  1127. }
  1128. /**
  1129. * Calculate the SHA-1 of an array of big-endian words, and a bit length
  1130. */
  1131. function binb(x, len) {
  1132. var i, j, t, olda, oldb, oldc, oldd, olde,
  1133. w = Array(80),
  1134. a = 1732584193,
  1135. b = -271733879,
  1136. c = -1732584194,
  1137. d = 271733878,
  1138. e = -1009589776;
  1139. /* append padding */
  1140. x[len >> 5] |= 0x80 << (24 - len % 32);
  1141. x[((len + 64 >> 9) << 4) + 15] = len;
  1142. for (i = 0; i < x.length; i += 16) {
  1143. olda = a,
  1144. oldb = b;
  1145. oldc = c;
  1146. oldd = d;
  1147. olde = e;
  1148. for (j = 0; j < 80; j+=1) {
  1149. if (j < 16) {
  1150. w[j] = x[i + j];
  1151. } else {
  1152. w[j] = bit_rol(w[j-3] ^ w[j-8] ^ w[j-14] ^ w[j-16], 1);
  1153. }
  1154. t = safe_add(safe_add(bit_rol(a, 5), sha1_ft(j, b, c, d)),
  1155. safe_add(safe_add(e, w[j]), sha1_kt(j)));
  1156. e = d;
  1157. d = c;
  1158. c = bit_rol(b, 30);
  1159. b = a;
  1160. a = t;
  1161. }
  1162. a = safe_add(a, olda);
  1163. b = safe_add(b, oldb);
  1164. c = safe_add(c, oldc);
  1165. d = safe_add(d, oldd);
  1166. e = safe_add(e, olde);
  1167. }
  1168. return Array(a, b, c, d, e);
  1169. }
  1170. /**
  1171. * Perform the appropriate triplet combination function for the current
  1172. * iteration
  1173. */
  1174. function sha1_ft(t, b, c, d) {
  1175. if (t < 20) { return (b & c) | ((~b) & d); }
  1176. if (t < 40) { return b ^ c ^ d; }
  1177. if (t < 60) { return (b & c) | (b & d) | (c & d); }
  1178. return b ^ c ^ d;
  1179. }
  1180. /**
  1181. * Determine the appropriate additive constant for the current iteration
  1182. */
  1183. function sha1_kt(t) {
  1184. return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
  1185. (t < 60) ? -1894007588 : -899497514;
  1186. }
  1187. },
  1188. /**
  1189. * @class Hashes.SHA256
  1190. * @param {config}
  1191. *
  1192. * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined in FIPS 180-2
  1193. * Version 2.2 Copyright Angel Marin, Paul Johnston 2000 - 2009.
  1194. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  1195. * See http://pajhome.org.uk/crypt/md5 for details.
  1196. * Also http://anmar.eu.org/projects/jssha2/
  1197. */
  1198. SHA256 : function (options) {
  1199. /**
  1200. * Private properties configuration variables. You may need to tweak these to be compatible with
  1201. * the server-side, but the defaults work in most cases.
  1202. * @see this.setUpperCase() method
  1203. * @see this.setPad() method
  1204. */
  1205. var hexcase = (options && typeof options.uppercase === 'boolean') ? options.uppercase : false, // hexadecimal output case format. false - lowercase; true - uppercase */
  1206. b64pad = (options && typeof options.pad === 'string') ? options.pda : '=', /* base-64 pad character. Default '=' for strict RFC compliance */
  1207. utf8 = (options && typeof options.utf8 === 'boolean') ? options.utf8 : true, /* enable/disable utf8 encoding */
  1208. sha256_K;
  1209. /* privileged (public) methods */
  1210. this.hex = function (s) {
  1211. return rstr2hex(rstr(s, utf8));
  1212. };
  1213. this.b64 = function (s) {
  1214. return rstr2b64(rstr(s, utf8), b64pad);
  1215. };
  1216. this.any = function (s, e) {
  1217. return rstr2any(rstr(s, utf8), e);
  1218. };
  1219. this.hex_hmac = function (k, d) {
  1220. return rstr2hex(rstr_hmac(k, d));
  1221. };
  1222. this.b64_hmac = function (k, d) {
  1223. return rstr2b64(rstr_hmac(k, d), b64pad);
  1224. };
  1225. this.any_hmac = function (k, d, e) {
  1226. return rstr2any(rstr_hmac(k, d), e);
  1227. };
  1228. /**
  1229. * Perform a simple self-test to see if the VM is working
  1230. * @return {String} Hexadecimal hash sample
  1231. * @public
  1232. */
  1233. this.vm_test = function () {
  1234. return hex('abc').toLowerCase() === '900150983cd24fb0d6963f7d28e17f72';
  1235. };
  1236. /**
  1237. * Enable/disable uppercase hexadecimal returned string
  1238. * @param {boolean}
  1239. * @return {Object} this
  1240. * @public
  1241. */
  1242. this.setUpperCase = function (a) {
  1243. if (typeof a === 'boolean') {
  1244. hexcase = a;
  1245. }
  1246. return this;
  1247. };
  1248. /**
  1249. * @description Defines a base64 pad string
  1250. * @param {string} Pad
  1251. * @return {Object} this
  1252. * @public
  1253. */
  1254. this.setPad = function (a) {
  1255. b64pad = a || b64pad;
  1256. return this;
  1257. };
  1258. /**
  1259. * Defines a base64 pad string
  1260. * @param {boolean}
  1261. * @return {Object} this
  1262. * @public
  1263. */
  1264. this.setUTF8 = function (a) {
  1265. if (typeof a === 'boolean') {
  1266. utf8 = a;
  1267. }
  1268. return this;
  1269. };
  1270. // private methods
  1271. /**
  1272. * Calculate the SHA-512 of a raw string
  1273. */
  1274. function rstr(s, utf8) {
  1275. s = (utf8) ? utf8Encode(s) : s;
  1276. return binb2rstr(binb(rstr2binb(s), s.length * 8));
  1277. }
  1278. /**
  1279. * Calculate the HMAC-sha256 of a key and some data (raw strings)
  1280. */
  1281. function rstr_hmac(key, data) {
  1282. key = (utf8) ? utf8Encode(key) : key;
  1283. data = (utf8) ? utf8Encode(data) : data;
  1284. var hash, i = 0,
  1285. bkey = rstr2binb(key),
  1286. ipad = Array(16),
  1287. opad = Array(16);
  1288. if (bkey.length > 16) { bkey = binb(bkey, key.length * 8); }
  1289. for (; i < 16; i+=1) {
  1290. ipad[i] = bkey[i] ^ 0x36363636;
  1291. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  1292. }
  1293. hash = binb(ipad.concat(rstr2binb(data)), 512 + data.length * 8);
  1294. return binb2rstr(binb(opad.concat(hash), 512 + 256));
  1295. }
  1296. /*
  1297. * Main sha256 function, with its support functions
  1298. */
  1299. function sha256_S (X, n) {return ( X >>> n ) | (X << (32 - n));}
  1300. function sha256_R (X, n) {return ( X >>> n );}
  1301. function sha256_Ch(x, y, z) {return ((x & y) ^ ((~x) & z));}
  1302. function sha256_Maj(x, y, z) {return ((x & y) ^ (x & z) ^ (y & z));}
  1303. function sha256_Sigma0256(x) {return (sha256_S(x, 2) ^ sha256_S(x, 13) ^ sha256_S(x, 22));}
  1304. function sha256_Sigma1256(x) {return (sha256_S(x, 6) ^ sha256_S(x, 11) ^ sha256_S(x, 25));}
  1305. function sha256_Gamma0256(x) {return (sha256_S(x, 7) ^ sha256_S(x, 18) ^ sha256_R(x, 3));}
  1306. function sha256_Gamma1256(x) {return (sha256_S(x, 17) ^ sha256_S(x, 19) ^ sha256_R(x, 10));}
  1307. function sha256_Sigma0512(x) {return (sha256_S(x, 28) ^ sha256_S(x, 34) ^ sha256_S(x, 39));}
  1308. function sha256_Sigma1512(x) {return (sha256_S(x, 14) ^ sha256_S(x, 18) ^ sha256_S(x, 41));}
  1309. function sha256_Gamma0512(x) {return (sha256_S(x, 1) ^ sha256_S(x, 8) ^ sha256_R(x, 7));}
  1310. function sha256_Gamma1512(x) {return (sha256_S(x, 19) ^ sha256_S(x, 61) ^ sha256_R(x, 6));}
  1311. sha256_K = [
  1312. 1116352408, 1899447441, -1245643825, -373957723, 961987163, 1508970993,
  1313. -1841331548, -1424204075, -670586216, 310598401, 607225278, 1426881987,
  1314. 1925078388, -2132889090, -1680079193, -1046744716, -459576895, -272742522,
  1315. 264347078, 604807628, 770255983, 1249150122, 1555081692, 1996064986,
  1316. -1740746414, -1473132947, -1341970488, -1084653625, -958395405, -710438585,
  1317. 113926993, 338241895, 666307205, 773529912, 1294757372, 1396182291,
  1318. 1695183700, 1986661051, -2117940946, -1838011259, -1564481375, -1474664885,
  1319. -1035236496, -949202525, -778901479, -694614492, -200395387, 275423344,
  1320. 430227734, 506948616, 659060556, 883997877, 958139571, 1322822218,
  1321. 1537002063, 1747873779, 1955562222, 2024104815, -2067236844, -1933114872,
  1322. -1866530822, -1538233109, -1090935817, -965641998
  1323. ];
  1324. function binb(m, l) {
  1325. var HASH = [1779033703, -1150833019, 1013904242, -1521486534,
  1326. 1359893119, -1694144372, 528734635, 1541459225];
  1327. var W = new Array(64);
  1328. var a, b, c, d, e, f, g, h;
  1329. var i, j, T1, T2;
  1330. /* append padding */
  1331. m[l >> 5] |= 0x80 << (24 - l % 32);
  1332. m[((l + 64 >> 9) << 4) + 15] = l;
  1333. for (i = 0; i < m.length; i += 16)
  1334. {
  1335. a = HASH[0];
  1336. b = HASH[1];
  1337. c = HASH[2];
  1338. d = HASH[3];
  1339. e = HASH[4];
  1340. f = HASH[5];
  1341. g = HASH[6];
  1342. h = HASH[7];
  1343. for (j = 0; j < 64; j+=1)
  1344. {
  1345. if (j < 16) {
  1346. W[j] = m[j + i];
  1347. } else {
  1348. W[j] = safe_add(safe_add(safe_add(sha256_Gamma1256(W[j - 2]), W[j - 7]),
  1349. sha256_Gamma0256(W[j - 15])), W[j - 16]);
  1350. }
  1351. T1 = safe_add(safe_add(safe_add(safe_add(h, sha256_Sigma1256(e)), sha256_Ch(e, f, g)),
  1352. sha256_K[j]), W[j]);
  1353. T2 = safe_add(sha256_Sigma0256(a), sha256_Maj(a, b, c));
  1354. h = g;
  1355. g = f;
  1356. f = e;
  1357. e = safe_add(d, T1);
  1358. d = c;
  1359. c = b;
  1360. b = a;
  1361. a = safe_add(T1, T2);
  1362. }
  1363. HASH[0] = safe_add(a, HASH[0]);
  1364. HASH[1] = safe_add(b, HASH[1]);
  1365. HASH[2] = safe_add(c, HASH[2]);
  1366. HASH[3] = safe_add(d, HASH[3]);
  1367. HASH[4] = safe_add(e, HASH[4]);
  1368. HASH[5] = safe_add(f, HASH[5]);
  1369. HASH[6] = safe_add(g, HASH[6]);
  1370. HASH[7] = safe_add(h, HASH[7]);
  1371. }
  1372. return HASH;
  1373. }
  1374. },
  1375. /**
  1376. * @class Hashes.SHA512
  1377. * @param {config}
  1378. *
  1379. * A JavaScript implementation of the Secure Hash Algorithm, SHA-512, as defined in FIPS 180-2
  1380. * Version 2.2 Copyright Anonymous Contributor, Paul Johnston 2000 - 2009.
  1381. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  1382. * See http://pajhome.org.uk/crypt/md5 for details.
  1383. */
  1384. SHA512 : function (options) {
  1385. /**
  1386. * Private properties configuration variables. You may need to tweak these to be compatible with
  1387. * the server-side, but the defaults work in most cases.
  1388. * @see this.setUpperCase() method
  1389. * @see this.setPad() method
  1390. */
  1391. var hexcase = (options && typeof options.uppercase === 'boolean') ? options.uppercase : false , /* hexadecimal output case format. false - lowercase; true - uppercase */
  1392. b64pad = (options && typeof options.pad === 'string') ? options.pda : '=', /* base-64 pad character. Default '=' for strict RFC compliance */
  1393. utf8 = (options && typeof options.utf8 === 'boolean') ? options.utf8 : true, /* enable/disable utf8 encoding */
  1394. sha512_k;
  1395. /* privileged (public) methods */
  1396. this.hex = function (s) {
  1397. return rstr2hex(rstr(s));
  1398. };
  1399. this.b64 = function (s) {
  1400. return rstr2b64(rstr(s), b64pad);
  1401. };
  1402. this.any = function (s, e) {
  1403. return rstr2any(rstr(s), e);
  1404. };
  1405. this.hex_hmac = function (k, d) {
  1406. return rstr2hex(rstr_hmac(k, d));
  1407. };
  1408. this.b64_hmac = function (k, d) {
  1409. return rstr2b64(rstr_hmac(k, d), b64pad);
  1410. };
  1411. this.any_hmac = function (k, d, e) {
  1412. return rstr2any(rstr_hmac(k, d), e);
  1413. };
  1414. /**
  1415. * Perform a simple self-test to see if the VM is working
  1416. * @return {String} Hexadecimal hash sample
  1417. * @public
  1418. */
  1419. this.vm_test = function () {
  1420. return hex('abc').toLowerCase() === '900150983cd24fb0d6963f7d28e17f72';
  1421. };
  1422. /**
  1423. * @description Enable/disable uppercase hexadecimal returned string
  1424. * @param {boolean}
  1425. * @return {Object} this
  1426. * @public
  1427. */
  1428. this.setUpperCase = function (a) {
  1429. if (typeof a === 'boolean') {
  1430. hexcase = a;
  1431. }
  1432. return this;
  1433. };
  1434. /**
  1435. * @description Defines a base64 pad string
  1436. * @param {string} Pad
  1437. * @return {Object} this
  1438. * @public
  1439. */
  1440. this.setPad = function (a) {
  1441. b64pad = a || b64pad;
  1442. return this;
  1443. };
  1444. /**
  1445. * @description Defines a base64 pad string
  1446. * @param {boolean}
  1447. * @return {Object} this
  1448. * @public
  1449. */
  1450. this.setUTF8 = function (a) {
  1451. if (typeof a === 'boolean') {
  1452. utf8 = a;
  1453. }
  1454. return this;
  1455. };
  1456. /* private methods */
  1457. /**
  1458. * Calculate the SHA-512 of a raw string
  1459. */
  1460. function rstr(s) {
  1461. s = (utf8) ? utf8Encode(s) : s;
  1462. return binb2rstr(binb(rstr2binb(s), s.length * 8));
  1463. }
  1464. /*
  1465. * Calculate the HMAC-SHA-512 of a key and some data (raw strings)
  1466. */
  1467. function rstr_hmac(key, data) {
  1468. key = (utf8) ? utf8Encode(key) : key;
  1469. data = (utf8) ? utf8Encode(data) : data;
  1470. var hash, i = 0,
  1471. bkey = rstr2binb(key),
  1472. ipad = Array(32), opad = Array(32);
  1473. if (bkey.length > 32) { bkey = binb(bkey, key.length * 8); }
  1474. for (; i < 32; i+=1) {
  1475. ipad[i] = bkey[i] ^ 0x36363636;
  1476. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  1477. }
  1478. hash = binb(ipad.concat(rstr2binb(data)), 1024 + data.length * 8);
  1479. return binb2rstr(binb(opad.concat(hash), 1024 + 512));
  1480. }
  1481. /**
  1482. * Calculate the SHA-512 of an array of big-endian dwords, and a bit length
  1483. */
  1484. function binb(x, len) {
  1485. var j, i, l,
  1486. W = new Array(80),
  1487. hash = new Array(16),
  1488. //Initial hash values
  1489. H = [
  1490. new int64(0x6a09e667, -205731576),
  1491. new int64(-1150833019, -2067093701),
  1492. new int64(0x3c6ef372, -23791573),
  1493. new int64(-1521486534, 0x5f1d36f1),
  1494. new int64(0x510e527f, -1377402159),
  1495. new int64(-1694144372, 0x2b3e6c1f),
  1496. new int64(0x1f83d9ab, -79577749),
  1497. new int64(0x5be0cd19, 0x137e2179)
  1498. ],
  1499. T1 = new int64(0, 0),
  1500. T2 = new int64(0, 0),
  1501. a = new int64(0,0),
  1502. b = new int64(0,0),
  1503. c = new int64(0,0),
  1504. d = new int64(0,0),
  1505. e = new int64(0,0),
  1506. f = new int64(0,0),
  1507. g = new int64(0,0),
  1508. h = new int64(0,0),
  1509. //Temporary variables not specified by the document
  1510. s0 = new int64(0, 0),
  1511. s1 = new int64(0, 0),
  1512. Ch = new int64(0, 0),
  1513. Maj = new int64(0, 0),
  1514. r1 = new int64(0, 0),
  1515. r2 = new int64(0, 0),
  1516. r3 = new int64(0, 0);
  1517. if (sha512_k === undefined) {
  1518. //SHA512 constants
  1519. sha512_k = [
  1520. new int64(0x428a2f98, -685199838), new int64(0x71374491, 0x23ef65cd),
  1521. new int64(-1245643825, -330482897), new int64(-373957723, -2121671748),
  1522. new int64(0x3956c25b, -213338824), new int64(0x59f111f1, -1241133031),
  1523. new int64(-1841331548, -1357295717), new int64(-1424204075, -630357736),
  1524. new int64(-670586216, -1560083902), new int64(0x12835b01, 0x45706fbe),
  1525. new int64(0x243185be, 0x4ee4b28c), new int64(0x550c7dc3, -704662302),
  1526. new int64(0x72be5d74, -226784913), new int64(-2132889090, 0x3b1696b1),
  1527. new int64(-1680079193, 0x25c71235), new int64(-1046744716, -815192428),
  1528. new int64(-459576895, -1628353838), new int64(-272742522, 0x384f25e3),
  1529. new int64(0xfc19dc6, -1953704523), new int64(0x240ca1cc, 0x77ac9c65),
  1530. new int64(0x2de92c6f, 0x592b0275), new int64(0x4a7484aa, 0x6ea6e483),
  1531. new int64(0x5cb0a9dc, -1119749164), new int64(0x76f988da, -2096016459),
  1532. new int64(-1740746414, -295247957), new int64(-1473132947, 0x2db43210),
  1533. new int64(-1341970488, -1728372417), new int64(-1084653625, -1091629340),
  1534. new int64(-958395405, 0x3da88fc2), new int64(-710438585, -1828018395),
  1535. new int64(0x6ca6351, -536640913), new int64(0x14292967, 0xa0e6e70),
  1536. new int64(0x27b70a85, 0x46d22ffc), new int64(0x2e1b2138, 0x5c26c926),
  1537. new int64(0x4d2c6dfc, 0x5ac42aed), new int64(0x53380d13, -1651133473),
  1538. new int64(0x650a7354, -1951439906), new int64(0x766a0abb, 0x3c77b2a8),
  1539. new int64(-2117940946, 0x47edaee6), new int64(-1838011259, 0x1482353b),
  1540. new int64(-1564481375, 0x4cf10364), new int64(-1474664885, -1136513023),
  1541. new int64(-1035236496, -789014639), new int64(-949202525, 0x654be30),
  1542. new int64(-778901479, -688958952), new int64(-694614492, 0x5565a910),
  1543. new int64(-200395387, 0x5771202a), new int64(0x106aa070, 0x32bbd1b8),
  1544. new int64(0x19a4c116, -1194143544), new int64(0x1e376c08, 0x5141ab53),
  1545. new int64(0x2748774c, -544281703), new int64(0x34b0bcb5, -509917016),
  1546. new int64(0x391c0cb3, -976659869), new int64(0x4ed8aa4a, -482243893),
  1547. new int64(0x5b9cca4f, 0x7763e373), new int64(0x682e6ff3, -692930397),
  1548. new int64(0x748f82ee, 0x5defb2fc), new int64(0x78a5636f, 0x43172f60),
  1549. new int64(-2067236844, -1578062990), new int64(-1933114872, 0x1a6439ec),
  1550. new int64(-1866530822, 0x23631e28), new int64(-1538233109, -561857047),
  1551. new int64(-1090935817, -1295615723), new int64(-965641998, -479046869),
  1552. new int64(-903397682, -366583396), new int64(-779700025, 0x21c0c207),
  1553. new int64(-354779690, -840897762), new int64(-176337025, -294727304),
  1554. new int64(0x6f067aa, 0x72176fba), new int64(0xa637dc5, -1563912026),
  1555. new int64(0x113f9804, -1090974290), new int64(0x1b710b35, 0x131c471b),
  1556. new int64(0x28db77f5, 0x23047d84), new int64(0x32caab7b, 0x40c72493),
  1557. new int64(0x3c9ebe0a, 0x15c9bebc), new int64(0x431d67c4, -1676669620),
  1558. new int64(0x4cc5d4be, -885112138), new int64(0x597f299c, -60457430),
  1559. new int64(0x5fcb6fab, 0x3ad6faec), new int64(0x6c44198c, 0x4a475817)
  1560. ];
  1561. }
  1562. for (i=0; i<80; i+=1) {
  1563. W[i] = new int64(0, 0);
  1564. }
  1565. // append padding to the source string. The format is described in the FIPS.
  1566. x[len >> 5] |= 0x80 << (24 - (len & 0x1f));
  1567. x[((len + 128 >> 10)<< 5) + 31] = len;
  1568. l = x.length;
  1569. for (i = 0; i<l; i+=32) { //32 dwords is the block size
  1570. int64copy(a, H[0]);
  1571. int64copy(b, H[1]);
  1572. int64copy(c, H[2]);
  1573. int64copy(d, H[3]);
  1574. int64copy(e, H[4]);
  1575. int64copy(f, H[5]);
  1576. int64copy(g, H[6]);
  1577. int64copy(h, H[7]);
  1578. for (j=0; j<16; j+=1) {
  1579. W[j].h = x[i + 2*j];
  1580. W[j].l = x[i + 2*j + 1];
  1581. }
  1582. for (j=16; j<80; j+=1) {
  1583. //sigma1
  1584. int64rrot(r1, W[j-2], 19);
  1585. int64revrrot(r2, W[j-2], 29);
  1586. int64shr(r3, W[j-2], 6);
  1587. s1.l = r1.l ^ r2.l ^ r3.l;
  1588. s1.h = r1.h ^ r2.h ^ r3.h;
  1589. //sigma0
  1590. int64rrot(r1, W[j-15], 1);
  1591. int64rrot(r2, W[j-15], 8);
  1592. int64shr(r3, W[j-15], 7);
  1593. s0.l = r1.l ^ r2.l ^ r3.l;
  1594. s0.h = r1.h ^ r2.h ^ r3.h;
  1595. int64add4(W[j], s1, W[j-7], s0, W[j-16]);
  1596. }
  1597. for (j = 0; j < 80; j+=1) {
  1598. //Ch
  1599. Ch.l = (e.l & f.l) ^ (~e.l & g.l);
  1600. Ch.h = (e.h & f.h) ^ (~e.h & g.h);
  1601. //Sigma1
  1602. int64rrot(r1, e, 14);
  1603. int64rrot(r2, e, 18);
  1604. int64revrrot(r3, e, 9);
  1605. s1.l = r1.l ^ r2.l ^ r3.l;
  1606. s1.h = r1.h ^ r2.h ^ r3.h;
  1607. //Sigma0
  1608. int64rrot(r1, a, 28);
  1609. int64revrrot(r2, a, 2);
  1610. int64revrrot(r3, a, 7);
  1611. s0.l = r1.l ^ r2.l ^ r3.l;
  1612. s0.h = r1.h ^ r2.h ^ r3.h;
  1613. //Maj
  1614. Maj.l = (a.l & b.l) ^ (a.l & c.l) ^ (b.l & c.l);
  1615. Maj.h = (a.h & b.h) ^ (a.h & c.h) ^ (b.h & c.h);
  1616. int64add5(T1, h, s1, Ch, sha512_k[j], W[j]);
  1617. int64add(T2, s0, Maj);
  1618. int64copy(h, g);
  1619. int64copy(g, f);
  1620. int64copy(f, e);
  1621. int64add(e, d, T1);
  1622. int64copy(d, c);
  1623. int64copy(c, b);
  1624. int64copy(b, a);
  1625. int64add(a, T1, T2);
  1626. }
  1627. int64add(H[0], H[0], a);
  1628. int64add(H[1], H[1], b);
  1629. int64add(H[2], H[2], c);
  1630. int64add(H[3], H[3], d);
  1631. int64add(H[4], H[4], e);
  1632. int64add(H[5], H[5], f);
  1633. int64add(H[6], H[6], g);
  1634. int64add(H[7], H[7], h);
  1635. }
  1636. //represent the hash as an array of 32-bit dwords
  1637. for (i=0; i<8; i+=1) {
  1638. hash[2*i] = H[i].h;
  1639. hash[2*i + 1] = H[i].l;
  1640. }
  1641. return hash;
  1642. }
  1643. //A constructor for 64-bit numbers
  1644. function int64(h, l) {
  1645. this.h = h;
  1646. this.l = l;
  1647. //this.toString = int64toString;
  1648. }
  1649. //Copies src into dst, assuming both are 64-bit numbers
  1650. function int64copy(dst, src) {
  1651. dst.h = src.h;
  1652. dst.l = src.l;
  1653. }
  1654. //Right-rotates a 64-bit number by shift
  1655. //Won't handle cases of shift>=32
  1656. //The function revrrot() is for that
  1657. function int64rrot(dst, x, shift) {
  1658. dst.l = (x.l >>> shift) | (x.h << (32-shift));
  1659. dst.h = (x.h >>> shift) | (x.l << (32-shift));
  1660. }
  1661. //Reverses the dwords of the source and then rotates right by shift.
  1662. //This is equivalent to rotation by 32+shift
  1663. function int64revrrot(dst, x, shift) {
  1664. dst.l = (x.h >>> shift) | (x.l << (32-shift));
  1665. dst.h = (x.l >>> shift) | (x.h << (32-shift));
  1666. }
  1667. //Bitwise-shifts right a 64-bit number by shift
  1668. //Won't handle shift>=32, but it's never needed in SHA512
  1669. function int64shr(dst, x, shift) {
  1670. dst.l = (x.l >>> shift) | (x.h << (32-shift));
  1671. dst.h = (x.h >>> shift);
  1672. }
  1673. //Adds two 64-bit numbers
  1674. //Like the original implementation, does not rely on 32-bit operations
  1675. function int64add(dst, x, y) {
  1676. var w0 = (x.l & 0xffff) + (y.l & 0xffff);
  1677. var w1 = (x.l >>> 16) + (y.l >>> 16) + (w0 >>> 16);
  1678. var w2 = (x.h & 0xffff) + (y.h & 0xffff) + (w1 >>> 16);
  1679. var w3 = (x.h >>> 16) + (y.h >>> 16) + (w2 >>> 16);
  1680. dst.l = (w0 & 0xffff) | (w1 << 16);
  1681. dst.h = (w2 & 0xffff) | (w3 << 16);
  1682. }
  1683. //Same, except with 4 addends. Works faster than adding them one by one.
  1684. function int64add4(dst, a, b, c, d) {
  1685. var w0 = (a.l & 0xffff) + (b.l & 0xffff) + (c.l & 0xffff) + (d.l & 0xffff);
  1686. var w1 = (a.l >>> 16) + (b.l >>> 16) + (c.l >>> 16) + (d.l >>> 16) + (w0 >>> 16);
  1687. var w2 = (a.h & 0xffff) + (b.h & 0xffff) + (c.h & 0xffff) + (d.h & 0xffff) + (w1 >>> 16);
  1688. var w3 = (a.h >>> 16) + (b.h >>> 16) + (c.h >>> 16) + (d.h >>> 16) + (w2 >>> 16);
  1689. dst.l = (w0 & 0xffff) | (w1 << 16);
  1690. dst.h = (w2 & 0xffff) | (w3 << 16);
  1691. }
  1692. //Same, except with 5 addends
  1693. function int64add5(dst, a, b, c, d, e) {
  1694. var w0 = (a.l & 0xffff) + (b.l & 0xffff) + (c.l & 0xffff) + (d.l & 0xffff) + (e.l & 0xffff),
  1695. w1 = (a.l >>> 16) + (b.l >>> 16) + (c.l >>> 16) + (d.l >>> 16) + (e.l >>> 16) + (w0 >>> 16),
  1696. w2 = (a.h & 0xffff) + (b.h & 0xffff) + (c.h & 0xffff) + (d.h & 0xffff) + (e.h & 0xffff) + (w1 >>> 16),
  1697. w3 = (a.h >>> 16) + (b.h >>> 16) + (c.h >>> 16) + (d.h >>> 16) + (e.h >>> 16) + (w2 >>> 16);
  1698. dst.l = (w0 & 0xffff) | (w1 << 16);
  1699. dst.h = (w2 & 0xffff) | (w3 << 16);
  1700. }
  1701. },
  1702. /**
  1703. * @class Hashes.RMD160
  1704. * @constructor
  1705. * @param {Object} [config]
  1706. *
  1707. * A JavaScript implementation of the RIPEMD-160 Algorithm
  1708. * Version 2.2 Copyright Jeremy Lin, Paul Johnston 2000 - 2009.
  1709. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  1710. * See http://pajhome.org.uk/crypt/md5 for details.
  1711. * Also http://www.ocf.berkeley.edu/~jjlin/jsotp/
  1712. */
  1713. RMD160 : function (options) {
  1714. /**
  1715. * Private properties configuration variables. You may need to tweak these to be compatible with
  1716. * the server-side, but the defaults work in most cases.
  1717. * @see this.setUpperCase() method
  1718. * @see this.setPad() method
  1719. */
  1720. var hexcase = (options && typeof options.uppercase === 'boolean') ? options.uppercase : false, /* hexadecimal output case format. false - lowercase; true - uppercase */
  1721. b64pad = (options && typeof options.pad === 'string') ? options.pda : '=', /* base-64 pad character. Default '=' for strict RFC compliance */
  1722. utf8 = (options && typeof options.utf8 === 'boolean') ? options.utf8 : true, /* enable/disable utf8 encoding */
  1723. rmd160_r1 = [
  1724. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  1725. 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
  1726. 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
  1727. 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
  1728. 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13
  1729. ],
  1730. rmd160_r2 = [
  1731. 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
  1732. 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
  1733. 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
  1734. 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
  1735. 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11
  1736. ],
  1737. rmd160_s1 = [
  1738. 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
  1739. 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
  1740. 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
  1741. 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
  1742. 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6
  1743. ],
  1744. rmd160_s2 = [
  1745. 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
  1746. 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
  1747. 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
  1748. 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
  1749. 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11
  1750. ];
  1751. /* privileged (public) methods */
  1752. this.hex = function (s) {
  1753. return rstr2hex(rstr(s, utf8));
  1754. };
  1755. this.b64 = function (s) {
  1756. return rstr2b64(rstr(s, utf8), b64pad);
  1757. };
  1758. this.any = function (s, e) {
  1759. return rstr2any(rstr(s, utf8), e);
  1760. };
  1761. this.hex_hmac = function (k, d) {
  1762. return rstr2hex(rstr_hmac(k, d));
  1763. };
  1764. this.b64_hmac = function (k, d) {
  1765. return rstr2b64(rstr_hmac(k, d), b64pad);
  1766. };
  1767. this.any_hmac = function (k, d, e) {
  1768. return rstr2any(rstr_hmac(k, d), e);
  1769. };
  1770. /**
  1771. * Perform a simple self-test to see if the VM is working
  1772. * @return {String} Hexadecimal hash sample
  1773. * @public
  1774. */
  1775. this.vm_test = function () {
  1776. return hex('abc').toLowerCase() === '900150983cd24fb0d6963f7d28e17f72';
  1777. };
  1778. /**
  1779. * @description Enable/disable uppercase hexadecimal returned string
  1780. * @param {boolean}
  1781. * @return {Object} this
  1782. * @public
  1783. */
  1784. this.setUpperCase = function (a) {
  1785. if (typeof a === 'boolean' ) { hexcase = a; }
  1786. return this;
  1787. };
  1788. /**
  1789. * @description Defines a base64 pad string
  1790. * @param {string} Pad
  1791. * @return {Object} this
  1792. * @public
  1793. */
  1794. this.setPad = function (a) {
  1795. if (typeof a !== 'undefined' ) { b64pad = a; }
  1796. return this;
  1797. };
  1798. /**
  1799. * @description Defines a base64 pad string
  1800. * @param {boolean}
  1801. * @return {Object} this
  1802. * @public
  1803. */
  1804. this.setUTF8 = function (a) {
  1805. if (typeof a === 'boolean') { utf8 = a; }
  1806. return this;
  1807. };
  1808. /* private methods */
  1809. /**
  1810. * Calculate the rmd160 of a raw string
  1811. */
  1812. function rstr(s) {
  1813. s = (utf8) ? utf8Encode(s) : s;
  1814. return binl2rstr(binl(rstr2binl(s), s.length * 8));
  1815. }
  1816. /**
  1817. * Calculate the HMAC-rmd160 of a key and some data (raw strings)
  1818. */
  1819. function rstr_hmac(key, data) {
  1820. key = (utf8) ? utf8Encode(key) : key;
  1821. data = (utf8) ? utf8Encode(data) : data;
  1822. var i, hash,
  1823. bkey = rstr2binl(key),
  1824. ipad = Array(16), opad = Array(16);
  1825. if (bkey.length > 16) {
  1826. bkey = binl(bkey, key.length * 8);
  1827. }
  1828. for (i = 0; i < 16; i+=1) {
  1829. ipad[i] = bkey[i] ^ 0x36363636;
  1830. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  1831. }
  1832. hash = binl(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
  1833. return binl2rstr(binl(opad.concat(hash), 512 + 160));
  1834. }
  1835. /**
  1836. * Convert an array of little-endian words to a string
  1837. */
  1838. function binl2rstr(input) {
  1839. var i, output = '', l = input.length * 32;
  1840. for (i = 0; i < l; i += 8) {
  1841. output += String.fromCharCode((input[i>>5] >>> (i % 32)) & 0xFF);
  1842. }
  1843. return output;
  1844. }
  1845. /**
  1846. * Calculate the RIPE-MD160 of an array of little-endian words, and a bit length.
  1847. */
  1848. function binl(x, len) {
  1849. var T, j, i, l,
  1850. h0 = 0x67452301,
  1851. h1 = 0xefcdab89,
  1852. h2 = 0x98badcfe,
  1853. h3 = 0x10325476,
  1854. h4 = 0xc3d2e1f0,
  1855. A1, B1, C1, D1, E1,
  1856. A2, B2, C2, D2, E2;
  1857. /* append padding */
  1858. x[len >> 5] |= 0x80 << (len % 32);
  1859. x[(((len + 64) >>> 9) << 4) + 14] = len;
  1860. l = x.length;
  1861. for (i = 0; i < l; i+=16) {
  1862. A1 = A2 = h0; B1 = B2 = h1; C1 = C2 = h2; D1 = D2 = h3; E1 = E2 = h4;
  1863. for (j = 0; j <= 79; j+=1) {
  1864. T = safe_add(A1, rmd160_f(j, B1, C1, D1));
  1865. T = safe_add(T, x[i + rmd160_r1[j]]);
  1866. T = safe_add(T, rmd160_K1(j));
  1867. T = safe_add(bit_rol(T, rmd160_s1[j]), E1);
  1868. A1 = E1; E1 = D1; D1 = bit_rol(C1, 10); C1 = B1; B1 = T;
  1869. T = safe_add(A2, rmd160_f(79-j, B2, C2, D2));
  1870. T = safe_add(T, x[i + rmd160_r2[j]]);
  1871. T = safe_add(T, rmd160_K2(j));
  1872. T = safe_add(bit_rol(T, rmd160_s2[j]), E2);
  1873. A2 = E2; E2 = D2; D2 = bit_rol(C2, 10); C2 = B2; B2 = T;
  1874. }
  1875. T = safe_add(h1, safe_add(C1, D2));
  1876. h1 = safe_add(h2, safe_add(D1, E2));
  1877. h2 = safe_add(h3, safe_add(E1, A2));
  1878. h3 = safe_add(h4, safe_add(A1, B2));
  1879. h4 = safe_add(h0, safe_add(B1, C2));
  1880. h0 = T;
  1881. }
  1882. return [h0, h1, h2, h3, h4];
  1883. }
  1884. // specific algorithm methods
  1885. function rmd160_f(j, x, y, z) {
  1886. return ( 0 <= j && j <= 15) ? (x ^ y ^ z) :
  1887. (16 <= j && j <= 31) ? (x & y) | (~x & z) :
  1888. (32 <= j && j <= 47) ? (x | ~y) ^ z :
  1889. (48 <= j && j <= 63) ? (x & z) | (y & ~z) :
  1890. (64 <= j && j <= 79) ? x ^ (y | ~z) :
  1891. 'rmd160_f: j out of range';
  1892. }
  1893. function rmd160_K1(j) {
  1894. return ( 0 <= j && j <= 15) ? 0x00000000 :
  1895. (16 <= j && j <= 31) ? 0x5a827999 :
  1896. (32 <= j && j <= 47) ? 0x6ed9eba1 :
  1897. (48 <= j && j <= 63) ? 0x8f1bbcdc :
  1898. (64 <= j && j <= 79) ? 0xa953fd4e :
  1899. 'rmd160_K1: j out of range';
  1900. }
  1901. function rmd160_K2(j){
  1902. return ( 0 <= j && j <= 15) ? 0x50a28be6 :
  1903. (16 <= j && j <= 31) ? 0x5c4dd124 :
  1904. (32 <= j && j <= 47) ? 0x6d703ef3 :
  1905. (48 <= j && j <= 63) ? 0x7a6d76e9 :
  1906. (64 <= j && j <= 79) ? 0x00000000 :
  1907. 'rmd160_K2: j out of range';
  1908. }
  1909. }
  1910. };
  1911. // exposes Hashes
  1912. (function( window, undefined ) {
  1913. var freeExports = false;
  1914. if (typeof exports === 'object' ) {
  1915. freeExports = exports;
  1916. if (exports && typeof global === 'object' && global && global === global.global ) { window = global; }
  1917. }
  1918. if (typeof define === 'function' && typeof define.amd === 'object' && define.amd) {
  1919. // define as an anonymous module, so, through path mapping, it can be aliased
  1920. define(function () { return Hashes; });
  1921. }
  1922. else if ( freeExports ) {
  1923. // in Node.js or RingoJS v0.8.0+
  1924. if ( typeof module === 'object' && module && module.exports === freeExports ) {
  1925. module.exports = Hashes;
  1926. }
  1927. // in Narwhal or RingoJS v0.7.0-
  1928. else {
  1929. freeExports.Hashes = Hashes;
  1930. }
  1931. }
  1932. else {
  1933. // in a browser or Rhino
  1934. window.Hashes = Hashes;
  1935. }
  1936. }( this ));
  1937. }()); // IIFE
  1938. })(window)
  1939. },{}],2:[function(require,module,exports){
  1940. 'use strict';
  1941. var hashes = require('jshashes'),
  1942. xtend = require('xtend'),
  1943. sha1 = new hashes.SHA1();
  1944. var ohauth = {};
  1945. ohauth.qsString = function(obj) {
  1946. return Object.keys(obj).sort().map(function(key) {
  1947. return ohauth.percentEncode(key) + '=' +
  1948. ohauth.percentEncode(obj[key]);
  1949. }).join('&');
  1950. };
  1951. ohauth.stringQs = function(str) {
  1952. return str.split('&').reduce(function(obj, pair){
  1953. var parts = pair.split('=');
  1954. obj[decodeURIComponent(parts[0])] = (null === parts[1]) ?
  1955. '' : decodeURIComponent(parts[1]);
  1956. return obj;
  1957. }, {});
  1958. };
  1959. ohauth.rawxhr = function(method, url, data, headers, callback) {
  1960. var xhr = new XMLHttpRequest(),
  1961. twoHundred = /^20\d$/;
  1962. xhr.onreadystatechange = function() {
  1963. if (4 == xhr.readyState && 0 !== xhr.status) {
  1964. if (twoHundred.test(xhr.status)) callback(null, xhr);
  1965. else return callback(xhr, null);
  1966. }
  1967. };
  1968. xhr.onerror = function(e) { return callback(e, null); };
  1969. xhr.open(method, url, true);
  1970. for (var h in headers) xhr.setRequestHeader(h, headers[h]);
  1971. xhr.send(data);
  1972. };
  1973. ohauth.xhr = function(method, url, auth, data, options, callback) {
  1974. var headers = (options && options.header) || {
  1975. 'Content-Type': 'application/x-www-form-urlencoded'
  1976. };
  1977. headers.Authorization = 'OAuth ' + ohauth.authHeader(auth);
  1978. ohauth.rawxhr(method, url, data, headers, callback);
  1979. };
  1980. ohauth.nonce = function() {
  1981. for (var o = ''; o.length < 6;) {
  1982. o += '0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZabcdefghiklmnopqrstuvwxyz'[Math.floor(Math.random() * 61)];
  1983. }
  1984. return o;
  1985. };
  1986. ohauth.authHeader = function(obj) {
  1987. return Object.keys(obj).sort().map(function(key) {
  1988. return encodeURIComponent(key) + '="' + encodeURIComponent(obj[key]) + '"';
  1989. }).join(', ');
  1990. };
  1991. ohauth.timestamp = function() { return ~~((+new Date()) / 1000); };
  1992. ohauth.percentEncode = function(s) {
  1993. return encodeURIComponent(s)
  1994. .replace(/\!/g, '%21').replace(/\'/g, '%27')
  1995. .replace(/\*/g, '%2A').replace(/\(/g, '%28').replace(/\)/g, '%29');
  1996. };
  1997. ohauth.baseString = function(method, url, params) {
  1998. if (params.oauth_signature) delete params.oauth_signature;
  1999. return [
  2000. method,
  2001. ohauth.percentEncode(url),
  2002. ohauth.percentEncode(ohauth.qsString(params))].join('&');
  2003. };
  2004. ohauth.signature = function(oauth_secret, token_secret, baseString) {
  2005. return sha1.b64_hmac(
  2006. ohauth.percentEncode(oauth_secret) + '&' +
  2007. ohauth.percentEncode(token_secret),
  2008. baseString);
  2009. };
  2010. /**
  2011. * Takes an options object for configuration (consumer_key,
  2012. * consumer_secret, version, signature_method, token) and returns a
  2013. * function that generates the Authorization header for given data.
  2014. *
  2015. * The returned function takes these parameters:
  2016. * - method: GET/POST/...
  2017. * - uri: full URI with protocol, port, path and query string
  2018. * - extra_params: any extra parameters (that are passed in the POST data),
  2019. * can be an object or a from-urlencoded string.
  2020. *
  2021. * Returned function returns full OAuth header with "OAuth" string in it.
  2022. */
  2023. ohauth.headerGenerator = function(options) {
  2024. options = options || {};
  2025. var consumer_key = options.consumer_key || '',
  2026. consumer_secret = options.consumer_secret || '',
  2027. signature_method = options.signature_method || 'HMAC-SHA1',
  2028. version = options.version || '1.0',
  2029. token = options.token || '';
  2030. return function(method, uri, extra_params) {
  2031. method = method.toUpperCase();
  2032. if (typeof extra_params === 'string' && extra_params.length > 0) {
  2033. extra_params = ohauth.stringQs(extra_params);
  2034. }
  2035. var uri_parts = uri.split('?', 2),
  2036. base_uri = uri_parts[0];
  2037. var query_params = uri_parts.length === 2 ?
  2038. ohauth.stringQs(uri_parts[1]) : {};
  2039. var oauth_params = {
  2040. oauth_consumer_key: consumer_key,
  2041. oauth_signature_method: signature_method,
  2042. oauth_version: version,
  2043. oauth_timestamp: ohauth.timestamp(),
  2044. oauth_nonce: ohauth.nonce()
  2045. };
  2046. if (token) oauth_params.oauth_token = token;
  2047. var all_params = xtend({}, oauth_params, query_params, extra_params),
  2048. base_str = ohauth.baseString(method, base_uri, all_params);
  2049. oauth_params.oauth_signature = ohauth.signature(consumer_secret, token, base_str);
  2050. return 'OAuth ' + ohauth.authHeader(oauth_params);
  2051. };
  2052. };
  2053. module.exports = ohauth;
  2054. },{"jshashes":7,"xtend":4}],6:[function(require,module,exports){
  2055. module.exports = Object.keys || require('./shim');
  2056. },{"./shim":8}],8:[function(require,module,exports){
  2057. (function () {
  2058. "use strict";
  2059. // modified from https://github.com/kriskowal/es5-shim
  2060. var has = Object.prototype.hasOwnProperty,
  2061. is = require('is'),
  2062. forEach = require('foreach'),
  2063. hasDontEnumBug = !({'toString': null}).propertyIsEnumerable('toString'),
  2064. dontEnums = [
  2065. "toString",
  2066. "toLocaleString",
  2067. "valueOf",
  2068. "hasOwnProperty",
  2069. "isPrototypeOf",
  2070. "propertyIsEnumerable",
  2071. "constructor"
  2072. ],
  2073. keysShim;
  2074. keysShim = function keys(object) {
  2075. if (!is.object(object) && !is.array(object)) {
  2076. throw new TypeError("Object.keys called on a non-object");
  2077. }
  2078. var name, theKeys = [];
  2079. for (name in object) {
  2080. if (has.call(object, name)) {
  2081. theKeys.push(name);
  2082. }
  2083. }
  2084. if (hasDontEnumBug) {
  2085. forEach(dontEnums, function (dontEnum) {
  2086. if (has.call(object, dontEnum)) {
  2087. theKeys.push(dontEnum);
  2088. }
  2089. });
  2090. }
  2091. return theKeys;
  2092. };
  2093. module.exports = keysShim;
  2094. }());
  2095. },{"is":9,"foreach":10}],9:[function(require,module,exports){
  2096. /**!
  2097. * is
  2098. * the definitive JavaScript type testing library
  2099. *
  2100. * @copyright 2013 Enrico Marino
  2101. * @license MIT
  2102. */
  2103. var objProto = Object.prototype;
  2104. var owns = objProto.hasOwnProperty;
  2105. var toString = objProto.toString;
  2106. var isActualNaN = function (value) {
  2107. return value !== value;
  2108. };
  2109. var NON_HOST_TYPES = {
  2110. "boolean": 1,
  2111. "number": 1,
  2112. "string": 1,
  2113. "undefined": 1
  2114. };
  2115. /**
  2116. * Expose `is`
  2117. */
  2118. var is = module.exports = {};
  2119. /**
  2120. * Test general.
  2121. */
  2122. /**
  2123. * is.type
  2124. * Test if `value` is a type of `type`.
  2125. *
  2126. * @param {Mixed} value value to test
  2127. * @param {String} type type
  2128. * @return {Boolean} true if `value` is a type of `type`, false otherwise
  2129. * @api public
  2130. */
  2131. is.a =
  2132. is.type = function (value, type) {
  2133. return typeof value === type;
  2134. };
  2135. /**
  2136. * is.defined
  2137. * Test if `value` is defined.
  2138. *
  2139. * @param {Mixed} value value to test
  2140. * @return {Boolean} true if 'value' is defined, false otherwise
  2141. * @api public
  2142. */
  2143. is.defined = function (value) {
  2144. return value !== undefined;
  2145. };
  2146. /**
  2147. * is.empty
  2148. * Test if `value` is empty.
  2149. *
  2150. * @param {Mixed} value value to test
  2151. * @return {Boolean} true if `value` is empty, false otherwise
  2152. * @api public
  2153. */
  2154. is.empty = function (value) {
  2155. var type = toString.call(value);
  2156. var key;
  2157. if ('[object Array]' === type || '[object Arguments]' === type) {
  2158. return value.length === 0;
  2159. }
  2160. if ('[object Object]' === type) {
  2161. for (key in value) if (owns.call(value, key)) return false;
  2162. return true;
  2163. }
  2164. if ('[object String]' === type) {
  2165. return '' === value;
  2166. }
  2167. return false;
  2168. };
  2169. /**
  2170. * is.equal
  2171. * Test if `value` is equal to `other`.
  2172. *
  2173. * @param {Mixed} value value to test
  2174. * @param {Mixed} other value to compare with
  2175. * @return {Boolean} true if `value` is equal to `other`, false otherwise
  2176. */
  2177. is.equal = function (value, other) {
  2178. var type = toString.call(value)
  2179. var key;
  2180. if (type !== toString.call(other)) {
  2181. return false;
  2182. }
  2183. if ('[object Object]' === type) {
  2184. for (key in value) {
  2185. if (!is.equal(value[key], other[key])) {
  2186. return false;
  2187. }
  2188. }
  2189. return true;
  2190. }
  2191. if ('[object Array]' === type) {
  2192. key = value.length;
  2193. if (key !== other.length) {
  2194. return false;
  2195. }
  2196. while (--key) {
  2197. if (!is.equal(value[key], other[key])) {
  2198. return false;
  2199. }
  2200. }
  2201. return true;
  2202. }
  2203. if ('[object Function]' === type) {
  2204. return value.prototype === other.prototype;
  2205. }
  2206. if ('[object Date]' === type) {
  2207. return value.getTime() === other.getTime();
  2208. }
  2209. return value === other;
  2210. };
  2211. /**
  2212. * is.hosted
  2213. * Test if `value` is hosted by `host`.
  2214. *
  2215. * @param {Mixed} value to test
  2216. * @param {Mixed} host host to test with
  2217. * @return {Boolean} true if `value` is hosted by `host`, false otherwise
  2218. * @api public
  2219. */
  2220. is.hosted = function (value, host) {
  2221. var type = typeof host[value];
  2222. return type === 'object' ? !!host[value] : !NON_HOST_TYPES[type];
  2223. };
  2224. /**
  2225. * is.instance
  2226. * Test if `value` is an instance of `constructor`.
  2227. *
  2228. * @param {Mixed} value value to test
  2229. * @return {Boolean} true if `value` is an instance of `constructor`
  2230. * @api public
  2231. */
  2232. is.instance = is['instanceof'] = function (value, constructor) {
  2233. return value instanceof constructor;
  2234. };
  2235. /**
  2236. * is.null
  2237. * Test if `value` is null.
  2238. *
  2239. * @param {Mixed} value value to test
  2240. * @return {Boolean} true if `value` is null, false otherwise
  2241. * @api public
  2242. */
  2243. is['null'] = function (value) {
  2244. return value === null;
  2245. };
  2246. /**
  2247. * is.undefined
  2248. * Test if `value` is undefined.
  2249. *
  2250. * @param {Mixed} value value to test
  2251. * @return {Boolean} true if `value` is undefined, false otherwise
  2252. * @api public
  2253. */
  2254. is.undefined = function (value) {
  2255. return value === undefined;
  2256. };
  2257. /**
  2258. * Test arguments.
  2259. */
  2260. /**
  2261. * is.arguments
  2262. * Test if `value` is an arguments object.
  2263. *
  2264. * @param {Mixed} value value to test
  2265. * @return {Boolean} true if `value` is an arguments object, false otherwise
  2266. * @api public
  2267. */
  2268. is.arguments = function (value) {
  2269. var isStandardArguments = '[object Arguments]' === toString.call(value);
  2270. var isOldArguments = !is.array(value) && is.arraylike(value) && is.object(value) && is.fn(value.callee);
  2271. return isStandardArguments || isOldArguments;
  2272. };
  2273. /**
  2274. * Test array.
  2275. */
  2276. /**
  2277. * is.array
  2278. * Test if 'value' is an array.
  2279. *
  2280. * @param {Mixed} value value to test
  2281. * @return {Boolean} true if `value` is an array, false otherwise
  2282. * @api public
  2283. */
  2284. is.array = function (value) {
  2285. return '[object Array]' === toString.call(value);
  2286. };
  2287. /**
  2288. * is.arguments.empty
  2289. * Test if `value` is an empty arguments object.
  2290. *
  2291. * @param {Mixed} value value to test
  2292. * @return {Boolean} true if `value` is an empty arguments object, false otherwise
  2293. * @api public
  2294. */
  2295. is.arguments.empty = function (value) {
  2296. return is.arguments(value) && value.length === 0;
  2297. };
  2298. /**
  2299. * is.array.empty
  2300. * Test if `value` is an empty array.
  2301. *
  2302. * @param {Mixed} value value to test
  2303. * @return {Boolean} true if `value` is an empty array, false otherwise
  2304. * @api public
  2305. */
  2306. is.array.empty = function (value) {
  2307. return is.array(value) && value.length === 0;
  2308. };
  2309. /**
  2310. * is.arraylike
  2311. * Test if `value` is an arraylike object.
  2312. *
  2313. * @param {Mixed} value value to test
  2314. * @return {Boolean} true if `value` is an arguments object, false otherwise
  2315. * @api public
  2316. */
  2317. is.arraylike = function (value) {
  2318. return !!value && !is.boolean(value)
  2319. && owns.call(value, 'length')
  2320. && isFinite(value.length)
  2321. && is.number(value.length)
  2322. && value.length >= 0;
  2323. };
  2324. /**
  2325. * Test boolean.
  2326. */
  2327. /**
  2328. * is.boolean
  2329. * Test if `value` is a boolean.
  2330. *
  2331. * @param {Mixed} value value to test
  2332. * @return {Boolean} true if `value` is a boolean, false otherwise
  2333. * @api public
  2334. */
  2335. is.boolean = function (value) {
  2336. return '[object Boolean]' === toString.call(value);
  2337. };
  2338. /**
  2339. * is.false
  2340. * Test if `value` is false.
  2341. *
  2342. * @param {Mixed} value value to test
  2343. * @return {Boolean} true if `value` is false, false otherwise
  2344. * @api public
  2345. */
  2346. is['false'] = function (value) {
  2347. return is.boolean(value) && (value === false || value.valueOf() === false);
  2348. };
  2349. /**
  2350. * is.true
  2351. * Test if `value` is true.
  2352. *
  2353. * @param {Mixed} value value to test
  2354. * @return {Boolean} true if `value` is true, false otherwise
  2355. * @api public
  2356. */
  2357. is['true'] = function (value) {
  2358. return is.boolean(value) && (value === true || value.valueOf() === true);
  2359. };
  2360. /**
  2361. * Test date.
  2362. */
  2363. /**
  2364. * is.date
  2365. * Test if `value` is a date.
  2366. *
  2367. * @param {Mixed} value value to test
  2368. * @return {Boolean} true if `value` is a date, false otherwise
  2369. * @api public
  2370. */
  2371. is.date = function (value) {
  2372. return '[object Date]' === toString.call(value);
  2373. };
  2374. /**
  2375. * Test element.
  2376. */
  2377. /**
  2378. * is.element
  2379. * Test if `value` is an html element.
  2380. *
  2381. * @param {Mixed} value value to test
  2382. * @return {Boolean} true if `value` is an HTML Element, false otherwise
  2383. * @api public
  2384. */
  2385. is.element = function (value) {
  2386. return value !== undefined
  2387. && typeof HTMLElement !== 'undefined'
  2388. && value instanceof HTMLElement
  2389. && value.nodeType === 1;
  2390. };
  2391. /**
  2392. * Test error.
  2393. */
  2394. /**
  2395. * is.error
  2396. * Test if `value` is an error object.
  2397. *
  2398. * @param {Mixed} value value to test
  2399. * @return {Boolean} true if `value` is an error object, false otherwise
  2400. * @api public
  2401. */
  2402. is.error = function (value) {
  2403. return '[object Error]' === toString.call(value);
  2404. };
  2405. /**
  2406. * Test function.
  2407. */
  2408. /**
  2409. * is.fn / is.function (deprecated)
  2410. * Test if `value` is a function.
  2411. *
  2412. * @param {Mixed} value value to test
  2413. * @return {Boolean} true if `value` is a function, false otherwise
  2414. * @api public
  2415. */
  2416. is.fn = is['function'] = function (value) {
  2417. var isAlert = typeof window !== 'undefined' && value === window.alert;
  2418. return isAlert || '[object Function]' === toString.call(value);
  2419. };
  2420. /**
  2421. * Test number.
  2422. */
  2423. /**
  2424. * is.number
  2425. * Test if `value` is a number.
  2426. *
  2427. * @param {Mixed} value value to test
  2428. * @return {Boolean} true if `value` is a number, false otherwise
  2429. * @api public
  2430. */
  2431. is.number = function (value) {
  2432. return '[object Number]' === toString.call(value);
  2433. };
  2434. /**
  2435. * is.infinite
  2436. * Test if `value` is positive or negative infinity.
  2437. *
  2438. * @param {Mixed} value value to test
  2439. * @return {Boolean} true if `value` is positive or negative Infinity, false otherwise
  2440. * @api public
  2441. */
  2442. is.infinite = function (value) {
  2443. return value === Infinity || value === -Infinity;
  2444. };
  2445. /**
  2446. * is.decimal
  2447. * Test if `value` is a decimal number.
  2448. *
  2449. * @param {Mixed} value value to test
  2450. * @return {Boolean} true if `value` is a decimal number, false otherwise
  2451. * @api public
  2452. */
  2453. is.decimal = function (value) {
  2454. return is.number(value) && !isActualNaN(value) && value % 1 !== 0;
  2455. };
  2456. /**
  2457. * is.divisibleBy
  2458. * Test if `value` is divisible by `n`.
  2459. *
  2460. * @param {Number} value value to test
  2461. * @param {Number} n dividend
  2462. * @return {Boolean} true if `value` is divisible by `n`, false otherwise
  2463. * @api public
  2464. */
  2465. is.divisibleBy = function (value, n) {
  2466. var isDividendInfinite = is.infinite(value);
  2467. var isDivisorInfinite = is.infinite(n);
  2468. var isNonZeroNumber = is.number(value) && !isActualNaN(value) && is.number(n) && !isActualNaN(n) && n !== 0;
  2469. return isDividendInfinite || isDivisorInfinite || (isNonZeroNumber && value % n === 0);
  2470. };
  2471. /**
  2472. * is.int
  2473. * Test if `value` is an integer.
  2474. *
  2475. * @param value to test
  2476. * @return {Boolean} true if `value` is an integer, false otherwise
  2477. * @api public
  2478. */
  2479. is.int = function (value) {
  2480. return is.number(value) && !isActualNaN(value) && value % 1 === 0;
  2481. };
  2482. /**
  2483. * is.maximum
  2484. * Test if `value` is greater than 'others' values.
  2485. *
  2486. * @param {Number} value value to test
  2487. * @param {Array} others values to compare with
  2488. * @return {Boolean} true if `value` is greater than `others` values
  2489. * @api public
  2490. */
  2491. is.maximum = function (value, others) {
  2492. if (isActualNaN(value)) {
  2493. throw new TypeError('NaN is not a valid value');
  2494. } else if (!is.arraylike(others)) {
  2495. throw new TypeError('second argument must be array-like');
  2496. }
  2497. var len = others.length;
  2498. while (--len >= 0) {
  2499. if (value < others[len]) {
  2500. return false;
  2501. }
  2502. }
  2503. return true;
  2504. };
  2505. /**
  2506. * is.minimum
  2507. * Test if `value` is less than `others` values.
  2508. *
  2509. * @param {Number} value value to test
  2510. * @param {Array} others values to compare with
  2511. * @return {Boolean} true if `value` is less than `others` values
  2512. * @api public
  2513. */
  2514. is.minimum = function (value, others) {
  2515. if (isActualNaN(value)) {
  2516. throw new TypeError('NaN is not a valid value');
  2517. } else if (!is.arraylike(others)) {
  2518. throw new TypeError('second argument must be array-like');
  2519. }
  2520. var len = others.length;
  2521. while (--len >= 0) {
  2522. if (value > others[len]) {
  2523. return false;
  2524. }
  2525. }
  2526. return true;
  2527. };
  2528. /**
  2529. * is.nan
  2530. * Test if `value` is not a number.
  2531. *
  2532. * @param {Mixed} value value to test
  2533. * @return {Boolean} true if `value` is not a number, false otherwise
  2534. * @api public
  2535. */
  2536. is.nan = function (value) {
  2537. return !is.number(value) || value !== value;
  2538. };
  2539. /**
  2540. * is.even
  2541. * Test if `value` is an even number.
  2542. *
  2543. * @param {Number} value value to test
  2544. * @return {Boolean} true if `value` is an even number, false otherwise
  2545. * @api public
  2546. */
  2547. is.even = function (value) {
  2548. return is.infinite(value) || (is.number(value) && value === value && value % 2 === 0);
  2549. };
  2550. /**
  2551. * is.odd
  2552. * Test if `value` is an odd number.
  2553. *
  2554. * @param {Number} value value to test
  2555. * @return {Boolean} true if `value` is an odd number, false otherwise
  2556. * @api public
  2557. */
  2558. is.odd = function (value) {
  2559. return is.infinite(value) || (is.number(value) && value === value && value % 2 !== 0);
  2560. };
  2561. /**
  2562. * is.ge
  2563. * Test if `value` is greater than or equal to `other`.
  2564. *
  2565. * @param {Number} value value to test
  2566. * @param {Number} other value to compare with
  2567. * @return {Boolean}
  2568. * @api public
  2569. */
  2570. is.ge = function (value, other) {
  2571. if (isActualNaN(value) || isActualNaN(other)) {
  2572. throw new TypeError('NaN is not a valid value');
  2573. }
  2574. return !is.infinite(value) && !is.infinite(other) && value >= other;
  2575. };
  2576. /**
  2577. * is.gt
  2578. * Test if `value` is greater than `other`.
  2579. *
  2580. * @param {Number} value value to test
  2581. * @param {Number} other value to compare with
  2582. * @return {Boolean}
  2583. * @api public
  2584. */
  2585. is.gt = function (value, other) {
  2586. if (isActualNaN(value) || isActualNaN(other)) {
  2587. throw new TypeError('NaN is not a valid value');
  2588. }
  2589. return !is.infinite(value) && !is.infinite(other) && value > other;
  2590. };
  2591. /**
  2592. * is.le
  2593. * Test if `value` is less than or equal to `other`.
  2594. *
  2595. * @param {Number} value value to test
  2596. * @param {Number} other value to compare with
  2597. * @return {Boolean} if 'value' is less than or equal to 'other'
  2598. * @api public
  2599. */
  2600. is.le = function (value, other) {
  2601. if (isActualNaN(value) || isActualNaN(other)) {
  2602. throw new TypeError('NaN is not a valid value');
  2603. }
  2604. return !is.infinite(value) && !is.infinite(other) && value <= other;
  2605. };
  2606. /**
  2607. * is.lt
  2608. * Test if `value` is less than `other`.
  2609. *
  2610. * @param {Number} value value to test
  2611. * @param {Number} other value to compare with
  2612. * @return {Boolean} if `value` is less than `other`
  2613. * @api public
  2614. */
  2615. is.lt = function (value, other) {
  2616. if (isActualNaN(value) || isActualNaN(other)) {
  2617. throw new TypeError('NaN is not a valid value');
  2618. }
  2619. return !is.infinite(value) && !is.infinite(other) && value < other;
  2620. };
  2621. /**
  2622. * is.within
  2623. * Test if `value` is within `start` and `finish`.
  2624. *
  2625. * @param {Number} value value to test
  2626. * @param {Number} start lower bound
  2627. * @param {Number} finish upper bound
  2628. * @return {Boolean} true if 'value' is is within 'start' and 'finish'
  2629. * @api public
  2630. */
  2631. is.within = function (value, start, finish) {
  2632. if (isActualNaN(value) || isActualNaN(start) || isActualNaN(finish)) {
  2633. throw new TypeError('NaN is not a valid value');
  2634. } else if (!is.number(value) || !is.number(start) || !is.number(finish)) {
  2635. throw new TypeError('all arguments must be numbers');
  2636. }
  2637. var isAnyInfinite = is.infinite(value) || is.infinite(start) || is.infinite(finish);
  2638. return isAnyInfinite || (value >= start && value <= finish);
  2639. };
  2640. /**
  2641. * Test object.
  2642. */
  2643. /**
  2644. * is.object
  2645. * Test if `value` is an object.
  2646. *
  2647. * @param {Mixed} value value to test
  2648. * @return {Boolean} true if `value` is an object, false otherwise
  2649. * @api public
  2650. */
  2651. is.object = function (value) {
  2652. return value && '[object Object]' === toString.call(value);
  2653. };
  2654. /**
  2655. * is.hash
  2656. * Test if `value` is a hash - a plain object literal.
  2657. *
  2658. * @param {Mixed} value value to test
  2659. * @return {Boolean} true if `value` is a hash, false otherwise
  2660. * @api public
  2661. */
  2662. is.hash = function (value) {
  2663. return is.object(value) && value.constructor === Object && !value.nodeType && !value.setInterval;
  2664. };
  2665. /**
  2666. * Test regexp.
  2667. */
  2668. /**
  2669. * is.regexp
  2670. * Test if `value` is a regular expression.
  2671. *
  2672. * @param {Mixed} value value to test
  2673. * @return {Boolean} true if `value` is a regexp, false otherwise
  2674. * @api public
  2675. */
  2676. is.regexp = function (value) {
  2677. return '[object RegExp]' === toString.call(value);
  2678. };
  2679. /**
  2680. * Test string.
  2681. */
  2682. /**
  2683. * is.string
  2684. * Test if `value` is a string.
  2685. *
  2686. * @param {Mixed} value value to test
  2687. * @return {Boolean} true if 'value' is a string, false otherwise
  2688. * @api public
  2689. */
  2690. is.string = function (value) {
  2691. return '[object String]' === toString.call(value);
  2692. };
  2693. },{}],10:[function(require,module,exports){
  2694. var hasOwn = Object.prototype.hasOwnProperty;
  2695. var toString = Object.prototype.toString;
  2696. module.exports = function forEach (obj, fn, ctx) {
  2697. if (toString.call(fn) !== '[object Function]') {
  2698. throw new TypeError('iterator must be a function');
  2699. }
  2700. var l = obj.length;
  2701. if (l === +l) {
  2702. for (var i = 0; i < l; i++) {
  2703. fn.call(ctx, obj[i], i, obj);
  2704. }
  2705. } else {
  2706. for (var k in obj) {
  2707. if (hasOwn.call(obj, k)) {
  2708. fn.call(ctx, obj[k], k, obj);
  2709. }
  2710. }
  2711. }
  2712. };
  2713. },{}]},{},[1])(1)
  2714. });
  2715. ;