Jump to content
zbeng

VisualBasic2005

Recommended Posts

This will be the first of the few Tutorials of how to create fun applications using VisualBasic2005 Express Edition. A Basic experience of using VisualBasic2005 is expected.

I will not be going into how the MD5 algorithm works. For that you can look at the following URL

http://www.ietf.org/rfc/rfc1321.txt

Now, create a new project after you launch VisualBasic2005.

1-> Rename "Form1.vb" as "HashCalc_MainFrm"

2-> Create a label on the form and name it "InputFileName"(this will hold the name of the file you are using)

3-> Create a textbox on the form and name it "CalcMD5Hash"(this will hold the calculated MD5 Hash)

4-> Create a button on the form and name it "SelectInputFile" and put "...." in the text for the button.

5-> Now in the toolbox browse to dialogs, select the openfiledialog(toolbox->dialogs->OpenFileDialog) and drag-drop onto the form. Rename this dialog as "OpenFileForHashing"

Now you are all set to go Very Happy.

Look at the image below to get an idea of what i am talking about.

calcmd5hashrd3.png

Now click on the "view code" button to goto the code view of the form and paste the following code.


Imports System.IO
Imports System
Imports System.Security.Cryptography
Imports System.Text

Public Class HashCalc_MainFrm

Private Sub SelectInputFile_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles SelectInputFile.Click

'Set some options for the FileOpenDialog
OpenFileForHashing.CheckFileExists = True
OpenFileForHashing.CheckPathExists = True

'Show the FileOpenDialog
OpenFileForHashing.ShowDialog()
'Show the name of the file selected in the label on the form
InputFileName.Text = OpenFileForHashing.FileName

Dim fileOpen As FileStream = New FileStream(OpenFileForHashing.FileName, FileMode.Open)
' Create a new instance of the MD5 object.
Dim md5Hasher As MD5 = MD5.Create()
' Convert the input file to a byte array and compute the hash.
Dim data As Byte() = md5Hasher.ComputeHash(fileOpen)
' Create a new Stringbuilder to collect the bytes
' and create a string.
Dim sBuilder As New StringBuilder()
' Loop through each byte of the hashed data
' and format each one as a hexadecimal string.
Dim i As Integer
For i = 0 To data.Length - 1
sBuilder.Append(data(i).ToString("x2"))
Next i
fileOpen.Dispose()
CalcMD5Hash.Text = sBuilder.ToString().ToUpper
End Sub

Private Sub HashCalc_MainFrm_FormClosing(ByVal sender As Object, ByVal e As System.Windows.Forms.FormClosingEventArgs) Handles Me.FormClosing
'Clean up
InputFileName.Text = Nothing
CalcMD5Hash.Text = Nothing

End Sub


End Class

Here is how your application looks like when you run it. You can calculate the hash for any kind of file.

calcmd5hashworkyz3.jpg

Happy Coding Very Happy

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
×
  • Create New...