Home Tutorials C# Calculating an MD5 checksum for a File

Login Form



Compilr, Powered by Joomla! and designed by SiteGround web hosting

Calculating an MD5 checksum for a File PDF Print E-mail
Written by Kyle Hankinson   
Friday, 29 February 2008 12:23

If you have ever wanted to try and calculate an MD5 checksum for .Net, you have come to the right place. This is a very simple and straight forward procedure. The first thing that you need to do, is include the System.Security.Cryptography namespace. Once this is done you can create an instance of the MD5 class with the following code:

private static MD5 md5 = new MD5 ( );

Our new instance will allow us to call the md5.ComputeHash function, which can take a stream in as an argument. Here is an example of calculating our checksum for a file.

   using ( FileStream stream = File.OpenRead ( file ) )
{
byte [] checksum = md5.ComputeHash ( stream );
return ( BitConverter.ToString ( checksum ).Replace ( "-", string.Empty );
} // End of using fileStream

To view a complete project example, visit:

http://compilr.com/IDE/31-md5/

Last Updated ( Friday, 29 February 2008 16:58 )
 

Advertisement


Designed by:
Joomla 1.5 Joomla Templates
Joomla hosting pack
Compilr, Powered by Joomla! and designed by SiteGround web hosting