导航
  • 主页
  • VBS
  • .NET技术
  • VBA
  • VB网络编程
  • VB多媒体
  • VB数据库
  • VB控件
  • VB COM/DCOM
  • VB基础类
  • VB API
  • 问答

[原]VB.NET常用的哈希算法集 差不多30种.

华芸智森 ESSA 项目总监  2016-05-13 08:56:37
加精

VB.NET常用的哈希算法集.其中包括了著名的暴雪的哈希,T33哈希.......全部是网上的 C/C++代码改的(VB.NET的资源真的很少).
不同的哈希算法在分布式,布降过滤器,位图MAP等等应用得比较多...

'' </summary>
Public Class MyUnchecked

#Region "UInt64"

<StructLayout(LayoutKind.Explicit)>
Public Structure UncheckedUInt64

<FieldOffset(0)>
Private longValue As UInt64
<FieldOffset(0)>
Private intValueLo As UInt32
<FieldOffset(4)>
Private intValueHi As UInt32 '//

Private Sub New(newLongValue As UInt64)
longValue = newLongValue
End Sub

Public Overloads Shared Widening Operator CType(value As UInt64) As UncheckedUInt64
Return New UncheckedUInt64(value)
End Operator

Public Overloads Shared Widening Operator CType(value As UncheckedUInt64) As UInt64
Return value.longValue
End Operator

Public Overloads Shared Operator *(x As UncheckedUInt64, y As UInt64) As UncheckedUInt64
Return New UncheckedUInt64(x.longValue * y)
End Operator

Public Overloads Shared Operator /(x As UncheckedUInt64, y As UInt64) As UncheckedUInt64
Return New UncheckedUInt64(x.longValue / y)
End Operator

Public Overloads Shared Operator ^(x As UncheckedUInt64, y As Double) As UncheckedUInt64
Return New UncheckedUInt64(x.longValue ^ y)
End Operator

Public Overloads Shared Operator ^(x As UncheckedUInt64, y As UInt32) As UncheckedUInt64
Return New UncheckedUInt64(MyConvert.MyPower(x.longValue, y))
End Operator

Public Overloads Shared Operator Xor(x As UncheckedUInt64, y As UInt64) As UncheckedUInt64
Return New UncheckedUInt64(x.longValue Xor y)
End Operator

Public Overloads Shared Operator +(x As UncheckedUInt64, y As UInt64) As UncheckedUInt64
Return New UncheckedUInt64(x.longValue + y)
End Operator

Public Overloads Shared Operator -(x As UncheckedUInt64, y As UInt64) As UncheckedUInt64
Return New UncheckedUInt64(x.longValue - y)
End Operator

Public Overloads Shared Operator <<(x As UncheckedUInt64, y As Int32) As UncheckedUInt64
Return New UncheckedUInt64(x.longValue << y)
End Operator

Public Overloads Shared Operator >>(x As UncheckedUInt64, y As Int32) As UncheckedUInt64
Return New UncheckedUInt64(x.longValue >> y)
End Operator

Public Overloads Shared Operator And(x As UncheckedUInt64, y As UInt64) As UncheckedUInt64
Return New UncheckedUInt64(x.longValue And y)
End Operator

Public Overloads Shared Operator =(x As UncheckedUInt64, y As UInt64) As UncheckedUInt64
Return New UncheckedUInt64(x.longValue)
End Operator

Public Overloads Shared Operator <>(x As UncheckedUInt64, y As UInt64) As UncheckedUInt64
Return New UncheckedUInt64(x.longValue <> y)
End Operator

Public Overloads Shared Operator Not(x As UncheckedUInt64) As UncheckedUInt64
Return New UncheckedUInt64(Not x.longValue)
End Operator

End Structure

