Question: Explanation for the Python class: x 1 and x 2 are messages: x 1 = Bob sends 1 0 to Oscar's account x 2 =

Explanation for the Python class: x1 and x2 are messages: x1= "Bob sends 10 to Oscar's account" x2= "Bob sends1000 to Oscar's account" For x1 and x2, Oscar wants to create two messages m1 and m2 that satisfy the following properties: 1) hash(m1)== hash(m2)(the implemented hash function below, called "h")2) Semantic meaning of original messages x1 and x2 must be preserved. In other words, m1 and m2 should look like x1 and x2, respectively. Hint: You can create such messages by appending invisible characters (spaces and tab) to the original messages. (This is what I have been attempting to do)3) The collision search must be effective. According to the argument of birthday attack, the number of messages you need to generate until the first collision found is O(2^(n/2)), where n is the number of hash output bits. The output bits I am testing with are 40 and 50.(The expected outputs are listed below). Expected Output: == elapsed time in seconds: 4.381968021392822 Bob sends 10 to Oscar's account (Whitespace / tabs following with a period after them) Bob sends1000 to Oscar's account (whitespace and tabs following with a period after them) True == Given / already completed portion of the Class (is not to be edited or changed!): import time class Collision: __slots__=["H1","H2", "output_bits", "modulus", "hash_func"] def __init__(self, output_bits, hash_func): self.H1= self.H2= self.output_bits = output_bits self.modulus = pow(2, output_bits) self.hash_func= hash_func def h(self, x): return self.hash_func(x)
My code:
import time
class Collision:
__slots__=["H1","H2", "output_bits", "modulus", "hash_func"]
def __init__(self, output_bits, hash_func):
self.H1={}
self.H2={}
self.output_bits = output_bits
self.modulus = pow(2, output_bits)
self.hash_func= hash_func
def h(self, x):
return self.hash_func(x)% self.modulus
def search(self, x1, x2, depth=40):
hash1= self.h(x1)
hash2= self.h(x2)
if hash1== hash2:
return x1, x2
if depth >0:
# Recurse on x1+"" and x2+""
collision = self.search(x1+"", x2+"", depth -1)
if collision:
return collision
# Recurse on x1+"\t" and x2+"\t"
return self.search(x1+"\t", x2+"\t", depth -1)
# If depth limit reached
return None
def confirmCollision(self, m1, m2):
hash1= self.h(m1)
hash2= self.h(m2)
return hash1== hash2 and hash1 in self.H1 and hash2 in self.H2
def main():
output_bits =40 # try 10 or 20 first
x1= "Bob sends $10 to Oscar's account"
x2= "Bob sends $1000 to Oscar's account"
collision = Collision(output_bits, hash) # Python hash funciton is used, though any hash functions should work
start = time.time()
result = collision.search(x1, x2)
end = time.time()
if result:
m1, m2= result
print("elapsed time in seconds:", end - start)
print(m1,".")
print(m2,".")
print(collision.confirmCollision(m1, m2))
else:
print("No collision found within the depth limit.")
if __name__=="__main__":
main()
The only thing that can be modified is search and main in order to complete the collision search with a depth of 40, it should take around 5 seconds to complete

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!