Xarth
Xarth

Reputation: 93

C# Faster generation of MD5 hashes

I have a project where I have been given a MD5 hash of a number between 1 and 2 billion, and I have to write a distributed program which obtains the number by brute force. I have successfully coded this program and it works. I was wondering if there is a way to speed up the generation of hashes?

Here is my current function to generate the hashes:

    static string generateHash(string input)
    {
        MD5 md5Hasher = MD5.Create();
        byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
        StringBuilder sBuilder = new StringBuilder();
        for (int i = 0; i < data.Length; i++)
        {
            sBuilder.Append(data[i].ToString("x2"));
        }
        return sBuilder.ToString();
    }

Thanks for any help

Upvotes: 9

Views: 7294

Answers (1)

Sani Huttunen
Sani Huttunen

Reputation: 24385

You could use BitConverter.ToString

static string generateHash(string input)
{
  MD5 md5Hasher = MD5.Create();
  byte[] data = md5Hasher.ComputeHash(Encoding.Default.GetBytes(input));
  return BitConverter.ToString(data);
}

Upvotes: 11

Related Questions