Joseph Michael Pesch
VP Programming

CheckSum Utility

by 2. January 2012 16:32

Below is a C# implementation of two CheckSum utilities, the SHA256Managed uses the native .Net Security.Cryptography; while the Md5SumByProcess uses a windows implementation of Unix MD5Sum.exe (zip file attached to this blog can be downloaded from SourceForge at: http://sourceforge.net/projects/unxutils/files/unxutils/current/UnxUtils.zip/download). 

using System;
using System.Diagnostics;
using System.IO;
using System.Security.Cryptography;

  class CheckSumUtil
  {
    public static string Md5SumByProcess(string file)
    {
      var p = new Process();
      p.StartInfo.FileName = "md5sum.exe";
      p.StartInfo.Arguments = file;
      p.StartInfo.UseShellExecute = false;
      p.StartInfo.RedirectStandardOutput = true;
      p.Start();
      p.WaitForExit();
      string output = p.StandardOutput.ReadToEnd();
      return output.Split(' ')[0].Substring(1).ToUpper();
    }

    public static string SHA256Managed(string file)
    {
      using(var stream = new BufferedStream(File.OpenRead(file), 1200000))
      {     
        SHA256Managed sha = new SHA256Managed();
        byte[] checksum = sha.ComputeHash(stream);
        return BitConverter.ToString(checksum).Replace("-", String.Empty);
      }
    }

  }
}

UnxUtils.zip (3.21 mb)

Tags:

C#

Comments are closed