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.
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))
NextTo 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.

Sign In
Register
Help



MultiQuote