Guest User

Untitled

a guest
Jun 11th, 2018
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.10 KB | None | 0 0
  1. /**
  2. * Brute force the string
  3. * @param {Number} len
  4. * @param {Function} checkFunc that return Promise resolved to boolean if bruted value is matching
  5. * @returns {Promise} Result of brute force
  6. */
  7. const bruteString = (len, checkFunc) => {
  8. const startTime = Date.now();
  9. const symbols = [..."0123456789qwertyuiopasdfghjklzzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM!@#$%^&*()_-=+{}[]\\|?/.>,<;:'\"`~"];
  10. const limit = Math.pow(symbols.length, len) - 1;
  11. const loopLimit = 32767;
  12. const loopDelay = 10;
  13. const infoIntervalDelay = 1000;
  14.  
  15. const wordIndexArray = [];
  16. for (let i = 0; i < len; i++) wordIndexArray.push(0);
  17.  
  18. let totalIterations = 0;
  19. let finished = false;
  20.  
  21. const updateIndexes = () => {
  22. let changingSymbol = 0;
  23. let shift = true;
  24. while (shift) {
  25. if (changingSymbol > wordIndexArray.length - 1) {
  26. finished = true;
  27. shift = false;
  28. } else if (wordIndexArray[changingSymbol] < symbols.length - 1) {
  29. wordIndexArray[changingSymbol]++;
  30. shift = false;
  31. } else {
  32. wordIndexArray[changingSymbol] = 0;
  33. changingSymbol++;
  34. }
  35. }
  36. };
  37.  
  38. const nextWord = () => {
  39. totalIterations++;
  40. let word = "";
  41. for (let i = 0; i < len; i++) word += symbols[wordIndexArray[i]];
  42. updateIndexes();
  43. return word;
  44. };
  45.  
  46. function* brute() {
  47. while (true) {
  48. let guess = nextWord();
  49. if (finished) return null;
  50. let match = yield checkFunc(guess);
  51. if (match) return guess;
  52. yield null;
  53. }
  54. }
  55.  
  56. const getTimeString = (time) => {
  57. const seconds = Math.floor(time / 1000)
  58. const minutes = Math.floor(seconds / 60)
  59. const hours = Math.floor(minutes / 60)
  60. const days = Math.floor(hours / 24)
  61. switch (true) {
  62. case days >= 1:
  63. return `${days} days`
  64. case hours >= 1:
  65. return `${hours} hours ${minutes % 60} minutes`
  66. case minutes >= 1:
  67. return `${minutes} minutes ${seconds % 60} seconds`
  68. default:
  69. return `${seconds} seconds`
  70. break;
  71. }
  72. }
  73.  
  74. // Spams info on bruteforce process
  75. const infoInterval = setInterval(() => {
  76. let doneCoef = totalIterations / limit;
  77. let intPrs = Math.round(doneCoef * 100);
  78. const durationTime = Date.now() - startTime;
  79. const timeLeft = Math.round(durationTime / doneCoef) - durationTime;
  80. console.info(`Progress: ${(intPrs * 100) / 100}% (${totalIterations} / ${limit})\nTime running: ${getTimeString(durationTime)}\nTime left: ${getTimeString(timeLeft)}`)
  81. }, infoIntervalDelay);
  82.  
  83. return new Promise((resolve, reject) => {
  84. const generatorInstance = brute();
  85. const generatorRunner = (result) => {
  86. const nextIteration = () => {
  87. if (result.value !== null && typeof result.value.then === "function") {
  88. result.value.then((check) => {
  89. generatorRunner(generatorInstance.next(check));
  90. })
  91. } else {
  92. generatorRunner(generatorInstance.next());
  93. }
  94. };
  95.  
  96. if (result.done) {
  97. clearTimeout(infoInterval);
  98. if (result.value !== null) {
  99. resolve(result.value);
  100. }
  101. else reject("No match found")
  102. } else {
  103. if (totalIterations % loopLimit === 0) {
  104. setTimeout(() => {
  105. nextIteration();
  106. }, loopDelay)
  107. } else {
  108. nextIteration();
  109. }
  110. }
  111. };
  112. generatorRunner(generatorInstance.next())
  113. });
  114. };
  115.  
  116. // exemple of usage
  117.  
  118. const correctCheck = (value) => {
  119. return Promise.resolve(value === "pass")
  120. }
  121.  
  122. bruteString(4, correctCheck)
  123. .then((res) => {
  124. console.log("Match found:", res);
  125. })
  126. .catch((err) => {
  127. console.error(err);
  128. });
Add Comment
Please, Sign In to add comment