-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathApp.js
78 lines (70 loc) · 2.36 KB
/
App.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
const fs = require('fs');
const EOL = require('os').EOL;
const splitToChunks = (array, parts) => {
let result = [];
let copy = [...array];
for (let i = parts; i > 0; i--) {
result.push(copy.splice(0, Math.ceil(copy.length / i)));
}
return result;
};
const wordGen = (prefix, letters) => letters.map((e) => prefix + '' + e);
const passGen = async (possibleFirstLetters, passLength, allChars, tid) => {
let passwords = [];
for (let i in possibleFirstLetters) {
let firstLetter = possibleFirstLetters[i];
let words = [];
while (words.length == 0 || words[0].length < passLength) {
if (!words.length) words = wordGen(firstLetter, allChars);
else words = words.map((e) => wordGen(e, allChars)).flat();
}
passwords.push(words);
}
return passwords.flat();
};
const savePasses = (passes) => {
const filepath = 'passes.csv';
if (fs.existsSync(filepath)) {
fs.unlinkSync(filepath);
}
const file = fs.createWriteStream(filepath);
file.on('error', function (err) {
console.log(err);
});
passes.forEach(function (v) {
file.write(v + EOL);
});
file.end();
};
const passGenThreads = async (
threadsQtd = 1,
passLength,
allChars,
save = false
) => {
//const comb = Math.pow(allChars.length, passLength);
console.log(`\n\nGenerating words with ${passLength} char each!`);
console.log(`Using ${threads} threads`);
const startTime = new Date();
const splitLetters = splitToChunks(allChars, Math.max(1, threadsQtd));
let passes = [];
for (let i in splitLetters) {
passes.push(passGen(splitLetters[i], passLength, allChars, i));
}
passes = await Promise.all(passes);
const endTime = new Date();
const seconds = ((endTime.getTime() - startTime.getTime()) / 1000).toFixed(2);
passes = passes.flat();
console.log(`Generated ${passes.length} words in ${seconds} seconds!\n\n`);
if (save) savePasses(passes);
return passes;
};
//prettier-ignore
const upper = ['A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z'];
const allPossibleChars = upper
.concat(upper.map((e) => e.toLowerCase()))
.concat([0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
const stringLength = process.argv.slice(2)[0] || 4;
const threads = process.argv.slice(2)[1] || 10;
const save = process.argv.slice(2).indexOf('--save') != -1;
passGenThreads(threads, stringLength, allPossibleChars, save);