-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhash2.js
59 lines (43 loc) · 1.25 KB
/
hash2.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
const crypto = require('crypto');
const md5 = (value) => crypto.createHash('md5').update(value).digest('hex');
const computeHash = (salt, index) => md5(`${salt}${index}`);
const stretch = (hash, iterations = 2016) => {
let currentHash = hash;
for (let i = 0; i < iterations; i++) {
currentHash = md5(currentHash);
}
return currentHash;
};
const hasSequence = (hash, length) => {
for (let i = 0; i <= hash.length - length; i++) {
if (hash.slice(i, i + length).split('').every((x) => x === hash[i])) {
return hash[i];
}
}
return null;
};
module.exports = (salt) => {
let stretchedHashes = Array
.from({ length: 1001 })
.map((_, i) => stretch(computeHash(salt, i)));
let secret = 0;
let keys = 0;
while (keys < 64) {
const stretchedHash = stretchedHashes.shift();
stretchedHashes.push(stretch(computeHash(salt, secret + 1000)));
const character = hasSequence(stretchedHash, 3);
if (character !== null) {
for (let i = 0; i < stretchedHashes.length; i++) {
const next = hasSequence(stretchedHashes[i], 5);
if (next === character) {
keys++;
if (keys === 64) {
return secret - 1;
}
break;
}
}
}
secret++;
}
};