Skip to content
Myr

Improve bruteforce output

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Andrey Sitnik · committed by Andrey Sitnik · parent efae81c · Browse files · 1 file+55 added−18 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

1 file+55 added−18 removed

  • tests:+55 added−18 removed

1 file+55 added−18 removed

modified:test/bruteforce+55 added-18 removed
4 unmodified lines
5var path = require('path')6var fs = require('fs')78var filePath = path.join(process.cwd(), process.argv[2])9var best = {10 value: '',11 size: Infinity12}13var lastString9var used = { }1015function shuffle (a) {11function shuffle (str) {12 var j, x, i17 for (i = a.length - 1; i > 0; i--) {13 for (i = str.length - 1; i > 0; i--) {14 j = Math.floor(Math.random() * (i + 1))19 x = a[i]20 a[i] = a[j]21 a[j] = x15 x = str[i]16 str[i] = str[j]17 str[j] = x18 }23 return a19 return str20}2126function saveFirstString () {22function shuffleUnique (str) {23 var mixed24 while (true) {25 mixed = shuffle(str)26 if (!used[mixed]) {27 used[mixed] = true28 return mixed29 }30 }31}3233function factorial (number) {34 var total = 135 while (number > 0) {36 total *= number37 number = number - 138 }39 return total40}4142var best = {43 value: '',44 size: Infinity45}46var lastString47var step = 048var prevPercent = 049var maxSteps = factorial(64)5051function findInitialString () {52 var data = fs.readFileSync(filePath, 'utf8')28 data.replace(/[A-Za-z0-9~_]{58}/g, function (match) {53 data.replace(/[A-Za-z0-9~_]{64}/, function (match) {54 lastString = match55 })56}5758function shuffleStringInFile () {59 var data = fs.readFileSync(filePath, 'utf8')6036 var result = data.replace(/[A-Za-z0-9~_]{64}/g, function (match) {37 lastString = shuffle(match.split('')).join('')61 var result = data.replace(/[A-Za-z0-9~_]{64}/, function (match) {62 lastString = shuffleUnique(match.split('')).join('')63 return lastString64 })6566 fs.writeFileSync(filePath + '', result, 'utf8')67}6869function checkSize () {70 step += 171 sizeLimit(filePath).then(function (size) {72 if (size.gzip < best.size) {73 best.value = lastString74 best.size = size.gzip75 process.stdout.write(76 chalk.green.bold(best.size + 'B') + ' ' + best.value + '\n')77 } else {78 var percent = Math.floor(100 * step / maxSteps)79 if (percent > prevPercent) {80 prevPercent = percent81 process.stdout.write(chalk.gray(percent + '%') + '\n')82 }83 }84 if (step === maxSteps) {85 process.exit(0)86 } else {52 shuffleStringInFile()53 checkSize()87 shuffleStringInFile()88 checkSize()89 }90 })91}9257saveFirstString()93findInitialString()94if (typeof lastString === 'undefined') {95 process.stderr.write(chalk.red('Alphabet was not found') + '\n')96 process.exit(1)97}9899checkSize()