Speed up bruteforce
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 90a7549 · Browse files · 3 files·+48 added−32 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+48 added−32 removed
- tests:+44 added−30 removed
- other:+4 added−2 removed
Provenance
- Commit
9fc7fa2- Tree
348fce6- Author
Andrey Sitnik <andrey@sitnik.ru>- Committer
Andrey Sitnik <andrey@sitnik.ru>- Parent
90a7549
3 files changed+48 added−32 removed
modified:test/bruteforce+44 added-30 removed
1 #!/usr/bin/env node2 3-var sizeLimit = require('size-limit')3+var UglifyJS = require('uglify-js')4+var gzipSize = require('gzip-size')5 var chalk = require('chalk')6 var path = require('path')7 var fs = require('fs')8 9 var filePath = path.join(process.cwd(), process.argv[2])10+var file11 var used = { }12 13 function shuffle (str) {14+ var array = str.split('')15 var j, x, i13- for (i = str.length - 1; i > 0; i--) {16+ for (i = array.length - 1; i > 0; i--) {17 j = Math.floor(Math.random() * (i + 1))15- x = str[i]16- str[i] = str[j]17- str[j] = x18+ x = array[i]19+ array[i] = array[j]20+ array[j] = x21 }19- return str22+ return array.join('')23 }24 25 function shuffleUnique (str) {26 var mixed24 unmodified lines51 var prevPercent = 052 var maxSteps = factorial(64)53 54 function findInitialString () {52- var data = fs.readFileSync(filePath, 'utf8')53- data.replace(/[A-Za-z0-9~_]{64}/, function (match) {54- lastString = match55- })55+ file = fs.readFileSync(filePath, 'utf8')56+ file = UglifyJS.minify(file).code.replace('module.', 'e.')57+ file = '!function(e){var t={};function r(n){if(t[n])return t[n].exports;' +58+ 'var o=t[n]={i:n,l:!1,exports:{}};return ' +59+ 'e[n].call(o.exports,o,o.exports,r),o.l=!0,o.exports}' +60+ 'r.m=e,r.c=t,r.d=function(e,t,n){r.o(e,t)||' +61+ 'Object.defineProperty(e,t,{enumerable:!0,get:n})},r.r=function(e){' +62+ '"undefined"!=typeof Symbol&&Symbol.toStringTag&&' +63+ 'Object.defineProperty(e,Symbol.toStringTag,{value:"Module"}),' +64+ 'Object.defineProperty(e,"__esModule",{value:!0})},' +65+ 'r.t=function(e,t){if(1&t&&(e=r(e)),8&t)' +66+ 'return e;if(4&t&&"object"==typeof e&&e&&e.__esModule)return e;' +67+ 'var n=Object.create(null);if(r.r(n),Object.defineProperty(' +68+ 'n,"default",{enumerable:!0,value:e}),2&t&&"string"!=typeof e)' +69+ 'for(var o in e)r.d(n,o,function(t){return e[t]}.bind(null,o));' +70+ 'return n},r.n=function(e){var t=e&&e.__esModule?function(){' +71+ 'return e.default}:function(){return e};return r.d(t,"a",t),t}' +72+ ',r.o=function(e,t){return Object.prototype.hasOwnProperty' +73+ '.call(e,t)},r.p="",r(r.s=0)}([function(e,t){' + file + '}]);'74+ var match = file.match(/[A-Za-z0-9~_]{64}/)75+ if (!match) {76+ process.stderr.write(chalk.red('Alphabet was not found') + '\n')77+ process.exit(1)78+ }79+ lastString = match[0]80 }81 58-function shuffleStringInFile () {59- var data = fs.readFileSync(filePath, 'utf8')60-61- var result = data.replace(/[A-Za-z0-9~_]{64}/, function (match) {62- lastString = shuffleUnique(match.split('')).join('')63- return lastString64- })65-66- fs.writeFileSync(filePath + '', result, 'utf8')82+function shuffleString () {83+ lastString = shuffleUnique(lastString)84+ file = file.replace(/[A-Za-z0-9~_]{64}/, lastString)85 }86 87 function checkSize () {88 step += 171- sizeLimit(filePath).then(function (size) {72- if (size.gzip < best.size) {89+ gzipSize(file).then(function (size) {90+ size = size - 46191+ if (size < best.size) {92 best.value = lastString74- best.size = size.gzip93+ best.size = size94 process.stdout.write(95 chalk.green.bold(best.size + 'B') + ' ' + best.value + '\n')96 } else {78- var percent = Math.floor(100 * step / maxSteps)97+ var percent = Math.floor(10000 * step / maxSteps)98 if (percent > prevPercent) {99 prevPercent = percent81- process.stdout.write(chalk.gray(percent + '%') + '\n')100+ process.stdout.write(chalk.gray((percent / 10000.0) + '%') + '\n')101 }102 }103 if (step === maxSteps) {104 process.exit(0)105 } else {87- shuffleStringInFile()106+ shuffleString()107 checkSize()108 }109 })110 }111 112 findInitialString()94-if (typeof lastString === 'undefined') {95- process.stderr.write(chalk.red('Alphabet was not found') + '\n')96- process.exit(1)97-}98-113 checkSize()modified:package.json+2 added
30 unmodified lines3132333435363738392 unmodified lines424344454647484950125 unmodified linesExpand all30 unmodified lines "eslint-plugin-node": "^7.0.1", "eslint-plugin-promise": "^4.0.1", "eslint-plugin-security": "^1.4.0", "eslint-plugin-standard": "^4.0.0", "gzip-size": "^5.0.0", "html-webpack-plugin": "^3.2.0", "jest": "^23.6.0", "jsdoc": "^3.5.5", "lint-staged": "^7.3.0",2 unmodified lines "rimraf": "^2.6.2", "rndm": "^1.2.0", "shortid": "^2.2.13", "size-limit": "^0.20.0", "uglify-js": "^3.4.9", "uid-safe": "^2.1.5", "uuid": "^3.3.2", "webpack": "^4.20.2", "webpack-cli": "^3.1.2",125 unmodified linesExpand allmodified:yarn.lock+2 added-2 removed
6438 unmodified lines6439 version "0.1.1"6440 resolved "https://registry.yarnpkg.com/sisteransi/-/sisteransi-0.1.1.tgz#5431447d5f7d1675aac667ccd0b865a4994cb3ce"6441 integrity sha512-PmGOd02bM9YO5ifxpw36nrNMBTptEtfRl4qUYl9SndkolplkrZZOW7PGHjrZL53QvMVj9nQ+TKqUnRsw4tJa4g==6442 6443-size-limit@^0.20.1:6443+size-limit@^0.20.0:6444 version "0.20.1"6445 resolved "https://registry.yarnpkg.com/size-limit/-/size-limit-0.20.1.tgz#327436ea50dbda8df24689a56b71d42993ef5d17"6446 integrity sha512-n6ECm/MV2dNe1ukEu6k1IjedVVd0U8fSW+C2FdnWQl+SqYSvDLTflM9rZJlczovW7jh/3TZFXv+2jZMYTuysDg==6447 dependencies:652 unmodified lines7100 dependencies:7101 commander "~2.13.0"7102 source-map "~0.6.1"7103 7104-uglify-js@3.4.x, uglify-js@^3.1.4:7104+uglify-js@3.4.x, uglify-js@^3.1.4, uglify-js@^3.4.9:7105 version "3.4.9"7106 resolved "https://registry.yarnpkg.com/uglify-js/-/uglify-js-3.4.9.tgz#af02f180c1207d76432e473ed24a28f4a782bae3"7107 integrity sha512-8CJsbKOtEbnJsTyv6LE6m6ZKniqMiFWmm9sRbopbkGs3gMPPfd3Fh8iIA4Ykv5MgaTbqHr4BaoGLJLZNhsrW1Q==7108 dependencies:664 unmodified lines