|
-
Jul 30th, 2004, 04:31 AM
#1
Thread Starter
Junior Member
md5 hash of a whole file RESOLVED
Well, how can I get the md5 hashvalue of a whole file?
I only found how to encrypt strings in the web, but nothing about whole files.
I tried it myself, but the result is not the same as when I use a freeware programm (and I assume the error is in MY code):
Code:
Private Sub testHash(ByVal filepath As System.String)
Dim hashvalue As String
Dim inputStream As FileStream
inputStream = File.OpenRead(filepath)
Dim tmpSource(1024) As Byte
Dim tmpHash() As Byte
inputStream.Read(tmpSource, 0, tmpSource.Length)
tmpHash = New MD5CryptoServiceProvider().ComputeHash(tmpSource)
Dim i As Integer
Dim sOutput As New StringBuilder(tmpHash.Length)
For i = 0 To tmpHash.Length - 1
sOutput.Append(tmpHash(i).ToString("X2"))
Next
hashvalue = sOutput.ToString()
inputStream.Close()
MessageBox.Show(hashvalue)
End Sub
any help appreciated,
thanx
Usul
Last edited by Usul; Jul 30th, 2004 at 07:25 AM.
-
Jul 30th, 2004, 06:06 AM
#2
Hi. I have absolutely no idea about MD5 hashes, but there is one thing that strikes me as odd about your code.
it's the "Dim tmpSource(1024) As Byte" line.
This makes an array of 1025 bytes. I would imagine that it should be 1024, hence the line should be : "Dim tmpSource(1023) As Byte"
As I said, I have no idea about MD5, but I just thought it to be logical to use 1024 bytes and not 1025.
Hope it helps...If not then please ignore it
I wish I could think of something witty to put in my sig...
...Currently using VS2013...
-
Jul 30th, 2004, 06:34 AM
#3
Thread Starter
Junior Member
no it doesnt matter, the array is only supposed to be big enough to contain the file.
-
Jul 30th, 2004, 06:58 AM
#4
Ok, I see.
I have another idea though...
Could it be because of old data in the array when reaching the end of the file?
Imagine you have an array of 10 containing value 1,2,3,4,5,6,7,8,9,10.
If the file if 15 bytes long the last read would return only 5 bytes.
So the array would be a,b,c,d,e,6,7,8,9,10.
So then 6-10 would be computed again.
I think the "inputStream.Read" method returns the number of bytes actually read. I think you have make a new array matching the size or at least clear the old array to 0's first.
Another way may be to make the array big enough to contain the whole file in one go, or only read 1 byte at the time.
I wish I could think of something witty to put in my sig...
...Currently using VS2013...
-
Jul 30th, 2004, 07:24 AM
#5
Thread Starter
Junior Member
thanx for helping.
but i found a solution now. took me hours, but it finally works.
VB Code:
Private Function GenerateHash(ByVal thepath As System.String, ByVal thefile As System.String)
Dim filepath As System.String
filepath = thepath + thefile
Dim hashvalue As String
Dim inputStream As FileStream
inputStream = File.OpenRead(filepath)
Dim tmpHash() As Byte
tmpHash = New MD5CryptoServiceProvider().ComputeHash(inputStream)
Dim i As Integer
Dim sOutput As New StringBuilder(tmpHash.Length)
For i = 0 To tmpHash.Length - 1
sOutput.Append(tmpHash(i).ToString("X2"))
Next
hashvalue = sOutput.ToString()
hashvalue = hashvalue.ToLower()
inputStream.Close()
Return hashvalue
End Function
-
Jul 30th, 2004, 07:29 AM
#6
Cool.
So you just had to put the stream directly into Compute function instead of an array. I think that is due to the reason I explained above, although I can't be sure.
I wish I could think of something witty to put in my sig...
...Currently using VS2013...
-
Jul 30th, 2004, 07:56 AM
#7
Thread Starter
Junior Member
i think you are right. it most likely is the problem you described.
-
Jul 30th, 2004, 08:17 AM
#8
yay gay
There is a code in C# in codeproject that creates md5 hashes of streams/strings. Just search for it
\m/  \m/
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|