vb.net - VB中的CRC64计算
问题描述
我正在使用我在 [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
解决方案
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
推荐阅读
- sql - 如何解码 SQL 查询中的 HTML 字符
- regex - 从选定的 Unicode 希伯来文文本中删除希伯来元音 (nikkud)
- python - 布里渊多样性指数
- javascript - 将 HTML 结构拆分为一组标签
- ruby - 使用 selenium ruby 查找两个似乎具有相同类的文本字段
- javascript - javascript应用元素的类时,CSS关键帧动画未运行
- c# - 找到两组之间完全匹配的等效数
- java - jTable移动到下一个指定列被中断
- ios - 将图像上传到 Firebase 实时数据库或存储?
- c# - 在移动设备浏览器上使用 i18n for ASP.NET