CRC32位校验码的源代码

原创 2004年06月25日 13:00:00

' Copyright (c) Wrox Press Ltd 2002. All rights reserved.
' Buyers of the Visual Basic .NET Solutions Toolkit book (ISBN 1861007396) can use this code without restriction.
' This code cannot be copied, compiled, or altered except by buyers of the aforementioned book.
' Wrox Press Ltd retains copyright of this code in all cases and unauthorized transmission, duplication, and
' publishing is forbidden.

Imports System
Imports System.IO

Namespace Wrox.Toolkit.Util
    Public Class Crc32
        Private Const TABLESIZE As Integer = 256
        Private Const DEFAULTPOLYNOMIAL As Integer = &HEDB88320
        Private Const DEFAULTINITIALVALUE As Integer = &HFFFFFFFF

        Private lookup(TABLESIZE - 1) As Integer
        Private crcPolynomial As Integer = 0

        Public Sub New()
            Me.New(DEFAULTPOLYNOMIAL)
        End Sub

        Public Sub New(ByVal crcPolynomial As Integer)
            Me.crcPolynomial = crcPolynomial
            InitLookupTable()
        End Sub

        Public Property Polynomial() As Integer
            Get
                Return crcPolynomial
            End Get

            Set(ByVal Value As Integer)
                Me.crcPolynomial = value
                InitLookupTable()
            End Set
        End Property

        Public Overloads Function CalculateBlock(ByVal bytes() As Byte) _
                                                 As Integer
            Return CalculateBlock(bytes, 0, bytes.Length)
        End Function

        Public Overloads Function CalculateBlock(ByVal bytes() As Byte, _
                                                 ByVal index As Integer, _
                                                 ByVal length As Integer _
                                                ) As Integer
            Return CalculateBlock(bytes, index, length, DEFAULTINITIALVALUE)
        End Function

        Public Overloads Function CalculateBlock( _
                                  ByVal bytes() As Byte, _
                                  ByVal index As Integer, _
                                  ByVal length As Integer, _
                                  ByVal initialValue As Integer) _
                                  As Integer
            If bytes Is Nothing Then
                Throw New ArgumentNullException("CalculateBlock(): bytes")
            ElseIf index < 0 Or length <= 0 _
                   Or index + length > bytes.Length Then
                Throw New ArgumentOutOfRangeException()
            End If

            Return Not InternalCalculateBlock(bytes, index, _
                                              length, initialValue)
        End Function

        Private Function InternalCalculateBlock( _
                         ByVal bytes() As Byte, _
                         ByVal index As Integer, _
                         ByVal length As Integer, _
                         ByVal initialValue As Integer) _
                         As Integer
            Dim crc As Integer = initialValue
            Dim shiftedCrc As Integer

            Dim position As Integer
            For position = index To length - 1

                shiftedCrc = crc And &HFFFFFF00
                shiftedCrc = shiftedCrc / &H100
                shiftedCrc = shiftedCrc And &HFFFFFF

                crc = shiftedCrc Xor lookup(bytes(position) Xor _
                                                            (crc And &HFF))
            Next

            Return crc
        End Function

        Public Overloads Function CalculateFile(ByVal path As String) _
                                                As Integer
            Return CalculateFile(path, DEFAULTINITIALVALUE)
        End Function

        Public Overloads Function CalculateFile( _
                                  ByVal path As String, _
                                  ByVal initialValue As Integer) _
                                  As Integer
            If path Is Nothing Then
                Throw New ArgumentNullException("path")
            ElseIf path.Length = 0 Then
                Throw New ArgumentException("Invalid path")
            End If

            Return Not InternalCalculateFile(path, initialValue)
        End Function

        Private Function InternalCalculateFile( _
                         ByVal path As String, _
                         ByVal initialValue As Integer) _
                         As Integer
            Const blockSize As Integer = 4096
            Dim count As Integer
            Dim inStream As FileStream
            Dim bytes(blockSize - 1) As Byte
            Dim crc As Integer = initialValue

            Try
                inStream = File.Open(path, FileMode.Open, FileAccess.Read)

                While inStream.Position < inStream.Length
                    count = inStream.Read(bytes, 0, blockSize)
                    crc = InternalCalculateBlock(bytes, 0, count, crc)
                End While
            Finally
                If Not inStream Is Nothing Then
                    inStream.Close()
                End If
            End Try

            Return crc
        End Function

        Private Sub InitLookupTable()
            Dim byteCount, bitCount As Integer
            Dim crc, shiftedCrc As Integer

            For byteCount = 0 To TABLESIZE - 1
                crc = byteCount

                For bitCount = 0 To 7

                    shiftedCrc = crc And &HFFFFFFFE
                    shiftedCrc = shiftedCrc / &H2
                    shiftedCrc = shiftedCrc And &H7FFFFFFF

                    If (crc And &H1) Then
                        crc = shiftedCrc Xor crcPolynomial
                    Else
                        crc = shiftedCrc
                    End If
                Next

                lookup(byteCount) = crc
            Next
        End Sub
    End Class
