Support Forums

Full Version: [Source] Hash My File
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Just a simple file hashing tool.

[Image: NNMHL.png]

Form Codes:
Code:
Public Class Form1

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Try
            With odlgFile

                ' Check to ensure that the selected file exists.  
                ' Dialog box displays a warning otherwise.
                .CheckFileExists = True

                ' Check to ensure that the selected path exists.  Dialog box displays
                ' a warning otherwise.
                .CheckPathExists = True

                ' Return the file referenced by a link? If False, simply returns the selected link
                ' file. If True, returns the file linked to the LNK file.
                .DereferenceLinks = True

                .Multiselect = False

                ' Restore the original directory when done selecting
                ' a file? If False, the current directory changes
                ' to the directory in which you selected the file.
                ' Set this to True to put the current folder back
                ' where it was when you started.
                ' The default is False.
                .RestoreDirectory = True

                ' Show the Help button and Read-Only checkbox?
                .ShowHelp = False
                .ShowReadOnly = False

                ' Start out with the read-only check box checked?
                ' This only make sense if ShowReadOnly is True.
                .ReadOnlyChecked = False

                .Title = "Select a file to open"

                ' Only accept valid Win32 file names?
                .ValidateNames = True

                If .ShowDialog = Windows.Forms.DialogResult.OK Then
                    Try
                        TextBox1.Text = odlgFile.FileName

                        Dim path As String = TextBox1.Text
                        TextBox2.Text = MD5CalcFile(path)
                        TextBox3.Text = getSHA1Hash(path)
                        TextBox4.Text = getSHA256Hash(path)
                        TextBox5.Text = GetCRC32(path)

                    Catch fileException As Exception
                        Throw fileException
                    End Try
                End If

            End With
        Catch ex As Exception
            MsgBox(ex.Message, MsgBoxStyle.Exclamation, Me.Text)
        End Try

    End Sub

End Class

Module Codes:
Code:
Imports System.Security.Cryptography
Imports System.Text
Imports System.IO

Module Module1

    Public Function GetCRC32(ByVal sFileName As String) As String
        Try
            Dim FS As FileStream = New FileStream(sFileName, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
            Dim CRC32Result As Integer = &HFFFFFFFF
            Dim Buffer(4096) As Byte
            Dim ReadSize As Integer = 4096
            Dim Count As Integer = FS.Read(Buffer, 0, ReadSize)
            Dim CRC32Table(256) As Integer
            Dim DWPolynomial As Integer = &HEDB88320
            Dim DWCRC As Integer
            Dim i As Integer, j As Integer, n As Integer

            'Create CRC32 Table
            For i = 0 To 255
                DWCRC = i
                For j = 8 To 1 Step -1
                    If (DWCRC And 1) Then
                        DWCRC = ((DWCRC And &HFFFFFFFE) \ 2&) And &H7FFFFFFF
                        DWCRC = DWCRC Xor DWPolynomial
                    Else
                        DWCRC = ((DWCRC And &HFFFFFFFE) \ 2&) And &H7FFFFFFF
                    End If
                Next j
                CRC32Table(i) = DWCRC
            Next i

            'Calcualting CRC32 Hash
            Do While (Count > 0)
                For i = 0 To Count - 1
                    n = (CRC32Result And &HFF) Xor Buffer(i)
                    CRC32Result = ((CRC32Result And &HFFFFFF00) \ &H100) And &HFFFFFF
                    CRC32Result = CRC32Result Xor CRC32Table(n)
                Next i
                Count = FS.Read(Buffer, 0, ReadSize)
            Loop
            Return Hex(Not (CRC32Result))
        Catch ex As Exception
            Return ""
        End Try
    End Function

    Public Function MD5CalcFile(ByVal filepath As String) As String
        Try
            Using reader As New System.IO.FileStream(filepath, IO.FileMode.Open, IO.FileAccess.Read)
                Using md5 As New System.Security.Cryptography.MD5CryptoServiceProvider
                    Dim hash() As Byte = md5.ComputeHash(reader)
                    Return ByteArrayToString(hash)
                End Using
            End Using
        Catch ex As Exception
            Return ""
        End Try
    End Function

    Public Function ByteArrayToString(ByVal arrInput() As Byte) As String
        Try
            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
        Catch ex As Exception
            Return ""
        End Try
    End Function

    'Imports System.Security.Cryptography

    Function getSHA1Hash(ByVal strToHash As String) As String
        Try
            Dim sha1Obj As New Security.Cryptography.SHA1CryptoServiceProvider
            Dim bytesToHash() As Byte = System.Text.Encoding.ASCII.GetBytes(strToHash)
            bytesToHash = sha1Obj.ComputeHash(bytesToHash)
            Dim strResult As String = ""
            For Each b As Byte In bytesToHash
                strResult += b.ToString("x2")
            Next
            Return strResult
        Catch ex As Exception
            Return ""
        End Try
    End Function

    'Imports System.Security.Cryptography

    Function getSHA256Hash(ByVal Txt As String) As String
        Try
            Dim sha As New SHA256Managed()
            Dim ae As New ASCIIEncoding()
            Dim Hash() As Byte = sha.ComputeHash(ae.GetBytes(Txt))
            Dim sb As New StringBuilder(Hash.Length * 2)
            Dim ndx As Integer
            For ndx = 0 To Hash.Length - 1
                sb.Append(Right("0" & Hex(Hash(ndx)), 2))
            Next
            Return sb.ToString
        Catch ex As Exception
            Return ""
        End Try
    End Function
End Module