Results 1 to 8 of 8

Thread: md5 hash of a whole file RESOLVED

  1. #1

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    27

    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.

  2. #2
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    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...

  3. #3

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    27
    no it doesnt matter, the array is only supposed to be big enough to contain the file.

  4. #4
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    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...

  5. #5

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    27
    thanx for helping.

    but i found a solution now. took me hours, but it finally works.

    VB Code:
    1. Private Function GenerateHash(ByVal thepath As System.String, ByVal thefile As System.String)
    2.         Dim filepath As System.String
    3.         filepath = thepath + thefile
    4.         Dim hashvalue As String
    5.         Dim inputStream As FileStream
    6.         inputStream = File.OpenRead(filepath)
    7.         Dim tmpHash() As Byte
    8.  
    9.         tmpHash = New MD5CryptoServiceProvider().ComputeHash(inputStream)
    10.         Dim i As Integer
    11.         Dim sOutput As New StringBuilder(tmpHash.Length)
    12.         For i = 0 To tmpHash.Length - 1
    13.             sOutput.Append(tmpHash(i).ToString("X2"))
    14.         Next
    15.         hashvalue = sOutput.ToString()
    16.         hashvalue = hashvalue.ToLower()
    17.         inputStream.Close()
    18.  
    19.         Return hashvalue
    20.     End Function

  6. #6
    Fanatic Member pax's Avatar
    Join Date
    Mar 2001
    Location
    Denmark
    Posts
    840
    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...

  7. #7

    Thread Starter
    Junior Member
    Join Date
    Jul 2004
    Posts
    27
    i think you are right. it most likely is the problem you described.

  8. #8
    yay gay PT Exorcist's Avatar
    Join Date
    Apr 2002
    Location
    . . . my reason of shame
    Posts
    2,729
    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
  •  



Click Here to Expand Forum to Full Width