como calcular um md5
Olá pessoal,
Sou novato no .NET. Até hoje programei só no php e precisava calcular o md5 do conteudo de um arquivo.
no php basta isso:
$md5 = md5_file($arquivo, true)
agora, no .NET eu não estou conseguindo. Criei o método abaixo no vb.net:
Public Function md5_file(ByVal filepath As String, Optional ByVal raw_output As Boolean = False) As String
Try
If Not System.IO.File.Exists(filepath) Then
Throw New Exception("Arquivo inexistente")
End If
Dim contentFile As System.IO.StreamReader
contentFile = New System.IO.StreamReader(filepath)
Dim btyScr() As Byte = System.Text.Encoding.Unicode.GetBytes(contentFile.ReadToEnd)
contentFile.Close()
Dim objMd5 As New MD5CryptoServiceProvider()
Dim btyRes() As Byte = objMd5.ComputeHash(btyScr)
Dim strResult As String = ""
For i As Integer = 0 To btyRes.Length - 1
If raw_output Then
strResult += Convert.ToChar(btyRes(i))
Else
strResult += btyRes(i).ToString("x2").ToLower
End If
Next
Return strResult
Catch ex As Exception
Return ex.Message
End Try
End Function
O problema é que para arquivos de texto com caracteres comuns, o hash do .net é igual ao hash do php.
porém, para arquivos executáveis é diferente.
O que estou errando?
Discussão (4)
Carregando comentários...