encrypt.js 70 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775
  1. /**
  2. layui hash extend
  3. **/
  4. layui.define(function (exports) {
  5. /**
  6. * jshashes - https://github.com/h2non/jshashes
  7. * Released under the "New BSD" license
  8. *
  9. * Algorithms specification:
  10. *
  11. * MD5 - http://www.ietf.org/rfc/rfc1321.txt
  12. * RIPEMD-160 - http://homes.esat.kuleuven.be/~bosselae/ripemd160.html
  13. * SHA1 - http://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf
  14. * SHA256 - http://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf
  15. * SHA512 - http://csrc.nist.gov/publications/fips/fips180-4/fips-180-4.pdf
  16. * HMAC - http://www.ietf.org/rfc/rfc2104.txt
  17. */
  18. var pearHash;
  19. function utf8Encode(str) {
  20. var x, y, output = '',
  21. i = -1,
  22. l;
  23. if (str && str.length) {
  24. l = str.length;
  25. while ((i += 1) < l) {
  26. /* Decode utf-16 surrogate pairs */
  27. x = str.charCodeAt(i);
  28. y = i + 1 < l ? str.charCodeAt(i + 1) : 0;
  29. if (0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF) {
  30. x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);
  31. i += 1;
  32. }
  33. /* Encode output as utf-8 */
  34. if (x <= 0x7F) {
  35. output += String.fromCharCode(x);
  36. } else if (x <= 0x7FF) {
  37. output += String.fromCharCode(0xC0 | ((x >>> 6) & 0x1F),
  38. 0x80 | (x & 0x3F));
  39. } else if (x <= 0xFFFF) {
  40. output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F),
  41. 0x80 | ((x >>> 6) & 0x3F),
  42. 0x80 | (x & 0x3F));
  43. } else if (x <= 0x1FFFFF) {
  44. output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07),
  45. 0x80 | ((x >>> 12) & 0x3F),
  46. 0x80 | ((x >>> 6) & 0x3F),
  47. 0x80 | (x & 0x3F));
  48. }
  49. }
  50. }
  51. return output;
  52. }
  53. function utf8Decode(str) {
  54. var i, ac, c1, c2, c3, arr = [],
  55. l;
  56. i = ac = c1 = c2 = c3 = 0;
  57. if (str && str.length) {
  58. l = str.length;
  59. str += '';
  60. while (i < l) {
  61. c1 = str.charCodeAt(i);
  62. ac += 1;
  63. if (c1 < 128) {
  64. arr[ac] = String.fromCharCode(c1);
  65. i += 1;
  66. } else if (c1 > 191 && c1 < 224) {
  67. c2 = str.charCodeAt(i + 1);
  68. arr[ac] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
  69. i += 2;
  70. } else {
  71. c2 = str.charCodeAt(i + 1);
  72. c3 = str.charCodeAt(i + 2);
  73. arr[ac] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
  74. i += 3;
  75. }
  76. }
  77. }
  78. return arr.join('');
  79. }
  80. /**
  81. * Add integers, wrapping at 2^32. This uses 16-bit operations internally
  82. * to work around bugs in some JS interpreters.
  83. */
  84. function safe_add(x, y) {
  85. var lsw = (x & 0xFFFF) + (y & 0xFFFF),
  86. msw = (x >> 16) + (y >> 16) + (lsw >> 16);
  87. return (msw << 16) | (lsw & 0xFFFF);
  88. }
  89. /**
  90. * Bitwise rotate a 32-bit number to the left.
  91. */
  92. function bit_rol(num, cnt) {
  93. return (num << cnt) | (num >>> (32 - cnt));
  94. }
  95. /**
  96. * Convert a raw string to a hex string
  97. */
  98. function rstr2hex(input, hexcase) {
  99. var hex_tab = hexcase ? '0123456789ABCDEF' : '0123456789abcdef',
  100. output = '',
  101. x, i = 0,
  102. l = input.length;
  103. for (; i < l; i += 1) {
  104. x = input.charCodeAt(i);
  105. output += hex_tab.charAt((x >>> 4) & 0x0F) + hex_tab.charAt(x & 0x0F);
  106. }
  107. return output;
  108. }
  109. /**
  110. * Encode a string as utf-16
  111. */
  112. function str2rstr_utf16le(input) {
  113. var i, l = input.length,
  114. output = '';
  115. for (i = 0; i < l; i += 1) {
  116. output += String.fromCharCode(input.charCodeAt(i) & 0xFF, (input.charCodeAt(i) >>> 8) & 0xFF);
  117. }
  118. return output;
  119. }
  120. function str2rstr_utf16be(input) {
  121. var i, l = input.length,
  122. output = '';
  123. for (i = 0; i < l; i += 1) {
  124. output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF, input.charCodeAt(i) & 0xFF);
  125. }
  126. return output;
  127. }
  128. /**
  129. * Convert an array of big-endian words to a string
  130. */
  131. function binb2rstr(input) {
  132. var i, l = input.length * 32,
  133. output = '';
  134. for (i = 0; i < l; i += 8) {
  135. output += String.fromCharCode((input[i >> 5] >>> (24 - i % 32)) & 0xFF);
  136. }
  137. return output;
  138. }
  139. /**
  140. * Convert an array of little-endian words to a string
  141. */
  142. function binl2rstr(input) {
  143. var i, l = input.length * 32,
  144. output = '';
  145. for (i = 0; i < l; i += 8) {
  146. output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xFF);
  147. }
  148. return output;
  149. }
  150. /**
  151. * Convert a raw string to an array of little-endian words
  152. * Characters >255 have their high-byte silently ignored.
  153. */
  154. function rstr2binl(input) {
  155. var i, l = input.length * 8,
  156. output = Array(input.length >> 2),
  157. lo = output.length;
  158. for (i = 0; i < lo; i += 1) {
  159. output[i] = 0;
  160. }
  161. for (i = 0; i < l; i += 8) {
  162. output[i >> 5] |= (input.charCodeAt(i / 8) & 0xFF) << (i % 32);
  163. }
  164. return output;
  165. }
  166. /**
  167. * Convert a raw string to an array of big-endian words
  168. * Characters >255 have their high-byte silently ignored.
  169. */
  170. function rstr2binb(input) {
  171. var i, l = input.length * 8,
  172. output = Array(input.length >> 2),
  173. lo = output.length;
  174. for (i = 0; i < lo; i += 1) {
  175. output[i] = 0;
  176. }
  177. for (i = 0; i < l; i += 8) {
  178. output[i >> 5] |= (input.charCodeAt(i / 8) & 0xFF) << (24 - i % 32);
  179. }
  180. return output;
  181. }
  182. /**
  183. * Convert a raw string to an arbitrary string encoding
  184. */
  185. function rstr2any(input, encoding) {
  186. var divisor = encoding.length,
  187. remainders = Array(),
  188. i, q, x, ld, quotient, dividend, output, full_length;
  189. /* Convert to an array of 16-bit big-endian values, forming the dividend */
  190. dividend = Array(Math.ceil(input.length / 2));
  191. ld = dividend.length;
  192. for (i = 0; i < ld; i += 1) {
  193. dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
  194. }
  195. /**
  196. * Repeatedly perform a long division. The binary array forms the dividend,
  197. * the length of the encoding is the divisor. Once computed, the quotient
  198. * forms the dividend for the next step. We stop when the dividend is zerHashes.
  199. * All remainders are stored for later use.
  200. */
  201. while (dividend.length > 0) {
  202. quotient = Array();
  203. x = 0;
  204. for (i = 0; i < dividend.length; i += 1) {
  205. x = (x << 16) + dividend[i];
  206. q = Math.floor(x / divisor);
  207. x -= q * divisor;
  208. if (quotient.length > 0 || q > 0) {
  209. quotient[quotient.length] = q;
  210. }
  211. }
  212. remainders[remainders.length] = x;
  213. dividend = quotient;
  214. }
  215. /* Convert the remainders to the output string */
  216. output = '';
  217. for (i = remainders.length - 1; i >= 0; i--) {
  218. output += encoding.charAt(remainders[i]);
  219. }
  220. /* Append leading zero equivalents */
  221. full_length = Math.ceil(input.length * 8 / (Math.log(encoding.length) / Math.log(2)));
  222. for (i = output.length; i < full_length; i += 1) {
  223. output = encoding[0] + output;
  224. }
  225. return output;
  226. }
  227. /**
  228. * Convert a raw string to a base-64 string
  229. */
  230. function rstr2b64(input, b64pad) {
  231. var tab = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
  232. output = '',
  233. len = input.length,
  234. i, j, triplet;
  235. b64pad = b64pad || '=';
  236. for (i = 0; i < len; i += 3) {
  237. triplet = (input.charCodeAt(i) << 16) | (i + 1 < len ? input.charCodeAt(i + 1) << 8 : 0) | (i + 2 < len ? input.charCodeAt(i + 2) : 0);
  238. for (j = 0; j < 4; j += 1) {
  239. if (i * 8 + j * 6 > input.length * 8) {
  240. output += b64pad;
  241. } else {
  242. output += tab.charAt((triplet >>> 6 * (3 - j)) & 0x3F);
  243. }
  244. }
  245. }
  246. return output;
  247. }
  248. pearHash = {
  249. /**
  250. * @property {String} version
  251. * @readonly
  252. */
  253. VERSION: '1.0.6',
  254. /**
  255. * @member Hashes
  256. * @class Base64
  257. * @constructor
  258. */
  259. Base64: function () {
  260. // private properties
  261. var tab = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/',
  262. pad = '=', // default pad according with the RFC standard
  263. url = false, // URL encoding support @todo
  264. utf8 = true; // by default enable UTF-8 support encoding
  265. // public method for encoding
  266. this.encode = function (input) {
  267. var i, j, triplet,
  268. output = '',
  269. len = input.length;
  270. pad = pad || '=';
  271. input = (utf8) ? utf8Encode(input) : input;
  272. for (i = 0; i < len; i += 3) {
  273. triplet = (input.charCodeAt(i) << 16) | (i + 1 < len ? input.charCodeAt(i + 1) << 8 : 0) | (i + 2 < len ? input.charCodeAt(i + 2) : 0);
  274. for (j = 0; j < 4; j += 1) {
  275. if (i * 8 + j * 6 > len * 8) {
  276. output += pad;
  277. } else {
  278. output += tab.charAt((triplet >>> 6 * (3 - j)) & 0x3F);
  279. }
  280. }
  281. }
  282. return output;
  283. };
  284. // public method for decoding
  285. this.decode = function (input) {
  286. // var b64 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/=';
  287. var i, o1, o2, o3, h1, h2, h3, h4, bits, ac,
  288. dec = '',
  289. arr = [];
  290. if (!input) {
  291. return input;
  292. }
  293. i = ac = 0;
  294. input = input.replace(new RegExp('\\' + pad, 'gi'), ''); // use '='
  295. //input += '';
  296. do { // unpack four hexets into three octets using index points in b64
  297. h1 = tab.indexOf(input.charAt(i += 1));
  298. h2 = tab.indexOf(input.charAt(i += 1));
  299. h3 = tab.indexOf(input.charAt(i += 1));
  300. h4 = tab.indexOf(input.charAt(i += 1));
  301. bits = h1 << 18 | h2 << 12 | h3 << 6 | h4;
  302. o1 = bits >> 16 & 0xff;
  303. o2 = bits >> 8 & 0xff;
  304. o3 = bits & 0xff;
  305. ac += 1;
  306. if (h3 === 64) {
  307. arr[ac] = String.fromCharCode(o1);
  308. } else if (h4 === 64) {
  309. arr[ac] = String.fromCharCode(o1, o2);
  310. } else {
  311. arr[ac] = String.fromCharCode(o1, o2, o3);
  312. }
  313. } while (i < input.length);
  314. dec = arr.join('');
  315. dec = (utf8) ? utf8Decode(dec) : dec;
  316. return dec;
  317. };
  318. // set custom pad string
  319. this.setPad = function (str) {
  320. pad = str || pad;
  321. return this;
  322. };
  323. // set custom tab string characters
  324. this.setTab = function (str) {
  325. tab = str || tab;
  326. return this;
  327. };
  328. this.setUTF8 = function (bool) {
  329. if (typeof bool === 'boolean') {
  330. utf8 = bool;
  331. }
  332. return this;
  333. };
  334. },
  335. /**
  336. * CRC-32 calculation
  337. * @member Hashes
  338. * @method CRC32
  339. * @static
  340. * @param {String} str Input String
  341. * @return {String}
  342. */
  343. CRC32: function (str) {
  344. var crc = 0,
  345. x = 0,
  346. y = 0,
  347. table, i, iTop;
  348. str = utf8Encode(str);
  349. table = [
  350. '00000000 77073096 EE0E612C 990951BA 076DC419 706AF48F E963A535 9E6495A3 0EDB8832 ',
  351. '79DCB8A4 E0D5E91E 97D2D988 09B64C2B 7EB17CBD E7B82D07 90BF1D91 1DB71064 6AB020F2 F3B97148 ',
  352. '84BE41DE 1ADAD47D 6DDDE4EB F4D4B551 83D385C7 136C9856 646BA8C0 FD62F97A 8A65C9EC 14015C4F ',
  353. '63066CD9 FA0F3D63 8D080DF5 3B6E20C8 4C69105E D56041E4 A2677172 3C03E4D1 4B04D447 D20D85FD ',
  354. 'A50AB56B 35B5A8FA 42B2986C DBBBC9D6 ACBCF940 32D86CE3 45DF5C75 DCD60DCF ABD13D59 26D930AC ',
  355. '51DE003A C8D75180 BFD06116 21B4F4B5 56B3C423 CFBA9599 B8BDA50F 2802B89E 5F058808 C60CD9B2 ',
  356. 'B10BE924 2F6F7C87 58684C11 C1611DAB B6662D3D 76DC4190 01DB7106 98D220BC EFD5102A 71B18589 ',
  357. '06B6B51F 9FBFE4A5 E8B8D433 7807C9A2 0F00F934 9609A88E E10E9818 7F6A0DBB 086D3D2D 91646C97 ',
  358. 'E6635C01 6B6B51F4 1C6C6162 856530D8 F262004E 6C0695ED 1B01A57B 8208F4C1 F50FC457 65B0D9C6 ',
  359. '12B7E950 8BBEB8EA FCB9887C 62DD1DDF 15DA2D49 8CD37CF3 FBD44C65 4DB26158 3AB551CE A3BC0074 ',
  360. 'D4BB30E2 4ADFA541 3DD895D7 A4D1C46D D3D6F4FB 4369E96A 346ED9FC AD678846 DA60B8D0 44042D73 ',
  361. '33031DE5 AA0A4C5F DD0D7CC9 5005713C 270241AA BE0B1010 C90C2086 5768B525 206F85B3 B966D409 ',
  362. 'CE61E49F 5EDEF90E 29D9C998 B0D09822 C7D7A8B4 59B33D17 2EB40D81 B7BD5C3B C0BA6CAD EDB88320 ',
  363. '9ABFB3B6 03B6E20C 74B1D29A EAD54739 9DD277AF 04DB2615 73DC1683 E3630B12 94643B84 0D6D6A3E ',
  364. '7A6A5AA8 E40ECF0B 9309FF9D 0A00AE27 7D079EB1 F00F9344 8708A3D2 1E01F268 6906C2FE F762575D ',
  365. '806567CB 196C3671 6E6B06E7 FED41B76 89D32BE0 10DA7A5A 67DD4ACC F9B9DF6F 8EBEEFF9 17B7BE43 ',
  366. '60B08ED5 D6D6A3E8 A1D1937E 38D8C2C4 4FDFF252 D1BB67F1 A6BC5767 3FB506DD 48B2364B D80D2BDA ',
  367. 'AF0A1B4C 36034AF6 41047A60 DF60EFC3 A867DF55 316E8EEF 4669BE79 CB61B38C BC66831A 256FD2A0 ',
  368. '5268E236 CC0C7795 BB0B4703 220216B9 5505262F C5BA3BBE B2BD0B28 2BB45A92 5CB36A04 C2D7FFA7 ',
  369. 'B5D0CF31 2CD99E8B 5BDEAE1D 9B64C2B0 EC63F226 756AA39C 026D930A 9C0906A9 EB0E363F 72076785 ',
  370. '05005713 95BF4A82 E2B87A14 7BB12BAE 0CB61B38 92D28E9B E5D5BE0D 7CDCEFB7 0BDBDF21 86D3D2D4 ',
  371. 'F1D4E242 68DDB3F8 1FDA836E 81BE16CD F6B9265B 6FB077E1 18B74777 88085AE6 FF0F6A70 66063BCA ',
  372. '11010B5C 8F659EFF F862AE69 616BFFD3 166CCF45 A00AE278 D70DD2EE 4E048354 3903B3C2 A7672661 ',
  373. 'D06016F7 4969474D 3E6E77DB AED16A4A D9D65ADC 40DF0B66 37D83BF0 A9BCAE53 DEBB9EC5 47B2CF7F ',
  374. '30B5FFE9 BDBDF21C CABAC28A 53B39330 24B4A3A6 BAD03605 CDD70693 54DE5729 23D967BF B3667A2E ',
  375. 'C4614AB8 5D681B02 2A6F2B94 B40BBE37 C30C8EA1 5A05DF1B 2D02EF8D'
  376. ].join('');
  377. crc = crc ^ (-1);
  378. for (i = 0, iTop = str.length; i < iTop; i += 1) {
  379. y = (crc ^ str.charCodeAt(i)) & 0xFF;
  380. x = '0x' + table.substr(y * 9, 8);
  381. crc = (crc >>> 8) ^ x;
  382. }
  383. // always return a positive number (that's what >>> 0 does)
  384. return (crc ^ (-1)) >>> 0;
  385. },
  386. /**
  387. * @member Hashes
  388. * @class MD5
  389. * @constructor
  390. * @param {Object} [config]
  391. *
  392. * A JavaScript implementation of the RSA Data Security, Inc. MD5 Message
  393. * Digest Algorithm, as defined in RFC 1321.
  394. * Version 2.2 Copyright (C) Paul Johnston 1999 - 2009
  395. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  396. * See <http://pajhome.org.uk/crypt/md5> for more infHashes.
  397. */
  398. MD5: function (options) {
  399. /**
  400. * Private config properties. You may need to tweak these to be compatible with
  401. * the server-side, but the defaults work in most cases.
  402. * See {@link Hashes.MD5#method-setUpperCase} and {@link Hashes.SHA1#method-setUpperCase}
  403. */
  404. var hexcase = (options && typeof options.uppercase === 'boolean') ? options.uppercase : false, // hexadecimal output case format. false - lowercase; true - uppercase
  405. b64pad = (options && typeof options.pad === 'string') ? options.pad : '=', // base-64 pad character. Defaults to '=' for strict RFC compliance
  406. utf8 = (options && typeof options.utf8 === 'boolean') ? options.utf8 : true; // enable/disable utf8 encoding
  407. // privileged (public) methods
  408. this.hex = function (s) {
  409. return rstr2hex(rstr(s, utf8), hexcase);
  410. };
  411. this.b64 = function (s) {
  412. return rstr2b64(rstr(s), b64pad);
  413. };
  414. this.any = function (s, e) {
  415. return rstr2any(rstr(s, utf8), e);
  416. };
  417. this.raw = function (s) {
  418. return rstr(s, utf8);
  419. };
  420. this.hex_hmac = function (k, d) {
  421. return rstr2hex(rstr_hmac(k, d), hexcase);
  422. };
  423. this.b64_hmac = function (k, d) {
  424. return rstr2b64(rstr_hmac(k, d), b64pad);
  425. };
  426. this.any_hmac = function (k, d, e) {
  427. return rstr2any(rstr_hmac(k, d), e);
  428. };
  429. /**
  430. * Perform a simple self-test to see if the VM is working
  431. * @return {String} Hexadecimal hash sample
  432. */
  433. this.vm_test = function () {
  434. return hex('abc').toLowerCase() === '900150983cd24fb0d6963f7d28e17f72';
  435. };
  436. /**
  437. * Enable/disable uppercase hexadecimal returned string
  438. * @param {Boolean}
  439. * @return {Object} this
  440. */
  441. this.setUpperCase = function (a) {
  442. if (typeof a === 'boolean') {
  443. hexcase = a;
  444. }
  445. return this;
  446. };
  447. /**
  448. * Defines a base64 pad string
  449. * @param {String} Pad
  450. * @return {Object} this
  451. */
  452. this.setPad = function (a) {
  453. b64pad = a || b64pad;
  454. return this;
  455. };
  456. /**
  457. * Defines a base64 pad string
  458. * @param {Boolean}
  459. * @return {Object} [this]
  460. */
  461. this.setUTF8 = function (a) {
  462. if (typeof a === 'boolean') {
  463. utf8 = a;
  464. }
  465. return this;
  466. };
  467. // private methods
  468. /**
  469. * Calculate the MD5 of a raw string
  470. */
  471. function rstr(s) {
  472. s = (utf8) ? utf8Encode(s) : s;
  473. return binl2rstr(binl(rstr2binl(s), s.length * 8));
  474. }
  475. /**
  476. * Calculate the HMAC-MD5, of a key and some data (raw strings)
  477. */
  478. function rstr_hmac(key, data) {
  479. var bkey, ipad, opad, hash, i;
  480. key = (utf8) ? utf8Encode(key) : key;
  481. data = (utf8) ? utf8Encode(data) : data;
  482. bkey = rstr2binl(key);
  483. if (bkey.length > 16) {
  484. bkey = binl(bkey, key.length * 8);
  485. }
  486. ipad = Array(16), opad = Array(16);
  487. for (i = 0; i < 16; i += 1) {
  488. ipad[i] = bkey[i] ^ 0x36363636;
  489. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  490. }
  491. hash = binl(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
  492. return binl2rstr(binl(opad.concat(hash), 512 + 128));
  493. }
  494. /**
  495. * Calculate the MD5 of an array of little-endian words, and a bit length.
  496. */
  497. function binl(x, len) {
  498. var i, olda, oldb, oldc, oldd,
  499. a = 1732584193,
  500. b = -271733879,
  501. c = -1732584194,
  502. d = 271733878;
  503. /* append padding */
  504. x[len >> 5] |= 0x80 << ((len) % 32);
  505. x[(((len + 64) >>> 9) << 4) + 14] = len;
  506. for (i = 0; i < x.length; i += 16) {
  507. olda = a;
  508. oldb = b;
  509. oldc = c;
  510. oldd = d;
  511. a = md5_ff(a, b, c, d, x[i + 0], 7, -680876936);
  512. d = md5_ff(d, a, b, c, x[i + 1], 12, -389564586);
  513. c = md5_ff(c, d, a, b, x[i + 2], 17, 606105819);
  514. b = md5_ff(b, c, d, a, x[i + 3], 22, -1044525330);
  515. a = md5_ff(a, b, c, d, x[i + 4], 7, -176418897);
  516. d = md5_ff(d, a, b, c, x[i + 5], 12, 1200080426);
  517. c = md5_ff(c, d, a, b, x[i + 6], 17, -1473231341);
  518. b = md5_ff(b, c, d, a, x[i + 7], 22, -45705983);
  519. a = md5_ff(a, b, c, d, x[i + 8], 7, 1770035416);
  520. d = md5_ff(d, a, b, c, x[i + 9], 12, -1958414417);
  521. c = md5_ff(c, d, a, b, x[i + 10], 17, -42063);
  522. b = md5_ff(b, c, d, a, x[i + 11], 22, -1990404162);
  523. a = md5_ff(a, b, c, d, x[i + 12], 7, 1804603682);
  524. d = md5_ff(d, a, b, c, x[i + 13], 12, -40341101);
  525. c = md5_ff(c, d, a, b, x[i + 14], 17, -1502002290);
  526. b = md5_ff(b, c, d, a, x[i + 15], 22, 1236535329);
  527. a = md5_gg(a, b, c, d, x[i + 1], 5, -165796510);
  528. d = md5_gg(d, a, b, c, x[i + 6], 9, -1069501632);
  529. c = md5_gg(c, d, a, b, x[i + 11], 14, 643717713);
  530. b = md5_gg(b, c, d, a, x[i + 0], 20, -373897302);
  531. a = md5_gg(a, b, c, d, x[i + 5], 5, -701558691);
  532. d = md5_gg(d, a, b, c, x[i + 10], 9, 38016083);
  533. c = md5_gg(c, d, a, b, x[i + 15], 14, -660478335);
  534. b = md5_gg(b, c, d, a, x[i + 4], 20, -405537848);
  535. a = md5_gg(a, b, c, d, x[i + 9], 5, 568446438);
  536. d = md5_gg(d, a, b, c, x[i + 14], 9, -1019803690);
  537. c = md5_gg(c, d, a, b, x[i + 3], 14, -187363961);
  538. b = md5_gg(b, c, d, a, x[i + 8], 20, 1163531501);
  539. a = md5_gg(a, b, c, d, x[i + 13], 5, -1444681467);
  540. d = md5_gg(d, a, b, c, x[i + 2], 9, -51403784);
  541. c = md5_gg(c, d, a, b, x[i + 7], 14, 1735328473);
  542. b = md5_gg(b, c, d, a, x[i + 12], 20, -1926607734);
  543. a = md5_hh(a, b, c, d, x[i + 5], 4, -378558);
  544. d = md5_hh(d, a, b, c, x[i + 8], 11, -2022574463);
  545. c = md5_hh(c, d, a, b, x[i + 11], 16, 1839030562);
  546. b = md5_hh(b, c, d, a, x[i + 14], 23, -35309556);
  547. a = md5_hh(a, b, c, d, x[i + 1], 4, -1530992060);
  548. d = md5_hh(d, a, b, c, x[i + 4], 11, 1272893353);
  549. c = md5_hh(c, d, a, b, x[i + 7], 16, -155497632);
  550. b = md5_hh(b, c, d, a, x[i + 10], 23, -1094730640);
  551. a = md5_hh(a, b, c, d, x[i + 13], 4, 681279174);
  552. d = md5_hh(d, a, b, c, x[i + 0], 11, -358537222);
  553. c = md5_hh(c, d, a, b, x[i + 3], 16, -722521979);
  554. b = md5_hh(b, c, d, a, x[i + 6], 23, 76029189);
  555. a = md5_hh(a, b, c, d, x[i + 9], 4, -640364487);
  556. d = md5_hh(d, a, b, c, x[i + 12], 11, -421815835);
  557. c = md5_hh(c, d, a, b, x[i + 15], 16, 530742520);
  558. b = md5_hh(b, c, d, a, x[i + 2], 23, -995338651);
  559. a = md5_ii(a, b, c, d, x[i + 0], 6, -198630844);
  560. d = md5_ii(d, a, b, c, x[i + 7], 10, 1126891415);
  561. c = md5_ii(c, d, a, b, x[i + 14], 15, -1416354905);
  562. b = md5_ii(b, c, d, a, x[i + 5], 21, -57434055);
  563. a = md5_ii(a, b, c, d, x[i + 12], 6, 1700485571);
  564. d = md5_ii(d, a, b, c, x[i + 3], 10, -1894986606);
  565. c = md5_ii(c, d, a, b, x[i + 10], 15, -1051523);
  566. b = md5_ii(b, c, d, a, x[i + 1], 21, -2054922799);
  567. a = md5_ii(a, b, c, d, x[i + 8], 6, 1873313359);
  568. d = md5_ii(d, a, b, c, x[i + 15], 10, -30611744);
  569. c = md5_ii(c, d, a, b, x[i + 6], 15, -1560198380);
  570. b = md5_ii(b, c, d, a, x[i + 13], 21, 1309151649);
  571. a = md5_ii(a, b, c, d, x[i + 4], 6, -145523070);
  572. d = md5_ii(d, a, b, c, x[i + 11], 10, -1120210379);
  573. c = md5_ii(c, d, a, b, x[i + 2], 15, 718787259);
  574. b = md5_ii(b, c, d, a, x[i + 9], 21, -343485551);
  575. a = safe_add(a, olda);
  576. b = safe_add(b, oldb);
  577. c = safe_add(c, oldc);
  578. d = safe_add(d, oldd);
  579. }
  580. return Array(a, b, c, d);
  581. }
  582. /**
  583. * These functions implement the four basic operations the algorithm uses.
  584. */
  585. function md5_cmn(q, a, b, x, s, t) {
  586. return safe_add(bit_rol(safe_add(safe_add(a, q), safe_add(x, t)), s), b);
  587. }
  588. function md5_ff(a, b, c, d, x, s, t) {
  589. return md5_cmn((b & c) | ((~b) & d), a, b, x, s, t);
  590. }
  591. function md5_gg(a, b, c, d, x, s, t) {
  592. return md5_cmn((b & d) | (c & (~d)), a, b, x, s, t);
  593. }
  594. function md5_hh(a, b, c, d, x, s, t) {
  595. return md5_cmn(b ^ c ^ d, a, b, x, s, t);
  596. }
  597. function md5_ii(a, b, c, d, x, s, t) {
  598. return md5_cmn(c ^ (b | (~d)), a, b, x, s, t);
  599. }
  600. },
  601. /**
  602. * @member Hashes
  603. * @class Hashes.SHA1
  604. * @param {Object} [config]
  605. * @constructor
  606. *
  607. * A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined in FIPS 180-1
  608. * Version 2.2 Copyright Paul Johnston 2000 - 2009.
  609. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  610. * See http://pajhome.org.uk/crypt/md5 for details.
  611. */
  612. SHA1: function (options) {
  613. /**
  614. * Private config properties. You may need to tweak these to be compatible with
  615. * the server-side, but the defaults work in most cases.
  616. * See {@link Hashes.MD5#method-setUpperCase} and {@link Hashes.SHA1#method-setUpperCase}
  617. */
  618. var hexcase = (options && typeof options.uppercase === 'boolean') ? options.uppercase : false, // hexadecimal output case format. false - lowercase; true - uppercase
  619. b64pad = (options && typeof options.pad === 'string') ? options.pad : '=', // base-64 pad character. Defaults to '=' for strict RFC compliance
  620. utf8 = (options && typeof options.utf8 === 'boolean') ? options.utf8 : true; // enable/disable utf8 encoding
  621. // public methods
  622. this.hex = function (s) {
  623. return rstr2hex(rstr(s, utf8), hexcase);
  624. };
  625. this.b64 = function (s) {
  626. return rstr2b64(rstr(s, utf8), b64pad);
  627. };
  628. this.any = function (s, e) {
  629. return rstr2any(rstr(s, utf8), e);
  630. };
  631. this.raw = function (s) {
  632. return rstr(s, utf8);
  633. };
  634. this.hex_hmac = function (k, d) {
  635. return rstr2hex(rstr_hmac(k, d));
  636. };
  637. this.b64_hmac = function (k, d) {
  638. return rstr2b64(rstr_hmac(k, d), b64pad);
  639. };
  640. this.any_hmac = function (k, d, e) {
  641. return rstr2any(rstr_hmac(k, d), e);
  642. };
  643. /**
  644. * Perform a simple self-test to see if the VM is working
  645. * @return {String} Hexadecimal hash sample
  646. * @public
  647. */
  648. this.vm_test = function () {
  649. return hex('abc').toLowerCase() === '900150983cd24fb0d6963f7d28e17f72';
  650. };
  651. /**
  652. * @description Enable/disable uppercase hexadecimal returned string
  653. * @param {boolean}
  654. * @return {Object} this
  655. * @public
  656. */
  657. this.setUpperCase = function (a) {
  658. if (typeof a === 'boolean') {
  659. hexcase = a;
  660. }
  661. return this;
  662. };
  663. /**
  664. * @description Defines a base64 pad string
  665. * @param {string} Pad
  666. * @return {Object} this
  667. * @public
  668. */
  669. this.setPad = function (a) {
  670. b64pad = a || b64pad;
  671. return this;
  672. };
  673. /**
  674. * @description Defines a base64 pad string
  675. * @param {boolean}
  676. * @return {Object} this
  677. * @public
  678. */
  679. this.setUTF8 = function (a) {
  680. if (typeof a === 'boolean') {
  681. utf8 = a;
  682. }
  683. return this;
  684. };
  685. // private methods
  686. /**
  687. * Calculate the SHA-512 of a raw string
  688. */
  689. function rstr(s) {
  690. s = (utf8) ? utf8Encode(s) : s;
  691. return binb2rstr(binb(rstr2binb(s), s.length * 8));
  692. }
  693. /**
  694. * Calculate the HMAC-SHA1 of a key and some data (raw strings)
  695. */
  696. function rstr_hmac(key, data) {
  697. var bkey, ipad, opad, i, hash;
  698. key = (utf8) ? utf8Encode(key) : key;
  699. data = (utf8) ? utf8Encode(data) : data;
  700. bkey = rstr2binb(key);
  701. if (bkey.length > 16) {
  702. bkey = binb(bkey, key.length * 8);
  703. }
  704. ipad = Array(16), opad = Array(16);
  705. for (i = 0; i < 16; i += 1) {
  706. ipad[i] = bkey[i] ^ 0x36363636;
  707. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  708. }
  709. hash = binb(ipad.concat(rstr2binb(data)), 512 + data.length * 8);
  710. return binb2rstr(binb(opad.concat(hash), 512 + 160));
  711. }
  712. /**
  713. * Calculate the SHA-1 of an array of big-endian words, and a bit length
  714. */
  715. function binb(x, len) {
  716. var i, j, t, olda, oldb, oldc, oldd, olde,
  717. w = Array(80),
  718. a = 1732584193,
  719. b = -271733879,
  720. c = -1732584194,
  721. d = 271733878,
  722. e = -1009589776;
  723. /* append padding */
  724. x[len >> 5] |= 0x80 << (24 - len % 32);
  725. x[((len + 64 >> 9) << 4) + 15] = len;
  726. for (i = 0; i < x.length; i += 16) {
  727. olda = a;
  728. oldb = b;
  729. oldc = c;
  730. oldd = d;
  731. olde = e;
  732. for (j = 0; j < 80; j += 1) {
  733. if (j < 16) {
  734. w[j] = x[i + j];
  735. } else {
  736. w[j] = bit_rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
  737. }
  738. t = safe_add(safe_add(bit_rol(a, 5), sha1_ft(j, b, c, d)),
  739. safe_add(safe_add(e, w[j]), sha1_kt(j)));
  740. e = d;
  741. d = c;
  742. c = bit_rol(b, 30);
  743. b = a;
  744. a = t;
  745. }
  746. a = safe_add(a, olda);
  747. b = safe_add(b, oldb);
  748. c = safe_add(c, oldc);
  749. d = safe_add(d, oldd);
  750. e = safe_add(e, olde);
  751. }
  752. return Array(a, b, c, d, e);
  753. }
  754. /**
  755. * Perform the appropriate triplet combination function for the current
  756. * iteration
  757. */
  758. function sha1_ft(t, b, c, d) {
  759. if (t < 20) {
  760. return (b & c) | ((~b) & d);
  761. }
  762. if (t < 40) {
  763. return b ^ c ^ d;
  764. }
  765. if (t < 60) {
  766. return (b & c) | (b & d) | (c & d);
  767. }
  768. return b ^ c ^ d;
  769. }
  770. /**
  771. * Determine the appropriate additive constant for the current iteration
  772. */
  773. function sha1_kt(t) {
  774. return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
  775. (t < 60) ? -1894007588 : -899497514;
  776. }
  777. },
  778. /**
  779. * @class Hashes.SHA256
  780. * @param {config}
  781. *
  782. * A JavaScript implementation of the Secure Hash Algorithm, SHA-256, as defined in FIPS 180-2
  783. * Version 2.2 Copyright Angel Marin, Paul Johnston 2000 - 2009.
  784. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  785. * See http://pajhome.org.uk/crypt/md5 for details.
  786. * Also http://anmar.eu.org/projects/jssha2/
  787. */
  788. SHA256: function (options) {
  789. /**
  790. * Private properties configuration variables. You may need to tweak these to be compatible with
  791. * the server-side, but the defaults work in most cases.
  792. * @see this.setUpperCase() method
  793. * @see this.setPad() method
  794. */
  795. var hexcase = (options && typeof options.uppercase === 'boolean') ? options.uppercase : false, // hexadecimal output case format. false - lowercase; true - uppercase */
  796. b64pad = (options && typeof options.pad === 'string') ? options.pad : '=',
  797. /* base-64 pad character. Default '=' for strict RFC compliance */
  798. utf8 = (options && typeof options.utf8 === 'boolean') ? options.utf8 : true,
  799. /* enable/disable utf8 encoding */
  800. sha256_K;
  801. /* privileged (public) methods */
  802. this.hex = function (s) {
  803. return rstr2hex(rstr(s, utf8));
  804. };
  805. this.b64 = function (s) {
  806. return rstr2b64(rstr(s, utf8), b64pad);
  807. };
  808. this.any = function (s, e) {
  809. return rstr2any(rstr(s, utf8), e);
  810. };
  811. this.raw = function (s) {
  812. return rstr(s, utf8);
  813. };
  814. this.hex_hmac = function (k, d) {
  815. return rstr2hex(rstr_hmac(k, d));
  816. };
  817. this.b64_hmac = function (k, d) {
  818. return rstr2b64(rstr_hmac(k, d), b64pad);
  819. };
  820. this.any_hmac = function (k, d, e) {
  821. return rstr2any(rstr_hmac(k, d), e);
  822. };
  823. /**
  824. * Perform a simple self-test to see if the VM is working
  825. * @return {String} Hexadecimal hash sample
  826. * @public
  827. */
  828. this.vm_test = function () {
  829. return hex('abc').toLowerCase() === '900150983cd24fb0d6963f7d28e17f72';
  830. };
  831. /**
  832. * Enable/disable uppercase hexadecimal returned string
  833. * @param {boolean}
  834. * @return {Object} this
  835. * @public
  836. */
  837. this.setUpperCase = function (a) {
  838. if (typeof a === 'boolean') {
  839. hexcase = a;
  840. }
  841. return this;
  842. };
  843. /**
  844. * @description Defines a base64 pad string
  845. * @param {string} Pad
  846. * @return {Object} this
  847. * @public
  848. */
  849. this.setPad = function (a) {
  850. b64pad = a || b64pad;
  851. return this;
  852. };
  853. /**
  854. * Defines a base64 pad string
  855. * @param {boolean}
  856. * @return {Object} this
  857. * @public
  858. */
  859. this.setUTF8 = function (a) {
  860. if (typeof a === 'boolean') {
  861. utf8 = a;
  862. }
  863. return this;
  864. };
  865. // private methods
  866. /**
  867. * Calculate the SHA-512 of a raw string
  868. */
  869. function rstr(s, utf8) {
  870. s = (utf8) ? utf8Encode(s) : s;
  871. return binb2rstr(binb(rstr2binb(s), s.length * 8));
  872. }
  873. /**
  874. * Calculate the HMAC-sha256 of a key and some data (raw strings)
  875. */
  876. function rstr_hmac(key, data) {
  877. key = (utf8) ? utf8Encode(key) : key;
  878. data = (utf8) ? utf8Encode(data) : data;
  879. var hash, i = 0,
  880. bkey = rstr2binb(key),
  881. ipad = Array(16),
  882. opad = Array(16);
  883. if (bkey.length > 16) {
  884. bkey = binb(bkey, key.length * 8);
  885. }
  886. for (; i < 16; i += 1) {
  887. ipad[i] = bkey[i] ^ 0x36363636;
  888. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  889. }
  890. hash = binb(ipad.concat(rstr2binb(data)), 512 + data.length * 8);
  891. return binb2rstr(binb(opad.concat(hash), 512 + 256));
  892. }
  893. /*
  894. * Main sha256 function, with its support functions
  895. */
  896. function sha256_S(X, n) {
  897. return (X >>> n) | (X << (32 - n));
  898. }
  899. function sha256_R(X, n) {
  900. return (X >>> n);
  901. }
  902. function sha256_Ch(x, y, z) {
  903. return ((x & y) ^ ((~x) & z));
  904. }
  905. function sha256_Maj(x, y, z) {
  906. return ((x & y) ^ (x & z) ^ (y & z));
  907. }
  908. function sha256_Sigma0256(x) {
  909. return (sha256_S(x, 2) ^ sha256_S(x, 13) ^ sha256_S(x, 22));
  910. }
  911. function sha256_Sigma1256(x) {
  912. return (sha256_S(x, 6) ^ sha256_S(x, 11) ^ sha256_S(x, 25));
  913. }
  914. function sha256_Gamma0256(x) {
  915. return (sha256_S(x, 7) ^ sha256_S(x, 18) ^ sha256_R(x, 3));
  916. }
  917. function sha256_Gamma1256(x) {
  918. return (sha256_S(x, 17) ^ sha256_S(x, 19) ^ sha256_R(x, 10));
  919. }
  920. function sha256_Sigma0512(x) {
  921. return (sha256_S(x, 28) ^ sha256_S(x, 34) ^ sha256_S(x, 39));
  922. }
  923. function sha256_Sigma1512(x) {
  924. return (sha256_S(x, 14) ^ sha256_S(x, 18) ^ sha256_S(x, 41));
  925. }
  926. function sha256_Gamma0512(x) {
  927. return (sha256_S(x, 1) ^ sha256_S(x, 8) ^ sha256_R(x, 7));
  928. }
  929. function sha256_Gamma1512(x) {
  930. return (sha256_S(x, 19) ^ sha256_S(x, 61) ^ sha256_R(x, 6));
  931. }
  932. sha256_K = [
  933. 1116352408, 1899447441, -1245643825, -373957723, 961987163, 1508970993, -1841331548, -1424204075, -670586216, 310598401, 607225278, 1426881987,
  934. 1925078388, -2132889090, -1680079193, -1046744716, -459576895, -272742522,
  935. 264347078, 604807628, 770255983, 1249150122, 1555081692, 1996064986, -1740746414, -1473132947, -1341970488, -1084653625, -958395405, -710438585,
  936. 113926993, 338241895, 666307205, 773529912, 1294757372, 1396182291,
  937. 1695183700, 1986661051, -2117940946, -1838011259, -1564481375, -1474664885, -1035236496, -949202525, -778901479, -694614492, -200395387, 275423344,
  938. 430227734, 506948616, 659060556, 883997877, 958139571, 1322822218,
  939. 1537002063, 1747873779, 1955562222, 2024104815, -2067236844, -1933114872, -1866530822, -1538233109, -1090935817, -965641998
  940. ];
  941. function binb(m, l) {
  942. var HASH = [1779033703, -1150833019, 1013904242, -1521486534,
  943. 1359893119, -1694144372, 528734635, 1541459225
  944. ];
  945. var W = new Array(64);
  946. var a, b, c, d, e, f, g, h;
  947. var i, j, T1, T2;
  948. /* append padding */
  949. m[l >> 5] |= 0x80 << (24 - l % 32);
  950. m[((l + 64 >> 9) << 4) + 15] = l;
  951. for (i = 0; i < m.length; i += 16) {
  952. a = HASH[0];
  953. b = HASH[1];
  954. c = HASH[2];
  955. d = HASH[3];
  956. e = HASH[4];
  957. f = HASH[5];
  958. g = HASH[6];
  959. h = HASH[7];
  960. for (j = 0; j < 64; j += 1) {
  961. if (j < 16) {
  962. W[j] = m[j + i];
  963. } else {
  964. W[j] = safe_add(safe_add(safe_add(sha256_Gamma1256(W[j - 2]), W[j - 7]),
  965. sha256_Gamma0256(W[j - 15])), W[j - 16]);
  966. }
  967. T1 = safe_add(safe_add(safe_add(safe_add(h, sha256_Sigma1256(e)), sha256_Ch(e, f, g)),
  968. sha256_K[j]), W[j]);
  969. T2 = safe_add(sha256_Sigma0256(a), sha256_Maj(a, b, c));
  970. h = g;
  971. g = f;
  972. f = e;
  973. e = safe_add(d, T1);
  974. d = c;
  975. c = b;
  976. b = a;
  977. a = safe_add(T1, T2);
  978. }
  979. HASH[0] = safe_add(a, HASH[0]);
  980. HASH[1] = safe_add(b, HASH[1]);
  981. HASH[2] = safe_add(c, HASH[2]);
  982. HASH[3] = safe_add(d, HASH[3]);
  983. HASH[4] = safe_add(e, HASH[4]);
  984. HASH[5] = safe_add(f, HASH[5]);
  985. HASH[6] = safe_add(g, HASH[6]);
  986. HASH[7] = safe_add(h, HASH[7]);
  987. }
  988. return HASH;
  989. }
  990. },
  991. /**
  992. * @class Hashes.SHA512
  993. * @param {config}
  994. *
  995. * A JavaScript implementation of the Secure Hash Algorithm, SHA-512, as defined in FIPS 180-2
  996. * Version 2.2 Copyright Anonymous Contributor, Paul Johnston 2000 - 2009.
  997. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  998. * See http://pajhome.org.uk/crypt/md5 for details.
  999. */
  1000. SHA512: function (options) {
  1001. /**
  1002. * Private properties configuration variables. You may need to tweak these to be compatible with
  1003. * the server-side, but the defaults work in most cases.
  1004. * @see this.setUpperCase() method
  1005. * @see this.setPad() method
  1006. */
  1007. var hexcase = (options && typeof options.uppercase === 'boolean') ? options.uppercase : false,
  1008. /* hexadecimal output case format. false - lowercase; true - uppercase */
  1009. b64pad = (options && typeof options.pad === 'string') ? options.pad : '=',
  1010. /* base-64 pad character. Default '=' for strict RFC compliance */
  1011. utf8 = (options && typeof options.utf8 === 'boolean') ? options.utf8 : true,
  1012. /* enable/disable utf8 encoding */
  1013. sha512_k;
  1014. /* privileged (public) methods */
  1015. this.hex = function (s) {
  1016. return rstr2hex(rstr(s));
  1017. };
  1018. this.b64 = function (s) {
  1019. return rstr2b64(rstr(s), b64pad);
  1020. };
  1021. this.any = function (s, e) {
  1022. return rstr2any(rstr(s), e);
  1023. };
  1024. this.raw = function (s) {
  1025. return rstr(s, utf8);
  1026. };
  1027. this.hex_hmac = function (k, d) {
  1028. return rstr2hex(rstr_hmac(k, d));
  1029. };
  1030. this.b64_hmac = function (k, d) {
  1031. return rstr2b64(rstr_hmac(k, d), b64pad);
  1032. };
  1033. this.any_hmac = function (k, d, e) {
  1034. return rstr2any(rstr_hmac(k, d), e);
  1035. };
  1036. /**
  1037. * Perform a simple self-test to see if the VM is working
  1038. * @return {String} Hexadecimal hash sample
  1039. * @public
  1040. */
  1041. this.vm_test = function () {
  1042. return hex('abc').toLowerCase() === '900150983cd24fb0d6963f7d28e17f72';
  1043. };
  1044. /**
  1045. * @description Enable/disable uppercase hexadecimal returned string
  1046. * @param {boolean}
  1047. * @return {Object} this
  1048. * @public
  1049. */
  1050. this.setUpperCase = function (a) {
  1051. if (typeof a === 'boolean') {
  1052. hexcase = a;
  1053. }
  1054. return this;
  1055. };
  1056. /**
  1057. * @description Defines a base64 pad string
  1058. * @param {string} Pad
  1059. * @return {Object} this
  1060. * @public
  1061. */
  1062. this.setPad = function (a) {
  1063. b64pad = a || b64pad;
  1064. return this;
  1065. };
  1066. /**
  1067. * @description Defines a base64 pad string
  1068. * @param {boolean}
  1069. * @return {Object} this
  1070. * @public
  1071. */
  1072. this.setUTF8 = function (a) {
  1073. if (typeof a === 'boolean') {
  1074. utf8 = a;
  1075. }
  1076. return this;
  1077. };
  1078. /* private methods */
  1079. /**
  1080. * Calculate the SHA-512 of a raw string
  1081. */
  1082. function rstr(s) {
  1083. s = (utf8) ? utf8Encode(s) : s;
  1084. return binb2rstr(binb(rstr2binb(s), s.length * 8));
  1085. }
  1086. /*
  1087. * Calculate the HMAC-SHA-512 of a key and some data (raw strings)
  1088. */
  1089. function rstr_hmac(key, data) {
  1090. key = (utf8) ? utf8Encode(key) : key;
  1091. data = (utf8) ? utf8Encode(data) : data;
  1092. var hash, i = 0,
  1093. bkey = rstr2binb(key),
  1094. ipad = Array(32),
  1095. opad = Array(32);
  1096. if (bkey.length > 32) {
  1097. bkey = binb(bkey, key.length * 8);
  1098. }
  1099. for (; i < 32; i += 1) {
  1100. ipad[i] = bkey[i] ^ 0x36363636;
  1101. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  1102. }
  1103. hash = binb(ipad.concat(rstr2binb(data)), 1024 + data.length * 8);
  1104. return binb2rstr(binb(opad.concat(hash), 1024 + 512));
  1105. }
  1106. /**
  1107. * Calculate the SHA-512 of an array of big-endian dwords, and a bit length
  1108. */
  1109. function binb(x, len) {
  1110. var j, i, l,
  1111. W = new Array(80),
  1112. hash = new Array(16),
  1113. //Initial hash values
  1114. H = [
  1115. new int64(0x6a09e667, -205731576),
  1116. new int64(-1150833019, -2067093701),
  1117. new int64(0x3c6ef372, -23791573),
  1118. new int64(-1521486534, 0x5f1d36f1),
  1119. new int64(0x510e527f, -1377402159),
  1120. new int64(-1694144372, 0x2b3e6c1f),
  1121. new int64(0x1f83d9ab, -79577749),
  1122. new int64(0x5be0cd19, 0x137e2179)
  1123. ],
  1124. T1 = new int64(0, 0),
  1125. T2 = new int64(0, 0),
  1126. a = new int64(0, 0),
  1127. b = new int64(0, 0),
  1128. c = new int64(0, 0),
  1129. d = new int64(0, 0),
  1130. e = new int64(0, 0),
  1131. f = new int64(0, 0),
  1132. g = new int64(0, 0),
  1133. h = new int64(0, 0),
  1134. //Temporary variables not specified by the document
  1135. s0 = new int64(0, 0),
  1136. s1 = new int64(0, 0),
  1137. Ch = new int64(0, 0),
  1138. Maj = new int64(0, 0),
  1139. r1 = new int64(0, 0),
  1140. r2 = new int64(0, 0),
  1141. r3 = new int64(0, 0);
  1142. if (sha512_k === undefined) {
  1143. //SHA512 constants
  1144. sha512_k = [
  1145. new int64(0x428a2f98, -685199838), new int64(0x71374491, 0x23ef65cd),
  1146. new int64(-1245643825, -330482897), new int64(-373957723, -2121671748),
  1147. new int64(0x3956c25b, -213338824), new int64(0x59f111f1, -1241133031),
  1148. new int64(-1841331548, -1357295717), new int64(-1424204075, -630357736),
  1149. new int64(-670586216, -1560083902), new int64(0x12835b01, 0x45706fbe),
  1150. new int64(0x243185be, 0x4ee4b28c), new int64(0x550c7dc3, -704662302),
  1151. new int64(0x72be5d74, -226784913), new int64(-2132889090, 0x3b1696b1),
  1152. new int64(-1680079193, 0x25c71235), new int64(-1046744716, -815192428),
  1153. new int64(-459576895, -1628353838), new int64(-272742522, 0x384f25e3),
  1154. new int64(0xfc19dc6, -1953704523), new int64(0x240ca1cc, 0x77ac9c65),
  1155. new int64(0x2de92c6f, 0x592b0275), new int64(0x4a7484aa, 0x6ea6e483),
  1156. new int64(0x5cb0a9dc, -1119749164), new int64(0x76f988da, -2096016459),
  1157. new int64(-1740746414, -295247957), new int64(-1473132947, 0x2db43210),
  1158. new int64(-1341970488, -1728372417), new int64(-1084653625, -1091629340),
  1159. new int64(-958395405, 0x3da88fc2), new int64(-710438585, -1828018395),
  1160. new int64(0x6ca6351, -536640913), new int64(0x14292967, 0xa0e6e70),
  1161. new int64(0x27b70a85, 0x46d22ffc), new int64(0x2e1b2138, 0x5c26c926),
  1162. new int64(0x4d2c6dfc, 0x5ac42aed), new int64(0x53380d13, -1651133473),
  1163. new int64(0x650a7354, -1951439906), new int64(0x766a0abb, 0x3c77b2a8),
  1164. new int64(-2117940946, 0x47edaee6), new int64(-1838011259, 0x1482353b),
  1165. new int64(-1564481375, 0x4cf10364), new int64(-1474664885, -1136513023),
  1166. new int64(-1035236496, -789014639), new int64(-949202525, 0x654be30),
  1167. new int64(-778901479, -688958952), new int64(-694614492, 0x5565a910),
  1168. new int64(-200395387, 0x5771202a), new int64(0x106aa070, 0x32bbd1b8),
  1169. new int64(0x19a4c116, -1194143544), new int64(0x1e376c08, 0x5141ab53),
  1170. new int64(0x2748774c, -544281703), new int64(0x34b0bcb5, -509917016),
  1171. new int64(0x391c0cb3, -976659869), new int64(0x4ed8aa4a, -482243893),
  1172. new int64(0x5b9cca4f, 0x7763e373), new int64(0x682e6ff3, -692930397),
  1173. new int64(0x748f82ee, 0x5defb2fc), new int64(0x78a5636f, 0x43172f60),
  1174. new int64(-2067236844, -1578062990), new int64(-1933114872, 0x1a6439ec),
  1175. new int64(-1866530822, 0x23631e28), new int64(-1538233109, -561857047),
  1176. new int64(-1090935817, -1295615723), new int64(-965641998, -479046869),
  1177. new int64(-903397682, -366583396), new int64(-779700025, 0x21c0c207),
  1178. new int64(-354779690, -840897762), new int64(-176337025, -294727304),
  1179. new int64(0x6f067aa, 0x72176fba), new int64(0xa637dc5, -1563912026),
  1180. new int64(0x113f9804, -1090974290), new int64(0x1b710b35, 0x131c471b),
  1181. new int64(0x28db77f5, 0x23047d84), new int64(0x32caab7b, 0x40c72493),
  1182. new int64(0x3c9ebe0a, 0x15c9bebc), new int64(0x431d67c4, -1676669620),
  1183. new int64(0x4cc5d4be, -885112138), new int64(0x597f299c, -60457430),
  1184. new int64(0x5fcb6fab, 0x3ad6faec), new int64(0x6c44198c, 0x4a475817)
  1185. ];
  1186. }
  1187. for (i = 0; i < 80; i += 1) {
  1188. W[i] = new int64(0, 0);
  1189. }
  1190. // append padding to the source string. The format is described in the FIPS.
  1191. x[len >> 5] |= 0x80 << (24 - (len & 0x1f));
  1192. x[((len + 128 >> 10) << 5) + 31] = len;
  1193. l = x.length;
  1194. for (i = 0; i < l; i += 32) { //32 dwords is the block size
  1195. int64copy(a, H[0]);
  1196. int64copy(b, H[1]);
  1197. int64copy(c, H[2]);
  1198. int64copy(d, H[3]);
  1199. int64copy(e, H[4]);
  1200. int64copy(f, H[5]);
  1201. int64copy(g, H[6]);
  1202. int64copy(h, H[7]);
  1203. for (j = 0; j < 16; j += 1) {
  1204. W[j].h = x[i + 2 * j];
  1205. W[j].l = x[i + 2 * j + 1];
  1206. }
  1207. for (j = 16; j < 80; j += 1) {
  1208. //sigma1
  1209. int64rrot(r1, W[j - 2], 19);
  1210. int64revrrot(r2, W[j - 2], 29);
  1211. int64shr(r3, W[j - 2], 6);
  1212. s1.l = r1.l ^ r2.l ^ r3.l;
  1213. s1.h = r1.h ^ r2.h ^ r3.h;
  1214. //sigma0
  1215. int64rrot(r1, W[j - 15], 1);
  1216. int64rrot(r2, W[j - 15], 8);
  1217. int64shr(r3, W[j - 15], 7);
  1218. s0.l = r1.l ^ r2.l ^ r3.l;
  1219. s0.h = r1.h ^ r2.h ^ r3.h;
  1220. int64add4(W[j], s1, W[j - 7], s0, W[j - 16]);
  1221. }
  1222. for (j = 0; j < 80; j += 1) {
  1223. //Ch
  1224. Ch.l = (e.l & f.l) ^ (~e.l & g.l);
  1225. Ch.h = (e.h & f.h) ^ (~e.h & g.h);
  1226. //Sigma1
  1227. int64rrot(r1, e, 14);
  1228. int64rrot(r2, e, 18);
  1229. int64revrrot(r3, e, 9);
  1230. s1.l = r1.l ^ r2.l ^ r3.l;
  1231. s1.h = r1.h ^ r2.h ^ r3.h;
  1232. //Sigma0
  1233. int64rrot(r1, a, 28);
  1234. int64revrrot(r2, a, 2);
  1235. int64revrrot(r3, a, 7);
  1236. s0.l = r1.l ^ r2.l ^ r3.l;
  1237. s0.h = r1.h ^ r2.h ^ r3.h;
  1238. //Maj
  1239. Maj.l = (a.l & b.l) ^ (a.l & c.l) ^ (b.l & c.l);
  1240. Maj.h = (a.h & b.h) ^ (a.h & c.h) ^ (b.h & c.h);
  1241. int64add5(T1, h, s1, Ch, sha512_k[j], W[j]);
  1242. int64add(T2, s0, Maj);
  1243. int64copy(h, g);
  1244. int64copy(g, f);
  1245. int64copy(f, e);
  1246. int64add(e, d, T1);
  1247. int64copy(d, c);
  1248. int64copy(c, b);
  1249. int64copy(b, a);
  1250. int64add(a, T1, T2);
  1251. }
  1252. int64add(H[0], H[0], a);
  1253. int64add(H[1], H[1], b);
  1254. int64add(H[2], H[2], c);
  1255. int64add(H[3], H[3], d);
  1256. int64add(H[4], H[4], e);
  1257. int64add(H[5], H[5], f);
  1258. int64add(H[6], H[6], g);
  1259. int64add(H[7], H[7], h);
  1260. }
  1261. //represent the hash as an array of 32-bit dwords
  1262. for (i = 0; i < 8; i += 1) {
  1263. hash[2 * i] = H[i].h;
  1264. hash[2 * i + 1] = H[i].l;
  1265. }
  1266. return hash;
  1267. }
  1268. //A constructor for 64-bit numbers
  1269. function int64(h, l) {
  1270. this.h = h;
  1271. this.l = l;
  1272. //this.toString = int64toString;
  1273. }
  1274. //Copies src into dst, assuming both are 64-bit numbers
  1275. function int64copy(dst, src) {
  1276. dst.h = src.h;
  1277. dst.l = src.l;
  1278. }
  1279. //Right-rotates a 64-bit number by shift
  1280. //Won't handle cases of shift>=32
  1281. //The function revrrot() is for that
  1282. function int64rrot(dst, x, shift) {
  1283. dst.l = (x.l >>> shift) | (x.h << (32 - shift));
  1284. dst.h = (x.h >>> shift) | (x.l << (32 - shift));
  1285. }
  1286. //Reverses the dwords of the source and then rotates right by shift.
  1287. //This is equivalent to rotation by 32+shift
  1288. function int64revrrot(dst, x, shift) {
  1289. dst.l = (x.h >>> shift) | (x.l << (32 - shift));
  1290. dst.h = (x.l >>> shift) | (x.h << (32 - shift));
  1291. }
  1292. //Bitwise-shifts right a 64-bit number by shift
  1293. //Won't handle shift>=32, but it's never needed in SHA512
  1294. function int64shr(dst, x, shift) {
  1295. dst.l = (x.l >>> shift) | (x.h << (32 - shift));
  1296. dst.h = (x.h >>> shift);
  1297. }
  1298. //Adds two 64-bit numbers
  1299. //Like the original implementation, does not rely on 32-bit operations
  1300. function int64add(dst, x, y) {
  1301. var w0 = (x.l & 0xffff) + (y.l & 0xffff);
  1302. var w1 = (x.l >>> 16) + (y.l >>> 16) + (w0 >>> 16);
  1303. var w2 = (x.h & 0xffff) + (y.h & 0xffff) + (w1 >>> 16);
  1304. var w3 = (x.h >>> 16) + (y.h >>> 16) + (w2 >>> 16);
  1305. dst.l = (w0 & 0xffff) | (w1 << 16);
  1306. dst.h = (w2 & 0xffff) | (w3 << 16);
  1307. }
  1308. //Same, except with 4 addends. Works faster than adding them one by one.
  1309. function int64add4(dst, a, b, c, d) {
  1310. var w0 = (a.l & 0xffff) + (b.l & 0xffff) + (c.l & 0xffff) + (d.l & 0xffff);
  1311. var w1 = (a.l >>> 16) + (b.l >>> 16) + (c.l >>> 16) + (d.l >>> 16) + (w0 >>> 16);
  1312. var w2 = (a.h & 0xffff) + (b.h & 0xffff) + (c.h & 0xffff) + (d.h & 0xffff) + (w1 >>> 16);
  1313. var w3 = (a.h >>> 16) + (b.h >>> 16) + (c.h >>> 16) + (d.h >>> 16) + (w2 >>> 16);
  1314. dst.l = (w0 & 0xffff) | (w1 << 16);
  1315. dst.h = (w2 & 0xffff) | (w3 << 16);
  1316. }
  1317. //Same, except with 5 addends
  1318. function int64add5(dst, a, b, c, d, e) {
  1319. var w0 = (a.l & 0xffff) + (b.l & 0xffff) + (c.l & 0xffff) + (d.l & 0xffff) + (e.l & 0xffff),
  1320. w1 = (a.l >>> 16) + (b.l >>> 16) + (c.l >>> 16) + (d.l >>> 16) + (e.l >>> 16) + (w0 >>> 16),
  1321. w2 = (a.h & 0xffff) + (b.h & 0xffff) + (c.h & 0xffff) + (d.h & 0xffff) + (e.h & 0xffff) + (w1 >>> 16),
  1322. w3 = (a.h >>> 16) + (b.h >>> 16) + (c.h >>> 16) + (d.h >>> 16) + (e.h >>> 16) + (w2 >>> 16);
  1323. dst.l = (w0 & 0xffff) | (w1 << 16);
  1324. dst.h = (w2 & 0xffff) | (w3 << 16);
  1325. }
  1326. },
  1327. /**
  1328. * @class Hashes.RMD160
  1329. * @constructor
  1330. * @param {Object} [config]
  1331. *
  1332. * A JavaScript implementation of the RIPEMD-160 Algorithm
  1333. * Version 2.2 Copyright Jeremy Lin, Paul Johnston 2000 - 2009.
  1334. * Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
  1335. * See http://pajhome.org.uk/crypt/md5 for details.
  1336. * Also http://www.ocf.berkeley.edu/~jjlin/jsotp/
  1337. */
  1338. RMD160: function (options) {
  1339. /**
  1340. * Private properties configuration variables. You may need to tweak these to be compatible with
  1341. * the server-side, but the defaults work in most cases.
  1342. * @see this.setUpperCase() method
  1343. * @see this.setPad() method
  1344. */
  1345. var hexcase = (options && typeof options.uppercase === 'boolean') ? options.uppercase : false,
  1346. /* hexadecimal output case format. false - lowercase; true - uppercase */
  1347. b64pad = (options && typeof options.pad === 'string') ? options.pa : '=',
  1348. /* base-64 pad character. Default '=' for strict RFC compliance */
  1349. utf8 = (options && typeof options.utf8 === 'boolean') ? options.utf8 : true,
  1350. /* enable/disable utf8 encoding */
  1351. rmd160_r1 = [
  1352. 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15,
  1353. 7, 4, 13, 1, 10, 6, 15, 3, 12, 0, 9, 5, 2, 14, 11, 8,
  1354. 3, 10, 14, 4, 9, 15, 8, 1, 2, 7, 0, 6, 13, 11, 5, 12,
  1355. 1, 9, 11, 10, 0, 8, 12, 4, 13, 3, 7, 15, 14, 5, 6, 2,
  1356. 4, 0, 5, 9, 7, 12, 2, 10, 14, 1, 3, 8, 11, 6, 15, 13
  1357. ],
  1358. rmd160_r2 = [
  1359. 5, 14, 7, 0, 9, 2, 11, 4, 13, 6, 15, 8, 1, 10, 3, 12,
  1360. 6, 11, 3, 7, 0, 13, 5, 10, 14, 15, 8, 12, 4, 9, 1, 2,
  1361. 15, 5, 1, 3, 7, 14, 6, 9, 11, 8, 12, 2, 10, 0, 4, 13,
  1362. 8, 6, 4, 1, 3, 11, 15, 0, 5, 12, 2, 13, 9, 7, 10, 14,
  1363. 12, 15, 10, 4, 1, 5, 8, 7, 6, 2, 13, 14, 0, 3, 9, 11
  1364. ],
  1365. rmd160_s1 = [
  1366. 11, 14, 15, 12, 5, 8, 7, 9, 11, 13, 14, 15, 6, 7, 9, 8,
  1367. 7, 6, 8, 13, 11, 9, 7, 15, 7, 12, 15, 9, 11, 7, 13, 12,
  1368. 11, 13, 6, 7, 14, 9, 13, 15, 14, 8, 13, 6, 5, 12, 7, 5,
  1369. 11, 12, 14, 15, 14, 15, 9, 8, 9, 14, 5, 6, 8, 6, 5, 12,
  1370. 9, 15, 5, 11, 6, 8, 13, 12, 5, 12, 13, 14, 11, 8, 5, 6
  1371. ],
  1372. rmd160_s2 = [
  1373. 8, 9, 9, 11, 13, 15, 15, 5, 7, 7, 8, 11, 14, 14, 12, 6,
  1374. 9, 13, 15, 7, 12, 8, 9, 11, 7, 7, 12, 7, 6, 15, 13, 11,
  1375. 9, 7, 15, 11, 8, 6, 6, 14, 12, 13, 5, 14, 13, 13, 7, 5,
  1376. 15, 5, 8, 11, 14, 14, 6, 14, 6, 9, 12, 9, 12, 5, 15, 8,
  1377. 8, 5, 12, 9, 12, 5, 14, 6, 8, 13, 6, 5, 15, 13, 11, 11
  1378. ];
  1379. /* privileged (public) methods */
  1380. this.hex = function (s) {
  1381. return rstr2hex(rstr(s, utf8));
  1382. };
  1383. this.b64 = function (s) {
  1384. return rstr2b64(rstr(s, utf8), b64pad);
  1385. };
  1386. this.any = function (s, e) {
  1387. return rstr2any(rstr(s, utf8), e);
  1388. };
  1389. this.raw = function (s) {
  1390. return rstr(s, utf8);
  1391. };
  1392. this.hex_hmac = function (k, d) {
  1393. return rstr2hex(rstr_hmac(k, d));
  1394. };
  1395. this.b64_hmac = function (k, d) {
  1396. return rstr2b64(rstr_hmac(k, d), b64pad);
  1397. };
  1398. this.any_hmac = function (k, d, e) {
  1399. return rstr2any(rstr_hmac(k, d), e);
  1400. };
  1401. /**
  1402. * Perform a simple self-test to see if the VM is working
  1403. * @return {String} Hexadecimal hash sample
  1404. * @public
  1405. */
  1406. this.vm_test = function () {
  1407. return hex('abc').toLowerCase() === '900150983cd24fb0d6963f7d28e17f72';
  1408. };
  1409. /**
  1410. * @description Enable/disable uppercase hexadecimal returned string
  1411. * @param {boolean}
  1412. * @return {Object} this
  1413. * @public
  1414. */
  1415. this.setUpperCase = function (a) {
  1416. if (typeof a === 'boolean') {
  1417. hexcase = a;
  1418. }
  1419. return this;
  1420. };
  1421. /**
  1422. * @description Defines a base64 pad string
  1423. * @param {string} Pad
  1424. * @return {Object} this
  1425. * @public
  1426. */
  1427. this.setPad = function (a) {
  1428. if (typeof a !== 'undefined') {
  1429. b64pad = a;
  1430. }
  1431. return this;
  1432. };
  1433. /**
  1434. * @description Defines a base64 pad string
  1435. * @param {boolean}
  1436. * @return {Object} this
  1437. * @public
  1438. */
  1439. this.setUTF8 = function (a) {
  1440. if (typeof a === 'boolean') {
  1441. utf8 = a;
  1442. }
  1443. return this;
  1444. };
  1445. /* private methods */
  1446. /**
  1447. * Calculate the rmd160 of a raw string
  1448. */
  1449. function rstr(s) {
  1450. s = (utf8) ? utf8Encode(s) : s;
  1451. return binl2rstr(binl(rstr2binl(s), s.length * 8));
  1452. }
  1453. /**
  1454. * Calculate the HMAC-rmd160 of a key and some data (raw strings)
  1455. */
  1456. function rstr_hmac(key, data) {
  1457. key = (utf8) ? utf8Encode(key) : key;
  1458. data = (utf8) ? utf8Encode(data) : data;
  1459. var i, hash,
  1460. bkey = rstr2binl(key),
  1461. ipad = Array(16),
  1462. opad = Array(16);
  1463. if (bkey.length > 16) {
  1464. bkey = binl(bkey, key.length * 8);
  1465. }
  1466. for (i = 0; i < 16; i += 1) {
  1467. ipad[i] = bkey[i] ^ 0x36363636;
  1468. opad[i] = bkey[i] ^ 0x5C5C5C5C;
  1469. }
  1470. hash = binl(ipad.concat(rstr2binl(data)), 512 + data.length * 8);
  1471. return binl2rstr(binl(opad.concat(hash), 512 + 160));
  1472. }
  1473. /**
  1474. * Convert an array of little-endian words to a string
  1475. */
  1476. function binl2rstr(input) {
  1477. var i, output = '',
  1478. l = input.length * 32;
  1479. for (i = 0; i < l; i += 8) {
  1480. output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xFF);
  1481. }
  1482. return output;
  1483. }
  1484. /**
  1485. * Calculate the RIPE-MD160 of an array of little-endian words, and a bit length.
  1486. */
  1487. function binl(x, len) {
  1488. var T, j, i, l,
  1489. h0 = 0x67452301,
  1490. h1 = 0xefcdab89,
  1491. h2 = 0x98badcfe,
  1492. h3 = 0x10325476,
  1493. h4 = 0xc3d2e1f0,
  1494. A1, B1, C1, D1, E1,
  1495. A2, B2, C2, D2, E2;
  1496. /* append padding */
  1497. x[len >> 5] |= 0x80 << (len % 32);
  1498. x[(((len + 64) >>> 9) << 4) + 14] = len;
  1499. l = x.length;
  1500. for (i = 0; i < l; i += 16) {
  1501. A1 = A2 = h0;
  1502. B1 = B2 = h1;
  1503. C1 = C2 = h2;
  1504. D1 = D2 = h3;
  1505. E1 = E2 = h4;
  1506. for (j = 0; j <= 79; j += 1) {
  1507. T = safe_add(A1, rmd160_f(j, B1, C1, D1));
  1508. T = safe_add(T, x[i + rmd160_r1[j]]);
  1509. T = safe_add(T, rmd160_K1(j));
  1510. T = safe_add(bit_rol(T, rmd160_s1[j]), E1);
  1511. A1 = E1;
  1512. E1 = D1;
  1513. D1 = bit_rol(C1, 10);
  1514. C1 = B1;
  1515. B1 = T;
  1516. T = safe_add(A2, rmd160_f(79 - j, B2, C2, D2));
  1517. T = safe_add(T, x[i + rmd160_r2[j]]);
  1518. T = safe_add(T, rmd160_K2(j));
  1519. T = safe_add(bit_rol(T, rmd160_s2[j]), E2);
  1520. A2 = E2;
  1521. E2 = D2;
  1522. D2 = bit_rol(C2, 10);
  1523. C2 = B2;
  1524. B2 = T;
  1525. }
  1526. T = safe_add(h1, safe_add(C1, D2));
  1527. h1 = safe_add(h2, safe_add(D1, E2));
  1528. h2 = safe_add(h3, safe_add(E1, A2));
  1529. h3 = safe_add(h4, safe_add(A1, B2));
  1530. h4 = safe_add(h0, safe_add(B1, C2));
  1531. h0 = T;
  1532. }
  1533. return [h0, h1, h2, h3, h4];
  1534. }
  1535. // specific algorithm methods
  1536. function rmd160_f(j, x, y, z) {
  1537. return (0 <= j && j <= 15) ? (x ^ y ^ z) :
  1538. (16 <= j && j <= 31) ? (x & y) | (~x & z) :
  1539. (32 <= j && j <= 47) ? (x | ~y) ^ z :
  1540. (48 <= j && j <= 63) ? (x & z) | (y & ~z) :
  1541. (64 <= j && j <= 79) ? x ^ (y | ~z) :
  1542. 'rmd160_f: j out of range';
  1543. }
  1544. function rmd160_K1(j) {
  1545. return (0 <= j && j <= 15) ? 0x00000000 :
  1546. (16 <= j && j <= 31) ? 0x5a827999 :
  1547. (32 <= j && j <= 47) ? 0x6ed9eba1 :
  1548. (48 <= j && j <= 63) ? 0x8f1bbcdc :
  1549. (64 <= j && j <= 79) ? 0xa953fd4e :
  1550. 'rmd160_K1: j out of range';
  1551. }
  1552. function rmd160_K2(j) {
  1553. return (0 <= j && j <= 15) ? 0x50a28be6 :
  1554. (16 <= j && j <= 31) ? 0x5c4dd124 :
  1555. (32 <= j && j <= 47) ? 0x6d703ef3 :
  1556. (48 <= j && j <= 63) ? 0x7a6d76e9 :
  1557. (64 <= j && j <= 79) ? 0x00000000 :
  1558. 'rmd160_K2: j out of range';
  1559. }
  1560. }
  1561. };
  1562. // new MD5 instance
  1563. var MD5 = new pearHash.MD5
  1564. // new SHA1 instance
  1565. var SHA1 = new pearHash.SHA1
  1566. // new SHA256 instance
  1567. var SHA256 = new pearHash.SHA256
  1568. // new SHA512 instace
  1569. var SHA512 = new pearHash.SHA512
  1570. // new RIPEMD-160 instace
  1571. var RMD160 = new pearHash.RMD160
  1572. var Base64 = new pearHash.Base64
  1573. pearHash.md5 = function (str) {
  1574. return MD5.hex(str);
  1575. }
  1576. pearHash.sha1 = function (str) {
  1577. return SHA1.hex(str);
  1578. }
  1579. pearHash.sha256 = function (str) {
  1580. return SHA256.hex(str);
  1581. }
  1582. pearHash.sha512 = function (str) {
  1583. return SHA512.hex(str);
  1584. }
  1585. pearHash.crc32 = pearHash.CRC32;
  1586. pearHash.rmd160 = function (str) {
  1587. return RMD160.hex(str);
  1588. }
  1589. pearHash.Base64Encode = function (str) {
  1590. return Base64.encode(str);
  1591. }
  1592. pearHash.Base64Decode = function (str) {
  1593. return Base64.decode(str);
  1594. }
  1595. exports('encrypt', pearHash);
  1596. });