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:
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);