Skip to content
Myr

Add new alphabet bruteforcer

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 b75f62c · Browse files · 2 files+167 added−0 removed
This commit

State

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

Diff stats

2 files+167 added−0 removed

  • tests:+167 added−0 removed

2 files+167 added−0 removed

added:test/alphabet+98 added
1#!/usr/bin/node --experimental-worker23/* eslint-disable node/no-missing-require */45var WORKERS = 867var spawnSync = require('child_process').spawnSync8var Worker = require('worker_threads').Worker9var chalk = require('chalk')10var path = require('path')11var fs = require('fs')1213if (!process.argv[2]) {14 process.stderr.write(chalk.red('Pass a file with alphabet as argument\n'))15 process.exit(1)16}1718var input = path.join(process.cwd(), process.argv[2])19var output = path.join(__dirname, '..', 'compiled.js')2021spawnSync('npx', ['webpack', '--mode', 'production', '--output', output, input])22var js = fs.readFileSync(output).toString()23fs.unlinkSync(output)2425var match = js.match(/[A-Za-z0-9~_]{30,}/)26if (!match) {27 process.stderr.write(chalk.red('Alphabet was not found\n'))28 process.exit(1)29}30var alphabet = match[0].split('').sort().join('')3132var best = {33 size: Infinity34}3536var newline = false3738var all = js.length * js.length * js.length39var step = 040var lastPart = 04142function print (str) {43 process.stdout.write(str)44 newline = true45}4647function printLine (str) {48 if (newline) process.stdout.write('\n')49 newline = false50 process.stdout.write(str + '\n')51}5253new Promise(function (resolve) {54 for (var i = 0; i < WORKERS; i++) {55 var finished = 056 var worker = new Worker(path.join(__dirname, 'alphabet-worker.js'), {57 workerData: {58 alphabet: alphabet,59 start: i,60 step: WORKERS,61 js: js62 }63 })64 worker.on('message', function (data) {65 if (data.finished) {66 finished += 167 if (finished === WORKERS) {68 printLine(69 chalk.bold(step) + ' variants were checked. The best alphabet:\n' +70 chalk.green(best.alphabet)71 )72 resolve()73 }74 } else {75 if (data.size && data.size < best.size) {76 if (newline) process.stdout.write('\n')77 newline = false78 best = data79 printLine(best.alphabet)80 }81 step += 182 var part = Math.floor(80 * 100 * step / all)83 if (part > lastPart) {84 lastPart = part85 if (part % 80 === 0) {86 print(chalk.gray('.'))87 printLine(chalk.gray((part / 80) + '%'))88 } else {89 print(chalk.gray('.'))90 }91 }92 }93 })94 worker.on('error', function (e) {95 throw e96 })97 }98})
added:test/alphabet-worker.js+69 added