-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path14_otp.py
69 lines (61 loc) · 1.93 KB
/
14_otp.py
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
import hashlib
import binascii
def has_repeated(hashed, n=3, pattern=None):
if pattern is None:
reps = [hashed[x:x+n] for x in range(len(hashed)-(n-1)) if hashed[x]*n == hashed[x:x+n]]
else:
reps = [hashed[x:x+n] for x in range(len(hashed)-(n-1)) if pattern == hashed[x:x+n]]
if len(reps) > 0:
return reps[0]
else:
return None
def update_hash_cache(hashes, key, second=False):
m = hashlib.md5()
if key in hashes.keys():
return hashes[key]
else:
if second:
digest = key
for _ in range(2017):
m = hashlib.md5()
m.update(binascii.a2b_qp(digest))
digest = m.hexdigest()
hashes[key] = digest
return hashes[key]
else:
m.update(binascii.a2b_qp(key))
hashes[key] = m.hexdigest()
return hashes[key]
def solve(init_text):
hashes = {}
keys = []
candidate = None
trip = None
idx_m = 0
idx_c = 0
while len(keys) < 64:
if candidate is None:
digest = update_hash_cache(hashes, init_text + str(idx_m), True)
trip = has_repeated(digest)
if trip is not None:
candidate = digest
idx_c = 1
else:
idx_m += 1
else:
if idx_c > 1000:
candidate = None
idx_m += 1
continue
digest = update_hash_cache(hashes, init_text + str(idx_m+idx_c), True)
reps = has_repeated(digest, 5, trip[0]*5)
if reps is not None:
keys.append(candidate)
idx_c = 1001
print('Current idx {0}, current number of keys {1}'.format(idx_m,len(keys)))
else:
idx_c += 1
return idx_m
if __name__ == '__main__':
solution = solve('ahsbgdzn')
print('Secret password is {0}!'.format(solution))