From 20d8dba604c000ee2a57ad10adc78ffd8f06b297 Mon Sep 17 00:00:00 2001 From: Daniel <59575049+lemoentjiez@users.noreply.github.com> Date: Thu, 3 Nov 2022 15:03:57 +0200 Subject: [PATCH] Update birthday_attack.py --- LabPSA_1/birthday_attack.py | 19 ++++++++----------- 1 file changed, 8 insertions(+), 11 deletions(-) diff --git a/LabPSA_1/birthday_attack.py b/LabPSA_1/birthday_attack.py index f0236da..0f91f1a 100644 --- a/LabPSA_1/birthday_attack.py +++ b/LabPSA_1/birthday_attack.py @@ -2,23 +2,20 @@ import hashlib import os import binascii -nr_collisions = 0 -hashed_dict = {} -strn_table = {} -n = 1 +nr_collisions, hashed_dict, strn_table, n = 0, {}, {}, 10 file = open("results.txt", "w") -while n < 11: +while n: strn = binascii.b2a_hex(os.urandom(16)) hashed_strn = hashlib.md5(strn).hexdigest() - sliced_hash = hashed_strn[:n] + sliced_hash = hashed_strn[:10] 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() + x = "-----------------------------------------------------------------------------------" + z = "Collision number: " + str(nr_collisions) + " " * 18 + "Collision: " + str(sliced_hash) + a = "Hashes: " + " " * 10 + str(hashed_dict[sliced_hash]) + " " + str(hashed_strn) + b = "Values: " + " " * 10 + strn.decode() + " " + strn_table[sliced_hash].decode() file.write(x + "\n" + z + "\n" + a + "\n" + b + "\n") - n += 1 + n -= 1 else: hashed_dict[sliced_hash] = hashed_strn strn_table[sliced_hash] = strn