65.9K
CodeProject is changing. Read more.
Home

Get MD5 and SHA-1 (SHA1) of any file

starIconstarIconstarIcon
emptyStarIcon
starIcon
emptyStarIcon

3.62/5 (40 votes)

Feb 11, 2005

MIT
viewsIcon

137877

downloadIcon

2809

Get file's hash (SHA-1, MD5) - This program needs .NET Framework 1.1.

Sample Image - DT_File_Hasher.jpg

Introduction

With this program, after you click on Open File, you can select any file and after clicking on Open button, you will get MD5 and SHA-1 (SHA1) files' hash. This program is very useful for somebody who wants to get and save some important files' hash. You must note that, if somebody changes even a bit of some content file, the file's hash will be changed. So try it and enjoy it!

For this application, I created a class for getting MD5 and SHA-1 files' hash:

namespace IranianExperts
{
 public sealed class DTHasher
 {
  private DTHasher(){}

  private static byte[] ConvertStringToByteArray(string data)
  {
   return(new System.Text.UnicodeEncoding()).GetBytes(data);
  }

  private static System.IO.FileStream GetFileStream(string pathName)
  {
   return(new System.IO.FileStream(pathName, System.IO.FileMode.Open, 
             System.IO.FileAccess.Read, System.IO.FileShare.ReadWrite));
  }

  public static string GetSHA1Hash(string pathName)
  {
   string strResult = "";
   string strHashData = "";

   byte[] arrbytHashValue;
   System.IO.FileStream oFileStream = null;

   System.Security.Cryptography.SHA1CryptoServiceProvider oSHA1Hasher=
              new System.Security.Cryptography.SHA1CryptoServiceProvider();

   try
   {
    oFileStream = GetFileStream(pathName);
    arrbytHashValue = oSHA1Hasher.ComputeHash(oFileStream);
    oFileStream.Close();

    strHashData = System.BitConverter.ToString(arrbytHashValue);
    strHashData = strHashData.Replace("-", "");
    strResult = strHashData;
   }
   catch(System.Exception ex)
   {
    System.Windows.Forms.MessageBox.Show(ex.Message, "Error!", 
             System.Windows.Forms.MessageBoxButtons.OK, 
             System.Windows.Forms.MessageBoxIcon.Error, 
             System.Windows.Forms.MessageBoxDefaultButton.Button1);
   }

   return(strResult);
  }

  public static string GetMD5Hash(string pathName)
  {
   string strResult = "";
   string strHashData = "";

   byte[] arrbytHashValue;
   System.IO.FileStream oFileStream = null;

   System.Security.Cryptography.MD5CryptoServiceProvider oMD5Hasher=
              new System.Security.Cryptography.MD5CryptoServiceProvider();

   try
   {
    oFileStream = GetFileStream(pathName);
    arrbytHashValue = oMD5Hasher.ComputeHash(oFileStream);
    oFileStream.Close();

    strHashData = System.BitConverter.ToString(arrbytHashValue);
    strHashData = strHashData.Replace("-", "");
    strResult = strHashData;
   }
   catch(System.Exception ex)
   {
    System.Windows.Forms.MessageBox.Show(ex.Message, "Error!", 
               System.Windows.Forms.MessageBoxButtons.OK, 
               System.Windows.Forms.MessageBoxIcon.Error, 
               System.Windows.Forms.MessageBoxDefaultButton.Button1);
   }

   return(strResult);
  }
 }
}