Level Extreme platform
Subscription
Corporate profile
Products & Services
Support
Legal
Français
CRC32 returns negative on big files
Message
From
30/07/2011 14:21:37
 
 
To
30/07/2011 13:48:16
General information
Forum:
ASP.NET
Category:
Coding, syntax and commands
Environment versions
Environment:
VB 9.0
OS:
Windows 7
Network:
Windows 2003 Server
Database:
MS SQL Server
Application:
Web
Miscellaneous
Thread ID:
01519635
Message ID:
01519700
Views:
31
Here is also another variant returning the same result. So, all seems to be ok with the code.
Public Class Crc322
    Inherits HashAlgorithm
    Public Const DefaultPolynomial As UInt32 = &HEDB88320UI
    Public Const DefaultSeed As UInt32 = &HFFFFFFFFUI

    Private hash As UInt32
    Private seed As UInt32
    Private table As UInt32()
    Private Shared defaultTable As UInt32()

    Public Sub New()
        table = InitializeTable(DefaultPolynomial)
        seed = DefaultSeed
        Initialize()
    End Sub

    Public Sub New(polynomial As UInt32, seed As UInt32)
        table = InitializeTable(polynomial)
        Me.seed = seed
        Initialize()
    End Sub

    Public Overrides Sub Initialize()
        hash = seed
    End Sub

    Protected Overrides Sub HashCore(buffer As Byte(), start As Integer, length As Integer)
        hash = CalculateHash(table, hash, buffer, start, length)
    End Sub

    Protected Overrides Function HashFinal() As Byte()
        Dim hashBuffer As Byte() = UInt32ToBigEndianBytes(Not hash)
        Me.HashValue = hashBuffer
        Return hashBuffer
    End Function

    Public Overrides ReadOnly Property HashSize() As Integer
        Get
            Return 32
        End Get
    End Property

    Public Shared Function Compute(buffer As Byte()) As UInt32
        Return Not CalculateHash(InitializeTable(DefaultPolynomial), DefaultSeed, buffer, 0, buffer.Length)
    End Function

    Public Shared Function Compute(seed As UInt32, buffer As Byte()) As UInt32
        Return Not CalculateHash(InitializeTable(DefaultPolynomial), seed, buffer, 0, buffer.Length)
    End Function

    Public Shared Function Compute(polynomial As UInt32, seed As UInt32, buffer As Byte()) As UInt32
        Return Not CalculateHash(InitializeTable(polynomial), seed, buffer, 0, buffer.Length)
    End Function

    Private Shared Function InitializeTable(polynomial As UInt32) As UInt32()
        If polynomial = DefaultPolynomial AndAlso defaultTable IsNot Nothing Then
            Return defaultTable
        End If

        Dim createTable As UInt32() = New UInt32(255) {}
        For i As Integer = 0 To 255
            Dim entry As UInt32 = CType(i, UInt32)
            For j As Integer = 0 To 7
                If (entry And 1) Then
                    entry = (entry >> 1) Xor polynomial
                Else
                    entry = entry >> 1
                End If
            Next
            createTable(i) = entry
        Next

        If polynomial = DefaultPolynomial Then
            defaultTable = createTable
        End If

        Return createTable
    End Function

    Private Shared Function CalculateHash(table As UInt32(), seed As UInt32, buffer As Byte(), start As Integer, size As Integer) As UInt32
        Dim crc As UInt32 = seed
        For i As Integer = start To size - 1
            crc = (crc >> 8) Xor table(buffer(i) Xor crc And &HFF)

        Next
        Return crc
    End Function

    Private Function UInt32ToBigEndianBytes(x As UInt32) As Byte()
        Return New Byte() {CByte((x >> 24) And &HFF), CByte((x >> 16) And &HFF), CByte((x >> 8) And &HFF), CByte(x And &HFF)}
    End Function
End Class
And, to call it:
Dim locrc322 As New Crc322()
        Dim hash As [String] = [String].Empty

        Using fs As FileStream = File.Open("PutYourFileHere", FileMode.Open)
            For Each b As Byte In locrc322.ComputeHash(fs)
                hash += b.ToString("x2").ToLower()
            Next
        End Using

        MessageBox.Show(hash)
Michel Fournier
Level Extreme Inc.
Designer, architect, owner of the Level Extreme Platform
Subscribe to the site at https://www.levelextreme.com/Home/DataEntry?Activator=55&NoStore=303
Subscription benefits https://www.levelextreme.com/Home/ViewPage?Activator=7&ID=52
Previous
Next
Reply
Map
View

Click here to load this message in the networking platform