user3024888
user3024888

Reputation: 49

How to find SHA1 hash?

i got interesting task at school. I have to find message which sha-1 hash lasts with my birthday example. if i was born on 4th may 1932 then the hash must end with 040532. Any suggestions how to find it out?

Upvotes: 3

Views: 1295

Answers (2)

user2864740
user2864740

Reputation: 61975

Start generating hashes from distinct messages1.

Eventually a hash will be generated with such a property. This is not that bad to brute-force as the range is only 224 (or ~16 million) and SHA is very fast.

There is no shortcut as SHA is a one way cryptographic hash function. In particular here, SHA has the property that "it is infeasible to generate a message that has a given hash".


1 The inputs should be distinct, and a simple counter will suffice. However, it may be more interesting to generate quasi-random messages based on the birthday being sought - e.g. including the date in various forms and sentences Mad Lib style. As long as this doesn't limit the domain, such that there is no qualifying hash, it'll work just as well as any other set of source messages.

Upvotes: 2

Stefan
Stefan

Reputation: 528

my solution in C#:

//A create Sha1 function:
using System.Security.Cryptography;
public static string GetSHA1Hash(string text)
    {
        var SHA1 = new SHA1CryptoServiceProvider();

        byte[] arrayData;
        byte[] arrayResult;
        string result = null;
        string temp = null;

        arrayData = Encoding.ASCII.GetBytes(text);
        arrayResult = SHA1.ComputeHash(arrayData);
        for (int i = 0; i < arrayResult.Length; i++)
        {
            temp = Convert.ToString(arrayResult[i], 16);
            if (temp.Length == 1)
                temp = "0" + temp;
            result += temp;
        }
        return result;
    }

Source

Then a Random String generator:

 private static Random random = new Random((int)DateTime.Now.Ticks);//thanks to McAden
 private string RandomString(int size)
    {
        StringBuilder builder = new StringBuilder();
        char ch;
        for (int i = 0; i < size; i++)
        {
            ch = Convert.ToChar(Convert.ToInt32(Math.Floor(26 * random.NextDouble() + 65)));
            builder.Append(ch);
        }

        return builder.ToString();
    }

Source

and now you can bruteforce for your combination:

        string search = "32";
        string result = String.Empty;
        int slen = 5;
        string myTry = RandomString(slen);

        while (!result.EndsWith(search))
        {
            myTry = RandomString(slen);
            result = GetSHA1Hash(myTry);

        }

        MessageBox.Show(result + "   " + myTry);

This would search for a Hash String ending with 32. Happy Bruteforcing :)

EDIT: found one for your example: HXMQVNMRFT gives e5c9fa9f6acff07b89c617c7fd16a9a043040532

Upvotes: 4

Related Questions