#End Region
End Class
...全文
6414 3 收藏 26
写回复
26 条回复
切换为时间正序
请发表友善的回复…
发表回复
wasish 2018-04-09
多谢楼主的分享..
回复
dyren99 2018-04-08
不错,不错,学习了。楼主威武了。
回复
水哥阿乐 2017-05-26
不是月神推一把,这等好贴我还没看到,月神真是一个合格的版主
回复
水哥阿乐 2017-05-26
论坛正需要你这样的人,装B的少一点,共享的实际代码多一点.你这指头轻点一下不要紧,从此中国网民百度有了哈希算法的完美资料,楼主是真的帅了
回复
SimilarDuckweed 2017-05-26
感谢楼主无私分享。
回复
x287634334 2017-03-16
不感谢楼主行吗?不行!必须得感谢楼主无私分享。
回复
blandlife 2017-02-06
不错的分享!!!
回复
叫我小才吧 2017-02-03
多谢楼主的分享..
回复
Winters_lee 2017-02-03
标注收藏,谢谢楼主
回复
lkj2016 2017-01-10
学习了
回复
上海-公子 2016-12-13
楼主牛B且心胸宽广,赞一个!
回复
cnavy_xu 2016-12-13
是俗话说的很多的
回复
leayh 2016-12-05
楼主牛B且心胸宽广,赞一个!
回复
正怒月神 2016-12-05
回复
感谢lz的原创和分享,给推荐下
回复
zenter 2016-12-03
楼主好人,好人发大财,一生平安
回复
华芸智森 2016-05-13

    Public Shared Function HashSDBM(Key As String) As UInt64

        Dim hash As MyUnchecked.UncheckedUInt64 = 0
        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray
        For i As Int32 = 0 To L - 1
            hash = Asc(KeyCharArr(i)) + (hash << 6) + (hash << 16) - hash
        Next

        Return (hash And &H7FFFFFFF)

    End Function

    Public Shared Function HashSDBM(KeyByte() As Byte) As UInt64

        Dim hash As MyUnchecked.UncheckedUInt64 = 0
        Dim L As Int32 = KeyByte.Length - 1

        For i As Int32 = 0 To L - 1
            hash = KeyByte(i) + (hash << 6) + (hash << 16) - hash
        Next

        Return (hash And &H7FFFFFFF)

    End Function


    Public Shared Function HashJS(Key As String) As UInt64
        Dim hash As MyUnchecked.UncheckedUInt64 = 1315423911
        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray
        For i As Int32 = 0 To L - 1
            hash = hash Xor (((hash << 5) + Asc(KeyCharArr(i)) + (hash >> 2)))
        Next
        Return hash
    End Function

    Public Shared Function HashJS(KeyByte() As Byte) As UInt64
        Dim hash As MyUnchecked.UncheckedUInt64 = 1315423911
        Dim L As Int32 = KeyByte.Length - 1

        For i As Int32 = 0 To L - 1
            hash = hash Xor (((hash << 5) + KeyByte(i) + (hash >> 2)))
        Next
        Return hash
    End Function

    Public Shared Function HashPJW(Key As String) As UInt64

        Dim BitsInUnsignedInt As UInt64 = CLng(4 * 8)
        Dim ThreeQuarters As UInt64 = CLng((BitsInUnsignedInt * 3) / 4)
        Dim OneEighth As UInt64 = CLng(BitsInUnsignedInt / 8)
        Dim HighBits As UInt64 = CLng(&HFFFFFFFF) << (BitsInUnsignedInt - OneEighth)
        Dim hash As MyUnchecked.UncheckedUInt64 = 0
        Dim test As UInt64 = 0
        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray

        For I As Int32 = 0 To L
            hash = (hash << OneEighth) + Asc(KeyCharArr(I))
            If ((test = hash And HighBits) <> 0) Then
                hash = ((hash Xor (test >> ThreeQuarters)) And (Not HighBits))
            End If

        Next

        Return hash

    End Function

    Public Shared Function HashPJW(KeyByte() As Byte) As UInt64

        Dim BitsInUnsignedInt As UInt64 = CLng(4 * 8)
        Dim ThreeQuarters As UInt64 = CLng((BitsInUnsignedInt * 3) / 4)
        Dim OneEighth As UInt64 = CLng(BitsInUnsignedInt / 8)
        Dim HighBits As UInt64 = CLng(&HFFFFFFFF) << (BitsInUnsignedInt - OneEighth)
        Dim hash As MyUnchecked.UncheckedUInt64 = 0
        Dim test As UInt64 = 0
        Dim L As Int32 = KeyByte.Length - 1

        For I As Int32 = 0 To L
            hash = (hash << OneEighth) + KeyByte(I)
            If ((test = hash And HighBits) <> 0) Then
                hash = ((hash Xor (test >> ThreeQuarters)) And (Not HighBits))
            End If

        Next

        Return hash

    End Function

    Public Shared Function HashAP(Key As String) As UInt64

        Dim hash As MyUnchecked.UncheckedUInt64 = &HAAAAAAAA
        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray
        For i As Int32 = 0 To L
            If ((i And 1) = 0) Then
                hash = hash Xor (((hash << 7) Xor Asc(KeyCharArr(i)) * (hash >> 3)))
            Else
                hash = hash Xor ((Not ((hash << 11) + Asc(KeyCharArr(i)) Xor (hash >> 5))))
            End If
        Next

        Return hash

    End Function

    Public Shared Function HashAP(KeyByte() As Byte) As UInt64

        Dim hash As MyUnchecked.UncheckedUInt64 = &HAAAAAAAA
        Dim L As Int32 = KeyByte.Length - 1

        For i As Int32 = 0 To L
            If ((i And 1) = 0) Then
                hash = hash Xor (((hash << 7) Xor KeyByte(i) * (hash >> 3)))
            Else
                hash = hash Xor ((Not ((hash << 11) + KeyByte(i) Xor (hash >> 5))))
            End If
        Next
        Return hash

    End Function

    Public Shared Function HashDEK(Key As String) As UInt64

        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray
        Dim hash As MyUnchecked.UncheckedUInt64 = L + 1
        For i As Int32 = 0 To L
            hash = ((hash << 5) Xor (hash >> 27)) Xor Asc(KeyCharArr(i))
        Next
        Return hash

    End Function


    Public Shared Function HashDEK(KeyByte() As Byte) As UInt64

        Dim L As Int32 = KeyByte.Length - 1
        Dim hash As MyUnchecked.UncheckedUInt64 = L + 1
        For i As Int32 = 0 To L
            hash = ((hash << 5) Xor (hash >> 27)) Xor KeyByte(i)
        Next
        Return hash

    End Function

    Public Shared Function HashELF(Key As String) As UInt64

        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray
        Dim hash As MyUnchecked.UncheckedUInt64 = 0
        Dim x As Long = 0
        For i As Int32 = 0 To L
            hash = (hash << 4) + Asc(KeyCharArr(i))
            x = hash And &HF0000000L
            If x <> 0 Then
                hash = hash Xor (x >> 24)
            End If
            hash = hash And (Not x)
        Next
        Return hash

    End Function

    Public Shared Function HashELF(KeyByte() As Byte) As UInt64

        Dim L As Int32 = KeyByte.Length - 1
        Dim hash As MyUnchecked.UncheckedUInt64 = 0
        Dim x As Long = 0
        For i As Int32 = 0 To L
            hash = (hash << 4) + KeyByte(i)
            x = hash And &HF0000000L
            If x <> 0 Then
                hash = hash Xor (x >> 24)
            End If
            hash = hash And (Not x)
        Next

        Return hash

    End Function


    Private Shared cryptTable(&H100 * 5 - 1) As UInt64
    Private Shared IsInitcryptTable As Boolean = False
    Public Shared Sub HashBlizzardInit()

        Dim seed As UInt64 = &H100001
        Dim index1 As UInt64 = 0
        Dim index2 As UInt64 = 0
        Dim I As UInt64
        Dim KKK As UInt64 = 0
        For index1 = 0 To &H100 - 1
            index2 = index1
            For I = 0 To 4
                Dim temp1, temp2 As UInt64
                seed = (seed * 125 + 3) Mod &H2AAAAB
                temp1 = (seed And &HFFFF) << &H10
                seed = (seed * 125 + 3) Mod &H2AAAAB
                temp2 = (seed And &HFFFF)
                cryptTable(index2) = (temp1 Or temp2) '//|
                index2 += &H100
            Next
        Next

        IsInitcryptTable = True

    End Sub

    ''' <summary>
    ''' 暴雪公司出名的哈希码.
    ''' 测试了 二千万 GUID, 没有重复.但运算量比较大。
    ''' </summary>
    ''' <param name="key"></param>
    ''' <param name="HasType">HasType =0 ,1 ,2 </param>
    ''' <returns></returns>
    Public Shared Function HashBlizzard(ByVal Key As String,
                                        Optional HasType As Long = 0) As UInt64

        If IsInitcryptTable = False Then HashBlizzardInit()

        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray
        Dim seed1 As MyUnchecked.UncheckedUInt64 = &H7FED7FED
        Dim seed2 As MyUnchecked.UncheckedUInt64 = &HEEEEEEEE
        Dim LoopID As Int32 = 0
        While (LoopID < L)
            Dim ascCode As Int32 = Asc(KeyCharArr(LoopID))
            seed1 = cryptTable((HasType << 8) + ascCode) Xor (seed1 + seed2)
            seed2 = ascCode + seed1 + seed2 + (seed2 << 5) + 3
            LoopID += 1
        End While

        Return seed1

    End Function

    ''' <summary>
    ''' 暴雪公司著名的 HashMap .
    ''' 测试了 二千万 GUID, 没有重复.但运算量比较大。
    ''' </summary>
    ''' <param name="KeyByte"></param>
    ''' <param name="HasType">HasType =[0 ,1 ,2] </param>
    ''' <returns></returns>
    Public Shared Function HashBlizzard(KeyByte() As Byte,
                                        Optional HasType As Long = 0) As UInt64

        If IsInitcryptTable = False Then HashBlizzardInit()

        Dim L As Int32 = KeyByte.Length - 1
        Dim seed1 As MyUnchecked.UncheckedUInt64 = &H7FED7FED
        Dim seed2 As MyUnchecked.UncheckedUInt64 = &HEEEEEEEE
        Dim LoopID As Int32 = 0
        While (LoopID < L)
            Dim ascCode As Int32 = KeyByte(LoopID)
            seed1 = cryptTable((HasType << 8) + ascCode) Xor (seed1 + seed2)
            seed2 = ascCode + seed1 + seed2 + (seed2 << 5) + 3
            LoopID += 1
        End While

        Return seed1

    End Function
