Quantcast
Channel: Geeks Hangout - Code
Viewing all articles
Browse latest Browse all 10

VB.Net Program to Return the MDS Hash of a file

$
0
0

The below VB.net Console Application will simply return the MD5 hash of a file you pass to it as an argument.

Exmaple Usage:

"C:\test\File_MDS_Hash_Generator.exe" C:\Program Files (x86)\Microsoft Office\OFFICE11\EXCEL.EXE <enter>
3D478B8AF281FA52E8EF1BD9E7895DA5

Do NOT enclose the argument with quotes even if your path contains spaces or you will get an nasty error.

Imports System.Security.Cryptography
Imports System.IO
Imports System.Text

Module Module1

    Sub Main()
        Dim fileToHash AsString
        Dim fileMD5 AsString

        fileToHash = Command$()

        fileMD5 = GenerateFileMD5(fileToHash)

        Console.Write(fileMD5)
    EndSub

    Function GenerateFileMD5(ByVal filePath AsString)
        Dim md5 As MD5CryptoServiceProvider = New MD5CryptoServiceProvider
        Dim f As FileStream = New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)

        f = New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
        md5.ComputeHash(f)
        f.Close()

        Dim hash AsByte() = md5.Hash
        Dim buff As StringBuilder = New StringBuilder
        Dim hashByte AsByte

        ForEach hashByte In hash
            buff.Append(String.Format("{0:X2}", hashByte))
        Next

        Dim md5string AsString
        md5string = buff.ToString()

        Return md5string
    EndFunction

End Module
AttachmentSize
File_MDS_Hash_Generator.exe11.5 KB

Viewing all articles
Browse latest Browse all 10

Trending Articles