0

I and trying find a how long it would take to find a collision for an 8 bit hash that executes SHA512. I created a table to store the random hash values and to print the output if there is a collision. Can anyone suggest how I may get my timer to work? Thanks.

import hashlib
import os
import time

lookup_table = {}
collision_counts = 0

for value in range(10):

    random_binary = os.urandom(8)
    result = hashlib.sha512(random_binary).digest()
    result = result[:4]
    if result in lookup_table:
        print("Collision")
        print(random_binary, result)
        print(lookup_table[result], result)
        collision_counts += 1
        timer = time.timer(result)
        print(timer)
    else:
        lookup_table[result] = random_binary

print("Number of collisions:", collision_counts)
BioGeek
  • 21,897
  • 23
  • 83
  • 145
NHP
  • 1
  • 1
  • Welcome to Stack Overflow. Please take the [tour] and read [ask]. What does "I am unable to bring it to where I need it to be" mean? – ChrisGPT was on strike Nov 08 '21 at 00:56
  • [There is no such thing as ``time.timer``](https://docs.python.org/3/library/time.html). What do you expect it to do? – MisterMiyagi Nov 08 '21 at 15:08
  • You're not going to find a collision between 4-byte hash values in a mere ten attempts. Even ten thousand attempts wouldn't make it likely. – jasonharper Nov 08 '21 at 15:12
  • See https://realpython.com/python-timer/ or https://stackoverflow.com/questions/1557571/how-do-i-get-time-of-a-python-programs-execution – BioGeek Nov 08 '21 at 15:33
  • Be aware that there just *are no* collisions for any 1-byte input for the leading 4-byte output of sha512. – MisterMiyagi Nov 08 '21 at 15:33

0 Answers0