Hash類

27292 ワード

ずいぶん前に書いたので,多くの実用的な関数を更新して,記録を残した.
 
Imports System.Security.Cryptography

Imports System.Text



''' <summary></summary>

Public NotInheritable Class Hash



    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

        CRC32 = UInt32.MaxValue

        For Each b As Byte In data

            b = b Xor (CRC32 And &HFF)

            CRC32 >>= 8

            CRC32 = CRC32 Xor crctbl(b)

        Next

        Return Not CRC32

    End Function



    ' Adler32  

    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

    Shared Function Adler32(ByVal data As Byte()) As UInteger

        Return Adler32(data, 0, data.Length)

    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

    ''' <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.Default

        Return enc.GetBytes(str)

    End Function

    Shared Function TextToBytesAddChaos(ByVal str As String, Optional ByVal c1 As String = "", Optional ByVal c2 As String = "", Optional ByVal enc As Encoding = Nothing) As Byte()

        str = c1 & str & c2

        If enc Is Nothing Then enc = Encoding.Default

        Return enc.GetBytes(str)

    End Function

    ''' <summary> </summary>

    Shared Function AddChaosText(ByVal str As String, Optional ByVal c1 As String = "", Optional ByVal c2 As String = "") As String

        Return c1 & str & c2

    End Function

    ''' <summary> </summary>

    Shared Function AddChaosBytes(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)

        bl.AddRange(c1)

        bl.AddRange(bs)

        bl.AddRange(c2)

        Return bl.ToArray

    End Function



End Class