Valhalla Legends Archive

Programming => General Programming => Visual Basic Programming => Topic started by: HIGHWAY99 on September 27, 2003, 05:01 PM

Title: CRC & MD5
Post by: HIGHWAY99 on September 27, 2003, 05:01 PM
Hey, got a friend who's wanting to learn how to get the CRC and MD5 hash from a file, any file.

So, I thought I'ld ask around and see if anyone can give me an example on how to in v.b. preferably but C++ might or might not help as well.  

So, if you knonw how to in v.b. atleast then drop me a line.
Title: Re:CRC & MD5
Post by: Yoni on September 27, 2003, 05:24 PM
VB code for CRC32: http://yoni.valhallalegends.com/BNLSChecksum.bas
C/C++ code for CRC32: http://yoni.valhallalegends.com/BNLSChecksum.cpp

(In both of these, the CRC32 function returns the CRC32 checksum of the given data, and the BNLSChecksum function is irrelevant to this forum.)

For MD5, you can probably find some code on PSCode.com or Google.
Title: Re:CRC & MD5
Post by: UserLoser on September 28, 2003, 08:32 PM
Since he can't figure it out and gets confused by the BNLSChecksum...

Private Const CRC32_POLYNOMIAL As Long = &HEDB88320
Private CRC32Table(0 To 255) As Long

Private Sub InitCRC32()
   Dim i As Long, J As Long, K As Long, XorVal As Long
   Static CRC32Initialized As Boolean
   If CRC32Initialized Then Exit Sub
   CRC32Initialized = True
   For i = 0 To 255
       K = i
       For J = 1 To 8
           If K And 1 Then XorVal = CRC32_POLYNOMIAL Else XorVal = 0
           If K < 0 Then K = ((K And &H7FFFFFFF) \ 2) Or &H40000000 Else K = K \ 2
           K = K Xor XorVal
       Next
       CRC32Table(i) = K
   Next
End Sub

Private Function CRC32(ByVal Data As String) As Long
   Dim i As Long, J As Long
   Call InitCRC32
   CRC32 = &HFFFFFFFF
   For i = 1 To Len(Data)
       J = CByte(Asc(Mid(Data, i, 1))) Xor (CRC32 And &HFF&)
       If CRC32 < 0 Then CRC32 = ((CRC32 And &H7FFFFFFF) \ &H100&) Or &H800000 Else CRC32 = CRC32 \ &H100&
       CRC32 = CRC32 Xor CRC32Table(J)
   Next
   CRC32 = Not CRC32
End Function
Title: Re:CRC & MD5
Post by: HIGHWAY99 on October 01, 2003, 08:22 PM
What I'm not getting though is how to use that to get the CRC/MD5 hashes of a file.  By that I mean another file, not the program that the coding is placed into.
-------------------------------------------------------
After reading through the moved peices to catch what I missed on this thread, I realize that I may have overlooked somethings when i looked over the code both times.  However it was my thinking that with what all was being said that people were getting confused in what was being asked about which is why I replied as I had done.  What I wasn't finding in the code within the files given links to for v.b. before hand was where it was actually accessing an given file to actually get the CRC or MD5
Hash(s) from the file.  Sorry if there is still any confusion on either side.  Like I said, thx for the coding so far, just a bit of confusion on my own part when it comes to seeing where in that coding it was doing what was needed or really rather it was there.