-
Notifications
You must be signed in to change notification settings - Fork 0
/
SHA1_collision.py
80 lines (72 loc) · 2.09 KB
/
SHA1_collision.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
70
71
72
73
74
75
76
77
78
79
80
import hashlib
import random
import string
def get_sha1(text):
hashed = hashlib.sha1(text)
sha1 = hashed.hexdigest()
return sha1
def get_dict_value(text, d):
keys = d.keys()
for i in keys:
if d[i] == text:
return i
def random_string():
length=10
letters = string.ascii_lowercase
result = ''.join(random.choice(letters)for i in range(length))
return result
ptext = random_string()
text = ptext
hashed = get_sha1(bytes(text,'utf-8'))
hf = hashed[0:6]
count = 0
right = text
all_hashes = {}
a=1
while count != 1:
print("COUNT: ",a)
#+1 each time on left
lo = get_sha1(bytes(text,'utf-8'))
#+2 each time on right
ro = get_sha1(bytes(right,'utf-8'))
rl = get_sha1(bytes(ro,'utf-8'))
lf = lo[0:6]
rf = rl[0:6]
if lf != rf:
if len(all_hashes) != 0:
vals = all_hashes.values()
for i in vals:
if rf == i:
v = get_dict_value(i, all_hashes)
if v == ro:
pass
else:
print("Same value found")
print(v, " and ", ro)
print(i, " and ", rf)
count = 1
break
elif lf == i:
v = get_dict_value(i, all_hashes)
if v == text:
pass
else:
print("Same value found")
print(v, " and ", text)
print(i, " and ", rlf)
count = 1
break
all_hashes[text]=lf
all_hashes[ro]=rf
text = lo
right = rl
else:
all_hashes[text]=lf
all_hashes[ro]=rf
text = lo
right = rl
elif lf == rf:
print("Same value found")
print(text, " and ", ro)
print(lf, " and ", rf)
a+=1