Source Code: How To Compare Two Files using HMACSHA1 with VB.NET or VB2005
How To Compare Two Files using HMACSHA1 with VB.NET or VB2005
This article is similar to the article How To Compare Two Files with VB.NET and VB2005.
The difference is this file comparison article and source code use the .NET System.Security.Cryptography HMACSHA1 Class to compare two files. The suggestion to use HMACSHA1 was made by reader Jim Parzych.
In tests I conducted, the HMACSHA1 class provided better performance than the byte-by-byte comparison in the first article.
The file comparison looks at file paths, file lengths, and file contents. NOTE: While source code for VB.NET is not provided, the code included in this article will work with VB.NET too. Be sure to Import System.IO.
Application Screen Shot
The source code includes a Windows Forms application.

Source Code Excerpt
After a user makes two file selections and clicks the 'Compare Files' button, the CompareFiles function in the source code performs the file comparison.
First CompareFiles compares the path to file one to the path to file two. If they are the same, file one and file two are the same.
Next, CompareFiles compares the length of file one to the length of file two. If they are different the files are not equal.
Finally, if file paths are different and the length of file one and file two are the same, CompareFiles compares the contents of file one to file two using hashes created by a HMACSHA1 object. If the bytes in file one are equal to the bytes in file two, the files are equal.
Private Function CompareFiles(ByVal filePathOne As String, ByVal filePathTwo As String) As Boolean
' If user has selected the same file as file one and file two..
If (filePathOne = filePathTwo) Then
' Files are the same.
Me.ResultsRichTextBox.Text = "Files are the same; file one is file two."
Return True
End If
' // User selected two different files; Compare length of file one to file two.
Dim fileOneStream As FileStream
Dim fileTwoStream As FileStream
' Open a FileStream for each file.
fileOneStream = New FileStream(filePathOne, FileMode.Open)
fileTwoStream = New FileStream(filePathTwo, FileMode.Open)
' If the files are not the same length...
If (fileOneStream.Length <> fileTwoStream.Length) Then
fileOneStream.Close()
fileTwoStream.Close()
' File's are not equal.
Me.ResultsRichTextBox.Text = "Files are not the same length; they are not equal."
Return False
Else
fileOneStream.Close()
fileTwoStream.Close()
End If
' // Files are the same length; compare file contents using .NET HMACSHA1 Class
Dim fileOneHash As Byte() = ComputeFileHashUsingHMACSHA1(filePathOne)
Dim fileTwoHash As Byte() = ComputeFileHashUsingHMACSHA1(filePathTwo)
Dim areFilesEqual As Boolean = True
' Make a byte-by-byte comparrison of file one's hash to file two's hash.
For i As Integer = 0 To UBound(fileOneHash)
' If a byte from file one is not equal to a byte from file two..
If (fileOneHash(i) <> fileTwoHash(i)) Then
' Files are not equal; byte in file one hash <> byte in file two hash.
Me.ResultsRichTextBox.Text = "Files are not equal; contents are different."
areFilesEqual = False
Exit For
End If
Next
' // Message user and return areFilesEqual
If areFilesEqual Then
' Files are equal; bytes in file one hash = bytes in file two hash.
Me.ResultsRichTextBox.Text = "Files are equal; contents are the same."
areFilesEqual = False
End If
Return areFilesEqual
End Function
Public Function ComputeFileHashUsingHMACSHA1(ByVal filePath As String) As Byte()
' Declare variable named theHMACSHA1 of type HMACSHA1.
Dim theHMACSHA1 As HMACSHA1
' Declare variable named theKeyValue of type Byte().
Dim theKeyValue As Byte()
' Declare variable named theHashValue of type Byte().
Dim theHashValue As Byte()
' Declare variable named theInStream of type Stream.
Dim theInStream As Stream
' Call the UnicodeEncoding class' shared GetBytes method to initialize theKeyValue object.
theKeyValue = (New System.Text.UnicodeEncoding).GetBytes("HashingKey")
' Call the HMACSHA1 class' constructor method to initialize theKeyValue object.
theHMACSHA1 = New HMACSHA1(theKeyValue, True)
' Open theInStream.
theInStream = New FileStream(filePath, FileMode.Open, FileAccess.Read)
' Calculate theHashValue.
theHashValue = theHMACSHA1.ComputeHash(theInStream)
' Close theInStream.
theInStream.Close()
' Return theHashValue.
Return theHashValue
For more information:
FileStream Class
HMACSHA1 Class
mike mcintyre http://www.getdotnetcode.com