12. January 2007 17:44
It seems there are a number of people out there who attempt to write their own checksum algorithms for validating the integrity of a file. However, these type of algorithms need to be incredibly reliable. Unfortunately, the "home-grown" version is often error prone. Typically, the developer will do enough testing to assure himself/herself that the algorithm works. Unfortunately, they usually find out it wasn't so reliable as once the code is in production.
Unless you have a compelling reason to create your own, why not use the what the .NET Framework already provides. The System.Security.Cryptography namespace contains several different implementations of cryptographic hash algorithms. These have already been thoroughly tested and are widely used. You can take your pick from MD5, SHA1, or any of the SHA2 variants.
Here is a snippet of creating a checksum via SHA256:
using System.IO;
using System.Security.Cryptography;
private static string GetChecksum(string file)
{
using (FileStream stream = File.OpenRead(file))
{
SHA256Managed sha = new SHA256Managed();
byte[] checksum = sha.ComputeHash(stream);
return BitConverter.ToString(checksum).Replace("-", String.Empty);
}
}
8fa4f62e-7be3-49dc-9c07-00752d9cee36|8|5.0
By: Jeff Barnes
Category:
Tags: