I'm trying to find a MD5 hash collision between 2 numbers such that one is prime and the other is composite (at most 1024-bit). I'm using fastcoll with random prefixes for each iteration.
For this I wrote this script:
import subprocess
from Crypto.Util.number import bytes_to_long, isPrime
import string
import random
won = False
N = 10
while not won:
# Run the fastcoll executable to generate the two messages
pfx = ''.join(random.choices(string.ascii_uppercase + string.digits, k=N))
file = open("prefix", "w")
file.write(pfx)
file.close()
subprocess.run(["./fastcoll_v1.0.0.5.exe", "-p", "prefix", "-o", "msg1.bin", "msg2.bin"])
# Convert the messages to integers
with open("msg1.bin", "rb") as f:
msg1_int = bytes_to_long(f.read())
if(msg1_int.bit_length() > 1024):
continue
with open("msg2.bin", "rb") as f:
msg2_int = bytes_to_long(f.read())
if(msg2_int.bit_length() > 1024):
continue
print(msg1_int)
print(isPrime(msg1_int))
print(msg2_int)
print(isPrime(msg2_int))
# Check if one message is prime and the other is composite
if isPrime(msg1_int) and not isPrime(msg2_int):
won = True
elif isPrime(msg2_int) and not isPrime(msg1_int):
won = True
However, I'm not sure this is the most efficient way so can someone suggest improvements? Any help would be appreciated.