File Checksum using .NET

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

Comments

2/8/2007 5:02:45 PM #

http://

Hi Jeff,
This is very helpful post.

Quick question. Are these checksums guaranteed to be unique for different files? I'm wanting to store documents in a SQL database, but before I insert a document I want to make sure it doesn't already exist. So I was thinking of storing each files checksum too. Then before I insert I'd run a quick select to check for duplicates.

Thanks!
Jonathan

http:// |

2/8/2007 10:36:13 PM #

Jonathan:

Yes, the checksum should be unique for different files.  Technically, there are some theoretical collisions that could occur depending upon the algorithm that is being used, but I haven't encountered any problems using SHA1 or higher.  

I hope this helps.

jeff.barnes |

Comments are closed

About Me

I'm a passionate software developer and advocate of the Microsoft .NET platform.  In my opinion, software development is a craft that necessitates a conscious effort to continually improve your skills rather than falling into the trap of complacency.  I was also awarded as a Microsoft MVP in Connected Systems in 2008, 2009, and 2010.


Can’t code withoutThe best C# & VB.NET refactoring plugin for Visual Studio
Follow jeff_barnes on Twitter

View Jeff Barnes's profile on LinkedIn

 

Shared Items

Disclaimer

Anything you read or see on this site is solely based on my own thoughts.  The material on this site does not necessarily reflect the views of my employer or anyone else.  In other words, I don't speak for anyone other than myself.  So, don't assume I am the official spokesperson for anyone.