我有一个奇怪的情况发生。我正在使用以下代码为小文件创建 MD5 哈希。通过将服务器端的哈希值与客户端的哈希值进行比较,我可以测试它是否已更改,以便我可以下载任何更新的副本。
Public Function MD5Hash(mFileInfo As FileInfo) As String
Dim returnString As String = ""
Try
Dim f As FileStream = New FileStream(mFileInfo.FullName, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
Dim md5 As New System.Security.Cryptography.MD5CryptoServiceProvider()
md5.ComputeHash(f)
returnString = GenerateHash(System.Text.Encoding.UTF8.GetString(md5.Hash))
f.Close()
Catch
'File doesn't exist
End Try
Return returnString
End Function
Public Shared Function GenerateHash(ByVal SourceText As String) As String
'Create an encoding object to ensure the encoding standard for the source text
Dim Ue As New UnicodeEncoding()
'Retrieve a byte array based on the source text
Dim ByteSourceText() As Byte = Ue.GetBytes(SourceText)
'Instantiate an MD5 Provider object
Dim Md5 As New MD5CryptoServiceProvider()
'Compute the hash value from the source
Dim ByteHash() As Byte = Md5.ComputeHash(ByteSourceText)
'And convert it to String format for return
Return Convert.ToBase64String(ByteHash)
End Function
问题是在服务器端我得到一个散列,而在客户端我得到一个不同的散列,即使文件是相同的。客户端始终在 WinXP 32bit 和 .NET2.0,服务器是 Win2008 64bit 和 .NET4.0
我已经尝试在服务器上压缩文件并在客户端上提取它以确保文件在下载过程中没有被修改,但我仍然得到相同的哈希不匹配。