.NET 内置了许多校验算法,却唯独不支持CRC32。
然而某些程序所生成的校验码是CRC32(比如ZIP、RAR、7Z等压缩格式),所以我们仍需要使用CRC32来校验数据的完整性。

我所分享的这个哈希类不仅有CRC32算法,还拥有许多CRC变种算法,比如CRC64、StrangeCRC、Adler32等等……
当然,也集成了.net本身就拥有的校验算法,全部打包在了一起。
除此之外,我还添加了一些数据加盐、文本转换等实用功能,应该足够一般状况下来使用了。

详细代码如下:
Imports System.Security.Cryptography
Imports System.Text

''' <summary>计算哈希校验值,本类型内方法仅供小型计算</summary>
Public NotInheritable Class ClsoHash
    Private Sub New()
    End Sub

    ' CRC32 算法
    Shared Function CRC32(ByVal data() As Byte) As UInt32
        Static crc As UInt32, crctbl(255) As UInt32
        If data.Length = 0 Then Return 0
        If crc = 0 Then
            For i As Short = 0 To 255
                crc = i
                For j As Byte = 0 To 7
                    If crc And 1 Then crc = (crc >> 1) Xor &HEDB88320& Else crc >>= 1
                Next
                crctbl(i) = crc
            Next
            crc = 1
        End If
        Dim ret As UInt32 = UInt32.MaxValue
        For Each b As Byte In data
            b = b Xor (ret And &HFF)
            ret >>= 8
            ret = ret Xor crctbl(b)
        Next
        Return Not ret
    End Function

    ' CRC64 算法
    Shared Function CRC64(ByVal data() As Byte) As UInt64
        Dim crc As ULong = &HFFFFFFFFFFFFFFFFUL
        Dim i As Integer = 0
        While i < data.Length
            Dim tableIndex As UInteger = ((CType((crc >> 56), UInteger)) Xor data(i)) And &HFF
            crc = s_CRC64Table(tableIndex) Xor (crc << 8)
            System.Math.Max(System.Threading.Interlocked.Increment(i), i - 1)
        End While
        Return (crc Xor &HFFFFFFFFFFFFFFFFUL)
    End Function
    Private Shared s_CRC64Table As UInt64() = New UInt64(255) {0UL, 4823603603198064275UL, 9647207206396128550UL, 14344283933443513269UL, 5274672035359026399UL, 847670339082705484UL, 14759040976900489721UL, 10241823793177474922UL, 10549344070718052798UL, 15030250704074698541UL, 1695340678165410968UL, 6158653484774949387UL, 15804726273676621153UL, 11071337880091427826UL, 6824194888265062471UL, 2036903512645398228UL, 7367177604490692079UL, 2651944067726553980UL, 16419204125234161865UL, 11613757334439845466UL, 3390681356330821936UL, 7926053118503640995UL, 12317306969549898774UL, 16726154088988619397UL, 17607865585094646865UL, 13162708473643690690UL, 8194994013375312247UL, 3695931686473304036UL, 13648389776530124942UL, 18417527692557321757UL, 4073807025290796456UL, 8825348881154370363UL, 14734355208981384158UL, 10271039580541631821UL, 5303888135453107960UL, 822984195088142443UL, 9604374506261047041UL, 14391664176758772114UL, 47380625301539367UL, 4780770595170139316UL, 6781362712661643872UL, 2084283301222999283UL, 15852106237007281990UL, 11028505464239851989UL, 1670654249350217407UL, 6187869865390245932UL, 10578560694269006745UL, 15005564104267687178UL, 12269926345859042865UL, 16768987096479742114UL, 3433514057002836759UL, 7878672873577829764UL, 16389988026750624494UL, 11638443477897467005UL, 7391863372946608072UL, 2622728278751721819UL, 4044590402276644751UL, 8850035479350698268UL, 13673076206955870889UL, 18388311311405091898UL, 8147614050581592912UL, 3738764100714335683UL, 17650697762308740726UL, 13115328684529279205UL, 15709965168302367023UL, 11021966344253216700UL, 6909860770376862729UL, 2095335087373712026UL, 10607776270906215920UL, 15115916238825782115UL, 1645968390176284886UL, 6063892853452478021UL, 5216239979862816913UL, 762004938812542466UL, 14808413130408695223UL, 10336584279807992612UL, 94761250603078734UL, 4872975272980325085UL, 9561541190340278632UL, 14285852213486374907UL, 13562725425323287744UL, 18359094313119879763UL, 4168566602445998566UL, 8874722219015798645UL, 17657238303940757535UL, 13257468400305012364UL, 8136561383943382329UL, 3610266854770152362UL, 3341308498700434814UL, 7831293060043656173UL, 12375739730780491864UL, 16811819059476047563UL, 7452841817450123681UL, 2710377314828461874UL, 16324444680414493831UL, 11564384134825822740UL, 1621282580641819377UL, 6093108618008534114UL, 10636992411005044695UL, 15091230119249932612UL, 6867028114005673518UL, 2142715359940571325UL, 15757345747155659528UL, 10979133309658045851UL, 9518708972583580495UL, 14333231979791697372UL, 142141253402664553UL, 4830142882085382394UL, 14783726745893216144UL, 10365800689136969987UL, 5245456557503443638UL, 737318311902463013UL, 8089180804553289502UL, 3653099890976004493UL, 17700070958701396536UL, 13210088128275084459UL, 4139350461810230209UL, 8899408340202190162UL, 13587411233247080167UL, 18329878549100632180UL, 16295228101163185824UL, 11589070762272702515UL, 7477528201428671366UL, 2681160907110034709UL, 12328359726370364031UL, 16854651450907929836UL, 3384140715920324441UL, 7783913295349006794UL, 17796789492404876493UL, 12973186262895182430UL, 8294265019745835499UL, 3597188614796881784UL, 13819721540753725458UL, 18246723593521770113UL, 4190670174747424052UL, 8707887697765516199UL, 7249714899603402099UL, 2768808468102880224UL, 16248400991498780757UL, 11785088403942012614UL, 3291936780352569772UL, 8025325358597240639UL, 12127785706904956042UL, 16915077318774037017UL, 10432479959725633826UL, 15147713122803500977UL, 1524009877625084932UL, 6329456346323069591UL, 15705454305770282493UL, 11170082187107838830UL, 6635271944638132443UL, 2226424485906433608UL, 189522501206157468UL, 4634679410803088911UL, 9745950545960650170UL, 14245012653811987241UL, 5445476407655580739UL, 676338306971005648UL, 14876502445374089573UL, 10124960353263198198UL, 4215391513593610003UL, 8678706776937023872UL, 13790540925671641653UL, 18271444552530207910UL, 8337133204891997132UL, 3549843186494580063UL, 17749444438031597290UL, 13016054137459951737UL, 12170653315997410989UL, 16867732534171963454UL, 3244592164593781643UL, 8068192726900473112UL, 16273122767886764658UL, 11755906975779290337UL, 7220533709540304724UL, 2793530071884239303UL, 6682616997400869628UL, 2183556611878603887UL, 15662586120087312346UL, 11217427617020813641UL, 1553190491096487459UL, 6304735387851432112UL, 10407758620342516485UL, 15176894045242543510UL, 14905683634900247362UL, 10100238751092381137UL, 5420754629656923748UL, 705519735670536439UL, 9793295161182637981UL, 14202145287119436046UL, 146654890503152315UL, 4682024195942093864UL, 3242565161283638754UL, 7930564333232481137UL, 12186217236017068228UL, 17000743249723264599UL, 7335380351123765565UL, 2827240748300537774UL, 16153640314560107547UL, 11735716164790313608UL, 13734056228011347036UL, 18188291445129067215UL, 4285430719881142650UL, 8757259798139230185UL, 17846161249714921603UL, 13067947420601767440UL, 8235833358291897765UL, 3511522545606540086UL, 5387043107155988493UL, 590673871457609374UL, 14925875833148783915UL, 10219719885873843128UL, 284282506805329106UL, 4684052045342640705UL, 9660285764170764788UL, 14186579979835500391UL, 15610694155489642931UL, 11120709418076880672UL, 6720936860919424149UL, 2284857304564388358UL, 10490913115006887276UL, 15233377424362361855UL, 1474636623804926026UL, 6234696958930763481UL, 16178361609106579004UL, 11706535215248140463UL, 7306199781952008986UL, 2851961734412043657UL, 12229085463316509411UL, 16953397843693241456UL, 3195220067441434565UL, 7973432182840617302UL, 8278700923620460418UL, 3464177731752866065UL, 17798816680404380324UL, 13110814815472245815UL, 4310152537884486493UL, 8728078392784608718UL, 13704874997943502459UL, 18213013024491712744UL, 9707630858549910483UL, 14143712128616820032UL, 241414281116563189UL, 4731397450835853414UL, 14955056402857342732UL, 10194998898151653791UL, 5362321814220069418UL, 619854820462849209UL, 1503817855483314797UL, 6209975379031176446UL, 10466191297540353867UL, 15262558828106308056UL, 6768281431840648882UL, 2241989909157107745UL, 15567826590698013588UL, 11168054230320002311UL}

    ' StrangeCRC 算法
    Shared Function StrangeCRC(ByVal data() As Byte) As UInt32
        Dim crc As UInteger = UInteger.MaxValue
        For i As Integer = 0 To data.Length - 1
            Dim temp As Integer = (crc >> 24) Xor data(i)
            If temp < 0 Then temp = 256 + temp
            crc = (crc << 8) Xor s_scrc32Table(temp)
        Next
        Return Not crc
    End Function
    Private Shared s_scrc32Table As UInteger() = {0, 79764919, 159529838, 222504665, 319059676, 398814059, 445009330, 507990021, 638119352, 583659535, 797628118, 726387553, 890018660, 835552979, 1015980042, 944750013, 1276238704, 1221641927, 1167319070, 1095957929, 1595256236, 1540665371, 1452775106, 1381403509, 1780037320, 1859660671, 1671105958, 1733955601, 2031960084, 2111593891, 1889500026, 1952343757, 2552477408, 2632100695, 2443283854, 2506133561, 2334638140, 2414271883, 2191915858, 2254759653, 3190512472, 3135915759, 3081330742, 3009969537, 2905550212, 2850959411, 2762807018, 2691435357, 3560074640, 3505614887, 3719321342, 3648080713, 3342211916, 3287746299, 3467911202, 3396681109, 4063920168, 4143685023, 4223187782, 4286162673, 3779000052, 3858754371, 3904687514, 3967668269, 881225847, 809987520, 1023691545, 969234094, 662832811, 591600412, 771767749, 717299826, 311336399, 374308984, 453813921, 533576470, 25881363, 88864420, 134795389, 214552010, 2023205639, 2086057648, 1897238633, 1976864222, 1804852699, 1867694188, 1645340341, 1724971778, 1587496639, 1516133128, 1461550545, 1406951526, 1302016099, 1230646740, 1142491917, 1087903418, 2896545431, 2825181984, 2770861561, 2716262478, 3215044683, 3143675388, 3055782693, 3001194130, 2326604591, 2389456536, 2200899649, 2280525302, 2578013683, 2640855108, 2418763421, 2498394922, 3769900519, 3832873040, 3912640137, 3992402750, 4088425275, 4151408268, 4197601365, 4277358050, 3334271071, 3263032808, 3476998961, 3422541446, 3585640067, 3514407732, 3694837229, 3640369242, 1762451694, 1842216281, 1619975040, 1682949687, 2047383090, 2127137669, 1938468188, 2001449195, 1325665622, 1271206113, 1183200824, 1111960463, 1543535498, 1489069629, 1434599652, 1363369299, 622672798, 568075817, 748617968, 677256519, 907627842, 853037301, 1067152940, 995781531, 51762726, 131386257, 177728840, 240578815, 269590778, 349224269, 429104020, 491947555, 4046411278, 4126034873, 4172115296, 4234965207, 3794477266, 3874110821, 3953728444, 4016571915, 3609705398, 3555108353, 3735388376, 3664026991, 3290680682, 3236090077, 3449943556, 3378572211, 3174993278, 3120533705, 3032266256, 2961025959, 2923101090, 2868635157, 2813903052, 2742672763, 2604032198, 2683796849, 2461293480, 2524268063, 2284983834, 2364738477, 2175806836, 2238787779, 1569362073, 1498123566, 1409854455, 1355396672, 1317987909, 1246755826, 1192025387, 1137557660, 2072149281, 2135122070, 1912620623, 1992383480, 1753615357, 1816598090, 1627664531, 1707420964, 295390185, 358241886, 404320391, 483945776, 43990325, 106832002, 186451547, 266083308, 932423249, 861060070, 1041341759, 986742920, 613929101, 542559546, 756411363, 701822548, 3316196985, 3244833742, 3425377559, 3370778784, 3601682597, 3530312978, 3744426955, 3689838204, 3819031489, 3881883254, 3928223919, 4007849240, 4037393693, 4100235434, 4180117107, 4259748804, 2310601993, 2373574846, 2151335527, 2231098320, 2596047829, 2659030626, 2470359227, 2550115596, 2947551409, 2876312838, 2788305887, 2733848168, 3165939309, 3094707162, 3040238851, 2985771188}


    ' Adler32 算法
    Shared Function Adler32(ByVal data As Byte()) As UInteger
        Return Adler32(data, 0, data.Length)
    End Function
    Shared Function Adler32(ByVal data() As Byte, ByVal offset As Integer, ByVal count As Integer) As UInteger
        Dim checksum As UInteger = 1
        Const BASE As UInteger = 65521
        Dim s1 As UInteger = checksum And 65535
        Dim s2 As UInteger = checksum >> 16
        While count > 0
            Dim n As Integer = 3800
            If n > count Then n = count
            count -= n
            While n > 0
                s1 = s1 + CUInt((data(offset) And 255)) : offset += 1
                s2 = s2 + s1
                n -= 1
            End While
            s1 = s1 Mod BASE
            s2 = s2 Mod BASE
        End While
        Return (s2 << 16) Or s1
    End Function


    ' MD5算法
    Shared Function MD5(ByVal data() As Byte) As Byte()
        Return (New MD5CryptoServiceProvider).ComputeHash(data)
    End Function
    ' MD5CSP算法
    Shared Function MD5CSP(ByVal data() As Byte) As Byte()
        Return (New MD5CryptoServiceProvider).ComputeHash(data)
    End Function
    ' HMACMD5算法
    Shared Function HMACMD5(ByVal data() As Byte) As Byte()
        Return (New HMACMD5).ComputeHash(data)
    End Function
    ' HMACRIPEMD160算法
    Shared Function HMACRIPEMD160(ByVal data() As Byte) As Byte()
        Return (New HMACRIPEMD160).ComputeHash(data)
    End Function

    ' SHA 1、256、384、512算法
    Shared Function SHA1(ByVal data() As Byte) As Byte()
        Return (New SHA1Managed).ComputeHash(data)
    End Function
    Shared Function SHA256(ByVal data() As Byte) As Byte()
        Return (New SHA256Managed).ComputeHash(data)
    End Function
    Shared Function SHA384(ByVal data() As Byte) As Byte()
        Return (New SHA384Managed).ComputeHash(data)
    End Function
    Shared Function SHA512(ByVal data() As Byte) As Byte()
        Return (New SHA512Managed).ComputeHash(data)
    End Function
    ' HMACSHA 1、256、384、512算法
    Shared Function HMACSHA1(ByVal data() As Byte) As Byte()
        Return (New HMACSHA1).ComputeHash(data)
    End Function
    Shared Function HMACSHA256(ByVal data() As Byte) As Byte()
        Return (New HMACSHA256).ComputeHash(data)
    End Function
    Shared Function HMACSHA384(ByVal data() As Byte) As Byte()
        Return (New HMACSHA384).ComputeHash(data)
    End Function
    Shared Function HMACSHA512(ByVal data() As Byte) As Byte()
        Return (New HMACSHA512).ComputeHash(data)
    End Function

    ' RIPEMD160算法
    Shared Function RIPEMD160(ByVal data() As Byte) As Byte()
        Return (New RIPEMD160Managed).ComputeHash(data)
    End Function



    ''' <summary>返回数据的字符表示</summary>
    Shared Function HashToText(ByVal data() As Byte) As String
        Dim sb As New System.Text.StringBuilder
        For Each b As Byte In data
            sb.Append(b.ToString("X2"))
        Next
        Return sb.ToString
    End Function
    ''' <summary>返回数据的字符表示</summary>
    Shared Function HashToText(ByVal data() As Byte, ByVal lower As Boolean) As String
        Dim sb As New System.Text.StringBuilder
        Dim sl As String = IIf(lower, "x2", "X2")
        For Each b As Byte In data
            sb.Append(b.ToString(sl))
        Next
        Return sb.ToString
    End Function
    ''' <summary>返回整数的字符表示</summary>
    Shared Function HashToText(ByVal uint As UInteger) As String
        Return Hex(uint)
    End Function
    ''' <summary>返回整数的字符表示</summary>
    Shared Function HashToText(ByVal int As Integer) As String
        Return Hex(int)
    End Function
    Shared Function HashToText(ByVal int As UInt64) As String
        Return Hex(int)
    End Function
    Shared Function HashToText(ByVal int As Int64) As String
        Return Hex(int)
    End Function
    ''' <summary>返回BASE64编码数据</summary>
    Shared Function HashToBase64(ByVal data() As Byte) As String
        Return Convert.ToBase64String(data)
    End Function


    ''' <summary>将文本数据化</summary>
    Shared Function TextToBytes(ByVal str As String, Optional ByVal enc As Encoding = Nothing) As Byte()
        If enc Is Nothing Then enc = Encoding.UTF8
        Return enc.GetBytes(str)
    End Function
    Shared Function TextToBytesAddSalt(ByVal str As String, Optional ByVal c1 As String = "", Optional ByVal c2 As String = "", Optional ByVal enc As Encoding = Nothing) As Byte()
        If c1 = "" Then c1 = "ClsO"
        If c2 = "" Then c2 = "hAsH"
        str = c1 & str & c2
        If enc Is Nothing Then enc = Encoding.UTF8
        Return enc.GetBytes(str)
    End Function
    ''' <summary>将文本加盐</summary>
    Shared Function AddSaltText(ByVal str As String, Optional ByVal c1 As String = "", Optional ByVal c2 As String = "") As String
        If c1 = "" Then c1 = "ClsO"
        If c2 = "" Then c2 = "hAsH"
        Return c1 & str & c2
    End Function
    ''' <summary>将数据加盐</summary>
    Shared Function AddSaltBytes(ByVal bs As Byte(), Optional ByVal c1 As Byte() = Nothing, Optional ByVal c2 As Byte() = Nothing) As Byte()
        Dim bl As New List(Of Byte)
        If c1 Is Nothing Then c1 = {67, 108, 115, 79}
        If c2 Is Nothing Then c2 = {104, 65, 115, 72}
        bl.AddRange(c1)
        bl.AddRange(bs)
        bl.AddRange(c2)
        Return bl.ToArray
    End Function

End Class