{

While there are a slew of cryptographic hashing algorithms a programmer finds at their disposal, and despite the documented vulnerabilities accompanying it, MD5 remains a popular approach to generating hashes. Hashes, as you know, are useful for a variety of things – verifying file integrity and password storage being just a few.

I discovered the Silverlight MD5 implementation because a project I work on leveraged MD5 for file integrity. I was a bit curious about this since it’s long been a recommendation to skip MD5 and use hashing algorithms like SHA-1 and SHA-2 which are proven to be more robust. It’s not just the predecessor of my project, many places still use MD5 despite the bad rap it takes. I posted on SuperUser and got some interesting answers, the most notable of which (I marked as the answer) detailed how MD5 is faster than SHA-1. Other comments can be summarized with this excerpt of an answer that wwas most highly upvoted:

“A MD5 hash is "good enough" for most menial tasks. Recall that it's still incredibly difficult to produce meaningful collisions in the same number of bytes.”

Silverlight’s core assemblies do not support MD5 but a person named Reid Borsuk at Microsoft created an implementation that is robust and easy to use. Because the code is hosted on MSDN Code Gallery you will need to put it into a project on your own but it’s not that hard. Here are some simple steps:

1. Create a Silverlight Library project

2. Download the MD5.cs and MD5Managed.cs (you can also download the MD5Test.cs if you’re interested in unit testing it)

3. Add both files to your Silverlight Library project.

4. Compile and you’re done!

If' that’s not straightforward enough, I’ve created a downloadable Visual Studio 2008 solution with the library, a Silverlight Application and a Web application to show it in action. Here is the code behind generating a hash from a file stream in a button’s click event:

 
private void cryptoButton_Click(object sender, RoutedEventArgs e)
{
OpenFileDialog ofd = new OpenFileDialog();
if (true == ofd.ShowDialog()) {
using (FileStream fs = ofd.File.OpenRead())
{
MD5Managed md5 = new MD5Managed();
byte[] hash = md5.ComputeHash(fs);

fs.Close();
}
}
}


Thanks a lot Reid!



}