Click here to Skip to main content
14,212,594 members
Click here to Skip to main content
Article
Posted 11 Feb 2005

Stats

119.3K views
2.7K downloads
58 bookmarked

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

,
Rate this:
3.62 (40 votes)
Please Sign up or sign in to vote.
3.62 (40 votes)
11 Feb 2005     MIT    
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);
  }
 }
}

License

This article, along with any associated source code and files, is licensed under The MIT License

Share

About the Author

Dariush Tasdighi
Web Developer Sematec Ins.
Iran (Islamic Republic of) Iran (Islamic Republic of)
My experiences are:

HTML 5.0, CSS 3.0
JQuery, Angular JS, Bootstrap

MVC 5.0, WEB API, c#

My Site URLs:
http://www.IranianExperts.ir
http://www.IranianExperts.com

My Yahoo Group URL: http://groups.yahoo.com/group/iranianexperts

Mobile: 0098-912-108-7461
Address: Tehran, Tehran, Iran

Comments and Discussions

 
QuestionThank you from Mexico Pin
demon40719-Jun-18 15:45
memberdemon40719-Jun-18 15:45 
QuestionThank you Pin
Gun Gun Febrianza3-Oct-12 15:30
member Gun Gun Febrianza3-Oct-12 15:30 
thank mr. i'm student from indonesia this helpfull for me. Smile | :)
Indonesian IT Intelijensi
Freedom of Revealing And Sharing Knowledge.

<img src="http://i564.photobucket.com/albums/ss86/febrianza/558392_461174860568975_902574845_n-1.jpg" border="0" alt="Photobucket">

www.net-sourcecode.blogspot.com

GeneralMy vote of 5 Pin
Gun Gun Febrianza3-Oct-12 15:29
member Gun Gun Febrianza3-Oct-12 15:29 
GeneralNice! Pin
metator20-Sep-11 6:37
membermetator20-Sep-11 6:37 
GeneralMSj Pin
msj383069921-Jan-09 2:15
membermsj383069921-Jan-09 2:15 
GeneralThanks Pin
Namazi4-Jan-08 23:04
memberNamazi4-Jan-08 23:04 
GeneralCools Pin
Chaudhary5-Mar-06 5:00
memberChaudhary5-Mar-06 5:00 
GeneralProgressbar Pin
Anonymous16-Mar-05 20:46
memberAnonymous16-Mar-05 20:46 
Generalwelldone Pin
GhazalAssadipour12-Feb-05 2:40
sussGhazalAssadipour12-Feb-05 2:40 
GeneralThanks Pin
Mark Hancock11-Feb-05 18:35
memberMark Hancock11-Feb-05 18:35 
GeneralWould be better with more documentation Pin
RayD11-Feb-05 7:06
memberRayD11-Feb-05 7:06 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Layout: fixed | fluid

Article Copyright 2005 by Dariush Tasdighi
Everything else Copyright © CodeProject, 1999-2019

Server Web01
Version 2.8.190619.2