MD5 Hash

  • Thread starter Thread starter Andy Phillips
  • Start date Start date
A

Andy Phillips

I'm looking for an example that given any filename as input an MD5-Hash hex
output would be achieved.

Anyone have this?
 
I'm looking for an example that given any filename as input an MD5-Hash hex
output would be achieved.

Anyone have this?

Try this:

Public Function MD5_Hash(ByVal FileName As String) As String
Dim MD5Hasher As MD5
Dim fs As FileStream
Dim hash() As Byte

fs = File.Open(FileName, FileMode.Open)
hash = MD5Hasher.ComputeHash(fs)
fs.Close()
Return BitConverter.ToString(hash)
End Function

I haven't tested it yet, so post back and let me know who it works.

Tibby
 
same idea, but working code from a thing that i use to check that I have
really downloaded the same file as the author meant...

Imports System.Security.Cryptography

Imports System.IO

Public Class frmMain

Private Sub btnBrowse_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnBrowse.Click

If (ofd.ShowDialog() = Windows.Forms.DialogResult.OK) Then

txtFileName.Text = ofd.FileName

End If

End Sub

Private Sub btnGenerateHash_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles btnGenerateHash.Click

Dim fs As FileStream

Dim byt() As Byte

If (New FileInfo(txtFileName.Text).Exists) Then

Try

fs = File.Open(txtFileName.Text, FileMode.Open, FileAccess.Read)

Dim md5 As New MD5CryptoServiceProvider()

byt = md5.ComputeHash(fs)

txtMD5.Text = BitConverter.ToString(byt)

Dim sha1 As New SHA1CryptoServiceProvider()

fs.Position = 0

byt = sha1.ComputeHash(fs)

txtSHA1.Text = BitConverter.ToString(byt)

Catch ex As Exception

MessageBox.Show(ex.Message)

Finally

If (Not IsNothing(fs)) Then fs.Close()

End Try

End If

End Sub

End Class
 
I've tried using the code but I get too many build errors. I'm a VB6 native
and hurting badly trying to move to DOTNET. I cant figure out where to place
the code in a project with out errors. If you could please throw me a bone.
Maybe insert the code into ??? and place this in a form?? or module or
whatever I'd greatly appreciate it.

Thanks
 
It works. Thanks tibby. Just had to cut out your diaglogs and textboxs and
get the code in a module correctly. Thanks Agin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top