回复
华芸智森 2016-05-13

    ''' <summary>
    ''' 经典times33算法。简单高效。[这个使用移位代替*33]
    ''' 测试一千万。没有重复哈希值。
    ''' </summary>
    ''' <param name="Key"></param>
    ''' <returns></returns>
    Public Shared Function HashCMyMap(Key As String) As UInt64

        Dim nHash As MyUnchecked.UncheckedUInt64 = 0
        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray
        Dim I As Int32 = 0

        While (I < L)
            nHash = (nHash << 5) + nHash + Asc(KeyCharArr(I)) + 3
            I += 1
        End While

        Return nHash

    End Function


    ''' <summary>
    ''' 经典times33算法。简单高效。[这个使用移位代替*33]
    ''' 测试一千万。没有重复哈希值。
    ''' </summary>
    ''' <param name="KeyByte"></param>
    ''' <returns></returns>
    Public Shared Function HashCMyMap(KeyByte() As Byte) As UInt64

        Dim nHash As MyUnchecked.UncheckedUInt64 = 0
        Dim L As Int32 = KeyByte.Length - 1
        Dim I As Int32 = 0

        While (I < L)
            nHash = (nHash << 5) + nHash + KeyByte(I) + 3
            I += 1
        End While

        Return nHash

    End Function


    ''' <summary>
    ''' 经典的Time算法。简单,高效。
    ''' Ngix使用的是 time31,Tokyo Cabinet使用的是 time37
    ''' 小写英文词汇适合33, 大小写混合使用65。time33比较适合的是英文词汇的hash.
    ''' </summary>
    ''' <param name="Key"></param>
    ''' <param name="seed">种子数。 31,33,37 。。。</param>
    ''' <returns></returns>
    Public Shared Function HashTimeMap(Key As String,
                                       seed As Int16) As UInt64

        Dim nHash As MyUnchecked.UncheckedUInt64 = 0
        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray
        Dim I As Int32 = 0

        While (I < L)
            nHash = seed * nHash + nHash + Asc(KeyCharArr(I)) + 3
            I += 1
        End While

        Return nHash

    End Function

    ''' <summary>
    ''' 经典的Time算法。简单,高效。
    ''' Ngix使用的是 time31,Tokyo Cabinet使用的是 time37
    ''' 小写英文词汇适合33, 大小写混合使用65。time33比较适合的是英文词汇的hash.
    ''' </summary>
    ''' <param name="KeyByte"></param>
    ''' <param name="seed">种子质数。 31,33,37 。。。</param>
    ''' <returns></returns>
    Public Shared Function HashTimeMap(KeyByte() As Byte,
                                       seed As UInt32) As UInt64

        Dim nHash As MyUnchecked.UncheckedUInt64 = 0
        Dim L As Int32 = KeyByte.Length - 1
        Dim I As Int32 = 0

        While (I < L)
            nHash = seed * nHash + nHash + KeyByte(I) + 3
            I += 1
        End While

        Return nHash

    End Function


