-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpassword_cracker.js
90 lines (74 loc) · 2.46 KB
/
password_cracker.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
79
80
81
82
83
84
85
86
87
88
89
90
(function(enteredKey, i) {
'use strict';
var fs = require('fs');
var list = [];
function start() {
function readLines(input, addToList) {
var remaining = '';
input.on('data', function(data) {
remaining += data;
var index = remaining.indexOf('\n');
while (index > -1) {
var line = remaining.substring(0, index);
remaining = remaining.substring(index + 1);
addToList(line);
index = remaining.indexOf('\n');
};
});
input.on('end', function() {
if (remaining.length > 0) {
addToList(remaining);
} else {
checkCipher()
};
});
}
function addToList(data) {
list.push(data);
};
var input = fs.createReadStream(process.argv[3]);
readLines(input, addToList);
};
function checkCipher() {
var found = false;
do {
for (var n = 1; n < 26; n++) {
var text = createCipher(list[i], n)
if (text === 'Not Found') {
found = true;
break;
}
console.log('Trying key: ' + text);
found = tryKey(text);
if (found) break;
};
i++;
}
while (found == false)
};
function createCipher(word, offset) {
if (!word) return 'Not Found';
var charArray = word.split('').map(function(char) {
return char.charCodeAt(0);
});
var encipheredCharArray = charArray.map(function(char) {
if ((char + offset) > 126) {
// if the ASCII code is above 126, then wrap round.
//First valid chaarcter is 32 so we wrap round and then add on 33.
return String.fromCharCode((char + offset) - (126 - 33));
} else {
return String.fromCharCode(char + offset);
}
});
var cipherText = encipheredCharArray.join('');
return cipherText;
};
function tryKey(cipherText) {
if (cipherText === enteredKey) {
console.log('\n=======\nFOUND: Key ' + enteredKey + ' = ' + list[i] + '\n=======');
return true;
};
return false;
};
start()
})(process.argv[2], 0);