【搬运】VB.NET和C#的CRC32校验,适合大型计算
2010-11-29 14:43
363 查看
VB代码
'VB源代码 '注意:本代码仅运行于VB 2005,不适用VB 2003 Public Class ClsoCRC Public Shared ReadOnly Table As UInteger() Shared Sub New() ReDim Table(256) For i As UInteger = 0 To 255 Dim r As UInteger = i For j As Integer = 0 To 7 If (r And 1) <> 0 Then r = (r >> 1) Xor 3988292384 Else r >>= 1 End If Next Table(i) = r Next End Sub Private _value As UInteger = UInteger.MaxValue ''' <summary>初始化类,重新计算CRC前请先调用本过程。</summary> Public Sub Init() _value = UInt32.MaxValue End Sub ''' <summary>更新并计算单个字节。(追加方式)</summary> ''' <param name="b">数据字节。</param> Public Sub UpdateByte(ByVal b As Byte) _value = Table(UInt32toByte(_value) Xor b) Xor (_value >> 8) End Sub ''' <summary>更新类并开始计算CRC。</summary> ''' <param name="data">需要计算的数据组。</param> ''' <param name="offset">偏移,一般设为0。</param> ''' <param name="size">大小,一般设为数据组的Length。</param> Public Sub Update(ByVal data As Byte(), ByVal offset As UInt32, ByVal size As UInt32) For i As UInteger = 0 To size - 1 _value = Table(UInt32toByte(_value) Xor data(offset + i)) Xor (_value >> 8) Next End Sub ''' <summary>获取CRC数据。</summary> Public Function GetDigest() As UInt32 Return _value Xor UInt32.MaxValue End Function ''' <summary>计算并获取CRC数据。(利用静态实例化实现)</summary> ''' <param name="data">需要计算的数据组。</param> ''' <param name="offset">偏移,一般设为0。</param> ''' <param name="size">大小,一般设为数据组的Length。</param> Public Shared Function CalculateDigest(ByVal data As Byte(), ByVal offset As UInt32, ByVal size As UInt32) As UInt32 Dim crc As New ClsoCRC 'crc.Init() crc.Update(data, offset, size) Return crc.GetDigest End Function ''' <summary>校验摘要</summary> ''' <param name="digest">摘要</param> ''' <param name="data">数据组</param> ''' <param name="offset">偏移</param> ''' <param name="size">大小</param> Private Shared Function VerifyDigest(ByVal digest As UInt32, ByVal data As Byte(), ByVal offset As UInt32, ByVal size As UInt32) As Boolean Return CalculateDigest(data, offset, size) = digest End Function ''' <summary>返回BYTE (CLE添加)</summary> Private Shared Function UInt32toByte(ByVal UInt32Value As UInteger) As Byte Return UInt32Value Mod 256 End Function End Class
相关文章推荐
- 大型webgame缓存研究 MemCached VB.Net C#
- C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 适合大型企业信息化应用使用的角色权限管理体系
- VB.NET ,C#逻辑运算符
- [C#.NET][VB.NET] 程式執行時拖曳控制項
- (.NET) IntelliSense difference of Extension Method name in Statement Completion for VB and C#.
- VB.NET与C#语法的比较
- VB.NET及C#调用API
- C#与VB.NET 代码转换网站
- 用Visual Studio 2005 开发VB.NET-C#语言转换插件
- asp.net C# 时间间隔的计算方法
- Wrox Professional ASP.NET 4 in C# and VB
- ASP.NET 2.0网站专案同时使C#与VB.NET之技巧(新书连载)
- C#/VB.NET 给Word文档添加/撤销书签
- 实现用C#和VB.NET调用Ghostscript的API,把Postscript文件转为PDF文件。
- C#.NET 大型企业信息化系统集成快速开发平台 4.2 版本 - 访问频率限制功能实现、防止黑客扫描、防止恶意刷屏
- .Net/C#/VB/T-SQL/Java/Script 实现: 将天文数字转换成中文大写 (2000 年前的思路,打劫的,一点儿技术含量都没有)
- VB.net c# 简体繁体之间的转换
- VB.net 编码规范(也适用于C#)
- VB.NET/C# and JavaScript communication
- [C#.NET][VB.NET]執行緒 Thread 共用資料 同步 鎖定