From 9af26a055c110264e146d436b363143cdac11654 Mon Sep 17 00:00:00 2001 From: Daniel <59575049+lemoentjiez@users.noreply.github.com> Date: Thu, 3 Nov 2022 11:42:40 +0200 Subject: [PATCH] Create birthday_attack.py --- LabPSA_1/birthday_attack.py | 25 +++++++++++++++++++++++++ 1 file changed, 25 insertions(+) create mode 100644 LabPSA_1/birthday_attack.py diff --git a/LabPSA_1/birthday_attack.py b/LabPSA_1/birthday_attack.py new file mode 100644 index 0000000..f0236da --- /dev/null +++ b/LabPSA_1/birthday_attack.py @@ -0,0 +1,25 @@ +import hashlib +import os +import binascii + +nr_collisions = 0 +hashed_dict = {} +strn_table = {} +n = 1 +file = open("results.txt", "w") +while n < 11: + strn = binascii.b2a_hex(os.urandom(16)) + hashed_strn = hashlib.md5(strn).hexdigest() + sliced_hash = hashed_strn[:n] + if sliced_hash in hashed_dict: + nr_collisions += 1 + x = "----------------------------------------------------------------------------------" + z = "Collision number: " + str(nr_collisions) + " "*n + "Collision: " + str(sliced_hash) + a = "Hashes: " + " "*(n-7) + str(hashed_dict[sliced_hash]) + " " + str(hashed_strn) + b = "Values: " + " "*(n-7) + strn.decode() + " " + strn_table[sliced_hash].decode() + file.write(x + "\n" + z + "\n" + a + "\n" + b + "\n") + n += 1 + else: + hashed_dict[sliced_hash] = hashed_strn + strn_table[sliced_hash] = strn +file.close()