github.com/zhongdalu/gf@v1.0.0/g/encoding/ghash/ghash.go (about) 1 // Copyright 2017 gf Author(https://github.com/zhongdalu/gf). All Rights Reserved. 2 // 3 // This Source Code Form is subject to the terms of the MIT License. 4 // If a copy of the MIT was not distributed with this file, 5 // You can obtain one at https://github.com/zhongdalu/gf. 6 7 // Package ghash provides some popular hash functions(uint32/uint64) in go. 8 package ghash 9 10 // BKDR Hash Function 11 func BKDRHash(str []byte) uint32 { 12 var seed uint32 = 131 // 31 131 1313 13131 131313 etc.. 13 var hash uint32 = 0 14 for i := 0; i < len(str); i++ { 15 hash = hash*seed + uint32(str[i]) 16 } 17 return hash 18 } 19 20 // BKDR Hash Function 64 21 func BKDRHash64(str []byte) uint64 { 22 var seed uint64 = 131 // 31 131 1313 13131 131313 etc.. 23 var hash uint64 = 0 24 for i := 0; i < len(str); i++ { 25 hash = hash*seed + uint64(str[i]) 26 } 27 return hash 28 } 29 30 // SDBM Hash 31 func SDBMHash(str []byte) uint32 { 32 var hash uint32 = 0 33 for i := 0; i < len(str); i++ { 34 // equivalent to: hash = 65599*hash + uint32(str[i]); 35 hash = uint32(str[i]) + (hash << 6) + (hash << 16) - hash 36 } 37 return hash 38 } 39 40 // SDBM Hash 64 41 func SDBMHash64(str []byte) uint64 { 42 var hash uint64 = 0 43 for i := 0; i < len(str); i++ { 44 // equivalent to: hash = 65599*hash + uint32(str[i]) 45 hash = uint64(str[i]) + (hash << 6) + (hash << 16) - hash 46 } 47 return hash 48 } 49 50 // RS Hash Function 51 func RSHash(str []byte) uint32 { 52 var b uint32 = 378551 53 var a uint32 = 63689 54 var hash uint32 = 0 55 for i := 0; i < len(str); i++ { 56 hash = hash*a + uint32(str[i]) 57 a *= b 58 } 59 return hash 60 } 61 62 // RS Hash Function 64 63 func RSHash64(str []byte) uint64 { 64 var b uint64 = 378551 65 var a uint64 = 63689 66 var hash uint64 = 0 67 for i := 0; i < len(str); i++ { 68 hash = hash*a + uint64(str[i]) 69 a *= b 70 } 71 return hash 72 } 73 74 // JS Hash Function 75 func JSHash(str []byte) uint32 { 76 var hash uint32 = 1315423911 77 for i := 0; i < len(str); i++ { 78 hash ^= (hash << 5) + uint32(str[i]) + (hash >> 2) 79 } 80 return hash 81 } 82 83 // JS Hash Function 64 84 func JSHash64(str []byte) uint64 { 85 var hash uint64 = 1315423911 86 for i := 0; i < len(str); i++ { 87 hash ^= (hash << 5) + uint64(str[i]) + (hash >> 2) 88 } 89 return hash 90 } 91 92 // P. J. Weinberger Hash Function 93 func PJWHash(str []byte) uint32 { 94 var BitsInUnignedInt uint32 = 4 * 8 95 var ThreeQuarters uint32 = (BitsInUnignedInt * 3) / 4 96 var OneEighth uint32 = BitsInUnignedInt / 8 97 var HighBits uint32 = (0xFFFFFFFF) << (BitsInUnignedInt - OneEighth) 98 var hash uint32 = 0 99 var test uint32 = 0 100 for i := 0; i < len(str); i++ { 101 hash = (hash << OneEighth) + uint32(str[i]) 102 if test = hash & HighBits; test != 0 { 103 hash = (hash ^ (test >> ThreeQuarters)) & (^HighBits + 1) 104 } 105 } 106 return hash 107 } 108 109 // P. J. Weinberger Hash Function 64 110 func PJWHash64(str []byte) uint64 { 111 var BitsInUnignedInt uint64 = 4 * 8 112 var ThreeQuarters uint64 = (BitsInUnignedInt * 3) / 4 113 var OneEighth uint64 = BitsInUnignedInt / 8 114 var HighBits uint64 = (0xFFFFFFFFFFFFFFFF) << (BitsInUnignedInt - OneEighth) 115 var hash uint64 = 0 116 var test uint64 = 0 117 for i := 0; i < len(str); i++ { 118 hash = (hash << OneEighth) + uint64(str[i]) 119 if test = hash & HighBits; test != 0 { 120 hash = (hash ^ (test >> ThreeQuarters)) & (^HighBits + 1) 121 } 122 } 123 return hash 124 } 125 126 // ELF Hash Function 127 func ELFHash(str []byte) uint32 { 128 var hash uint32 = 0 129 var x uint32 = 0 130 for i := 0; i < len(str); i++ { 131 hash = (hash << 4) + uint32(str[i]) 132 if x = hash & 0xF0000000; x != 0 { 133 hash ^= x >> 24 134 hash &= ^x + 1 135 } 136 } 137 return hash 138 } 139 140 // ELF Hash Function 64 141 func ELFHash64(str []byte) uint64 { 142 var hash uint64 = 0 143 var x uint64 = 0 144 for i := 0; i < len(str); i++ { 145 hash = (hash << 4) + uint64(str[i]) 146 if x = hash & 0xF000000000000000; x != 0 { 147 hash ^= x >> 24 148 hash &= ^x + 1 149 } 150 } 151 return hash 152 } 153 154 // DJB Hash Function 155 func DJBHash(str []byte) uint32 { 156 var hash uint32 = 5381 157 for i := 0; i < len(str); i++ { 158 hash += (hash << 5) + uint32(str[i]) 159 } 160 return hash 161 } 162 163 // DJB Hash Function 64 164 func DJBHash64(str []byte) uint64 { 165 var hash uint64 = 5381 166 for i := 0; i < len(str); i++ { 167 hash += (hash << 5) + uint64(str[i]) 168 } 169 return hash 170 } 171 172 // AP Hash Function 173 func APHash(str []byte) uint32 { 174 var hash uint32 = 0 175 for i := 0; i < len(str); i++ { 176 if (i & 1) == 0 { 177 hash ^= (hash << 7) ^ uint32(str[i]) ^ (hash >> 3) 178 } else { 179 hash ^= ^((hash << 11) ^ uint32(str[i]) ^ (hash >> 5)) + 1 180 } 181 } 182 return hash 183 } 184 185 // AP Hash Function 64 186 func APHash64(str []byte) uint64 { 187 var hash uint64 = 0 188 for i := 0; i < len(str); i++ { 189 if (i & 1) == 0 { 190 hash ^= (hash << 7) ^ uint64(str[i]) ^ (hash >> 3) 191 } else { 192 hash ^= ^((hash << 11) ^ uint64(str[i]) ^ (hash >> 5)) + 1 193 } 194 } 195 return hash 196 }