Results 1 to 2 of 2

Thread: Add String To File MD5 Hash In VB.NET?

  1. #1

    Thread Starter
    New Member
    Join Date
    Jul 2014

    Add String To File MD5 Hash In VB.NET?

    I'm trying to calculate the hash of a file with MD5 and then add the filename to the data before i encrypt it with MD5. The filename should be added to the end of the file before encryption

    So it looks something like md5(filedatabytes & filename)

    Here's my code:
        Public Class FileChecksum
        Private Const BUF_SIZE As Integer = 65536
        ''' <summary>
        ''' Returns the file integrity checksum hash, otherwise an empty string.
        ''' </summary>
        Public Shared Function IntegrityCheck(ByVal filePath As String) As String
            Dim dataBuffer(BUF_SIZE - 1) As Byte
            Dim dataBufferDummy(BUF_SIZE - 1) As Byte
            Dim dataBytesRead As Integer = 0
            Dim hashResult As String = String.Empty
            Dim hashAlg As HashAlgorithm = Nothing
            Dim fs As FileStream = Nothing
                hashAlg = New MD5CryptoServiceProvider ' or New SHA1CryptoServiceProvider
                fs = New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, BUF_SIZE)
                    dataBytesRead = fs.Read(dataBuffer, 0, BUF_SIZE)
                    hashAlg.TransformBlock(dataBuffer, 0, dataBytesRead, dataBufferDummy, 0)
                Loop Until dataBytesRead = 0
                hashAlg.TransformFinalBlock(dataBuffer, 0, 0)
                hashResult = BitConverter.ToString(hashAlg.Hash).Replace("-", "").ToLower
            Catch ex As IOException
                MsgBox(ex.Message, MsgBoxStyle.Critical, "IntegrityCheck")
            Catch ex As UnauthorizedAccessException
                MsgBox(ex.Message, MsgBoxStyle.Critical, "IntegrityCheck")
                If Not fs Is Nothing Then
                    fs = Nothing
                End If
                If Not hashAlg Is Nothing Then
                    hashAlg.Clear() 'Dispose()
                    hashAlg = Nothing
                End If
            End Try
            Return hashResult
        End Function
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            Label4.Text = IntegrityCheck("D:\My Documents\My Games\FarmingSimulator2015\mods\")
        End Sub
    End Class
    But how do i add the filename before encrypting?

    In this case it should add weight6000 before calculating the MD5.

    Here is what i try to achieve written in PHP:
    function getGiantsModMD5Hash($modFile)
      $info = pathinfo($modFile);
      // Add mod zip data
      $fileContent = file_get_contents($modFile);
      // Add basefile name string without extension
      $fileContent .= basename($modFile, '.' . $info['extension']);
      return md5($fileContent);
    Last edited by BoXon; Sep 11th, 2015 at 11:22 PM.

  2. #2
    .NUT jmcilhinney's Avatar
    Join Date
    May 2005
    Sydney, Australia

    Re: Add String To File MD5 Hash In VB.NET?

    I haven't tested this and haven't done much hashing myself, but it seems to me that you could use an appropriate Encoding object and call GetBytes to convert the file name to a Byte array and then pass that when you call TransformFinalBlock.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Click Here to Expand Forum to Full Width