VB.NET 获取文件MD5值
请不要使用以下方法,会计算出错!!PublicSharedFunctionMD5_(ByValfile_addressAsString)AsStringDimfstrea...
请不要使用以下方法,会计算出错!!
Public Shared Function MD5_(ByVal file_address As String) As String
Dim fstream As New FileStream(file_address, _
FileMode.Open, _
FileAccess.Read)
'Dim dataToHash(fstream.Length - 1) As Byte
Dim dataToHash(fstream.Length - 1) As Byte
fstream.Read(dataToHash, 0, fstream.Length)
fstream.Close()
Dim hashvalue As Byte() = CType(CryptoConfig.CreateFromName("MD5"), HashAlgorithm).ComputeHash(dataToHash)
Dim i As Integer
Dim result As String = ""
For i = 0 To hashvalue.Length - 1
result += Hex(hashvalue(i))
Next
If (result.Length = 32) Then
Return result
Else
Return "Error!"
End If
End Function 展开
Public Shared Function MD5_(ByVal file_address As String) As String
Dim fstream As New FileStream(file_address, _
FileMode.Open, _
FileAccess.Read)
'Dim dataToHash(fstream.Length - 1) As Byte
Dim dataToHash(fstream.Length - 1) As Byte
fstream.Read(dataToHash, 0, fstream.Length)
fstream.Close()
Dim hashvalue As Byte() = CType(CryptoConfig.CreateFromName("MD5"), HashAlgorithm).ComputeHash(dataToHash)
Dim i As Integer
Dim result As String = ""
For i = 0 To hashvalue.Length - 1
result += Hex(hashvalue(i))
Next
If (result.Length = 32) Then
Return result
Else
Return "Error!"
End If
End Function 展开
1个回答
展开全部
Public Function md5(ByVal a As String) As String
Dim tempmd5 As System.Security.Cryptography.MD5 = New System.Security.Cryptography.MD5CryptoServiceProvider()
Dim bytResult() As Byte = tempmd5.ComputeHash(System.Text.Encoding.Default.GetBytes(a))
Dim strResult As String = BitConverter.ToString(bytResult)
strResult = strResult.Replace("-", "")
Return strResult
End Function
如果要计算文件的就把参数改成字节数组就可以了,然后获取文件GetBytes()传进去就可以了。
Dim tempmd5 As System.Security.Cryptography.MD5 = New System.Security.Cryptography.MD5CryptoServiceProvider()
Dim bytResult() As Byte = tempmd5.ComputeHash(System.Text.Encoding.Default.GetBytes(a))
Dim strResult As String = BitConverter.ToString(bytResult)
strResult = strResult.Replace("-", "")
Return strResult
End Function
如果要计算文件的就把参数改成字节数组就可以了,然后获取文件GetBytes()传进去就可以了。
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询