Could any one show me an example of how to display CRC32-checksums of files and do comparison between them. Thanks
Printable View
Could any one show me an example of how to display CRC32-checksums of files and do comparison between them. Thanks
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.
In VB or just in general?
Thank u for u reply. In visualbasic 6Quote:
Originally Posted by supertotallyawesome
A very short Google search gave me this page at www.freevbcode.com
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:
Option Explicit Private pInititialized As Boolean Private pTable(0 To 255) As Long Public Sub CRCInit(Optional ByVal Poly As Long = &HEDB88320) 'Deklarationen: Dim crc As Long Dim i As Integer Dim j As Integer For i = 0 To 255 crc = i For j = 0 To 7 If crc And &H1 Then 'CRC = (CRC >>> 1) ^ Poly crc = ((crc And &HFFFFFFFE) \ &H2 And &H7FFFFFFF) Xor Poly Else 'CRC = (CRC >>> 1) crc = crc \ &H2 And &H7FFFFFFF End If Next j pTable(i) = crc Next i pInititialized = True End Sub Public Function CRC32File(Path As String) As Long 'Deklarationen: Dim Buffer() As Byte Dim BufferSize As Long Dim crc As Long Dim FileNr As Integer Dim Length As Long Dim i As Long If Not pInititialized Then CRCInit BufferSize = &H1000 '4 KB ReDim Buffer(1 To BufferSize) FileNr = FreeFile Open Path For Binary As #FileNr Length = LOF(FileNr) crc = &HFFFFFFFF Do While Length If Length < BufferSize Then BufferSize = Length ReDim Buffer(1 To Length) End If Get #FileNr, , Buffer For i = 1 To BufferSize crc = ((crc And &HFFFFFF00) \ &H100) And &HFFFFFF Xor pTable(Buffer(i) Xor crc And &HFF&) Next i Length = Length - BufferSize Loop CRC32File = Not crc Close #FileNr End Function
Woot!:D
Awesome code - just call Hex(CRC32file(.fileneme)) to get the hex checksum and it's perfect!
Here's another example.
Put this code in a Class named: clsCRC32
vb Code:
Option Explicit Private CRCTable(0 To 255) As Long Public Function CalcCRC32(FilePath As String) As Long Dim ByteArray() As Byte Dim Limit As Long Dim CRC As Long Dim Temp1 As Long Dim Temp2 As Long Dim I As Long Dim intFF As Integer intFF = FreeFile Open FilePath For Binary Access Read As #intFF Limit = LOF(intFF) ReDim ByteArray(Limit - 1) Get #intFF, , ByteArray Close #intFF Limit = Limit - 1 CRC = -1 For I = 0 To Limit If CRC < 0 Then Temp1 = CRC And &H7FFFFFFF Temp1 = Temp1 \ 256 Temp1 = (Temp1 Or &H800000) And &HFFFFFF Else Temp1 = (CRC \ 256) And &HFFFFFF End If Temp2 = ByteArray(I) ' get the byte Temp2 = CRCTable((CRC Xor Temp2) And &HFF) CRC = Temp1 Xor Temp2 Next I CRC = CRC Xor &HFFFFFFFF CalcCRC32 = CRC End Function Private Sub Class_Initialize() Dim I As Integer Dim J As Integer Dim Limit As Long Dim CRC As Long Dim Temp1 As Long Limit = &HEDB88320 For I = 0 To 255 CRC = I For J = 8 To 1 Step -1 If CRC < 0 Then Temp1 = CRC And &H7FFFFFFF Temp1 = Temp1 \ 2 Temp1 = Temp1 Or &H40000000 Else Temp1 = CRC \ 2 End If If CRC And 1 Then CRC = Temp1 Xor Limit Else CRC = Temp1 End If Next J CRCTable(I) = CRC Next I End Sub
In a form:
vb Code:
Option Explicit Private CRC32 As New clsCRC32 Private Sub Command1_Click() MsgBox Hex$(CRC32.CalcCRC32("c:\myfile.bmp")) End Sub