Bajsen
asked on
Hash a file and append the filename to the hash in VB.NET
Hi experts!
I want to hash a file with MD5 and compare it to another file's MD5 hash. According to the SDK this is how it's done in PHP:
/*function getGiantsModMD5Hash($modFi le)
{
$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);
}
That returns a correct hash, but now i want to do this in VB.NET and this is the code that i found do hash a file:
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
Try
hashAlg = New MD5CryptoServiceProvider ' or New SHA1CryptoServiceProvider
fs = New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, BUF_SIZE)
Do
dataBytesRead = fs.Read(dataBuffer, 0, BUF_SIZE)
hashAlg.TransformBlock(dat aBuffer, 0, dataBytesRead, dataBufferDummy, 0)
Loop Until dataBytesRead = 0
hashAlg.TransformFinalBloc k(dataBuff er, 0, 0)
hashResult = BitConverter.ToString(hash Alg.Hash). Replace("- ", "").ToLower
Catch ex As IOException
MsgBox(ex.Message, MsgBoxStyle.Critical, "IntegrityCheck")
Catch ex As UnauthorizedAccessExceptio n
MsgBox(ex.Message, MsgBoxStyle.Critical, "IntegrityCheck")
Finally
If Not fs Is Nothing Then
fs.Close()
fs.Dispose()
fs = Nothing
End If
If Not hashAlg Is Nothing Then
hashAlg.Clear() 'Dispose()
hashAlg = Nothing
End If
End Try
Return hashResult
End Function
However, as you can the in the PHP example, i must also append the filename whiteout the extension to the end before i calculate the MD5 hash. How in the world do i do that? I've been trying to solve this for the last two days now. :/
I want to hash a file with MD5 and compare it to another file's MD5 hash. According to the SDK this is how it's done in PHP:
/*function getGiantsModMD5Hash($modFi
{
$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);
}
That returns a correct hash, but now i want to do this in VB.NET and this is the code that i found do hash a file:
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
Try
hashAlg = New MD5CryptoServiceProvider ' or New SHA1CryptoServiceProvider
fs = New FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.None, BUF_SIZE)
Do
dataBytesRead = fs.Read(dataBuffer, 0, BUF_SIZE)
hashAlg.TransformBlock(dat
Loop Until dataBytesRead = 0
hashAlg.TransformFinalBloc
hashResult = BitConverter.ToString(hash
Catch ex As IOException
MsgBox(ex.Message, MsgBoxStyle.Critical, "IntegrityCheck")
Catch ex As UnauthorizedAccessExceptio
MsgBox(ex.Message, MsgBoxStyle.Critical, "IntegrityCheck")
Finally
If Not fs Is Nothing Then
fs.Close()
fs.Dispose()
fs = Nothing
End If
If Not hashAlg Is Nothing Then
hashAlg.Clear() 'Dispose()
hashAlg = Nothing
End If
End Try
Return hashResult
End Function
However, as you can the in the PHP example, i must also append the filename whiteout the extension to the end before i calculate the MD5 hash. How in the world do i do that? I've been trying to solve this for the last two days now. :/
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
System.Security.Cryptograp
MD5, RIPEMD160, SHA1, SHA256, SHA384, SHA512
you basically open a file stream, set the pointer to 0 (beginning of file)
have the hasher read the file and return the value.
http://us.informatiweb.net/programmation/36--generate-hashes-md5-sha-1-and-sha-256-of-a-file.html
https://msdn.microsoft.com/en-us/library/xa627k19%28v=vs.110%29.aspx?cs-save-lang=1&cs-lang=vb#code-snippet-2
Open in new window