Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reversing a hash function

Tags:

c#

algorithm

hash

I have the following hash function, and I'm trying to get my way to reverse it, so that I can find the key from a hashed value.

uint Hash(string s)
{
    uint result = 0;
    for (int i = 0; i < s.Length; i++)
    {
        result = ((result << 5) + result) + s[i];
    }
    return result;
}

The code is in C# but I assume it is clear.

I am aware that for one hashed value, there can be more than one key, but my intent is not to find them all, just one that satisfies the hash function suffices.

EDIT :

The string that the function accepts is formed only from digits 0 to 9 and the chars '*' and '#' hence the Unhash function must respect this criteria too.

Any ideas? Thank you.

like image 651
0xFF Avatar asked Dec 24 '10 00:12

0xFF


People also ask

Can you reverse a hash function?

Hash functions are not reversible in general. MD5 is a 128-bit hash, and so it maps any string, no matter how long, into 128 bits. Obviously if you run all strings of length, say, 129 bits, some of them have to hash to the same value. (Another win for the pigeon hole principle.)

How do you reverse a SHA256 hash?

SHA-256 is a cryptographic (one-way) hash function, so there is no direct way to decode it. The entire purpose of a cryptographic hash function is that you can't undo it.

Can a hashed password be reversed?

You can't "reverse" password hashes. You can't "unhash" or "dehash" passwords. You can't "reverse" or "invert" MD5, SHA256, bcrypt, SHA1, or similar hashes, salted or unsalted. You (usually) can't "decode" passwords, "decrypt" password hashes or "reverse" or "unscramble" password hashes at all.

Can you reverse engineer a SHA256 hash?

To answer your question, no, it's not possible to "unhash" 2 and obtain 1. In order to "crack" the second hash, you would have to brute force it by computing the sha256 of other strings and comparing the result with 2. If they match, then you (probably) have the original string.


1 Answers

This should reverse the operations:

string Unhash(uint hash)
{
    List<char> s = new List<char>();
    while (hash != 0)
    {
        s.Add((char)(hash % 33));
        hash /= 33;
    }
    s.Reverse();
    return new string(s.ToArray());
}

This should return a string that gives the same hash as the original string, but it is very unlikely to be the exact same string.

like image 188
Mark Byers Avatar answered Oct 25 '22 12:10

Mark Byers