#End Region
回复
华芸智森 2016-05-13

#Region "哈希算法"


    ''' <summary>
    ''' 和 HashCMyMap 基本一样.
    ''' </summary>
    ''' <param name="Key"></param>
    ''' <returns></returns>
    Public Shared Function HashDJB(Key As String) As UInt64
        Dim hash As MyUnchecked.UncheckedUInt64 = 5381
        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray

        For i As Int32 = 0 To L
            hash = ((hash << 5) + hash) + Asc(KeyCharArr(i)) + 3
        Next

        Return hash

    End Function

    ''' <summary>
    ''' 和 HashCMyMap 基本一样.
    ''' </summary>
    ''' <param name="KeyByte"></param>
    ''' <returns></returns>
    Public Shared Function HashDJB(ByVal KeyByte() As Byte) As UInt64
        Dim hash As MyUnchecked.UncheckedUInt64 = 5381
        Dim L As Int32 = KeyByte.Length - 1

        For i As Int32 = 0 To L
            hash = ((hash << 5) + hash) + KeyByte(i) + 3
        Next

        Return hash

    End Function

    ''' <summary>
    ''' BKDR 哈希
    ''' </summary>
    ''' <param name="Key"></param>
    ''' <param name="seed">种子.最好是使用质数.</param>
    ''' <returns></returns>
    Public Shared Function HashBKDR(ByVal Key As String,
                                    Optional seed As Long = 131) As UInt64
        Dim hash As MyUnchecked.UncheckedUInt64 = 0
        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray
        For i As Int32 = 0 To L - 1
            hash = (hash * seed) + Asc(KeyCharArr(i)) + 3
        Next

        Return (hash And &H7FFFFFFF)

    End Function

    ''' <summary>
    ''' BKDR 哈希
    ''' </summary>
    ''' <param name="KeyByte"></param>
    ''' <param name="seed">种子数</param>
    ''' <returns></returns>
    Public Shared Function HashBKDR(ByVal KeyByte() As Byte,
                                    Optional seed As Long = 131) As UInt64
        Dim hash As MyUnchecked.UncheckedUInt64 = 0
        Dim L As Int32 = KeyByte.Length - 1
        For i As Int32 = 0 To L - 1
            hash = (hash * seed) + KeyByte(i) + 3
        Next

        Return (hash And &H7FFFFFFF)

    End Function

    Public Shared Function HashRS(Key As String,
                                  Optional seed As Long = 131) As UInt64
        Dim hash As MyUnchecked.UncheckedUInt64 = 0
        Dim b As UInt64 = 378551
        Dim a As UInt64 = 63689
        Dim L As Int32 = Key.Length - 1
        Dim KeyCharArr() As Char = Key.ToArray
        For i As Int32 = 0 To L - 1
            hash = (hash * a) + Asc(KeyCharArr(i))
            a = a * b
        Next

        Return (hash And &H7FFFFFFF)

    End Function

    Public Shared Function HashRS(KeyByte() As Byte,
                                  Optional seed As Long = 131) As UInt64
        Dim hash As MyUnchecked.UncheckedUInt64 = 0
        Dim b As UInt64 = 378551
        Dim a As UInt64 = 63689
        Dim L As Int32 = KeyByte.Length - 1

        For i As Int32 = 0 To L - 1
            hash = (hash * a) + KeyByte(i)
            a = a * b
        Next

        Return (hash And &H7FFFFFFF)

    End Function
回复
发动态
发帖子
VB
创建于2007-09-28

1.5w+

社区成员

VB技术相关讨论,主要为经典vb,即VB6.0
申请成为版主
社区公告
暂无公告