-2

我正在使用我在 [here][1] 找到的代码来计算 CRC32 校验和。我还想计算一个 CRC64 校验和。但我无法弄清楚如何做到这一点。任何帮助,将不胜感激!

在我用于 CRC32 的“Magnus”代码下方。

    Private Sub Main()
        Crc32.ComputeChecksum(Encoding.UTF8.GetBytes("Some string")).Dump()
    End Sub
    
    Public Class Crc32
        Shared table As UInteger()
    
        Shared Sub New()
            Dim poly As UInteger = &Hedb88320UI
            table = New UInteger(255) {}
            Dim temp As UInteger = 0
            For i As UInteger = 0 To table.Length - 1
                temp = i
                For j As Integer = 8 To 1 Step -1
                    If (temp And 1) = 1 Then
                        temp = CUInt((temp >> 1) Xor poly)
                    Else
                        temp >>= 1
                    End If
                Next
                table(i) = temp
            Next
        End Sub
    
        Public Shared Function ComputeChecksum(bytes As Byte()) As UInteger
            Dim crc As UInteger = &HffffffffUI
            For i As Integer = 0 To bytes.Length - 1
                Dim index As Byte = CByte(((crc) And &Hff) Xor bytes(i))
                crc = CUInt((crc >> 8) Xor table(index))
            Next
            Return Not crc
        End Function
    End Class

感谢 Mark Adler,我得到了代码工作!

下面的代码产生以下结果:CRC64: 995DC9BBDF1939FA

Private Sub Main()
    Try
        MessageBox.Show("CRC64: " & UCase(Hex(CRC64.ComputeChecksum(System.Text.Encoding.UTF8.GetBytes("123456789")))))
    Catch ex As Exception
        MessageBox.Show(ex.ToString)
    End Try
End Sub


Public Class CRC64
    Shared table As ULong()
    Shared Sub New()
        Dim poly As ULong =  &Hc96c5795d7870f42UL
        table = New ULong(255) {}
        Dim temp As ULong = 0
        For i As ULong = 0 To table.Length - 1
            temp = i
            For j As Integer = 8 To 1 Step -1
                If (temp And 1UL) = 1 Then
                    temp = CULng((temp >> 1) Xor poly)
                Else
                    temp >>= 1
                End If
            Next
            table(i) = temp
        Next
    End Sub

    Public Shared Function ComputeChecksum(bytes As Byte()) As ULong
        Dim crc As ULong = &HffffffffffffffffUL
        Dim i As Integer
        For i = 0 To bytes.Length - 1
            Dim index As Byte = CByte(((crc) And &HffUL) Xor bytes(i))
            crc = CULng((crc >> 8) Xor table(index))
        Next i
    Return Not crc
    End Function
End Class

  [1]: https://stackoverflow.com/questions/15553697/calculate-crc32-of-an-string-or-byte-array
4

2 回答 2

0
Private Sub Main()
    Try
        MessageBox.Show("CRC64: " & UCase(Hex(CRC64.ComputeChecksum(System.Text.Encoding.UTF8.GetBytes("123456789")))))
    Catch ex As Exception
        MessageBox.Show(ex.ToString)
    End Try
End Sub


Public Class CRC64
    Shared table As ULong()
    Shared Sub New()
        Dim poly As ULong =  &Hc96c5795d7870f42UL
        table = New ULong(255) {}
        Dim temp As ULong = 0
        For i As ULong = 0 To table.Length - 1
            temp = i
            For j As Integer = 8 To 1 Step -1
                If (temp And 1UL) = 1 Then
                    temp = CULng((temp >> 1) Xor poly)
                Else
                    temp >>= 1
                End If
            Next
            table(i) = temp
        Next
    End Sub

    Public Shared Function ComputeChecksum(bytes As Byte()) As ULong
        Dim crc As ULong = &HffffffffffffffffUL
        Dim i As Integer
        For i = 0 To bytes.Length - 1
            Dim index As Byte = CByte(((crc) And &HffUL) Xor bytes(i))
            crc = CULng((crc >> 8) Xor table(index))
        Next i
    Return Not crc
    End Function
End Class
于 2021-03-15T11:25:21.180 回答
0

首先你需要找到一个 CRC-64 描述来实现。你不只是想选择一个随机多项式。从这里开始,CRC-64/XZ最容易将您的实现转换为:

width=64 poly=0x42f0e1eba9ea3693 init=0xffffffffffffffff refin=true refout=true xorout=0xffffffffffffffff check=0x995dc9bbdf1939fa residue=0x49958c9abd7d353f name="CRC-64/XZ"

您需要使用它poly并反转 64 位以用作您的poly. 您需要在计算中使用ULong而不是UInteger类型来保存 64 位。f对于 64 位中的全 1 值,您需要有两倍多的 s 。那么你应该很高兴。

通过替换来检查您的代码"Some string""123456789"看看您是否获得了check=上述 CRC 定义中的值。

于 2021-03-11T02:46:42.897 回答