Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Faster generation of MD5 hashes

Tags:

c#

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

like image 956
Xarth Avatar asked Mar 19 '12 12:03

Xarth


1 Answers

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);
}
like image 108
Sani Singh Huttunen Avatar answered Oct 03 '22 01:10

Sani Singh Huttunen