VB Dot NET Forum: Get MD5 of File - VB Dot NET Forum

Jump to content

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

Get MD5 of File

#1 User is offline   Dylan Icon

  • Advanced Member
  • PipPipPip
Group:
Root Admin
Posts:
1,055
Joined:
12-January 07
Location:
Atlanta, GA, USA
Interests:
Computer Science, Operating Systems

VB Knowledge: Experienced
VB Version:
VB 2008 (.NET 3.0/3.5)
OS: Mac OS X

Posted 24 June 2007 - 04:09 PM

Quote

In cryptography, MD5 (Message-Digest algorithm 5) is a widely used cryptographic hash function with a 128-bit hash value. As an Internet standard (RFC 1321), MD5 has been employed in a wide variety of security applications, and is also commonly used to check the integrity of files. An MD5 hash is typically a 32-character hexadecimal number.

MD5 was designed by Ronald Rivest in 1991 to replace an earlier hash function, MD4. In 1996, a flaw was found with the design of MD5; while it was not a clearly fatal weakness, cryptographers began to recommend using other algorithms, such as SHA-1. In 2004, more serious flaws were discovered making further use of the algorithm for security purposes questionable.


In this tutorial I will show you how to get the MD5 hash of a file.

We will first need to import a few of .NET's Classes.

Imports System.Security.Cryptography
Imports System.IO

Imports System.Text


IO is used to load the file into the MD5 hasher. Cryptography will take care of getting the raw MD5 hash while System.Text will append the MD5 hash.

Next, we will get the raw MD5 hash of our file. Please note that I will be using the file "C:\MyPic.jpg". You will need to change that to whatever file you'll be getting the hash of.

		Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
		Dim f As FileStream = New FileStream(TextBox1.Text, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
		f = New FileStream(TextBox1.Text, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
		md5.ComputeHash(f)
		Dim ObjFSO As Object = CreateObject("Scripting.FileSystemObject")
		Dim objFile = ObjFSO.GetFile(TextBox1.Text)


Now, we will use the StringBuilder class to append our string and make it the correct format.

	  
		Dim hash As Byte() = md5.Hash
		Dim buff As StringBuilder = New StringBuilder
		Dim hashByte As Byte
		For Each hashByte In hash
			buff.Append(String.Format("{0:X1}", hashByte))
		Next


To get the final MD5 Checksum, we will call this:

buff.ToString()


That's it! You now know how to get the MD5 checksum of a file in Visual Basic .NET.
0

#2 User is offline   Sean Icon

  • Newbie
  • Pip
Group:
Members
Posts:
1
Joined:
13-August 07

VB Knowledge: Experienced
VB Version:
VB 2008 (.NET 3.0/3.5)

Posted 13 August 2007 - 03:52 PM

Thanks, this is very useful.

You have a couple of errors in the code that I found when using it:
1. These two lines should be removed, I think you probably left them in from an round of testing
		Dim ObjFSO As Object = CreateObject("Scripting.FileSystemObject")
		Dim objFile = ObjFSO.GetFile(TextBox1.Text)

2. You need to use X2 for the hex format string otherwise bytes with a value from 0 to F will be missing their leading zero in the resulting MD5 string. For example l got "3D16CD3D1AC9414C2E035D62015E6C" instead of "3D16CD3D1AC90414C2E035D620015E6C". To fix this, change
	  
		Dim hash As Byte() = md5.Hash
		Dim buff As StringBuilder = New StringBuilder
		Dim hashByte As Byte
		For Each hashByte In hash
			buff.Append(String.Format("{0:X1}", hashByte))
		Next

to
	  
		Dim hash As Byte() = md5.Hash
		Dim buff As StringBuilder = New StringBuilder
		Dim hashByte As Byte
		For Each hashByte In hash
			buff.Append(String.Format("{0:X2}", hashByte))
		Next

Sean
0

#3 User is offline   Dylan Icon

  • Advanced Member
  • PipPipPip
Group:
Root Admin
Posts:
1,055
Joined:
12-January 07
Location:
Atlanta, GA, USA
Interests:
Computer Science, Operating Systems

VB Knowledge: Experienced
VB Version:
VB 2008 (.NET 3.0/3.5)
OS: Mac OS X

Posted 13 August 2007 - 07:00 PM

Thank you very much for pointing that out, Sean!

;)
0

#4 User is offline   Mark Icon

  • Newbie
  • Pip
Group:
Members
Posts:
1
Joined:
16-October 07

VB Knowledge: Experienced
VB Version:
VB 2005 (.NET 2.0)

Posted 16 October 2007 - 12:03 PM

I noticed one more thing that should be done after you are done with the file stream. Close it! Otherwise the file stays locked and can't be renamed or deleted.

Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
Dim f As FileStream = New FileStream(TextBox1.Text, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
f = New FileStream(TextBox1.Text, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)

md5.ComputeHash(f)

f.Close()


Mark
0

#5 User is offline   corey19981 Icon

  • Newbie
  • Pip
Group:
Members
Posts:
1
Joined:
14-April 08

VB Knowledge: Decent
VB Version:
VB 2008 (.NET 3.0/3.5)

Posted 14 April 2008 - 08:33 AM

GREAT tutorial, it works perfectly for me.

HOWEVER

My project involves the following

i want it to check the md5 hash of a file, then download a newer file version off the internet, and check the md5 hash again, then display the results.

If I use your code to make it check the md5 AFTER it downloads the new version, it works fine, however, If I move the md5 code so that it runs BEFORE the download begins, the download fails. (the program is unable to write the downloaded file over the old one.)

of course, if the md5 code runs after the download is completed it works fine, however I need it to run both BEFORE AND AFTER the download starts.

Please help!

thanks


I tried
f.close()

but it didn't work. Any ideas on how to properly unlock the file?
0

#6 User is offline   SpiritDevSS Icon

  • Newbie
  • Pip
Group:
Members
Posts:
1
Joined:
05-September 08

VB Knowledge: Decent
VB Version:
VB 2005 (.NET 2.0)

Posted 05 September 2008 - 05:00 AM

View Postcorey19981, on Apr 14 2008, 08:33 AM, said:

GREAT tutorial, it works perfectly for me.

HOWEVER

My project involves the following

i want it to check the md5 hash of a file, then download a newer file version off the internet, and check the md5 hash again, then display the results.

If I use your code to make it check the md5 AFTER it downloads the new version, it works fine, however, If I move the md5 code so that it runs BEFORE the download begins, the download fails. (the program is unable to write the downloaded file over the old one.)

of course, if the md5 code runs after the download is completed it works fine, however I need it to run both BEFORE AND AFTER the download starts.

Please help!

thanks
I tried
f.close()

but it didn't work. Any ideas on how to properly unlock the file?



Remove the line 'f = New FileStream(TextBox1.Text, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)', this will resolve the problem.

After fixing the issue, if file is already opened by user and then this code is tried to run in that case its giving an error.
0

#7 User is offline   MTY Icon

  • Newbie
  • Pip
Group:
Members
Posts:
1
Joined:
05-January 10

VB Knowledge: None
VB Version:
VB 2005 (.NET 2.0)
OS: Windows XP

Posted 05 January 2010 - 10:32 AM

Thank you so much for the code. It really is what I was looking for. Im new with .Net and I find it difficult for me right now.
I have a question. I need to pass the result (buff.ToString()) to a String Variable. Im working on SSIS and the code works perfect, but in order to continue with my package, I need to assig the value to a string variable.

Any advice?

Thank you
0

Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

1 User(s) are reading this topic
0 members, 1 guests, 0 anonymous users