This snippet demonstrates how to calculate the MD5 hash of a file using VB.Net.
Just add these lines to a new class.
Just add these lines to a new class.
' specify the path to a file and this routine will calculate your hash
Public Function MD5CalcFile(ByVal filepath As String) As String
' open file (as read-only)
Using reader As New System.IO.FileStream(filepath, IO.FileMode.Open, IO.FileAccess.Read)
Using md5 As New System.Security.Cryptography.MD5CryptoServiceProvider
' hash contents of this stream
Dim hash() As Byte = md5.ComputeHash(reader)
' return formatted hash
Return ByteArrayToString(hash)
End Using
End Using
End Function
' utility function to convert a byte array into a hex string
Private Function ByteArrayToString(ByVal arrInput() As Byte) As String
Dim sb As New System.Text.StringBuilder(arrInput.Length * 2)
For i As Integer = 0 To arrInput.Length - 1
sb.Append(arrInput(i).ToString("X2"))
Next
Return sb.ToString().ToLower
End Function
346 comments,
VB.Net, Thursday, August 5, 2004 16:02


