How to calculate md5 of a string in C#

Filed Under (c#, cryptography) by The Chef on 26-05-2009

Tagged Under : , , ,

If you have ever dared to write the md5 hashing algorithm in C#, you better stay calm because the MS guys thought of everything. It’s all in the framework. With just 2 lines of code the job is done. Take a look at the following piece of code:

String str="some text here";
MD5 md5 = new MD5CryptoServiceProvider();
String Hash = BitConverter.ToString(md5.ComputeHash(ASCIIEncoding.Default.GetBytes(str))).Replace("-","");

Easy, isn’t it ?