This is an old revision of the document!


Lab 04 - Hashes, Public Key Encryption

Exercise 1

In this exercise you will implement the Birthday attack on SHA-1. The goal is to obtain a collision in the first four bytes of the hash.

You can start from this code:

import hashlib
import random
 
N_MESSAGES = 2**16 # 2^16 random messages
N_COLL_BYTES = 4
 
def generate_messages():
    msgs = []
 
    for i in xrange(N_MESSAGES):
        msgs.append("Ana are %d mere si %d pere" % (i, random.randint(0, 100000)))
 
    return msgs
 
def find_collision(msgs):
    hashes = dict() # will keep first N_COLL_BYTES bytes of each hash
 
    # TODO - find collision and return it as a tuple (msg1, msg2)
 
    return None # return None if no collision is found
 
def main():
    while True:
        # Generate N_MESSAGES (2^16) different random messages
        msgs = generate_messages()
 
        # Find a hash collision
        collision = find_collision(msgs) # collision = (msg1, msg2)
 
        if collision:
            # print collision (the two messages and their hash) in a readable format
            print collision[0] # msg1
            print hashlib.sha1(collision[0]).hexdigest() # msg1 hash
            print collision[1] # msg2
            print hashlib.sha1(collision[1]).hexdigest() # msg2 hash
            break
 
if __name__ == '__main__':
    main()

Can you also find a collision in the first 5 bytes of the hash?

In order to compute a SHA-1 hash, you can use the hashlib module:

hashlib.sha1(msg).hexdigest()

ac/laboratoare/04.1539726657.txt.gz · Last modified: 2018/10/17 00:50 by tiberiu.iorgulescu
CC Attribution-Share Alike 3.0 Unported
www.chimeric.de Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0