Jonas Natten
Jonas Natten

Reputation: 17

How would i find the string of a hash?

This is the code(python) my teacher wrote. (with the prime numbers i've found using a bruteforce). I also have some test data with passwords that have matching hashes (not that it really matters now that i've found the two primes used in the function).

Can anyone help me on the way of finding a way to "reverse" this or atleast give me a tip?

def prime_number_hash(s , 17299, 209569):
    """
    Will hash the string s using the two prime numbers p and n.
    """
    h = 0
    for k,l in enumerate(s):
        v = ord(l)
        # k is the index of the letter 
        h += v * pow(p,k)
    return h % n

Upvotes: 0

Views: 124

Answers (2)

Jonas Natten
Jonas Natten

Reputation: 17

I've made it. I just made 6 loops and tried every combination of the letter. And an exception to break out when i found it. It was found within 5 seconds.

Upvotes: 0

NPE
NPE

Reputation: 500157

The simple answer is that you can't reconstruct the input string given its hash. The function has 209569 distinct output values, and there are many more possible input strings. See Pigeonhole principle.

If the task is to find a string that has the given hash, that is a different problem...

Upvotes: 4

Related Questions