Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.
 
 
 
 
 
 

2393 Zeilen
72 KiB

  1. (function(f){if(typeof exports==="object"&&typeof module!=="undefined"){module.exports=f()}else if(typeof define==="function"&&define.amd){define([],f)}else{var g;if(typeof window!=="undefined"){g=window}else if(typeof global!=="undefined"){g=global}else if(typeof self!=="undefined"){g=self}else{g=this}g.LessPass = f()}})(function(){var define,module,exports;return (function e(t,n,r){function s(o,u){if(!n[o]){if(!t[o]){var a=typeof require=="function"&&require;if(!u&&a)return a(o,!0);if(i)return i(o,!0);var f=new Error("Cannot find module '"+o+"'");throw f.code="MODULE_NOT_FOUND",f}var l=n[o]={exports:{}};t[o][0].call(l.exports,function(e){var n=t[o][1][e];return s(n?n:e)},l,l.exports,e,t,n,r)}return n[o].exports}var i=typeof require=="function"&&require;for(var o=0;o<r.length;o++)s(r[o]);return s})({1:[function(require,module,exports){
  2. var bigInt = (function (undefined) {
  3. "use strict";
  4. var BASE = 1e7,
  5. LOG_BASE = 7,
  6. MAX_INT = 9007199254740992,
  7. MAX_INT_ARR = smallToArray(MAX_INT),
  8. LOG_MAX_INT = Math.log(MAX_INT);
  9. function Integer(v, radix) {
  10. if (typeof v === "undefined") return Integer[0];
  11. if (typeof radix !== "undefined") return +radix === 10 ? parseValue(v) : parseBase(v, radix);
  12. return parseValue(v);
  13. }
  14. function BigInteger(value, sign) {
  15. this.value = value;
  16. this.sign = sign;
  17. this.isSmall = false;
  18. }
  19. BigInteger.prototype = Object.create(Integer.prototype);
  20. function SmallInteger(value) {
  21. this.value = value;
  22. this.sign = value < 0;
  23. this.isSmall = true;
  24. }
  25. SmallInteger.prototype = Object.create(Integer.prototype);
  26. function isPrecise(n) {
  27. return -MAX_INT < n && n < MAX_INT;
  28. }
  29. function smallToArray(n) { // For performance reasons doesn't reference BASE, need to change this function if BASE changes
  30. if (n < 1e7)
  31. return [n];
  32. if (n < 1e14)
  33. return [n % 1e7, Math.floor(n / 1e7)];
  34. return [n % 1e7, Math.floor(n / 1e7) % 1e7, Math.floor(n / 1e14)];
  35. }
  36. function arrayToSmall(arr) { // If BASE changes this function may need to change
  37. trim(arr);
  38. var length = arr.length;
  39. if (length < 4 && compareAbs(arr, MAX_INT_ARR) < 0) {
  40. switch (length) {
  41. case 0: return 0;
  42. case 1: return arr[0];
  43. case 2: return arr[0] + arr[1] * BASE;
  44. default: return arr[0] + (arr[1] + arr[2] * BASE) * BASE;
  45. }
  46. }
  47. return arr;
  48. }
  49. function trim(v) {
  50. var i = v.length;
  51. while (v[--i] === 0);
  52. v.length = i + 1;
  53. }
  54. function createArray(length) { // function shamelessly stolen from Yaffle's library https://github.com/Yaffle/BigInteger
  55. var x = new Array(length);
  56. var i = -1;
  57. while (++i < length) {
  58. x[i] = 0;
  59. }
  60. return x;
  61. }
  62. function truncate(n) {
  63. if (n > 0) return Math.floor(n);
  64. return Math.ceil(n);
  65. }
  66. function add(a, b) { // assumes a and b are arrays with a.length >= b.length
  67. var l_a = a.length,
  68. l_b = b.length,
  69. r = new Array(l_a),
  70. carry = 0,
  71. base = BASE,
  72. sum, i;
  73. for (i = 0; i < l_b; i++) {
  74. sum = a[i] + b[i] + carry;
  75. carry = sum >= base ? 1 : 0;
  76. r[i] = sum - carry * base;
  77. }
  78. while (i < l_a) {
  79. sum = a[i] + carry;
  80. carry = sum === base ? 1 : 0;
  81. r[i++] = sum - carry * base;
  82. }
  83. if (carry > 0) r.push(carry);
  84. return r;
  85. }
  86. function addAny(a, b) {
  87. if (a.length >= b.length) return add(a, b);
  88. return add(b, a);
  89. }
  90. function addSmall(a, carry) { // assumes a is array, carry is number with 0 <= carry < MAX_INT
  91. var l = a.length,
  92. r = new Array(l),
  93. base = BASE,
  94. sum, i;
  95. for (i = 0; i < l; i++) {
  96. sum = a[i] - base + carry;
  97. carry = Math.floor(sum / base);
  98. r[i] = sum - carry * base;
  99. carry += 1;
  100. }
  101. while (carry > 0) {
  102. r[i++] = carry % base;
  103. carry = Math.floor(carry / base);
  104. }
  105. return r;
  106. }
  107. BigInteger.prototype.add = function (v) {
  108. var n = parseValue(v);
  109. if (this.sign !== n.sign) {
  110. return this.subtract(n.negate());
  111. }
  112. var a = this.value, b = n.value;
  113. if (n.isSmall) {
  114. return new BigInteger(addSmall(a, Math.abs(b)), this.sign);
  115. }
  116. return new BigInteger(addAny(a, b), this.sign);
  117. };
  118. BigInteger.prototype.plus = BigInteger.prototype.add;
  119. SmallInteger.prototype.add = function (v) {
  120. var n = parseValue(v);
  121. var a = this.value;
  122. if (a < 0 !== n.sign) {
  123. return this.subtract(n.negate());
  124. }
  125. var b = n.value;
  126. if (n.isSmall) {
  127. if (isPrecise(a + b)) return new SmallInteger(a + b);
  128. b = smallToArray(Math.abs(b));
  129. }
  130. return new BigInteger(addSmall(b, Math.abs(a)), a < 0);
  131. };
  132. SmallInteger.prototype.plus = SmallInteger.prototype.add;
  133. function subtract(a, b) { // assumes a and b are arrays with a >= b
  134. var a_l = a.length,
  135. b_l = b.length,
  136. r = new Array(a_l),
  137. borrow = 0,
  138. base = BASE,
  139. i, difference;
  140. for (i = 0; i < b_l; i++) {
  141. difference = a[i] - borrow - b[i];
  142. if (difference < 0) {
  143. difference += base;
  144. borrow = 1;
  145. } else borrow = 0;
  146. r[i] = difference;
  147. }
  148. for (i = b_l; i < a_l; i++) {
  149. difference = a[i] - borrow;
  150. if (difference < 0) difference += base;
  151. else {
  152. r[i++] = difference;
  153. break;
  154. }
  155. r[i] = difference;
  156. }
  157. for (; i < a_l; i++) {
  158. r[i] = a[i];
  159. }
  160. trim(r);
  161. return r;
  162. }
  163. function subtractAny(a, b, sign) {
  164. var value;
  165. if (compareAbs(a, b) >= 0) {
  166. value = subtract(a,b);
  167. } else {
  168. value = subtract(b, a);
  169. sign = !sign;
  170. }
  171. value = arrayToSmall(value);
  172. if (typeof value === "number") {
  173. if (sign) value = -value;
  174. return new SmallInteger(value);
  175. }
  176. return new BigInteger(value, sign);
  177. }
  178. function subtractSmall(a, b, sign) { // assumes a is array, b is number with 0 <= b < MAX_INT
  179. var l = a.length,
  180. r = new Array(l),
  181. carry = -b,
  182. base = BASE,
  183. i, difference;
  184. for (i = 0; i < l; i++) {
  185. difference = a[i] + carry;
  186. carry = Math.floor(difference / base);
  187. difference %= base;
  188. r[i] = difference < 0 ? difference + base : difference;
  189. }
  190. r = arrayToSmall(r);
  191. if (typeof r === "number") {
  192. if (sign) r = -r;
  193. return new SmallInteger(r);
  194. } return new BigInteger(r, sign);
  195. }
  196. BigInteger.prototype.subtract = function (v) {
  197. var n = parseValue(v);
  198. if (this.sign !== n.sign) {
  199. return this.add(n.negate());
  200. }
  201. var a = this.value, b = n.value;
  202. if (n.isSmall)
  203. return subtractSmall(a, Math.abs(b), this.sign);
  204. return subtractAny(a, b, this.sign);
  205. };
  206. BigInteger.prototype.minus = BigInteger.prototype.subtract;
  207. SmallInteger.prototype.subtract = function (v) {
  208. var n = parseValue(v);
  209. var a = this.value;
  210. if (a < 0 !== n.sign) {
  211. return this.add(n.negate());
  212. }
  213. var b = n.value;
  214. if (n.isSmall) {
  215. return new SmallInteger(a - b);
  216. }
  217. return subtractSmall(b, Math.abs(a), a >= 0);
  218. };
  219. SmallInteger.prototype.minus = SmallInteger.prototype.subtract;
  220. BigInteger.prototype.negate = function () {
  221. return new BigInteger(this.value, !this.sign);
  222. };
  223. SmallInteger.prototype.negate = function () {
  224. var sign = this.sign;
  225. var small = new SmallInteger(-this.value);
  226. small.sign = !sign;
  227. return small;
  228. };
  229. BigInteger.prototype.abs = function () {
  230. return new BigInteger(this.value, false);
  231. };
  232. SmallInteger.prototype.abs = function () {
  233. return new SmallInteger(Math.abs(this.value));
  234. };
  235. function multiplyLong(a, b) {
  236. var a_l = a.length,
  237. b_l = b.length,
  238. l = a_l + b_l,
  239. r = createArray(l),
  240. base = BASE,
  241. product, carry, i, a_i, b_j;
  242. for (i = 0; i < a_l; ++i) {
  243. a_i = a[i];
  244. for (var j = 0; j < b_l; ++j) {
  245. b_j = b[j];
  246. product = a_i * b_j + r[i + j];
  247. carry = Math.floor(product / base);
  248. r[i + j] = product - carry * base;
  249. r[i + j + 1] += carry;
  250. }
  251. }
  252. trim(r);
  253. return r;
  254. }
  255. function multiplySmall(a, b) { // assumes a is array, b is number with |b| < BASE
  256. var l = a.length,
  257. r = new Array(l),
  258. base = BASE,
  259. carry = 0,
  260. product, i;
  261. for (i = 0; i < l; i++) {
  262. product = a[i] * b + carry;
  263. carry = Math.floor(product / base);
  264. r[i] = product - carry * base;
  265. }
  266. while (carry > 0) {
  267. r[i++] = carry % base;
  268. carry = Math.floor(carry / base);
  269. }
  270. return r;
  271. }
  272. function shiftLeft(x, n) {
  273. var r = [];
  274. while (n-- > 0) r.push(0);
  275. return r.concat(x);
  276. }
  277. function multiplyKaratsuba(x, y) {
  278. var n = Math.max(x.length, y.length);
  279. if (n <= 30) return multiplyLong(x, y);
  280. n = Math.ceil(n / 2);
  281. var b = x.slice(n),
  282. a = x.slice(0, n),
  283. d = y.slice(n),
  284. c = y.slice(0, n);
  285. var ac = multiplyKaratsuba(a, c),
  286. bd = multiplyKaratsuba(b, d),
  287. abcd = multiplyKaratsuba(addAny(a, b), addAny(c, d));
  288. var product = addAny(addAny(ac, shiftLeft(subtract(subtract(abcd, ac), bd), n)), shiftLeft(bd, 2 * n));
  289. trim(product);
  290. return product;
  291. }
  292. // The following function is derived from a surface fit of a graph plotting the performance difference
  293. // between long multiplication and karatsuba multiplication versus the lengths of the two arrays.
  294. function useKaratsuba(l1, l2) {
  295. return -0.012 * l1 - 0.012 * l2 + 0.000015 * l1 * l2 > 0;
  296. }
  297. BigInteger.prototype.multiply = function (v) {
  298. var n = parseValue(v),
  299. a = this.value, b = n.value,
  300. sign = this.sign !== n.sign,
  301. abs;
  302. if (n.isSmall) {
  303. if (b === 0) return Integer[0];
  304. if (b === 1) return this;
  305. if (b === -1) return this.negate();
  306. abs = Math.abs(b);
  307. if (abs < BASE) {
  308. return new BigInteger(multiplySmall(a, abs), sign);
  309. }
  310. b = smallToArray(abs);
  311. }
  312. if (useKaratsuba(a.length, b.length)) // Karatsuba is only faster for certain array sizes
  313. return new BigInteger(multiplyKaratsuba(a, b), sign);
  314. return new BigInteger(multiplyLong(a, b), sign);
  315. };
  316. BigInteger.prototype.times = BigInteger.prototype.multiply;
  317. function multiplySmallAndArray(a, b, sign) { // a >= 0
  318. if (a < BASE) {
  319. return new BigInteger(multiplySmall(b, a), sign);
  320. }
  321. return new BigInteger(multiplyLong(b, smallToArray(a)), sign);
  322. }
  323. SmallInteger.prototype._multiplyBySmall = function (a) {
  324. if (isPrecise(a.value * this.value)) {
  325. return new SmallInteger(a.value * this.value);
  326. }
  327. return multiplySmallAndArray(Math.abs(a.value), smallToArray(Math.abs(this.value)), this.sign !== a.sign);
  328. };
  329. BigInteger.prototype._multiplyBySmall = function (a) {
  330. if (a.value === 0) return Integer[0];
  331. if (a.value === 1) return this;
  332. if (a.value === -1) return this.negate();
  333. return multiplySmallAndArray(Math.abs(a.value), this.value, this.sign !== a.sign);
  334. };
  335. SmallInteger.prototype.multiply = function (v) {
  336. return parseValue(v)._multiplyBySmall(this);
  337. };
  338. SmallInteger.prototype.times = SmallInteger.prototype.multiply;
  339. function square(a) {
  340. var l = a.length,
  341. r = createArray(l + l),
  342. base = BASE,
  343. product, carry, i, a_i, a_j;
  344. for (i = 0; i < l; i++) {
  345. a_i = a[i];
  346. for (var j = 0; j < l; j++) {
  347. a_j = a[j];
  348. product = a_i * a_j + r[i + j];
  349. carry = Math.floor(product / base);
  350. r[i + j] = product - carry * base;
  351. r[i + j + 1] += carry;
  352. }
  353. }
  354. trim(r);
  355. return r;
  356. }
  357. BigInteger.prototype.square = function () {
  358. return new BigInteger(square(this.value), false);
  359. };
  360. SmallInteger.prototype.square = function () {
  361. var value = this.value * this.value;
  362. if (isPrecise(value)) return new SmallInteger(value);
  363. return new BigInteger(square(smallToArray(Math.abs(this.value))), false);
  364. };
  365. function divMod1(a, b) { // Left over from previous version. Performs faster than divMod2 on smaller input sizes.
  366. var a_l = a.length,
  367. b_l = b.length,
  368. base = BASE,
  369. result = createArray(b.length),
  370. divisorMostSignificantDigit = b[b_l - 1],
  371. // normalization
  372. lambda = Math.ceil(base / (2 * divisorMostSignificantDigit)),
  373. remainder = multiplySmall(a, lambda),
  374. divisor = multiplySmall(b, lambda),
  375. quotientDigit, shift, carry, borrow, i, l, q;
  376. if (remainder.length <= a_l) remainder.push(0);
  377. divisor.push(0);
  378. divisorMostSignificantDigit = divisor[b_l - 1];
  379. for (shift = a_l - b_l; shift >= 0; shift--) {
  380. quotientDigit = base - 1;
  381. if (remainder[shift + b_l] !== divisorMostSignificantDigit) {
  382. quotientDigit = Math.floor((remainder[shift + b_l] * base + remainder[shift + b_l - 1]) / divisorMostSignificantDigit);
  383. }
  384. // quotientDigit <= base - 1
  385. carry = 0;
  386. borrow = 0;
  387. l = divisor.length;
  388. for (i = 0; i < l; i++) {
  389. carry += quotientDigit * divisor[i];
  390. q = Math.floor(carry / base);
  391. borrow += remainder[shift + i] - (carry - q * base);
  392. carry = q;
  393. if (borrow < 0) {
  394. remainder[shift + i] = borrow + base;
  395. borrow = -1;
  396. } else {
  397. remainder[shift + i] = borrow;
  398. borrow = 0;
  399. }
  400. }
  401. while (borrow !== 0) {
  402. quotientDigit -= 1;
  403. carry = 0;
  404. for (i = 0; i < l; i++) {
  405. carry += remainder[shift + i] - base + divisor[i];
  406. if (carry < 0) {
  407. remainder[shift + i] = carry + base;
  408. carry = 0;
  409. } else {
  410. remainder[shift + i] = carry;
  411. carry = 1;
  412. }
  413. }
  414. borrow += carry;
  415. }
  416. result[shift] = quotientDigit;
  417. }
  418. // denormalization
  419. remainder = divModSmall(remainder, lambda)[0];
  420. return [arrayToSmall(result), arrayToSmall(remainder)];
  421. }
  422. function divMod2(a, b) { // Implementation idea shamelessly stolen from Silent Matt's library http://silentmatt.com/biginteger/
  423. // Performs faster than divMod1 on larger input sizes.
  424. var a_l = a.length,
  425. b_l = b.length,
  426. result = [],
  427. part = [],
  428. base = BASE,
  429. guess, xlen, highx, highy, check;
  430. while (a_l) {
  431. part.unshift(a[--a_l]);
  432. trim(part);
  433. if (compareAbs(part, b) < 0) {
  434. result.push(0);
  435. continue;
  436. }
  437. xlen = part.length;
  438. highx = part[xlen - 1] * base + part[xlen - 2];
  439. highy = b[b_l - 1] * base + b[b_l - 2];
  440. if (xlen > b_l) {
  441. highx = (highx + 1) * base;
  442. }
  443. guess = Math.ceil(highx / highy);
  444. do {
  445. check = multiplySmall(b, guess);
  446. if (compareAbs(check, part) <= 0) break;
  447. guess--;
  448. } while (guess);
  449. result.push(guess);
  450. part = subtract(part, check);
  451. }
  452. result.reverse();
  453. return [arrayToSmall(result), arrayToSmall(part)];
  454. }
  455. function divModSmall(value, lambda) {
  456. var length = value.length,
  457. quotient = createArray(length),
  458. base = BASE,
  459. i, q, remainder, divisor;
  460. remainder = 0;
  461. for (i = length - 1; i >= 0; --i) {
  462. divisor = remainder * base + value[i];
  463. q = truncate(divisor / lambda);
  464. remainder = divisor - q * lambda;
  465. quotient[i] = q | 0;
  466. }
  467. return [quotient, remainder | 0];
  468. }
  469. function divModAny(self, v) {
  470. var value, n = parseValue(v);
  471. var a = self.value, b = n.value;
  472. var quotient;
  473. if (b === 0) throw new Error("Cannot divide by zero");
  474. if (self.isSmall) {
  475. if (n.isSmall) {
  476. return [new SmallInteger(truncate(a / b)), new SmallInteger(a % b)];
  477. }
  478. return [Integer[0], self];
  479. }
  480. if (n.isSmall) {
  481. if (b === 1) return [self, Integer[0]];
  482. if (b == -1) return [self.negate(), Integer[0]];
  483. var abs = Math.abs(b);
  484. if (abs < BASE) {
  485. value = divModSmall(a, abs);
  486. quotient = arrayToSmall(value[0]);
  487. var remainder = value[1];
  488. if (self.sign) remainder = -remainder;
  489. if (typeof quotient === "number") {
  490. if (self.sign !== n.sign) quotient = -quotient;
  491. return [new SmallInteger(quotient), new SmallInteger(remainder)];
  492. }
  493. return [new BigInteger(quotient, self.sign !== n.sign), new SmallInteger(remainder)];
  494. }
  495. b = smallToArray(abs);
  496. }
  497. var comparison = compareAbs(a, b);
  498. if (comparison === -1) return [Integer[0], self];
  499. if (comparison === 0) return [Integer[self.sign === n.sign ? 1 : -1], Integer[0]];
  500. // divMod1 is faster on smaller input sizes
  501. if (a.length + b.length <= 200)
  502. value = divMod1(a, b);
  503. else value = divMod2(a, b);
  504. quotient = value[0];
  505. var qSign = self.sign !== n.sign,
  506. mod = value[1],
  507. mSign = self.sign;
  508. if (typeof quotient === "number") {
  509. if (qSign) quotient = -quotient;
  510. quotient = new SmallInteger(quotient);
  511. } else quotient = new BigInteger(quotient, qSign);
  512. if (typeof mod === "number") {
  513. if (mSign) mod = -mod;
  514. mod = new SmallInteger(mod);
  515. } else mod = new BigInteger(mod, mSign);
  516. return [quotient, mod];
  517. }
  518. BigInteger.prototype.divmod = function (v) {
  519. var result = divModAny(this, v);
  520. return {
  521. quotient: result[0],
  522. remainder: result[1]
  523. };
  524. };
  525. SmallInteger.prototype.divmod = BigInteger.prototype.divmod;
  526. BigInteger.prototype.divide = function (v) {
  527. return divModAny(this, v)[0];
  528. };
  529. SmallInteger.prototype.over = SmallInteger.prototype.divide = BigInteger.prototype.over = BigInteger.prototype.divide;
  530. BigInteger.prototype.mod = function (v) {
  531. return divModAny(this, v)[1];
  532. };
  533. SmallInteger.prototype.remainder = SmallInteger.prototype.mod = BigInteger.prototype.remainder = BigInteger.prototype.mod;
  534. BigInteger.prototype.pow = function (v) {
  535. var n = parseValue(v),
  536. a = this.value,
  537. b = n.value,
  538. value, x, y;
  539. if (b === 0) return Integer[1];
  540. if (a === 0) return Integer[0];
  541. if (a === 1) return Integer[1];
  542. if (a === -1) return n.isEven() ? Integer[1] : Integer[-1];
  543. if (n.sign) {
  544. return Integer[0];
  545. }
  546. if (!n.isSmall) throw new Error("The exponent " + n.toString() + " is too large.");
  547. if (this.isSmall) {
  548. if (isPrecise(value = Math.pow(a, b)))
  549. return new SmallInteger(truncate(value));
  550. }
  551. x = this;
  552. y = Integer[1];
  553. while (true) {
  554. if (b & 1 === 1) {
  555. y = y.times(x);
  556. --b;
  557. }
  558. if (b === 0) break;
  559. b /= 2;
  560. x = x.square();
  561. }
  562. return y;
  563. };
  564. SmallInteger.prototype.pow = BigInteger.prototype.pow;
  565. BigInteger.prototype.modPow = function (exp, mod) {
  566. exp = parseValue(exp);
  567. mod = parseValue(mod);
  568. if (mod.isZero()) throw new Error("Cannot take modPow with modulus 0");
  569. var r = Integer[1],
  570. base = this.mod(mod);
  571. while (exp.isPositive()) {
  572. if (base.isZero()) return Integer[0];
  573. if (exp.isOdd()) r = r.multiply(base).mod(mod);
  574. exp = exp.divide(2);
  575. base = base.square().mod(mod);
  576. }
  577. return r;
  578. };
  579. SmallInteger.prototype.modPow = BigInteger.prototype.modPow;
  580. function compareAbs(a, b) {
  581. if (a.length !== b.length) {
  582. return a.length > b.length ? 1 : -1;
  583. }
  584. for (var i = a.length - 1; i >= 0; i--) {
  585. if (a[i] !== b[i]) return a[i] > b[i] ? 1 : -1;
  586. }
  587. return 0;
  588. }
  589. BigInteger.prototype.compareAbs = function (v) {
  590. var n = parseValue(v),
  591. a = this.value,
  592. b = n.value;
  593. if (n.isSmall) return 1;
  594. return compareAbs(a, b);
  595. };
  596. SmallInteger.prototype.compareAbs = function (v) {
  597. var n = parseValue(v),
  598. a = Math.abs(this.value),
  599. b = n.value;
  600. if (n.isSmall) {
  601. b = Math.abs(b);
  602. return a === b ? 0 : a > b ? 1 : -1;
  603. }
  604. return -1;
  605. };
  606. BigInteger.prototype.compare = function (v) {
  607. // See discussion about comparison with Infinity:
  608. // https://github.com/peterolson/BigInteger.js/issues/61
  609. if (v === Infinity) {
  610. return -1;
  611. }
  612. if (v === -Infinity) {
  613. return 1;
  614. }
  615. var n = parseValue(v),
  616. a = this.value,
  617. b = n.value;
  618. if (this.sign !== n.sign) {
  619. return n.sign ? 1 : -1;
  620. }
  621. if (n.isSmall) {
  622. return this.sign ? -1 : 1;
  623. }
  624. return compareAbs(a, b) * (this.sign ? -1 : 1);
  625. };
  626. BigInteger.prototype.compareTo = BigInteger.prototype.compare;
  627. SmallInteger.prototype.compare = function (v) {
  628. if (v === Infinity) {
  629. return -1;
  630. }
  631. if (v === -Infinity) {
  632. return 1;
  633. }
  634. var n = parseValue(v),
  635. a = this.value,
  636. b = n.value;
  637. if (n.isSmall) {
  638. return a == b ? 0 : a > b ? 1 : -1;
  639. }
  640. if (a < 0 !== n.sign) {
  641. return a < 0 ? -1 : 1;
  642. }
  643. return a < 0 ? 1 : -1;
  644. };
  645. SmallInteger.prototype.compareTo = SmallInteger.prototype.compare;
  646. BigInteger.prototype.equals = function (v) {
  647. return this.compare(v) === 0;
  648. };
  649. SmallInteger.prototype.eq = SmallInteger.prototype.equals = BigInteger.prototype.eq = BigInteger.prototype.equals;
  650. BigInteger.prototype.notEquals = function (v) {
  651. return this.compare(v) !== 0;
  652. };
  653. SmallInteger.prototype.neq = SmallInteger.prototype.notEquals = BigInteger.prototype.neq = BigInteger.prototype.notEquals;
  654. BigInteger.prototype.greater = function (v) {
  655. return this.compare(v) > 0;
  656. };
  657. SmallInteger.prototype.gt = SmallInteger.prototype.greater = BigInteger.prototype.gt = BigInteger.prototype.greater;
  658. BigInteger.prototype.lesser = function (v) {
  659. return this.compare(v) < 0;
  660. };
  661. SmallInteger.prototype.lt = SmallInteger.prototype.lesser = BigInteger.prototype.lt = BigInteger.prototype.lesser;
  662. BigInteger.prototype.greaterOrEquals = function (v) {
  663. return this.compare(v) >= 0;
  664. };
  665. SmallInteger.prototype.geq = SmallInteger.prototype.greaterOrEquals = BigInteger.prototype.geq = BigInteger.prototype.greaterOrEquals;
  666. BigInteger.prototype.lesserOrEquals = function (v) {
  667. return this.compare(v) <= 0;
  668. };
  669. SmallInteger.prototype.leq = SmallInteger.prototype.lesserOrEquals = BigInteger.prototype.leq = BigInteger.prototype.lesserOrEquals;
  670. BigInteger.prototype.isEven = function () {
  671. return (this.value[0] & 1) === 0;
  672. };
  673. SmallInteger.prototype.isEven = function () {
  674. return (this.value & 1) === 0;
  675. };
  676. BigInteger.prototype.isOdd = function () {
  677. return (this.value[0] & 1) === 1;
  678. };
  679. SmallInteger.prototype.isOdd = function () {
  680. return (this.value & 1) === 1;
  681. };
  682. BigInteger.prototype.isPositive = function () {
  683. return !this.sign;
  684. };
  685. SmallInteger.prototype.isPositive = function () {
  686. return this.value > 0;
  687. };
  688. BigInteger.prototype.isNegative = function () {
  689. return this.sign;
  690. };
  691. SmallInteger.prototype.isNegative = function () {
  692. return this.value < 0;
  693. };
  694. BigInteger.prototype.isUnit = function () {
  695. return false;
  696. };
  697. SmallInteger.prototype.isUnit = function () {
  698. return Math.abs(this.value) === 1;
  699. };
  700. BigInteger.prototype.isZero = function () {
  701. return false;
  702. };
  703. SmallInteger.prototype.isZero = function () {
  704. return this.value === 0;
  705. };
  706. BigInteger.prototype.isDivisibleBy = function (v) {
  707. var n = parseValue(v);
  708. var value = n.value;
  709. if (value === 0) return false;
  710. if (value === 1) return true;
  711. if (value === 2) return this.isEven();
  712. return this.mod(n).equals(Integer[0]);
  713. };
  714. SmallInteger.prototype.isDivisibleBy = BigInteger.prototype.isDivisibleBy;
  715. function isBasicPrime(v) {
  716. var n = v.abs();
  717. if (n.isUnit()) return false;
  718. if (n.equals(2) || n.equals(3) || n.equals(5)) return true;
  719. if (n.isEven() || n.isDivisibleBy(3) || n.isDivisibleBy(5)) return false;
  720. if (n.lesser(25)) return true;
  721. // we don't know if it's prime: let the other functions figure it out
  722. }
  723. BigInteger.prototype.isPrime = function () {
  724. var isPrime = isBasicPrime(this);
  725. if (isPrime !== undefined) return isPrime;
  726. var n = this.abs(),
  727. nPrev = n.prev();
  728. var a = [2, 3, 5, 7, 11, 13, 17, 19],
  729. b = nPrev,
  730. d, t, i, x;
  731. while (b.isEven()) b = b.divide(2);
  732. for (i = 0; i < a.length; i++) {
  733. x = bigInt(a[i]).modPow(b, n);
  734. if (x.equals(Integer[1]) || x.equals(nPrev)) continue;
  735. for (t = true, d = b; t && d.lesser(nPrev) ; d = d.multiply(2)) {
  736. x = x.square().mod(n);
  737. if (x.equals(nPrev)) t = false;
  738. }
  739. if (t) return false;
  740. }
  741. return true;
  742. };
  743. SmallInteger.prototype.isPrime = BigInteger.prototype.isPrime;
  744. BigInteger.prototype.isProbablePrime = function (iterations) {
  745. var isPrime = isBasicPrime(this);
  746. if (isPrime !== undefined) return isPrime;
  747. var n = this.abs();
  748. var t = iterations === undefined ? 5 : iterations;
  749. // use the Fermat primality test
  750. for (var i = 0; i < t; i++) {
  751. var a = bigInt.randBetween(2, n.minus(2));
  752. if (!a.modPow(n.prev(), n).isUnit()) return false; // definitely composite
  753. }
  754. return true; // large chance of being prime
  755. };
  756. SmallInteger.prototype.isProbablePrime = BigInteger.prototype.isProbablePrime;
  757. BigInteger.prototype.modInv = function (n) {
  758. var t = bigInt.zero, newT = bigInt.one, r = parseValue(n), newR = this.abs(), q, lastT, lastR;
  759. while (!newR.equals(bigInt.zero)) {
  760. q = r.divide(newR);
  761. lastT = t;
  762. lastR = r;
  763. t = newT;
  764. r = newR;
  765. newT = lastT.subtract(q.multiply(newT));
  766. newR = lastR.subtract(q.multiply(newR));
  767. }
  768. if (!r.equals(1)) throw new Error(this.toString() + " and " + n.toString() + " are not co-prime");
  769. if (t.compare(0) === -1) {
  770. t = t.add(n);
  771. }
  772. if (this.isNegative()) {
  773. return t.negate();
  774. }
  775. return t;
  776. };
  777. SmallInteger.prototype.modInv = BigInteger.prototype.modInv;
  778. BigInteger.prototype.next = function () {
  779. var value = this.value;
  780. if (this.sign) {
  781. return subtractSmall(value, 1, this.sign);
  782. }
  783. return new BigInteger(addSmall(value, 1), this.sign);
  784. };
  785. SmallInteger.prototype.next = function () {
  786. var value = this.value;
  787. if (value + 1 < MAX_INT) return new SmallInteger(value + 1);
  788. return new BigInteger(MAX_INT_ARR, false);
  789. };
  790. BigInteger.prototype.prev = function () {
  791. var value = this.value;
  792. if (this.sign) {
  793. return new BigInteger(addSmall(value, 1), true);
  794. }
  795. return subtractSmall(value, 1, this.sign);
  796. };
  797. SmallInteger.prototype.prev = function () {
  798. var value = this.value;
  799. if (value - 1 > -MAX_INT) return new SmallInteger(value - 1);
  800. return new BigInteger(MAX_INT_ARR, true);
  801. };
  802. var powersOfTwo = [1];
  803. while (powersOfTwo[powersOfTwo.length - 1] <= BASE) powersOfTwo.push(2 * powersOfTwo[powersOfTwo.length - 1]);
  804. var powers2Length = powersOfTwo.length, highestPower2 = powersOfTwo[powers2Length - 1];
  805. function shift_isSmall(n) {
  806. return ((typeof n === "number" || typeof n === "string") && +Math.abs(n) <= BASE) ||
  807. (n instanceof BigInteger && n.value.length <= 1);
  808. }
  809. BigInteger.prototype.shiftLeft = function (n) {
  810. if (!shift_isSmall(n)) {
  811. throw new Error(String(n) + " is too large for shifting.");
  812. }
  813. n = +n;
  814. if (n < 0) return this.shiftRight(-n);
  815. var result = this;
  816. while (n >= powers2Length) {
  817. result = result.multiply(highestPower2);
  818. n -= powers2Length - 1;
  819. }
  820. return result.multiply(powersOfTwo[n]);
  821. };
  822. SmallInteger.prototype.shiftLeft = BigInteger.prototype.shiftLeft;
  823. BigInteger.prototype.shiftRight = function (n) {
  824. var remQuo;
  825. if (!shift_isSmall(n)) {
  826. throw new Error(String(n) + " is too large for shifting.");
  827. }
  828. n = +n;
  829. if (n < 0) return this.shiftLeft(-n);
  830. var result = this;
  831. while (n >= powers2Length) {
  832. if (result.isZero()) return result;
  833. remQuo = divModAny(result, highestPower2);
  834. result = remQuo[1].isNegative() ? remQuo[0].prev() : remQuo[0];
  835. n -= powers2Length - 1;
  836. }
  837. remQuo = divModAny(result, powersOfTwo[n]);
  838. return remQuo[1].isNegative() ? remQuo[0].prev() : remQuo[0];
  839. };
  840. SmallInteger.prototype.shiftRight = BigInteger.prototype.shiftRight;
  841. function bitwise(x, y, fn) {
  842. y = parseValue(y);
  843. var xSign = x.isNegative(), ySign = y.isNegative();
  844. var xRem = xSign ? x.not() : x,
  845. yRem = ySign ? y.not() : y;
  846. var xBits = [], yBits = [];
  847. var xStop = false, yStop = false;
  848. while (!xStop || !yStop) {
  849. if (xRem.isZero()) { // virtual sign extension for simulating two's complement
  850. xStop = true;
  851. xBits.push(xSign ? 1 : 0);
  852. }
  853. else if (xSign) xBits.push(xRem.isEven() ? 1 : 0); // two's complement for negative numbers
  854. else xBits.push(xRem.isEven() ? 0 : 1);
  855. if (yRem.isZero()) {
  856. yStop = true;
  857. yBits.push(ySign ? 1 : 0);
  858. }
  859. else if (ySign) yBits.push(yRem.isEven() ? 1 : 0);
  860. else yBits.push(yRem.isEven() ? 0 : 1);
  861. xRem = xRem.over(2);
  862. yRem = yRem.over(2);
  863. }
  864. var result = [];
  865. for (var i = 0; i < xBits.length; i++) result.push(fn(xBits[i], yBits[i]));
  866. var sum = bigInt(result.pop()).negate().times(bigInt(2).pow(result.length));
  867. while (result.length) {
  868. sum = sum.add(bigInt(result.pop()).times(bigInt(2).pow(result.length)));
  869. }
  870. return sum;
  871. }
  872. BigInteger.prototype.not = function () {
  873. return this.negate().prev();
  874. };
  875. SmallInteger.prototype.not = BigInteger.prototype.not;
  876. BigInteger.prototype.and = function (n) {
  877. return bitwise(this, n, function (a, b) { return a & b; });
  878. };
  879. SmallInteger.prototype.and = BigInteger.prototype.and;
  880. BigInteger.prototype.or = function (n) {
  881. return bitwise(this, n, function (a, b) { return a | b; });
  882. };
  883. SmallInteger.prototype.or = BigInteger.prototype.or;
  884. BigInteger.prototype.xor = function (n) {
  885. return bitwise(this, n, function (a, b) { return a ^ b; });
  886. };
  887. SmallInteger.prototype.xor = BigInteger.prototype.xor;
  888. var LOBMASK_I = 1 << 30, LOBMASK_BI = (BASE & -BASE) * (BASE & -BASE) | LOBMASK_I;
  889. function roughLOB(n) { // get lowestOneBit (rough)
  890. // SmallInteger: return Min(lowestOneBit(n), 1 << 30)
  891. // BigInteger: return Min(lowestOneBit(n), 1 << 14) [BASE=1e7]
  892. var v = n.value, x = typeof v === "number" ? v | LOBMASK_I : v[0] + v[1] * BASE | LOBMASK_BI;
  893. return x & -x;
  894. }
  895. function max(a, b) {
  896. a = parseValue(a);
  897. b = parseValue(b);
  898. return a.greater(b) ? a : b;
  899. }
  900. function min(a, b) {
  901. a = parseValue(a);
  902. b = parseValue(b);
  903. return a.lesser(b) ? a : b;
  904. }
  905. function gcd(a, b) {
  906. a = parseValue(a).abs();
  907. b = parseValue(b).abs();
  908. if (a.equals(b)) return a;
  909. if (a.isZero()) return b;
  910. if (b.isZero()) return a;
  911. var c = Integer[1], d, t;
  912. while (a.isEven() && b.isEven()) {
  913. d = Math.min(roughLOB(a), roughLOB(b));
  914. a = a.divide(d);
  915. b = b.divide(d);
  916. c = c.multiply(d);
  917. }
  918. while (a.isEven()) {
  919. a = a.divide(roughLOB(a));
  920. }
  921. do {
  922. while (b.isEven()) {
  923. b = b.divide(roughLOB(b));
  924. }
  925. if (a.greater(b)) {
  926. t = b; b = a; a = t;
  927. }
  928. b = b.subtract(a);
  929. } while (!b.isZero());
  930. return c.isUnit() ? a : a.multiply(c);
  931. }
  932. function lcm(a, b) {
  933. a = parseValue(a).abs();
  934. b = parseValue(b).abs();
  935. return a.divide(gcd(a, b)).multiply(b);
  936. }
  937. function randBetween(a, b) {
  938. a = parseValue(a);
  939. b = parseValue(b);
  940. var low = min(a, b), high = max(a, b);
  941. var range = high.subtract(low);
  942. if (range.isSmall) return low.add(Math.round(Math.random() * range));
  943. var length = range.value.length - 1;
  944. var result = [], restricted = true;
  945. for (var i = length; i >= 0; i--) {
  946. var top = restricted ? range.value[i] : BASE;
  947. var digit = truncate(Math.random() * top);
  948. result.unshift(digit);
  949. if (digit < top) restricted = false;
  950. }
  951. result = arrayToSmall(result);
  952. return low.add(typeof result === "number" ? new SmallInteger(result) : new BigInteger(result, false));
  953. }
  954. var parseBase = function (text, base) {
  955. var length = text.length;
  956. if (2 <= base && base <= 36) {
  957. if (length <= LOG_MAX_INT / Math.log(base)) {
  958. return new SmallInteger(parseInt(text, base));
  959. }
  960. }
  961. base = parseValue(base);
  962. var digits = [];
  963. var i;
  964. var isNegative = text[0] === "-";
  965. for (i = isNegative ? 1 : 0; i < text.length; i++) {
  966. var c = text[i].toLowerCase(),
  967. charCode = c.charCodeAt(0);
  968. if (48 <= charCode && charCode <= 57) digits.push(parseValue(c));
  969. else if (97 <= charCode && charCode <= 122) digits.push(parseValue(c.charCodeAt(0) - 87));
  970. else if (c === "<") {
  971. var start = i;
  972. do { i++; } while (text[i] !== ">");
  973. digits.push(parseValue(text.slice(start + 1, i)));
  974. }
  975. else throw new Error(c + " is not a valid character");
  976. }
  977. return parseBaseFromArray(digits, base, isNegative);
  978. };
  979. function parseBaseFromArray(digits, base, isNegative) {
  980. var val = Integer[0], pow = Integer[1], i;
  981. for (i = digits.length - 1; i >= 0; i--) {
  982. val = val.add(digits[i].times(pow));
  983. pow = pow.times(base);
  984. }
  985. return isNegative ? val.negate() : val;
  986. }
  987. function stringify(digit) {
  988. var v = digit.value;
  989. if (typeof v === "number") v = [v];
  990. if (v.length === 1 && v[0] <= 35) {
  991. return "0123456789abcdefghijklmnopqrstuvwxyz".charAt(v[0]);
  992. }
  993. return "<" + v + ">";
  994. }
  995. function toBase(n, base) {
  996. base = bigInt(base);
  997. if (base.isZero()) {
  998. if (n.isZero()) return "0";
  999. throw new Error("Cannot convert nonzero numbers to base 0.");
  1000. }
  1001. if (base.equals(-1)) {
  1002. if (n.isZero()) return "0";
  1003. if (n.isNegative()) return new Array(1 - n).join("10");
  1004. return "1" + new Array(+n).join("01");
  1005. }
  1006. var minusSign = "";
  1007. if (n.isNegative() && base.isPositive()) {
  1008. minusSign = "-";
  1009. n = n.abs();
  1010. }
  1011. if (base.equals(1)) {
  1012. if (n.isZero()) return "0";
  1013. return minusSign + new Array(+n + 1).join(1);
  1014. }
  1015. var out = [];
  1016. var left = n, divmod;
  1017. while (left.isNegative() || left.compareAbs(base) >= 0) {
  1018. divmod = left.divmod(base);
  1019. left = divmod.quotient;
  1020. var digit = divmod.remainder;
  1021. if (digit.isNegative()) {
  1022. digit = base.minus(digit).abs();
  1023. left = left.next();
  1024. }
  1025. out.push(stringify(digit));
  1026. }
  1027. out.push(stringify(left));
  1028. return minusSign + out.reverse().join("");
  1029. }
  1030. BigInteger.prototype.toString = function (radix) {
  1031. if (radix === undefined) radix = 10;
  1032. if (radix !== 10) return toBase(this, radix);
  1033. var v = this.value, l = v.length, str = String(v[--l]), zeros = "0000000", digit;
  1034. while (--l >= 0) {
  1035. digit = String(v[l]);
  1036. str += zeros.slice(digit.length) + digit;
  1037. }
  1038. var sign = this.sign ? "-" : "";
  1039. return sign + str;
  1040. };
  1041. SmallInteger.prototype.toString = function (radix) {
  1042. if (radix === undefined) radix = 10;
  1043. if (radix != 10) return toBase(this, radix);
  1044. return String(this.value);
  1045. };
  1046. BigInteger.prototype.valueOf = function () {
  1047. return +this.toString();
  1048. };
  1049. BigInteger.prototype.toJSNumber = BigInteger.prototype.valueOf;
  1050. SmallInteger.prototype.valueOf = function () {
  1051. return this.value;
  1052. };
  1053. SmallInteger.prototype.toJSNumber = SmallInteger.prototype.valueOf;
  1054. function parseStringValue(v) {
  1055. if (isPrecise(+v)) {
  1056. var x = +v;
  1057. if (x === truncate(x))
  1058. return new SmallInteger(x);
  1059. throw "Invalid integer: " + v;
  1060. }
  1061. var sign = v[0] === "-";
  1062. if (sign) v = v.slice(1);
  1063. var split = v.split(/e/i);
  1064. if (split.length > 2) throw new Error("Invalid integer: " + split.join("e"));
  1065. if (split.length === 2) {
  1066. var exp = split[1];
  1067. if (exp[0] === "+") exp = exp.slice(1);
  1068. exp = +exp;
  1069. if (exp !== truncate(exp) || !isPrecise(exp)) throw new Error("Invalid integer: " + exp + " is not a valid exponent.");
  1070. var text = split[0];
  1071. var decimalPlace = text.indexOf(".");
  1072. if (decimalPlace >= 0) {
  1073. exp -= text.length - decimalPlace - 1;
  1074. text = text.slice(0, decimalPlace) + text.slice(decimalPlace + 1);
  1075. }
  1076. if (exp < 0) throw new Error("Cannot include negative exponent part for integers");
  1077. text += (new Array(exp + 1)).join("0");
  1078. v = text;
  1079. }
  1080. var isValid = /^([0-9][0-9]*)$/.test(v);
  1081. if (!isValid) throw new Error("Invalid integer: " + v);
  1082. var r = [], max = v.length, l = LOG_BASE, min = max - l;
  1083. while (max > 0) {
  1084. r.push(+v.slice(min, max));
  1085. min -= l;
  1086. if (min < 0) min = 0;
  1087. max -= l;
  1088. }
  1089. trim(r);
  1090. return new BigInteger(r, sign);
  1091. }
  1092. function parseNumberValue(v) {
  1093. if (isPrecise(v)) {
  1094. if (v !== truncate(v)) throw new Error(v + " is not an integer.");
  1095. return new SmallInteger(v);
  1096. }
  1097. return parseStringValue(v.toString());
  1098. }
  1099. function parseValue(v) {
  1100. if (typeof v === "number") {
  1101. return parseNumberValue(v);
  1102. }
  1103. if (typeof v === "string") {
  1104. return parseStringValue(v);
  1105. }
  1106. return v;
  1107. }
  1108. // Pre-define numbers in range [-999,999]
  1109. for (var i = 0; i < 1000; i++) {
  1110. Integer[i] = new SmallInteger(i);
  1111. if (i > 0) Integer[-i] = new SmallInteger(-i);
  1112. }
  1113. // Backwards compatibility
  1114. Integer.one = Integer[1];
  1115. Integer.zero = Integer[0];
  1116. Integer.minusOne = Integer[-1];
  1117. Integer.max = max;
  1118. Integer.min = min;
  1119. Integer.gcd = gcd;
  1120. Integer.lcm = lcm;
  1121. Integer.isInstance = function (x) { return x instanceof BigInteger || x instanceof SmallInteger; };
  1122. Integer.randBetween = randBetween;
  1123. Integer.fromArray = function (digits, base, isNegative) {
  1124. return parseBaseFromArray(digits.map(parseValue), parseValue(base || 10), isNegative);
  1125. };
  1126. return Integer;
  1127. })();
  1128. // Node.js check
  1129. if (typeof module !== "undefined" && module.hasOwnProperty("exports")) {
  1130. module.exports = bigInt;
  1131. }
  1132. //amd check
  1133. if ( typeof define === "function" && define.amd ) {
  1134. define( "big-integer", [], function() {
  1135. return bigInt;
  1136. });
  1137. }
  1138. },{}],2:[function(require,module,exports){
  1139. /**
  1140. * lodash (Custom Build) <https://lodash.com/>
  1141. * Build: `lodash modularize exports="npm" -o ./`
  1142. * Copyright jQuery Foundation and other contributors <https://jquery.org/>
  1143. * Released under MIT license <https://lodash.com/license>
  1144. * Based on Underscore.js 1.8.3 <http://underscorejs.org/LICENSE>
  1145. * Copyright Jeremy Ashkenas, DocumentCloud and Investigative Reporters & Editors
  1146. */
  1147. /** Used as references for various `Number` constants. */
  1148. var MAX_SAFE_INTEGER = 9007199254740991;
  1149. /** `Object#toString` result references. */
  1150. var argsTag = '[object Arguments]',
  1151. funcTag = '[object Function]',
  1152. genTag = '[object GeneratorFunction]';
  1153. /** Used to detect unsigned integer values. */
  1154. var reIsUint = /^(?:0|[1-9]\d*)$/;
  1155. /**
  1156. * A faster alternative to `Function#apply`, this function invokes `func`
  1157. * with the `this` binding of `thisArg` and the arguments of `args`.
  1158. *
  1159. * @private
  1160. * @param {Function} func The function to invoke.
  1161. * @param {*} thisArg The `this` binding of `func`.
  1162. * @param {Array} args The arguments to invoke `func` with.
  1163. * @returns {*} Returns the result of `func`.
  1164. */
  1165. function apply(func, thisArg, args) {
  1166. switch (args.length) {
  1167. case 0: return func.call(thisArg);
  1168. case 1: return func.call(thisArg, args[0]);
  1169. case 2: return func.call(thisArg, args[0], args[1]);
  1170. case 3: return func.call(thisArg, args[0], args[1], args[2]);
  1171. }
  1172. return func.apply(thisArg, args);
  1173. }
  1174. /**
  1175. * The base implementation of `_.times` without support for iteratee shorthands
  1176. * or max array length checks.
  1177. *
  1178. * @private
  1179. * @param {number} n The number of times to invoke `iteratee`.
  1180. * @param {Function} iteratee The function invoked per iteration.
  1181. * @returns {Array} Returns the array of results.
  1182. */
  1183. function baseTimes(n, iteratee) {
  1184. var index = -1,
  1185. result = Array(n);
  1186. while (++index < n) {
  1187. result[index] = iteratee(index);
  1188. }
  1189. return result;
  1190. }
  1191. /**
  1192. * Creates a unary function that invokes `func` with its argument transformed.
  1193. *
  1194. * @private
  1195. * @param {Function} func The function to wrap.
  1196. * @param {Function} transform The argument transform.
  1197. * @returns {Function} Returns the new function.
  1198. */
  1199. function overArg(func, transform) {
  1200. return function(arg) {
  1201. return func(transform(arg));
  1202. };
  1203. }
  1204. /** Used for built-in method references. */
  1205. var objectProto = Object.prototype;
  1206. /** Used to check objects for own properties. */
  1207. var hasOwnProperty = objectProto.hasOwnProperty;
  1208. /**
  1209. * Used to resolve the
  1210. * [`toStringTag`](http://ecma-international.org/ecma-262/7.0/#sec-object.prototype.tostring)
  1211. * of values.
  1212. */
  1213. var objectToString = objectProto.toString;
  1214. /** Built-in value references. */
  1215. var propertyIsEnumerable = objectProto.propertyIsEnumerable;
  1216. /* Built-in method references for those with the same name as other `lodash` methods. */
  1217. var nativeKeys = overArg(Object.keys, Object),
  1218. nativeMax = Math.max;
  1219. /** Detect if properties shadowing those on `Object.prototype` are non-enumerable. */
  1220. var nonEnumShadows = !propertyIsEnumerable.call({ 'valueOf': 1 }, 'valueOf');
  1221. /**
  1222. * Creates an array of the enumerable property names of the array-like `value`.
  1223. *
  1224. * @private
  1225. * @param {*} value The value to query.
  1226. * @param {boolean} inherited Specify returning inherited property names.
  1227. * @returns {Array} Returns the array of property names.
  1228. */
  1229. function arrayLikeKeys(value, inherited) {
  1230. // Safari 8.1 makes `arguments.callee` enumerable in strict mode.
  1231. // Safari 9 makes `arguments.length` enumerable in strict mode.
  1232. var result = (isArray(value) || isArguments(value))
  1233. ? baseTimes(value.length, String)
  1234. : [];
  1235. var length = result.length,
  1236. skipIndexes = !!length;
  1237. for (var key in value) {
  1238. if ((inherited || hasOwnProperty.call(value, key)) &&
  1239. !(skipIndexes && (key == 'length' || isIndex(key, length)))) {
  1240. result.push(key);
  1241. }
  1242. }
  1243. return result;
  1244. }
  1245. /**
  1246. * Assigns `value` to `key` of `object` if the existing value is not equivalent
  1247. * using [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)
  1248. * for equality comparisons.
  1249. *
  1250. * @private
  1251. * @param {Object} object The object to modify.
  1252. * @param {string} key The key of the property to assign.
  1253. * @param {*} value The value to assign.
  1254. */
  1255. function assignValue(object, key, value) {
  1256. var objValue = object[key];
  1257. if (!(hasOwnProperty.call(object, key) && eq(objValue, value)) ||
  1258. (value === undefined && !(key in object))) {
  1259. object[key] = value;
  1260. }
  1261. }
  1262. /**
  1263. * The base implementation of `_.keys` which doesn't treat sparse arrays as dense.
  1264. *
  1265. * @private
  1266. * @param {Object} object The object to query.
  1267. * @returns {Array} Returns the array of property names.
  1268. */
  1269. function baseKeys(object) {
  1270. if (!isPrototype(object)) {
  1271. return nativeKeys(object);
  1272. }
  1273. var result = [];
  1274. for (var key in Object(object)) {
  1275. if (hasOwnProperty.call(object, key) && key != 'constructor') {
  1276. result.push(key);
  1277. }
  1278. }
  1279. return result;
  1280. }
  1281. /**
  1282. * The base implementation of `_.rest` which doesn't validate or coerce arguments.
  1283. *
  1284. * @private
  1285. * @param {Function} func The function to apply a rest parameter to.
  1286. * @param {number} [start=func.length-1] The start position of the rest parameter.
  1287. * @returns {Function} Returns the new function.
  1288. */
  1289. function baseRest(func, start) {
  1290. start = nativeMax(start === undefined ? (func.length - 1) : start, 0);
  1291. return function() {
  1292. var args = arguments,
  1293. index = -1,
  1294. length = nativeMax(args.length - start, 0),
  1295. array = Array(length);
  1296. while (++index < length) {
  1297. array[index] = args[start + index];
  1298. }
  1299. index = -1;
  1300. var otherArgs = Array(start + 1);
  1301. while (++index < start) {
  1302. otherArgs[index] = args[index];
  1303. }
  1304. otherArgs[start] = array;
  1305. return apply(func, this, otherArgs);
  1306. };
  1307. }
  1308. /**
  1309. * Copies properties of `source` to `object`.
  1310. *
  1311. * @private
  1312. * @param {Object} source The object to copy properties from.
  1313. * @param {Array} props The property identifiers to copy.
  1314. * @param {Object} [object={}] The object to copy properties to.
  1315. * @param {Function} [customizer] The function to customize copied values.
  1316. * @returns {Object} Returns `object`.
  1317. */
  1318. function copyObject(source, props, object, customizer) {
  1319. object || (object = {});
  1320. var index = -1,
  1321. length = props.length;
  1322. while (++index < length) {
  1323. var key = props[index];
  1324. var newValue = customizer
  1325. ? customizer(object[key], source[key], key, object, source)
  1326. : undefined;
  1327. assignValue(object, key, newValue === undefined ? source[key] : newValue);
  1328. }
  1329. return object;
  1330. }
  1331. /**
  1332. * Creates a function like `_.assign`.
  1333. *
  1334. * @private
  1335. * @param {Function} assigner The function to assign values.
  1336. * @returns {Function} Returns the new assigner function.
  1337. */
  1338. function createAssigner(assigner) {
  1339. return baseRest(function(object, sources) {
  1340. var index = -1,
  1341. length = sources.length,
  1342. customizer = length > 1 ? sources[length - 1] : undefined,
  1343. guard = length > 2 ? sources[2] : undefined;
  1344. customizer = (assigner.length > 3 && typeof customizer == 'function')
  1345. ? (length--, customizer)
  1346. : undefined;
  1347. if (guard && isIterateeCall(sources[0], sources[1], guard)) {
  1348. customizer = length < 3 ? undefined : customizer;
  1349. length = 1;
  1350. }
  1351. object = Object(object);
  1352. while (++index < length) {
  1353. var source = sources[index];
  1354. if (source) {
  1355. assigner(object, source, index, customizer);
  1356. }
  1357. }
  1358. return object;
  1359. });
  1360. }
  1361. /**
  1362. * Checks if `value` is a valid array-like index.
  1363. *
  1364. * @private
  1365. * @param {*} value The value to check.
  1366. * @param {number} [length=MAX_SAFE_INTEGER] The upper bounds of a valid index.
  1367. * @returns {boolean} Returns `true` if `value` is a valid index, else `false`.
  1368. */
  1369. function isIndex(value, length) {
  1370. length = length == null ? MAX_SAFE_INTEGER : length;
  1371. return !!length &&
  1372. (typeof value == 'number' || reIsUint.test(value)) &&
  1373. (value > -1 && value % 1 == 0 && value < length);
  1374. }
  1375. /**
  1376. * Checks if the given arguments are from an iteratee call.
  1377. *
  1378. * @private
  1379. * @param {*} value The potential iteratee value argument.
  1380. * @param {*} index The potential iteratee index or key argument.
  1381. * @param {*} object The potential iteratee object argument.
  1382. * @returns {boolean} Returns `true` if the arguments are from an iteratee call,
  1383. * else `false`.
  1384. */
  1385. function isIterateeCall(value, index, object) {
  1386. if (!isObject(object)) {
  1387. return false;
  1388. }
  1389. var type = typeof index;
  1390. if (type == 'number'
  1391. ? (isArrayLike(object) && isIndex(index, object.length))
  1392. : (type == 'string' && index in object)
  1393. ) {
  1394. return eq(object[index], value);
  1395. }
  1396. return false;
  1397. }
  1398. /**
  1399. * Checks if `value` is likely a prototype object.
  1400. *
  1401. * @private
  1402. * @param {*} value The value to check.
  1403. * @returns {boolean} Returns `true` if `value` is a prototype, else `false`.
  1404. */
  1405. function isPrototype(value) {
  1406. var Ctor = value && value.constructor,
  1407. proto = (typeof Ctor == 'function' && Ctor.prototype) || objectProto;
  1408. return value === proto;
  1409. }
  1410. /**
  1411. * Performs a
  1412. * [`SameValueZero`](http://ecma-international.org/ecma-262/7.0/#sec-samevaluezero)
  1413. * comparison between two values to determine if they are equivalent.
  1414. *
  1415. * @static
  1416. * @memberOf _
  1417. * @since 4.0.0
  1418. * @category Lang
  1419. * @param {*} value The value to compare.
  1420. * @param {*} other The other value to compare.
  1421. * @returns {boolean} Returns `true` if the values are equivalent, else `false`.
  1422. * @example
  1423. *
  1424. * var object = { 'a': 1 };
  1425. * var other = { 'a': 1 };
  1426. *
  1427. * _.eq(object, object);
  1428. * // => true
  1429. *
  1430. * _.eq(object, other);
  1431. * // => false
  1432. *
  1433. * _.eq('a', 'a');
  1434. * // => true
  1435. *
  1436. * _.eq('a', Object('a'));
  1437. * // => false
  1438. *
  1439. * _.eq(NaN, NaN);
  1440. * // => true
  1441. */
  1442. function eq(value, other) {
  1443. return value === other || (value !== value && other !== other);
  1444. }
  1445. /**
  1446. * Checks if `value` is likely an `arguments` object.
  1447. *
  1448. * @static
  1449. * @memberOf _
  1450. * @since 0.1.0
  1451. * @category Lang
  1452. * @param {*} value The value to check.
  1453. * @returns {boolean} Returns `true` if `value` is an `arguments` object,
  1454. * else `false`.
  1455. * @example
  1456. *
  1457. * _.isArguments(function() { return arguments; }());
  1458. * // => true
  1459. *
  1460. * _.isArguments([1, 2, 3]);
  1461. * // => false
  1462. */
  1463. function isArguments(value) {
  1464. // Safari 8.1 makes `arguments.callee` enumerable in strict mode.
  1465. return isArrayLikeObject(value) && hasOwnProperty.call(value, 'callee') &&
  1466. (!propertyIsEnumerable.call(value, 'callee') || objectToString.call(value) == argsTag);
  1467. }
  1468. /**
  1469. * Checks if `value` is classified as an `Array` object.
  1470. *
  1471. * @static
  1472. * @memberOf _
  1473. * @since 0.1.0
  1474. * @category Lang
  1475. * @param {*} value The value to check.
  1476. * @returns {boolean} Returns `true` if `value` is an array, else `false`.
  1477. * @example
  1478. *
  1479. * _.isArray([1, 2, 3]);
  1480. * // => true
  1481. *
  1482. * _.isArray(document.body.children);
  1483. * // => false
  1484. *
  1485. * _.isArray('abc');
  1486. * // => false
  1487. *
  1488. * _.isArray(_.noop);
  1489. * // => false
  1490. */
  1491. var isArray = Array.isArray;
  1492. /**
  1493. * Checks if `value` is array-like. A value is considered array-like if it's
  1494. * not a function and has a `value.length` that's an integer greater than or
  1495. * equal to `0` and less than or equal to `Number.MAX_SAFE_INTEGER`.
  1496. *
  1497. * @static
  1498. * @memberOf _
  1499. * @since 4.0.0
  1500. * @category Lang
  1501. * @param {*} value The value to check.
  1502. * @returns {boolean} Returns `true` if `value` is array-like, else `false`.
  1503. * @example
  1504. *
  1505. * _.isArrayLike([1, 2, 3]);
  1506. * // => true
  1507. *
  1508. * _.isArrayLike(document.body.children);
  1509. * // => true
  1510. *
  1511. * _.isArrayLike('abc');
  1512. * // => true
  1513. *
  1514. * _.isArrayLike(_.noop);
  1515. * // => false
  1516. */
  1517. function isArrayLike(value) {
  1518. return value != null && isLength(value.length) && !isFunction(value);
  1519. }
  1520. /**
  1521. * This method is like `_.isArrayLike` except that it also checks if `value`
  1522. * is an object.
  1523. *
  1524. * @static
  1525. * @memberOf _
  1526. * @since 4.0.0
  1527. * @category Lang
  1528. * @param {*} value The value to check.
  1529. * @returns {boolean} Returns `true` if `value` is an array-like object,
  1530. * else `false`.
  1531. * @example
  1532. *
  1533. * _.isArrayLikeObject([1, 2, 3]);
  1534. * // => true
  1535. *
  1536. * _.isArrayLikeObject(document.body.children);
  1537. * // => true
  1538. *
  1539. * _.isArrayLikeObject('abc');
  1540. * // => false
  1541. *
  1542. * _.isArrayLikeObject(_.noop);
  1543. * // => false
  1544. */
  1545. function isArrayLikeObject(value) {
  1546. return isObjectLike(value) && isArrayLike(value);
  1547. }
  1548. /**
  1549. * Checks if `value` is classified as a `Function` object.
  1550. *
  1551. * @static
  1552. * @memberOf _
  1553. * @since 0.1.0
  1554. * @category Lang
  1555. * @param {*} value The value to check.
  1556. * @returns {boolean} Returns `true` if `value` is a function, else `false`.
  1557. * @example
  1558. *
  1559. * _.isFunction(_);
  1560. * // => true
  1561. *
  1562. * _.isFunction(/abc/);
  1563. * // => false
  1564. */
  1565. function isFunction(value) {
  1566. // The use of `Object#toString` avoids issues with the `typeof` operator
  1567. // in Safari 8-9 which returns 'object' for typed array and other constructors.
  1568. var tag = isObject(value) ? objectToString.call(value) : '';
  1569. return tag == funcTag || tag == genTag;
  1570. }
  1571. /**
  1572. * Checks if `value` is a valid array-like length.
  1573. *
  1574. * **Note:** This method is loosely based on
  1575. * [`ToLength`](http://ecma-international.org/ecma-262/7.0/#sec-tolength).
  1576. *
  1577. * @static
  1578. * @memberOf _
  1579. * @since 4.0.0
  1580. * @category Lang
  1581. * @param {*} value The value to check.
  1582. * @returns {boolean} Returns `true` if `value` is a valid length, else `false`.
  1583. * @example
  1584. *
  1585. * _.isLength(3);
  1586. * // => true
  1587. *
  1588. * _.isLength(Number.MIN_VALUE);
  1589. * // => false
  1590. *
  1591. * _.isLength(Infinity);
  1592. * // => false
  1593. *
  1594. * _.isLength('3');
  1595. * // => false
  1596. */
  1597. function isLength(value) {
  1598. return typeof value == 'number' &&
  1599. value > -1 && value % 1 == 0 && value <= MAX_SAFE_INTEGER;
  1600. }
  1601. /**
  1602. * Checks if `value` is the
  1603. * [language type](http://www.ecma-international.org/ecma-262/7.0/#sec-ecmascript-language-types)
  1604. * of `Object`. (e.g. arrays, functions, objects, regexes, `new Number(0)`, and `new String('')`)
  1605. *
  1606. * @static
  1607. * @memberOf _
  1608. * @since 0.1.0
  1609. * @category Lang
  1610. * @param {*} value The value to check.
  1611. * @returns {boolean} Returns `true` if `value` is an object, else `false`.
  1612. * @example
  1613. *
  1614. * _.isObject({});
  1615. * // => true
  1616. *
  1617. * _.isObject([1, 2, 3]);
  1618. * // => true
  1619. *
  1620. * _.isObject(_.noop);
  1621. * // => true
  1622. *
  1623. * _.isObject(null);
  1624. * // => false
  1625. */
  1626. function isObject(value) {
  1627. var type = typeof value;
  1628. return !!value && (type == 'object' || type == 'function');
  1629. }
  1630. /**
  1631. * Checks if `value` is object-like. A value is object-like if it's not `null`
  1632. * and has a `typeof` result of "object".
  1633. *
  1634. * @static
  1635. * @memberOf _
  1636. * @since 4.0.0
  1637. * @category Lang
  1638. * @param {*} value The value to check.
  1639. * @returns {boolean} Returns `true` if `value` is object-like, else `false`.
  1640. * @example
  1641. *
  1642. * _.isObjectLike({});
  1643. * // => true
  1644. *
  1645. * _.isObjectLike([1, 2, 3]);
  1646. * // => true
  1647. *
  1648. * _.isObjectLike(_.noop);
  1649. * // => false
  1650. *
  1651. * _.isObjectLike(null);
  1652. * // => false
  1653. */
  1654. function isObjectLike(value) {
  1655. return !!value && typeof value == 'object';
  1656. }
  1657. /**
  1658. * Assigns own enumerable string keyed properties of source objects to the
  1659. * destination object. Source objects are applied from left to right.
  1660. * Subsequent sources overwrite property assignments of previous sources.
  1661. *
  1662. * **Note:** This method mutates `object` and is loosely based on
  1663. * [`Object.assign`](https://mdn.io/Object/assign).
  1664. *
  1665. * @static
  1666. * @memberOf _
  1667. * @since 0.10.0
  1668. * @category Object
  1669. * @param {Object} object The destination object.
  1670. * @param {...Object} [sources] The source objects.
  1671. * @returns {Object} Returns `object`.
  1672. * @see _.assignIn
  1673. * @example
  1674. *
  1675. * function Foo() {
  1676. * this.a = 1;
  1677. * }
  1678. *
  1679. * function Bar() {
  1680. * this.c = 3;
  1681. * }
  1682. *
  1683. * Foo.prototype.b = 2;
  1684. * Bar.prototype.d = 4;
  1685. *
  1686. * _.assign({ 'a': 0 }, new Foo, new Bar);
  1687. * // => { 'a': 1, 'c': 3 }
  1688. */
  1689. var assign = createAssigner(function(object, source) {
  1690. if (nonEnumShadows || isPrototype(source) || isArrayLike(source)) {
  1691. copyObject(source, keys(source), object);
  1692. return;
  1693. }
  1694. for (var key in source) {
  1695. if (hasOwnProperty.call(source, key)) {
  1696. assignValue(object, key, source[key]);
  1697. }
  1698. }
  1699. });
  1700. /**
  1701. * Creates an array of the own enumerable property names of `object`.
  1702. *
  1703. * **Note:** Non-object values are coerced to objects. See the
  1704. * [ES spec](http://ecma-international.org/ecma-262/7.0/#sec-object.keys)
  1705. * for more details.
  1706. *
  1707. * @static
  1708. * @since 0.1.0
  1709. * @memberOf _
  1710. * @category Object
  1711. * @param {Object} object The object to query.
  1712. * @returns {Array} Returns the array of property names.
  1713. * @example
  1714. *
  1715. * function Foo() {
  1716. * this.a = 1;
  1717. * this.b = 2;
  1718. * }
  1719. *
  1720. * Foo.prototype.c = 3;
  1721. *
  1722. * _.keys(new Foo);
  1723. * // => ['a', 'b'] (iteration order is not guaranteed)
  1724. *
  1725. * _.keys('hi');
  1726. * // => ['0', '1']
  1727. */
  1728. function keys(object) {
  1729. return isArrayLike(object) ? arrayLikeKeys(object) : baseKeys(object);
  1730. }
  1731. module.exports = assign;
  1732. },{}],3:[function(require,module,exports){
  1733. (function () {
  1734. 'use strict';
  1735. function utf8ToBinaryString(str) {
  1736. var escstr = encodeURIComponent(str);
  1737. // replaces any uri escape sequence, such as %0A,
  1738. // with binary escape, such as 0x0A
  1739. var binstr = escstr.replace(/%([0-9A-F]{2})/g, function(match, p1) {
  1740. return String.fromCharCode(parseInt(p1, 16));
  1741. });
  1742. return binstr;
  1743. }
  1744. function utf8ToBuffer(str) {
  1745. var binstr = utf8ToBinaryString(str);
  1746. var buf = binaryStringToBuffer(binstr);
  1747. return buf;
  1748. }
  1749. function utf8ToBase64(str) {
  1750. var binstr = utf8ToBinaryString(str);
  1751. return btoa(binstr);
  1752. }
  1753. function binaryStringToUtf8(binstr) {
  1754. var escstr = binstr.replace(/(.)/g, function (m, p) {
  1755. var code = p.charCodeAt(0).toString(16).toUpperCase();
  1756. if (code.length < 2) {
  1757. code = '0' + code;
  1758. }
  1759. return '%' + code;
  1760. });
  1761. return decodeURIComponent(escstr);
  1762. }
  1763. function bufferToUtf8(buf) {
  1764. var binstr = bufferToBinaryString(buf);
  1765. return binaryStringToUtf8(binstr);
  1766. }
  1767. function base64ToUtf8(b64) {
  1768. var binstr = atob(b64);
  1769. return binaryStringToUtf8(binstr);
  1770. }
  1771. function bufferToBinaryString(buf) {
  1772. var binstr = Array.prototype.map.call(buf, function (ch) {
  1773. return String.fromCharCode(ch);
  1774. }).join('');
  1775. return binstr;
  1776. }
  1777. function bufferToBase64(arr) {
  1778. var binstr = bufferToBinaryString(arr);
  1779. return btoa(binstr);
  1780. }
  1781. function binaryStringToBuffer(binstr) {
  1782. var buf;
  1783. if ('undefined' !== typeof Uint8Array) {
  1784. buf = new Uint8Array(binstr.length);
  1785. } else {
  1786. buf = [];
  1787. }
  1788. Array.prototype.forEach.call(binstr, function (ch, i) {
  1789. buf[i] = ch.charCodeAt(0);
  1790. });
  1791. return buf;
  1792. }
  1793. function base64ToBuffer(base64) {
  1794. var binstr = atob(base64);
  1795. var buf = binaryStringToBuffer(binstr);
  1796. return buf;
  1797. }
  1798. window.Unibabel = {
  1799. utf8ToBinaryString: utf8ToBinaryString
  1800. , utf8ToBuffer: utf8ToBuffer
  1801. , utf8ToBase64: utf8ToBase64
  1802. , binaryStringToUtf8: binaryStringToUtf8
  1803. , bufferToUtf8: bufferToUtf8
  1804. , base64ToUtf8: base64ToUtf8
  1805. , bufferToBinaryString: bufferToBinaryString
  1806. , bufferToBase64: bufferToBase64
  1807. , binaryStringToBuffer: binaryStringToBuffer
  1808. , base64ToBuffer: base64ToBuffer
  1809. // compat
  1810. , strToUtf8Arr: utf8ToBuffer
  1811. , utf8ArrToStr: bufferToUtf8
  1812. , arrToBase64: bufferToBase64
  1813. , base64ToArr: base64ToBuffer
  1814. };
  1815. }());
  1816. },{}],4:[function(require,module,exports){
  1817. (function () {
  1818. 'use strict';
  1819. function bufferToHex(arr) {
  1820. var i;
  1821. var len;
  1822. var hex = '';
  1823. var c;
  1824. for (i = 0, len = arr.length; i < len; i += 1) {
  1825. c = arr[i].toString(16);
  1826. if (c.length < 2) {
  1827. c = '0' + c;
  1828. }
  1829. hex += c;
  1830. }
  1831. return hex;
  1832. }
  1833. function hexToBuffer(hex) {
  1834. // TODO use Uint8Array or ArrayBuffer or DataView
  1835. var i;
  1836. var byteLen = hex.length / 2;
  1837. var arr;
  1838. var j = 0;
  1839. if (byteLen !== parseInt(byteLen, 10)) {
  1840. throw new Error("Invalid hex length '" + hex.length + "'");
  1841. }
  1842. arr = new Uint8Array(byteLen);
  1843. for (i = 0; i < byteLen; i += 1) {
  1844. arr[i] = parseInt(hex[j] + hex[j + 1], 16);
  1845. j += 2;
  1846. }
  1847. return arr;
  1848. }
  1849. // Hex Convenience Functions
  1850. window.Unibabel.hexToBuffer = hexToBuffer;
  1851. window.Unibabel.bufferToHex = bufferToHex;
  1852. }());
  1853. },{}],5:[function(require,module,exports){
  1854. require("unibabel");
  1855. require("unibabel/unibabel.hex");
  1856. module.exports = function(digest, string, salt) {
  1857. var algorithms = {
  1858. sha1: "SHA-1",
  1859. "sha-1": "SHA-1",
  1860. sha256: "SHA-256",
  1861. "sha-256": "SHA-256",
  1862. sha512: "SHA-512",
  1863. "sha-512": "SHA-512"
  1864. };
  1865. return window.crypto.subtle
  1866. .importKey(
  1867. "raw",
  1868. Unibabel.utf8ToBuffer(string),
  1869. {
  1870. name: "HMAC",
  1871. hash: { name: algorithms[digest.toLowerCase()] }
  1872. },
  1873. true,
  1874. ["sign", "verify"]
  1875. )
  1876. .then(function(key) {
  1877. return window.crypto.subtle
  1878. .sign({ name: "HMAC" }, key, Unibabel.utf8ToBuffer(salt || ""))
  1879. .then(function(signature) {
  1880. return Unibabel.bufferToHex(new Uint8Array(signature));
  1881. });
  1882. });
  1883. };
  1884. },{"unibabel":3,"unibabel/unibabel.hex":4}],6:[function(require,module,exports){
  1885. var v1 = require("./v1");
  1886. var v2 = require("./v2");
  1887. var hmac = require("./hmac");
  1888. module.exports = {
  1889. generatePassword: function(site, login, masterPassword, options) {
  1890. if (typeof options !== "undefined" && options.version === 1) {
  1891. return v1.generatePassword(site, login, masterPassword, options);
  1892. }
  1893. return v2.generatePassword(site, login, masterPassword, options);
  1894. },
  1895. createFingerprint: function(str) {
  1896. return hmac("sha256", str);
  1897. }
  1898. };
  1899. },{"./hmac":5,"./v1":8,"./v2":9}],7:[function(require,module,exports){
  1900. require("unibabel");
  1901. require("unibabel/unibabel.hex");
  1902. module.exports = function(password, salt, iterations, keylen, digest) {
  1903. var algorithms = {
  1904. sha1: "SHA-1",
  1905. "sha-1": "SHA-1",
  1906. sha256: "SHA-256",
  1907. "sha-256": "SHA-256",
  1908. sha512: "SHA-512",
  1909. "sha-512": "SHA-512"
  1910. };
  1911. return window.crypto.subtle
  1912. .importKey("raw", Unibabel.utf8ToBuffer(password), "PBKDF2", false, [
  1913. "deriveKey"
  1914. ])
  1915. .then(function(key) {
  1916. var algo = {
  1917. name: "PBKDF2",
  1918. salt: Unibabel.utf8ToBuffer(salt),
  1919. iterations: iterations,
  1920. hash: algorithms[digest.toLowerCase()]
  1921. };
  1922. return window.crypto.subtle.deriveKey(
  1923. algo,
  1924. key,
  1925. {
  1926. name: "AES-CTR",
  1927. length: keylen * 8
  1928. },
  1929. true,
  1930. ["encrypt", "decrypt"]
  1931. );
  1932. })
  1933. .then(function(derivedKey) {
  1934. return window.crypto.subtle
  1935. .exportKey("raw", derivedKey)
  1936. .then(function(keyArray) {
  1937. return Unibabel.bufferToHex(new Uint8Array(keyArray));
  1938. });
  1939. });
  1940. };
  1941. },{"unibabel":3,"unibabel/unibabel.hex":4}],8:[function(require,module,exports){
  1942. var pbkdf2 = require("./pbkdf2");
  1943. var assign = require("lodash.assign");
  1944. var hmac = require("./hmac");
  1945. module.exports = {
  1946. generatePassword: generatePassword,
  1947. _renderPassword: renderPassword,
  1948. _createHmac: createHmac,
  1949. _deriveEncryptedLogin: deriveEncryptedLogin,
  1950. _getPasswordTemplate: getPasswordTemplate,
  1951. _prettyPrint: prettyPrint,
  1952. _string2charCodes: string2charCodes,
  1953. _getCharType: getCharType,
  1954. _getPasswordChar: getPasswordChar
  1955. };
  1956. var defaultOptions = {
  1957. version: 1,
  1958. lowercase: true,
  1959. numbers: true,
  1960. uppercase: true,
  1961. symbols: true,
  1962. keylen: 32,
  1963. digest: "sha256",
  1964. length: 12,
  1965. counter: 1,
  1966. iterations: 8192
  1967. };
  1968. function generatePassword(site, login, masterPassword, options) {
  1969. var _options = assign({}, defaultOptions, options);
  1970. return pbkdf2(
  1971. masterPassword,
  1972. login,
  1973. _options.iterations,
  1974. _options.keylen,
  1975. "sha256"
  1976. ).then(function(encryptedLogin) {
  1977. return renderPassword(encryptedLogin, site, _options).then(function(
  1978. generatedPassword
  1979. ) {
  1980. return generatedPassword;
  1981. });
  1982. });
  1983. }
  1984. function renderPassword(encryptedLogin, site, passwordOptions) {
  1985. return deriveEncryptedLogin(
  1986. encryptedLogin,
  1987. site,
  1988. passwordOptions
  1989. ).then(function(derivedEncryptedLogin) {
  1990. var template =
  1991. passwordOptions.template || getPasswordTemplate(passwordOptions);
  1992. return prettyPrint(derivedEncryptedLogin, template);
  1993. });
  1994. }
  1995. function createHmac(encryptedLogin, salt) {
  1996. return new Promise(function(resolve) {
  1997. resolve(hmac("sha256", encryptedLogin, salt));
  1998. });
  1999. }
  2000. function deriveEncryptedLogin(encryptedLogin, site, options) {
  2001. var _options = options !== undefined ? options : {};
  2002. var length = _options.length || 12;
  2003. var counter = _options.counter || 1;
  2004. var salt = site + counter.toString();
  2005. return createHmac(encryptedLogin, salt).then(function(derivedHash) {
  2006. return derivedHash.substring(0, length);
  2007. });
  2008. }
  2009. function getPasswordTemplate(passwordTypes) {
  2010. var templates = {
  2011. lowercase: "vc",
  2012. uppercase: "VC",
  2013. numbers: "n",
  2014. symbols: "s"
  2015. };
  2016. var returnedTemplate = "";
  2017. Object.keys(templates).forEach(function(template) {
  2018. if (passwordTypes.hasOwnProperty(template) && passwordTypes[template]) {
  2019. returnedTemplate += templates[template];
  2020. }
  2021. });
  2022. return returnedTemplate;
  2023. }
  2024. function prettyPrint(hash, template) {
  2025. var password = "";
  2026. string2charCodes(hash).forEach(function(charCode, index) {
  2027. var charType = getCharType(template, index);
  2028. password += getPasswordChar(charType, charCode);
  2029. });
  2030. return password;
  2031. }
  2032. function string2charCodes(text) {
  2033. var charCodes = [];
  2034. for (var i = 0; i < text.length; i++) {
  2035. charCodes.push(text.charCodeAt(i));
  2036. }
  2037. return charCodes;
  2038. }
  2039. function getCharType(template, index) {
  2040. return template[index % template.length];
  2041. }
  2042. function getPasswordChar(charType, index) {
  2043. var passwordsChars = {
  2044. V: "AEIOUY",
  2045. C: "BCDFGHJKLMNPQRSTVWXZ",
  2046. v: "aeiouy",
  2047. c: "bcdfghjklmnpqrstvwxz",
  2048. A: "AEIOUYBCDFGHJKLMNPQRSTVWXZ",
  2049. a: "AEIOUYaeiouyBCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz",
  2050. n: "0123456789",
  2051. s: "@&%?,=[]_:-+*$#!'^~;()/.",
  2052. x: "AEIOUYaeiouyBCDFGHJKLMNPQRSTVWXZbcdfghjklmnpqrstvwxz0123456789@&%?,=[]_:-+*$#!'^~;()/."
  2053. };
  2054. var passwordChar = passwordsChars[charType];
  2055. return passwordChar[index % passwordChar.length];
  2056. }
  2057. },{"./hmac":5,"./pbkdf2":7,"lodash.assign":2}],9:[function(require,module,exports){
  2058. var pbkdf2 = require("./pbkdf2");
  2059. var bigInt = require("big-integer");
  2060. var assign = require("lodash.assign");
  2061. module.exports = {
  2062. generatePassword: generatePassword,
  2063. _calcEntropy: calcEntropy,
  2064. _consumeEntropy: consumeEntropy,
  2065. _getSetOfCharacters: getSetOfCharacters,
  2066. _getConfiguredRules: getConfiguredRules,
  2067. _insertStringPseudoRandomly: insertStringPseudoRandomly,
  2068. _getOneCharPerRule: getOneCharPerRule,
  2069. _renderPassword: renderPassword
  2070. };
  2071. var defaultOptions = {
  2072. version: 2,
  2073. lowercase: true,
  2074. numbers: true,
  2075. uppercase: true,
  2076. symbols: true,
  2077. keylen: 32,
  2078. digest: "sha256",
  2079. length: 16,
  2080. counter: 1,
  2081. iterations: 100000
  2082. };
  2083. function generatePassword(site, login, masterPassword, options) {
  2084. var _options = assign({}, defaultOptions, options);
  2085. return calcEntropy(site, login, masterPassword, _options).then(function(
  2086. entropy
  2087. ) {
  2088. return renderPassword(entropy, _options);
  2089. });
  2090. }
  2091. function calcEntropy(site, login, masterPassword, passwordProfile) {
  2092. var salt = site + login + passwordProfile.counter.toString(16);
  2093. return pbkdf2(
  2094. masterPassword,
  2095. salt,
  2096. passwordProfile.iterations,
  2097. passwordProfile.keylen,
  2098. passwordProfile.digest
  2099. );
  2100. }
  2101. var characterSubsets = {
  2102. lowercase: "abcdefghijklmnopqrstuvwxyz",
  2103. uppercase: "ABCDEFGHIJKLMNOPQRSTUVWXYZ",
  2104. numbers: "0123456789",
  2105. symbols: "!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~"
  2106. };
  2107. function getSetOfCharacters(rules) {
  2108. if (typeof rules === "undefined") {
  2109. return (
  2110. characterSubsets.lowercase +
  2111. characterSubsets.uppercase +
  2112. characterSubsets.numbers +
  2113. characterSubsets.symbols
  2114. );
  2115. }
  2116. var setOfChars = "";
  2117. rules.forEach(function(rule) {
  2118. setOfChars += characterSubsets[rule];
  2119. });
  2120. return setOfChars;
  2121. }
  2122. function consumeEntropy(
  2123. generatedPassword,
  2124. quotient,
  2125. setOfCharacters,
  2126. maxLength
  2127. ) {
  2128. if (generatedPassword.length >= maxLength) {
  2129. return { value: generatedPassword, entropy: quotient };
  2130. }
  2131. var longDivision = quotient.divmod(setOfCharacters.length);
  2132. generatedPassword += setOfCharacters[longDivision.remainder];
  2133. return consumeEntropy(
  2134. generatedPassword,
  2135. longDivision.quotient,
  2136. setOfCharacters,
  2137. maxLength
  2138. );
  2139. }
  2140. function insertStringPseudoRandomly(generatedPassword, entropy, string) {
  2141. for (var i = 0; i < string.length; i++) {
  2142. var longDivision = entropy.divmod(generatedPassword.length);
  2143. generatedPassword =
  2144. generatedPassword.slice(0, longDivision.remainder) +
  2145. string[i] +
  2146. generatedPassword.slice(longDivision.remainder);
  2147. entropy = longDivision.quotient;
  2148. }
  2149. return generatedPassword;
  2150. }
  2151. function getOneCharPerRule(entropy, rules) {
  2152. var oneCharPerRules = "";
  2153. rules.forEach(function(rule) {
  2154. var password = consumeEntropy("", entropy, characterSubsets[rule], 1);
  2155. oneCharPerRules += password.value;
  2156. entropy = password.entropy;
  2157. });
  2158. return { value: oneCharPerRules, entropy: entropy };
  2159. }
  2160. function getConfiguredRules(passwordProfile) {
  2161. return ["lowercase", "uppercase", "numbers", "symbols"].filter(function(
  2162. rule
  2163. ) {
  2164. return passwordProfile[rule];
  2165. });
  2166. }
  2167. function renderPassword(entropy, passwordProfile) {
  2168. var rules = getConfiguredRules(passwordProfile);
  2169. var setOfCharacters = getSetOfCharacters(rules);
  2170. var password = consumeEntropy(
  2171. "",
  2172. bigInt(entropy, 16),
  2173. setOfCharacters,
  2174. passwordProfile.length - rules.length
  2175. );
  2176. var charactersToAdd = getOneCharPerRule(password.entropy, rules);
  2177. return insertStringPseudoRandomly(
  2178. password.value,
  2179. charactersToAdd.entropy,
  2180. charactersToAdd.value
  2181. );
  2182. }
  2183. },{"./pbkdf2":7,"big-integer":1,"lodash.assign":2}]},{},[6])(6)
  2184. });