-
Notifications
You must be signed in to change notification settings - Fork 2
/
md5_collision.py
108 lines (70 loc) · 2.52 KB
/
md5_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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
from hashlib import md5
# Floyd's algorithm (https://en.wikipedia.org/wiki/Cycle_detection)
# We want to find collisions between two strings that begin with this prefix.
my_prefix = b'\x41\x61\x99'
# Get the first x bytes of the double md5 hash value
def hash_function(message, x=14, prefix=b'\x41\x61\x99', debug=False):
temp_hash = md5(md5(prefix + message).digest()).digest()
if debug is True:
print(message, x, prefix, temp_hash)
return temp_hash[:x]
def hash_function2(message, x=14, prefix=b'\x41\x61\x99', debug=False):
temp_hash = md5(md5(prefix + message).digest()).hexdigest()
if debug is True:
print(message, x, prefix, temp_hash)
return temp_hash[:x]
def floyd(x, initial):
# Set a few temp values to zero or none
x0 = initial
m0 = None
m1 = None
# Start
tortoise = hash_function(x0, x, my_prefix)
hare = hash_function(tortoise, x, my_prefix)
# First loop until our hashes are equal
while tortoise != hare:
tortoise = hash_function(tortoise, x, my_prefix)
hare = hash_function(hash_function(hare, x, my_prefix), x, my_prefix)
# Set pointer to initial value
tortoise = x0
# Secound loop
while tortoise != hare:
m0 = tortoise
tortoise = hash_function(tortoise, x, my_prefix)
hare = hash_function(hare, x, my_prefix)
# Loop many times until get second value
hare = hash_function(tortoise, x, my_prefix)
while tortoise != hare:
m1 = hare
hare = hash_function(hare, x, my_prefix)
# Save results to the file
save_results(m0, m1, hash_function(m0, x, my_prefix), hash_function2(m0, x*2, my_prefix))
def save_results(m0, m1, hash1, hash2):
"""Save results to file."""
with open("message0.bin", "wb") as file:
# Save first message:
file.write(my_prefix + m0)
with open("message1.bin", "wb") as file:
# Save second message:
file.write(my_prefix + m1)
with open("hash_1.bin", "wb") as file:
# Save bianry hash of messages:
file.write(hash1)
with open("wyniki.txt", "w") as file:
# Save of messages:
file.write("Message 0:\n")
file.write(my_prefix.hex() + m0.hex())
file.write("\n\nMessage 1:\n")
file.write(my_prefix.hex() + m1.hex())
file.write("\n\nHash hexdigest:\n")
file.write(hash2)
file.write("\n\nHash digest to hex():\n")
file.write(hash1.hex())
# Execute floyd funtion with initial value
floyd(x=7, initial=b"123hsdshd9fh933")
"""
b'\x41\x61\x99'.hex() -> '416199'
Message A: 42848253cf02b27a4a781
Message B: 42848259dd4879a8d98bb
They both hash to: 0a05085732df91
"""