Posts: 341
Threads: 34
Joined: Nov 2010
Reputation:
8
04-18-2011, 01:34 PM
(This post was last modified: 04-18-2011, 02:08 PM by KoBE.)
My Turn!!?!?!
Screenshot:
Click here to download the exe
Click here to download a demo project
Source (Commented):
Code: Imports System.Security.Cryptography
Imports System.IO
Public Class Form1
Private Sub btnCheck_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCheck.Click
'Create a new MD5 class
Dim hash As MD5 = MD5.Create()
'Compute the Hash of both files
Dim File1 As Byte() = hash.ComputeHash(My.Computer.FileSystem.ReadAllBytes(txtFile1.Text))
Dim File2 As Byte() = hash.ComputeHash(My.Computer.FileSystem.ReadAllBytes(txtFile2.Text))
'Convert the bytes to a readable string and take out the hyphens
Dim strFile1 As String = BitConverter.ToString(File1).Replace("-", "")
Dim strFile2 As String = BitConverter.ToString(File2).Replace("-", "")
'sets the lables to the string version of the MD5 Hash
lblFile1MD5.Text = strFile1
lblFile2MD5.Text = strFile2
'This is just one way to check, but it compares the string
'representation of the computed hash to check if it's the same
If strFile1 = strFile2 Then
lblResult.ForeColor = Color.LimeGreen
lblResult.Text = "The file has not been modified."
Else
lblResult.ForeColor = Color.Red
lblResult.Text = "The file has been modified."
End If
lblLast.Text = Path.GetFileName(txtFile2.Text)
End Sub
Private Sub btnFile1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFile1.Click
'new instance of an OpenFileDialog
Dim oFile As OpenFileDialog = New OpenFileDialog
'if they hit anything but OK do nothing
If Not oFile.ShowDialog() = DialogResult.OK Then Exit Sub
txtFile1.Text = oFile.FileName
End Sub
Private Sub btnFile2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFile2.Click
Dim oFile As OpenFileDialog = New OpenFileDialog
If Not oFile.ShowDialog() = DialogResult.OK Then Exit Sub
txtFile2.Text = oFile.FileName
End Sub
End Class
I didn't get around to error handling and checking if the files in the textbox were valid files. But I will implement that later. Also when I have time I'm going to try this out in C# as well.
Looks I'm the lone wolf when it comes to the way we did this project! My code is pretty simple though. Looks like everyone did it the hard way!
Everybody's seemed to get their programs to work. Another challenge bites the dust.
@Infinity
What are you using this for? Im thinking you forgot to take it out?
Code: Dim ObjFSO As Object = CreateObject("Scripting.FileSystemObject")
Dim objFile = ObjFSO.GetFile(TextBox1.Text)
(04-18-2011, 12:54 PM)Fragma Wrote: By the way guys.. Can I host the next challenge?
I don't see why not!
(04-18-2011, 10:21 AM)The High Roller Wrote: My Submission:
Screenshot:
Download Link:
Code: http://www.mediafire.com/?cp96hb8n4wx4uhn
Source Code:
Code: Public Class Form1
Public Function VerifyMD5HashCode(ByVal filepath As String) As String
Using verifythefile As New System.IO.FileStream(filepath, IO.FileMode.Open, IO.FileAccess.Read)
Using themd5code As New System.Security.Cryptography.MD5CryptoServiceProvider
Dim thahash() As Byte = themd5code.ComputeHash(verifythefile)
Return ByteArrayToString(thahash)
End Using
End Using
End Function
Private Function ByteArrayToString(ByVal arrInput() As Byte) As String
Dim StringBuilder As New System.Text.StringBuilder(arrInput.Length * 2)
For i As Integer = 0 To arrInput.Length - 1
StringBuilder.Append(arrInput(i).ToString("X2"))
Next
Return StringBuilder.ToString().ToLower
End Function
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
MD5Hash1.Text = VerifyMD5HashCode(filepath1.Text)
MD5Hash2.Text = VerifyMD5HashCode(filepath2.Text)
If filepath1.Text = filepath2.Text Then
MsgBox("The selected files are the same.", 64, "File Comparison Result")
TrueResults.Text = "Both Files Are Identical"
TrueResults.ForeColor = Color.LimeGreen
Else
MsgBox("The selected files are different.", 48, "File Comparison Result")
TrueResults.Text = "Both Files Are Unidentical"
TrueResults.ForeColor = Color.Red
End If
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
SelectFiles.ShowDialog()
SelectFiles.Title = "Select File #1..."
End Sub
Private Sub SelectFiles_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SelectFiles.FileOk
Dim file1 As String = SelectFiles.FileName
filepath1.Text = file1
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
SelectFiles2.ShowDialog()
SelectFiles2.Title = "Select File #2..."
End Sub
Private Sub SelectFiles2_FileOk(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles SelectFiles2.FileOk
Dim file2 As String = SelectFiles2.FileName
filepath2.Text = file2
End Sub
Private Sub filepath2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles filepath2.TextChanged
Button3.Enabled = True
End Sub
Private Sub PictureBox1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles PictureBox1.Click
Process.Start("http://www.supportforums.net/member.php?action=profile&uid=4502")
End Sub
End Class
Now this is what I call useful for something in the future. Great idea Infinity.
Where are the comment's yo?!?
Posts: 5,793
Threads: 268
Joined: Sep 2010
Reputation:
85
04-18-2011, 03:05 PM
(This post was last modified: 04-18-2011, 03:13 PM by AceInfinity.)
That's a good question lol, I tried a different method of getting the file location directory, but I changed my method and reverted it to the one that I have now. I don't know why I kept it in there, I copied the code out with minor modifications though, but that variable isn't even used after it's defined I just noticed. I took it out though.
I like the way you changed the string though, to whether it's a file that got modified or not, I don't see a big point in what the last file was, although it is a little fancy lol.
Where's the drag and drop feature and all of that though? lol If you added a button to copy out the MD5 hashes somehow that would be an improvement too I think.
(04-18-2011, 12:39 PM)dudesons123 Wrote: The code:
Code: Imports System.IO
Imports System.Security.Cryptography
Imports System.Text
Public Class Form1
Private Sub FButton1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FButton1.Click
If OpenFileDialog1.ShowDialog(Me) = 1 Then
txtFile1.Text = OpenFileDialog1.FileName
Label3.Text = GetFileSize(txtFile1.Text).ToString & "kb"
End If
End Sub
Private Function GetFileSize(ByVal MyFilePath As String) As Long
Dim MyFile As New FileInfo(MyFilePath)
Dim FileSize As Long = MyFile.Length
Return FileSize
End Function
Private Sub FButton2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FButton2.Click
If OpenFileDialog2.ShowDialog(Me) = 1 Then
txtFile2.Text = OpenFileDialog2.FileName
Label4.Text = GetFileSize(txtFile2.Text).ToString & "kb"
End If
End Sub
Private Sub FButton3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FButton3.Click
Me.Close()
End Sub
Private Sub FButton4_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles FButton4.Click
Dim pipi As String = CalculateMD5(txtFile1.Text)
Dim kaka As String = CalculateMD5(txtFile2.Text)
If pipi = kaka Then
MessageBox.Show("The 2 files are the same", "Yes!!")
Else
MessageBox.Show("Those are 2 different files", "Oh noes :(")
End If
End Sub
Private Function CalculateMD5(ByVal strFilepath As String) As String
Dim File() As Byte = System.IO.File.ReadAllBytes(strFilepath)
Dim Md5 As New MD5CryptoServiceProvider()
Dim byteHash() As Byte = Md5.ComputeHash(File)
Return ByteArrayToString(byteHash)
End Function
Private Function ByteArrayToString(ByVal arrInput() As Byte) As String
Dim sb As New System.Text.StringBuilder(arrInput.Length * 2)
For i As Integer = 0 To arrInput.Length - 1
sb.Append(arrInput(i).ToString("X2"))
Next
Return sb.ToString().ToLower
End Function
End Class
The download link: http://7019.a.hostable.me/CheckMD5.exe
And an image:
This isn't in correct MD5 format though, but it's a good attempt
Posts: 5,793
Threads: 268
Joined: Sep 2010
Reputation:
85
04-19-2011, 10:38 AM
(This post was last modified: 04-19-2011, 11:17 AM by AceInfinity.)
I updated my code and program functionality, it's a lot more advanced than it used to be, and no more annoying popup windows for MD5 confirmation. It's all in a label like KoBE's program. I got the idea of him, and I like it a lot better now that I added my code to make it that way.
Update: Added a paste function as well which will erase everything from the manual input MD5 hash textbox, and paste the data string from your clipboard. (The last piece of text you copied)
Direct Download (Immediate):
http://download1289.mediafire.com/96yfwb...k+v2.0.exe
Posts: 3,538
Threads: 348
Joined: Mar 2010
Reputation:
57
Can you post the source? I don't really want to download it because I have to much crap on my computer.
Posts: 5,793
Threads: 268
Joined: Sep 2010
Reputation:
85
(04-19-2011, 10:40 AM)Fragma Wrote: Can you post the source? I don't really want to download it because I have to much crap on my computer.
I already did, I just added a few things to it thats all. For the paste function I just added:
Code: Private Sub Paste_Button_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Paste_Button.Click
Manual_Input.Text = ""
Manual_Input.Paste()
End Sub
and for the new label for MD5 hash comparison confirmation:
Code: Private Sub Label4_Text(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Label4.TextChanged
If Label4.Text = "The file comparison confirmed that the MD5 Hashes match." Then
Label4.ForeColor = Color.LightGreen
ElseIf Label4.Text = "The file comparison confirmed that the MD5 Hashes do not match." Then
Label4.ForeColor = Color.Red
End If
End Sub
The program is only 345Kb lol
Posts: 3,538
Threads: 348
Joined: Mar 2010
Reputation:
57
(04-19-2011, 10:43 AM)Infinity Wrote: The program is only 345Kb lol
Thanks. The file size wasn't a problem, I just don't like having loads of programs cluttering my folders.
Did you change "{0:X1}" to "{0:X2}" like I recommended?
Posts: 5,793
Threads: 268
Joined: Sep 2010
Reputation:
85
04-19-2011, 10:50 AM
(This post was last modified: 04-19-2011, 09:31 PM by AceInfinity.)
(04-19-2011, 10:46 AM)Fragma Wrote: Thanks. The file size wasn't a problem, I just don't like having loads of programs cluttering my folders.
Did you change "{0:X1}" to "{0:X2}" like I recommended?
Yeah I did, I also changed a few lines of code to tidy things up. I don't have repeats of the same thing like I used to. Only a couple where I needed it
Edit: My app is finally complete. all errors were solved and handled. Works perfect
|