vb.net crc32 function for files?

Can someone check this module?  It seems to be creating incorrect 32bit file crc's


Imports System.IO
 
Module CRCMOD
    Private crc32Table() As Integer
    Private Const BUFFER_SIZE As Integer = 1024
 
    Public Function CalculateFile(ByVal TheFile As String) As String
        On Error Resume Next
      
 
 
 
 
        Dim Stream As FileStream
        Stream = New FileStream(TheFile, FileMode.Open, FileAccess.Read, FileShare.Read, 8192)
 
 
 
        Dim dwPolynomial As Integer = &HEDB88320
        Dim i As Integer, j As Integer
 
        ReDim crc32Table(256)
        Dim dwCrc As Integer
 
        For i = 0 To 255
            dwCrc = i
            For j = 8 To 1 Step -1
                If (dwCrc And 1) Then
                    dwCrc = ((dwCrc And &HFFFFFFFE) \ 2&) And &H7FFFFFFF
                    dwCrc = dwCrc Xor dwPolynomial
                Else
                    dwCrc = ((dwCrc And &HFFFFFFFE) \ 2&) And &H7FFFFFFF
                End If
            Next j
            crc32Table(i) = dwCrc
        Next i
 
 
        Dim crc32Result As Integer
        crc32Result = &HFFFFFFFF
 
        Dim buffer(BUFFER_SIZE) As Byte
        Dim readSize As Integer = BUFFER_SIZE
 
        Dim count As Integer = Stream.Read(buffer, 0, readSize)
 
        Dim iLookup As Integer
        Dim tot As Integer = 0
        Do While (count > 0)
            For i = 0 To count - 1
                iLookup = (crc32Result And &HFF) Xor buffer(i)
                crc32Result = ((crc32Result And &HFFFFFF00) \ &H100) And &HFFFFFF
                crc32Result = crc32Result Xor crc32Table(iLookup)
 
            Next i
            count = Stream.Read(buffer, 0, readSize)
        Loop
 
        CalculateFile = Not (crc32Result)
        CalculateFile = Hex(CalculateFile)
        CalculateFile = UCase(CalculateFile)
 
        Stream.Close()
 
     
 
    End Function
 
 
 
 
 
 
 
 
 
End Module

Open in new window

LVL 1
bluedragon99Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Luis PérezSoftware Architect in .NetCommented:
I always used this class and always worked well. Try it if you want.
Public Class CRC32
    Private crc32Table() As Integer
    Private Const BUFFER_SIZE As Integer = 1024
 
    Public Function GetCrc32(ByRef stream As System.IO.Stream) As Integer
 
        Dim crc32Result As Integer
        crc32Result = &HFFFFFFFF
 
        Dim buffer(BUFFER_SIZE) As Byte
        Dim readSize As Integer = BUFFER_SIZE
 
        Dim count As Integer = stream.Read(buffer, 0, readSize)
        Dim i As Integer
        Dim iLookup As Integer
        Dim tot As Integer = 0
        Do While (count > 0)
            For i = 0 To count - 1
                iLookup = (crc32Result And &HFF) Xor buffer(i)
                crc32Result = ((crc32Result And &HFFFFFF00) \ &H100) And
                 &HFFFFFF   ' nasty shr 8 with vb :/
                crc32Result = crc32Result Xor crc32Table(iLookup)
            Next i
            count = stream.Read(buffer, 0, readSize)
        Loop
 
        GetCrc32 = Not (crc32Result)
 
    End Function
 
    Public Sub New()
        ' This is the official polynomial used by CRC32 in PKZip.
        ' Often the polynomial is shown reversed (04C11DB7).
        Dim dwPolynomial As Integer = &HEDB88320
        Dim i As Integer, j As Integer
 
        ReDim crc32Table(256)
        Dim dwCrc As Integer
 
        For i = 0 To 255
            dwCrc = i
            For j = 8 To 1 Step -1
                If (dwCrc And 1) Then
                    dwCrc = ((dwCrc And &HFFFFFFFE) \ 2&) And &H7FFFFFFF
                    dwCrc = dwCrc Xor dwPolynomial
                Else
                    dwCrc = ((dwCrc And &HFFFFFFFE) \ 2&) And &H7FFFFFFF
                End If
            Next j
            crc32Table(i) = dwCrc
        Next i
    End Sub
 
End Class

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
bluedragon99Author Commented:
I had a re-entry problem with my function, after putting it in it's own class and using the dim class as new it works fine.  Thanks
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.