-
Notifications
You must be signed in to change notification settings - Fork 15
/
key-generator.js
93 lines (71 loc) · 2.34 KB
/
key-generator.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
91
92
93
const crypto = require('crypto');
const THREE_CHARS_REGEX = /(\w)\1\1/;
class KeyGenerator {
constructor(salt, use_stretch_hash = false) {
this.salt = salt;
this.index = 0;
this.use_stretch_hash = use_stretch_hash;
this.keys = [];
this.hashCache = {};
}
static MD5(str) {
return crypto
.createHash('md5')
.update(str)
.digest('hex');
}
static MD5_STRETCH(str) {
let hash = this.MD5(str);
for (let i = 0; i < 2016; i++) {
hash = this.MD5(hash);
}
return hash;
}
md5(str) {
if (!this.hashCache[str]) {
this.hashCache[str] = KeyGenerator.MD5(str);
}
return this.hashCache[str];
}
md5Stretch(str) {
if (!this.hashCache[str]) {
this.hashCache[str] = KeyGenerator.MD5_STRETCH(str);
}
return this.hashCache[str];
}
testHashForValidity(match) {
// Take our 3 char match and convert it to a 5 char match
match = match + match[0] + match[0];
for (let i = 0; i < 1000; i++) {
let input = `${this.salt}${this.index + i}`;
let hash = this.use_stretch_hash ? this.md5Stretch(input) : this.md5(input);
if (hash.includes(match)) {
return true;
}
}
return false;
}
generateNextKeys(n = 64) {
for (let key = 0; key < n; key++) {
let found_key = false;
while (found_key === false) {
let input = `${this.salt}${this.index++}`;
let hash = this.use_stretch_hash ? this.md5Stretch(input) : this.md5(input);
if (THREE_CHARS_REGEX.test(hash)) {
// Hash has three repeating characters in a row!
let [match] = hash.match(THREE_CHARS_REGEX);
if (this.testHashForValidity(match)) {
this.keys.push({
hash,
index: this.index - 1,
});
found_key = true;
}
}
}
}
// Return the last index where we generated our key (conveniently the answer to the riddle)
return this.index - 1;
}
}
module.exports = KeyGenerator;