Download as txt, pdf, or txt
Download as txt, pdf, or txt
You are on page 1of 3

-como clase ***http://www.vbforums.com/showthread.php?

412922-How-find-CRC32-
checksums-of-files
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
-----llamado--

Option Explicit

Private CRC32 As New clsCRC32

Private Sub Command1_Click()


MsgBox Hex$(CRC32.CalcCRC32("c:\myfile.bmp"))
End Sub
------
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
----------------------como clase----------------

You might also like