Results 1 to 8 of 8

Thread: How find CRC32-checksums of files?

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Arrow How find CRC32-checksums of files?

    Could any one show me an example of how to display CRC32-checksums of files and do comparison between them. Thanks

  2. #2
    Banned randem's Avatar
    Join Date
    Oct 2002
    Location
    Maui, Hawaii
    Posts
    11,385

    Re: How find CRC32-checksums of files?

    CRC32 Checksums are gathered by grabing a 32bit sequence from a file and adding it to a 32 bit value (binary) till the file is done from beginning to end to compute the checksum. You will have to be able to allow the rollover of the 32bit value past zero to accomplish this. Better done in C or Assembler.

  3. #3
    Lively Member
    Join Date
    Jun 2006
    Location
    City of Angles. Right Angles.
    Posts
    110

    Re: How find CRC32-checksums of files?

    In VB or just in general?

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2005
    Posts
    1,907

    Re: How find CRC32-checksums of files?

    Quote Originally Posted by supertotallyawesome
    In VB or just in general?
    Thank u for u reply. In visualbasic 6

  5. #5
    Frenzied Member
    Join Date
    Jun 2006
    Posts
    1,098

    Re: How find CRC32-checksums of files?

    A very short Google search gave me this page at www.freevbcode.com

  6. #6
    Lively Member Agilaz's Avatar
    Join Date
    Jun 2006
    Posts
    98

    Re: How find CRC32-checksums of files?

    i found this code somewhere but can't remember where. so credits to the unknown author

    the code is pretty fast because it uses a byte array instead of strings.
    i think it's a good example of how fast VB can be if you avoid strings and all the asc(mid(x,y,z)) stuff.

    VB Code:
    1. Option Explicit
    2.  
    3. Private pInititialized As Boolean
    4. Private pTable(0 To 255) As Long
    5.  
    6.  
    7. Public Sub CRCInit(Optional ByVal Poly As Long = &HEDB88320)
    8.   'Deklarationen:
    9.   Dim crc As Long
    10.   Dim i As Integer
    11.   Dim j As Integer
    12.  
    13.   For i = 0 To 255
    14.  
    15.     crc = i
    16.     For j = 0 To 7
    17.    
    18.       If crc And &H1 Then
    19.         'CRC = (CRC >>> 1) ^ Poly
    20.         crc = ((crc And &HFFFFFFFE) \ &H2 And &H7FFFFFFF) Xor Poly
    21.       Else
    22.         'CRC = (CRC >>> 1)
    23.         crc = crc \ &H2 And &H7FFFFFFF
    24.       End If
    25.    
    26.     Next j
    27.     pTable(i) = crc
    28.  
    29.   Next i
    30.   pInititialized = True
    31. End Sub
    32.  
    33.  
    34. Public Function CRC32File(Path As String) As Long
    35.   'Deklarationen:
    36.   Dim Buffer() As Byte
    37.   Dim BufferSize As Long
    38.   Dim crc As Long
    39.   Dim FileNr As Integer
    40.   Dim Length As Long
    41.   Dim i As Long
    42.  
    43.   If Not pInititialized Then CRCInit
    44.  
    45.  
    46.   BufferSize = &H1000 '4 KB
    47.   ReDim Buffer(1 To BufferSize)
    48.  
    49.   FileNr = FreeFile
    50.   Open Path For Binary As #FileNr
    51.  
    52.     Length = LOF(FileNr)
    53.    
    54.     crc = &HFFFFFFFF
    55.    
    56.     Do While Length
    57.    
    58.       If Length < BufferSize Then
    59.         BufferSize = Length
    60.         ReDim Buffer(1 To Length)
    61.       End If
    62.       Get #FileNr, , Buffer
    63.      
    64.       For i = 1 To BufferSize
    65.         crc = ((crc And &HFFFFFF00) \ &H100) And &HFFFFFF Xor pTable(Buffer(i) Xor crc And &HFF&)
    66.       Next i
    67.      
    68.       Length = Length - BufferSize
    69.    
    70.     Loop
    71.     CRC32File = Not crc
    72.  
    73.   Close #FileNr
    74. End Function

  7. #7
    Fanatic Member ididntdoit's Avatar
    Join Date
    Apr 2006
    Location
    :uoıʇɐɔoן
    Posts
    765

    Re: How find CRC32-checksums of files?

    Woot!

    Awesome code - just call Hex(CRC32file(.fileneme)) to get the hex checksum and it's perfect!
    Visit here to learn to make the VB interface fit you!.
    "I have not failed 10,000 times. I have successfully identified 10,000 ways that will not work" Thomas Edison
    "The day Microsoft makes something that doesn't suck is probably the day they start making vacuum cleaners" -- Ernst Jan Plugge

  8. #8
    Frenzied Member
    Join Date
    Nov 2005
    Posts
    1,834

    Re: How find CRC32-checksums of files?

    Here's another example.

    Put this code in a Class named: clsCRC32

    vb Code:
    1. Option Explicit
    2.  
    3. Private CRCTable(0 To 255) As Long
    4.  
    5. Public Function CalcCRC32(FilePath As String) As Long
    6. Dim ByteArray() As Byte
    7. Dim Limit As Long
    8. Dim CRC As Long
    9. Dim Temp1 As Long
    10. Dim Temp2 As Long
    11. Dim I As Long
    12. Dim intFF As Integer
    13.  
    14.   intFF = FreeFile
    15.   Open FilePath For Binary Access Read As #intFF
    16.     Limit = LOF(intFF)
    17.     ReDim ByteArray(Limit - 1)
    18.     Get #intFF, , ByteArray
    19.   Close #intFF
    20.  
    21.   Limit = Limit - 1
    22.   CRC = -1
    23.   For I = 0 To Limit
    24.     If CRC < 0 Then
    25.       Temp1 = CRC And &H7FFFFFFF
    26.       Temp1 = Temp1 \ 256
    27.       Temp1 = (Temp1 Or &H800000) And &HFFFFFF
    28.     Else
    29.       Temp1 = (CRC \ 256) And &HFFFFFF
    30.     End If
    31.     Temp2 = ByteArray(I)   ' get the byte
    32.     Temp2 = CRCTable((CRC Xor Temp2) And &HFF)
    33.     CRC = Temp1 Xor Temp2
    34.   Next I
    35.   CRC = CRC Xor &HFFFFFFFF
    36.   CalcCRC32 = CRC
    37. End Function
    38.  
    39. Private Sub Class_Initialize()
    40. Dim I As Integer
    41. Dim J As Integer
    42. Dim Limit As Long
    43. Dim CRC As Long
    44. Dim Temp1 As Long
    45.   Limit = &HEDB88320
    46.   For I = 0 To 255
    47.     CRC = I
    48.     For J = 8 To 1 Step -1
    49.       If CRC < 0 Then
    50.         Temp1 = CRC And &H7FFFFFFF
    51.         Temp1 = Temp1 \ 2
    52.         Temp1 = Temp1 Or &H40000000
    53.       Else
    54.         Temp1 = CRC \ 2
    55.       End If
    56.       If CRC And 1 Then
    57.         CRC = Temp1 Xor Limit
    58.       Else
    59.         CRC = Temp1
    60.       End If
    61.     Next J
    62.     CRCTable(I) = CRC
    63.   Next I
    64. End Sub

    In a form:

    vb Code:
    1. Option Explicit
    2.  
    3. Private CRC32 As New clsCRC32
    4.  
    5. Private Sub Command1_Click()
    6.     MsgBox Hex$(CRC32.CalcCRC32("c:\myfile.bmp"))
    7. End Sub

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