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"));
}
}
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.GetBytesfor some encoding… you need to work out which encoding though. Does this hash need to match the hash created somewhere else, for example?That’s because you’re using
MD5.Createincorrectly. The argument is an algorithm name. You should almost certainly just use the parameterless overload instead.I suspect you want something like: