29

Need to get MD5 hash from string.
Get an error MD5 is null.
I am tying to get a 32 character MD5 hash from a string.

using (System.Security.Cryptography.MD5 md5 = 
       System.Security.Cryptography.MD5.Create("TextToHash"))
{
    byte[] retVal = md5.Hash;
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < retVal.Length; i++)
    {
        sb.Append(retVal[i].ToString("x2"));
    }
}
paparazzo
  • 44,497
  • 23
  • 105
  • 176

3 Answers3

55

Need to get MD5 hash from string.

Then first you need to convert your string to binary data in some form. How you do that will depend on your requirements, but it'll probably be Encoding.GetBytes for some encoding... you need to work out which encoding though. Does this hash need to match the hash created somewhere else, for example?

Get an error MD5 is null.

That's because you're using MD5.Create incorrectly. The argument is an algorithm name. You should almost certainly just use the parameterless overload instead.

I suspect you want something like:

byte[] hash;
using (MD5 md5 = MD5.Create())
{
    hash = md5.ComputeHash(Encoding.UTF8.GetBytes(text));
}
// Now convert the binary hash into text if you must...
Jon Skeet
  • 1,421,763
  • 867
  • 9,128
  • 9,194
28

The string passed to Create is not the "text to hash", but the algorithm to use. I suspect you want:

using (System.Security.Cryptography.MD5 md5 = 
   System.Security.Cryptography.MD5.Create())
{
    byte[] retVal = md5.ComputeHash(Encoding.Unicode.GetBytes("TextToHash"));
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < retVal.Length; i++)
    {
        sb.Append(retVal[i].ToString("x2"));
    }
}
Reed Copsey
  • 554,122
  • 78
  • 1,158
  • 1,373
  • 1
    Hi Reed: I have a question: How do I know which encoding to use? In my case, I need to calc a hash that I received as a query string. (so when it was posted http, it was probably UTF-8), HOWEVER, once it was stored in a .Net string, would it now be in Unicode (UTF-16), because that is the native encoding? Did that transformation occur for me? – JMarsch May 15 '13 at 22:20
  • @JMarsch Yeah - it will likely "just work" if you use Encoding.Unicode, if it was UTF-8 originally. – Reed Copsey May 15 '13 at 22:37
  • Keep in mind that if your "source" MD5 was calculated on the utf-8 presentation of your string, you will get a different MD5 if you now use utf-16 instead. This is true even if you have just ASCII characters within your string. – brighty May 02 '14 at 09:44
  • 1
    Using Encoding.Unicode rather than UTF 8 will result in different hash than most of Javascript libs will produce on the client - most convert to utf8. This does not make the answer wrong but I write this only as a warning if somebody gets different MD5 hashes on the client and on the server - the preliminary encoding of the string may get under the radar and make somebody think there is a bug. – Ognyan Dimitrov Apr 22 '16 at 09:34
1

The reason you are getting a null return is the string parameter to the Create method speciies the algorithm, not the text that is being hashed. There is no TextToHash algorithmn hence you get null as the return.

JaredPar
  • 733,204
  • 149
  • 1,241
  • 1,454