End Namespace

' Copyright (c) Wrox Press Ltd 2002. All rights reserved.
' Buyers of the Visual Basic .NET Solutions Toolkit book (ISBN 1861007396) can use this code without restriction.
' This code cannot be copied, compiled, or altered except by buyers of the aforementioned book.
' Wrox Press Ltd retains copyright of this code in all cases and unauthorized transmission, duplication, and
' publishing is forbidden.

Imports System
Imports Wrox.Toolkit.Util

Module CRC32Test
    Sub Main(ByVal cmdArgs() As String)
        If cmdArgs.Length <> 1 Then
            Console.WriteLine("Usage: CRC32Test <path>")
            Exit Sub
        End If

        Try
            Dim crc32Val As Integer
            Dim crc As New Crc32()

            crc32Val = crc.CalculateFile(cmdArgs(0))

            Console.WriteLine("CRC32 is {0:x}", crc32Val)
        Catch e As Exception
            Console.WriteLine("An exception occurred: {0}", e.Message)
        End Try
    End Sub
End Module

使用QT5实现CRC校验的过程

用QT5写的CRC32的校验程序,win7 64位下安装的QT-mingw版测试通过。
  • xueyushenzhou
  • xueyushenzhou
  • 2016年04月21日 17:34
  • 2807

(转载)谈CRC32碰撞的概率和可能性

本文转载自:http://aiyooyoo.com/index.php/archives/399/前段时间跟某大牛叽歪的时候,才被提到我写的一篇文章里提到的CRC32算法有误。今天写代码,恰好需要用到...
  • longzhiwen888
  • longzhiwen888
  • 2015年06月19日 16:16
  • 2266

lua CRC32校验码生成

普通文件(如压缩文件)的crc32校验码计算方法: 生成多项式采用0xEDB88320,所生成的表为: This polynomial ( 0xEDB88320L) DOES generate t...
  • yuanfengyun
  • yuanfengyun
  • 2016年06月02日 15:02
  • 2095

CRC32算法详细推导(3)

From:http://blog.csdn.net/sparkliang/article/details/5671543 CRC32算法详细推导(3) 郁闷的位逆转 看起来我们已经得...
  • JoeBlackzqq
  • JoeBlackzqq
  • 2014年07月17日 17:31
  • 1528

java 实现CRC32校验

原文链接:http://blog.csdn.net/liuzhengkang/article/details/3116544 实现了CRC32校验,本来java那边是很简单的,直接用  java.u...
  • YJK2515279130
  • YJK2515279130
  • 2015年05月27日 13:56
  • 1191

CRC32校验原理及实现

CRC即循环冗余校验(Cyclic Redundancy Check):是数据通信领域中最常用的一种差错校验码,其特征是信息字段和校验字段的长度可以任意选定。 CRC校验实用程序库:在数据存储和数据通...
  • liudewen3
  • liudewen3
  • 2014年01月06日 19:43
  • 7839

校验码——揭开海明校验码求解之谜

引言        计算机系统在运行时,各个部件之间要进行数据交换,为了确保数据在传送过程中正确无误,通常使用校验码的方法来检测传送的数据是否出错。合理的设计错误编码以及编码规则,舍得数据在传送中出...
  • u013031565
  • u013031565
  • 2015年09月27日 12:43
  • 3647

ts crc32 验证与计算

/**************************************************************************** ***  作用  : ***      ...
  • zhengfl
  • zhengfl
  • 2014年04月09日 10:05
  • 1341

深入了解crc32算法

From: http://blog.csdn.net/isadream/article/details/2072760 由于项目需要,解决一个流媒体文件的crc32校验码。网上查了很多的资料...
  • JoeBlackzqq
  • JoeBlackzqq
  • 2014年07月17日 17:08
  • 2291

数据包校验记录——IP校验&FCS校验

数据包校验和 IP校验:IP校验范围为IP字段共ip_hdr.ip_hl*4个字节; code: /* 参数: buffer:IP段开始地址; length:IP段长度; 返回值: 成...
  • zbh19921021
  • zbh19921021
  • 2015年11月25日 15:06
  • 422
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:CRC32位校验码的源代码
举报原因:
原因补充:

(最多只允许输入30个字)