Friday, June 23, 2006 11:47 PM bart

MD5 and SHA1 File Checksum Calculcator

So here's a new tool for my personal toolbox: a simple MD5 checksum calculator for files, written in .NET 2.0.

The little "open Visual Studio 2005 trigger" story: A friend of mine reported a problem with his Vista Beta 2 installation which reported corruption, so the obvious first check was to calculate the MD5 checksum. However, a 5-seconds internet search didn't provide me with a simple tool, so I took the do-it-yourself-in-5-minutes approach and wrote such a tool myself.

In the end, the code comes down to:

string f = args[0];
MD5 md5 = MD5
.Create();
StringBuilder sb = new StringBuilder
();

using
(FileStream fs = File.Open(f, FileMode.Open))
{
     foreach (byte b in
md5.ComputeHash(fs))
          sb.Append(b.ToString(
"x2"
).ToLower());
}

Console.WriteLine("MD5 checksum: {0}", sb.ToString());

but of course I made it a little more fancy, with the new System.Console capabilities to put a "spinner" and the elapsed time on the screen (using System.Diagnostics.Stopwatch) while calculating the checksum (you know, a little thingie that does | / - \ continiously) using a background thread. I also found out the following thanks to IntelliSense:

Console.TreatControlCAsInput = true;

The result is a little .exe that can be downloaded over here. I'll create a PowerShell cmdlet for the same task as well, which I'll publish over here too.

Here's the Windows Vista Ultimate Beta 2 x86 DVD ISO MD5 output (elapsed time for an Intel Centrino Duo 2.16 GHz + 2GB RAM running Windows Vista Beta 2):

MD5 File Checksum Calculcator 1.0.0.0
Copyright (C) BdsSoft 2006. All rights reserved.

Calculating checksum...

Results
-------
File:         d:\Downloads\en_windows_vista_beta2_x86_dvd.iso
MD5 checksum: 0e733ab1a8e8ff9a8684fd3639332773
Elapsed time: 00:01:06.2558607

Another example of BCL power and richness...

Update: A SHA1 checker is available too over here (using the HashAlgorithm.Create factory method this was a matter of seconds). Output for Windows Vista Ultimate Beta 2 x86 DVD ISO (matches the MSDN Subscribers SHA1 checksum):

SHA1 File Checksum Calculcator 1.0.0.0
Copyright (C) BdsSoft 2006. All rights reserved.

Calculating checksum...

Results
-------
File:          d:\Downloads\en_windows_vista_beta2_x86_dvd.iso
SHA1 checksum: 2404153a60d81103861b876878893222a5529d3a
Elapsed time:  00:01:39.4758180

Del.icio.us | Digg It | Technorati | Blinklist | Furl | reddit | DotNetKicks

Filed under: , , ,

Comments

# re: MD5 File Checksum Calculcator

Saturday, June 24, 2006 2:22 AM by David Douglass

Check out the HashTab Shell Extension at http://beeblebrox.org/hashtab/index.php

# re: MD5 and SHA1 File Checksum Calculcator

Monday, June 26, 2006 6:34 PM by bart

Hi David,

Thanks for providing a link to this piece of handy software. Another intersting thing is on The Code Project: http://www.codeproject.com/csharp/columnhandler.asp

Cheers,
Bart

# PowerShell - A file hasher cmdlet and the Extended Type System explained

Thursday, November 30, 2006 4:30 AM by B# .NET Blog

Introduction Today (11/29/06) I received a mail from one of my blog readers: Hi Bart, in the following