35 lines
672 B
Python
35 lines
672 B
Python
import string
|
|
import random
|
|
import hashlib
|
|
|
|
letters = string.ascii_lowercase + string.ascii_uppercase
|
|
|
|
# 4a
|
|
|
|
def H(m: str) -> str:
|
|
return hashlib.sha256(m.encode()).hexdigest()[:2]
|
|
|
|
i = 0
|
|
hashes = []
|
|
messages = []
|
|
while True:
|
|
|
|
n_char = random.randint(1, 1000)
|
|
m = ''.join([random.choice(letters) for n in range(n_char)])
|
|
hm = H(m)
|
|
|
|
# print(i)
|
|
# print(m)
|
|
# print(hm)
|
|
|
|
if hm in hashes:
|
|
print(f'Message 1 "{m}" and message 2 "{messages[hashes.index(hm)]}" have the same hash {hm}.')
|
|
print(f'It took {i} iterations to find a collision.')
|
|
break
|
|
else:
|
|
hashes.append(hm)
|
|
messages.append(m)
|
|
|
|
i += 1
|
|
|