github.com/goproxy0/go@v0.0.0-20171111080102-49cc0c489d2c/src/hash/fnv/fnv.go (about) 1 // Copyright 2011 The Go Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style 3 // license that can be found in the LICENSE file. 4 5 // Package fnv implements FNV-1 and FNV-1a, non-cryptographic hash functions 6 // created by Glenn Fowler, Landon Curt Noll, and Phong Vo. 7 // See 8 // https://en.wikipedia.org/wiki/Fowler-Noll-Vo_hash_function. 9 package fnv 10 11 import ( 12 "errors" 13 "hash" 14 ) 15 16 type ( 17 sum32 uint32 18 sum32a uint32 19 sum64 uint64 20 sum64a uint64 21 sum128 [2]uint64 22 sum128a [2]uint64 23 ) 24 25 const ( 26 offset32 = 2166136261 27 offset64 = 14695981039346656037 28 offset128Lower = 0x62b821756295c58d 29 offset128Higher = 0x6c62272e07bb0142 30 prime32 = 16777619 31 prime64 = 1099511628211 32 prime128Lower = 0x13b 33 prime128Shift = 24 34 ) 35 36 // New32 returns a new 32-bit FNV-1 hash.Hash. 37 // Its Sum method will lay the value out in big-endian byte order. 38 func New32() hash.Hash32 { 39 var s sum32 = offset32 40 return &s 41 } 42 43 // New32a returns a new 32-bit FNV-1a hash.Hash. 44 // Its Sum method will lay the value out in big-endian byte order. 45 func New32a() hash.Hash32 { 46 var s sum32a = offset32 47 return &s 48 } 49 50 // New64 returns a new 64-bit FNV-1 hash.Hash. 51 // Its Sum method will lay the value out in big-endian byte order. 52 func New64() hash.Hash64 { 53 var s sum64 = offset64 54 return &s 55 } 56 57 // New64a returns a new 64-bit FNV-1a hash.Hash. 58 // Its Sum method will lay the value out in big-endian byte order. 59 func New64a() hash.Hash64 { 60 var s sum64a = offset64 61 return &s 62 } 63 64 // New128 returns a new 128-bit FNV-1 hash.Hash. 65 // Its Sum method will lay the value out in big-endian byte order. 66 func New128() hash.Hash { 67 var s sum128 68 s[0] = offset128Higher 69 s[1] = offset128Lower 70 return &s 71 } 72 73 // New128a returns a new 128-bit FNV-1a hash.Hash. 74 // Its Sum method will lay the value out in big-endian byte order. 75 func New128a() hash.Hash { 76 var s sum128a 77 s[0] = offset128Higher 78 s[1] = offset128Lower 79 return &s 80 } 81 82 func (s *sum32) Reset() { *s = offset32 } 83 func (s *sum32a) Reset() { *s = offset32 } 84 func (s *sum64) Reset() { *s = offset64 } 85 func (s *sum64a) Reset() { *s = offset64 } 86 func (s *sum128) Reset() { s[0] = offset128Higher; s[1] = offset128Lower } 87 func (s *sum128a) Reset() { s[0] = offset128Higher; s[1] = offset128Lower } 88 89 func (s *sum32) Sum32() uint32 { return uint32(*s) } 90 func (s *sum32a) Sum32() uint32 { return uint32(*s) } 91 func (s *sum64) Sum64() uint64 { return uint64(*s) } 92 func (s *sum64a) Sum64() uint64 { return uint64(*s) } 93 94 func (s *sum32) Write(data []byte) (int, error) { 95 hash := *s 96 for _, c := range data { 97 hash *= prime32 98 hash ^= sum32(c) 99 } 100 *s = hash 101 return len(data), nil 102 } 103 104 func (s *sum32a) Write(data []byte) (int, error) { 105 hash := *s 106 for _, c := range data { 107 hash ^= sum32a(c) 108 hash *= prime32 109 } 110 *s = hash 111 return len(data), nil 112 } 113 114 func (s *sum64) Write(data []byte) (int, error) { 115 hash := *s 116 for _, c := range data { 117 hash *= prime64 118 hash ^= sum64(c) 119 } 120 *s = hash 121 return len(data), nil 122 } 123 124 func (s *sum64a) Write(data []byte) (int, error) { 125 hash := *s 126 for _, c := range data { 127 hash ^= sum64a(c) 128 hash *= prime64 129 } 130 *s = hash 131 return len(data), nil 132 } 133 134 func (s *sum128) Write(data []byte) (int, error) { 135 for _, c := range data { 136 // Compute the multiplication in 4 parts to simplify carrying 137 s1l := (s[1] & 0xffffffff) * prime128Lower 138 s1h := (s[1] >> 32) * prime128Lower 139 s0l := (s[0]&0xffffffff)*prime128Lower + (s[1]&0xffffffff)<<prime128Shift 140 s0h := (s[0]>>32)*prime128Lower + (s[1]>>32)<<prime128Shift 141 // Carries 142 s1h += s1l >> 32 143 s0l += s1h >> 32 144 s0h += s0l >> 32 145 // Update the values 146 s[1] = (s1l & 0xffffffff) + (s1h << 32) 147 s[0] = (s0l & 0xffffffff) + (s0h << 32) 148 s[1] ^= uint64(c) 149 } 150 return len(data), nil 151 } 152 153 func (s *sum128a) Write(data []byte) (int, error) { 154 for _, c := range data { 155 s[1] ^= uint64(c) 156 // Compute the multiplication in 4 parts to simplify carrying 157 s1l := (s[1] & 0xffffffff) * prime128Lower 158 s1h := (s[1] >> 32) * prime128Lower 159 s0l := (s[0]&0xffffffff)*prime128Lower + (s[1]&0xffffffff)<<prime128Shift 160 s0h := (s[0]>>32)*prime128Lower + (s[1]>>32)<<prime128Shift 161 // Carries 162 s1h += s1l >> 32 163 s0l += s1h >> 32 164 s0h += s0l >> 32 165 // Update the values 166 s[1] = (s1l & 0xffffffff) + (s1h << 32) 167 s[0] = (s0l & 0xffffffff) + (s0h << 32) 168 } 169 return len(data), nil 170 } 171 172 func (s *sum32) Size() int { return 4 } 173 func (s *sum32a) Size() int { return 4 } 174 func (s *sum64) Size() int { return 8 } 175 func (s *sum64a) Size() int { return 8 } 176 func (s *sum128) Size() int { return 16 } 177 func (s *sum128a) Size() int { return 16 } 178 179 func (s *sum32) BlockSize() int { return 1 } 180 func (s *sum32a) BlockSize() int { return 1 } 181 func (s *sum64) BlockSize() int { return 1 } 182 func (s *sum64a) BlockSize() int { return 1 } 183 func (s *sum128) BlockSize() int { return 1 } 184 func (s *sum128a) BlockSize() int { return 1 } 185 186 func (s *sum32) Sum(in []byte) []byte { 187 v := uint32(*s) 188 return append(in, byte(v>>24), byte(v>>16), byte(v>>8), byte(v)) 189 } 190 191 func (s *sum32a) Sum(in []byte) []byte { 192 v := uint32(*s) 193 return append(in, byte(v>>24), byte(v>>16), byte(v>>8), byte(v)) 194 } 195 196 func (s *sum64) Sum(in []byte) []byte { 197 v := uint64(*s) 198 return append(in, byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8), byte(v)) 199 } 200 201 func (s *sum64a) Sum(in []byte) []byte { 202 v := uint64(*s) 203 return append(in, byte(v>>56), byte(v>>48), byte(v>>40), byte(v>>32), byte(v>>24), byte(v>>16), byte(v>>8), byte(v)) 204 } 205 206 func (s *sum128) Sum(in []byte) []byte { 207 return append(in, 208 byte(s[0]>>56), byte(s[0]>>48), byte(s[0]>>40), byte(s[0]>>32), byte(s[0]>>24), byte(s[0]>>16), byte(s[0]>>8), byte(s[0]), 209 byte(s[1]>>56), byte(s[1]>>48), byte(s[1]>>40), byte(s[1]>>32), byte(s[1]>>24), byte(s[1]>>16), byte(s[1]>>8), byte(s[1]), 210 ) 211 } 212 213 func (s *sum128a) Sum(in []byte) []byte { 214 return append(in, 215 byte(s[0]>>56), byte(s[0]>>48), byte(s[0]>>40), byte(s[0]>>32), byte(s[0]>>24), byte(s[0]>>16), byte(s[0]>>8), byte(s[0]), 216 byte(s[1]>>56), byte(s[1]>>48), byte(s[1]>>40), byte(s[1]>>32), byte(s[1]>>24), byte(s[1]>>16), byte(s[1]>>8), byte(s[1]), 217 ) 218 } 219 220 const ( 221 magic32 = "fnv\x01" 222 magic32a = "fnv\x02" 223 magic64 = "fnv\x03" 224 magic64a = "fnv\x04" 225 magic128 = "fnv\x05" 226 magic128a = "fnv\x06" 227 marshaledSize32 = len(magic32) + 4 228 marshaledSize64 = len(magic64) + 8 229 marshaledSize128 = len(magic128) + 8*2 230 ) 231 232 func (s *sum32) MarshalBinary() ([]byte, error) { 233 b := make([]byte, 0, marshaledSize32) 234 b = append(b, magic32...) 235 b = appendUint32(b, uint32(*s)) 236 return b, nil 237 } 238 239 func (s *sum32a) MarshalBinary() ([]byte, error) { 240 b := make([]byte, 0, marshaledSize32) 241 b = append(b, magic32a...) 242 b = appendUint32(b, uint32(*s)) 243 return b, nil 244 } 245 246 func (s *sum64) MarshalBinary() ([]byte, error) { 247 b := make([]byte, 0, marshaledSize64) 248 b = append(b, magic64...) 249 b = appendUint64(b, uint64(*s)) 250 return b, nil 251 252 } 253 254 func (s *sum64a) MarshalBinary() ([]byte, error) { 255 b := make([]byte, 0, marshaledSize64) 256 b = append(b, magic64a...) 257 b = appendUint64(b, uint64(*s)) 258 return b, nil 259 } 260 261 func (s *sum128) MarshalBinary() ([]byte, error) { 262 b := make([]byte, 0, marshaledSize128) 263 b = append(b, magic128...) 264 b = appendUint64(b, s[0]) 265 b = appendUint64(b, s[1]) 266 return b, nil 267 } 268 269 func (s *sum128a) MarshalBinary() ([]byte, error) { 270 b := make([]byte, 0, marshaledSize128) 271 b = append(b, magic128a...) 272 b = appendUint64(b, s[0]) 273 b = appendUint64(b, s[1]) 274 return b, nil 275 } 276 277 func (s *sum32) UnmarshalBinary(b []byte) error { 278 if len(b) < len(magic32) || string(b[:len(magic32)]) != magic32 { 279 return errors.New("hash/fnv: invalid hash state identifier") 280 } 281 if len(b) != marshaledSize32 { 282 return errors.New("hash/fnv: invalid hash state size") 283 } 284 *s = sum32(readUint32(b[4:])) 285 return nil 286 } 287 288 func (s *sum32a) UnmarshalBinary(b []byte) error { 289 if len(b) < len(magic32a) || string(b[:len(magic32a)]) != magic32a { 290 return errors.New("hash/fnv: invalid hash state identifier") 291 } 292 if len(b) != marshaledSize32 { 293 return errors.New("hash/fnv: invalid hash state size") 294 } 295 *s = sum32a(readUint32(b[4:])) 296 return nil 297 } 298 299 func (s *sum64) UnmarshalBinary(b []byte) error { 300 if len(b) < len(magic64) || string(b[:len(magic64)]) != magic64 { 301 return errors.New("hash/fnv: invalid hash state identifier") 302 } 303 if len(b) != marshaledSize64 { 304 return errors.New("hash/fnv: invalid hash state size") 305 } 306 *s = sum64(readUint64(b[4:])) 307 return nil 308 } 309 310 func (s *sum64a) UnmarshalBinary(b []byte) error { 311 if len(b) < len(magic64a) || string(b[:len(magic64a)]) != magic64a { 312 return errors.New("hash/fnv: invalid hash state identifier") 313 } 314 if len(b) != marshaledSize64 { 315 return errors.New("hash/fnv: invalid hash state size") 316 } 317 *s = sum64a(readUint64(b[4:])) 318 return nil 319 } 320 321 func (s *sum128) UnmarshalBinary(b []byte) error { 322 if len(b) < len(magic128) || string(b[:len(magic128)]) != magic128 { 323 return errors.New("hash/fnv: invalid hash state identifier") 324 } 325 if len(b) != marshaledSize128 { 326 return errors.New("hash/fnv: invalid hash state size") 327 } 328 s[0] = readUint64(b[4:]) 329 s[1] = readUint64(b[12:]) 330 return nil 331 } 332 333 func (s *sum128a) UnmarshalBinary(b []byte) error { 334 if len(b) < len(magic128a) || string(b[:len(magic128a)]) != magic128a { 335 return errors.New("hash/fnv: invalid hash state identifier") 336 } 337 if len(b) != marshaledSize128 { 338 return errors.New("hash/fnv: invalid hash state size") 339 } 340 s[0] = readUint64(b[4:]) 341 s[1] = readUint64(b[12:]) 342 return nil 343 } 344 345 func readUint32(b []byte) uint32 { 346 _ = b[3] 347 return uint32(b[3]) | uint32(b[2])<<8 | uint32(b[1])<<16 | uint32(b[0])<<24 348 } 349 350 func appendUint32(b []byte, x uint32) []byte { 351 a := [4]byte{ 352 byte(x >> 24), 353 byte(x >> 16), 354 byte(x >> 8), 355 byte(x), 356 } 357 return append(b, a[:]...) 358 } 359 360 func appendUint64(b []byte, x uint64) []byte { 361 a := [8]byte{ 362 byte(x >> 56), 363 byte(x >> 48), 364 byte(x >> 40), 365 byte(x >> 32), 366 byte(x >> 24), 367 byte(x >> 16), 368 byte(x >> 8), 369 byte(x), 370 } 371 return append(b, a[:]...) 372 } 373 374 func readUint64(b []byte) uint64 { 375 _ = b[7] 376 return uint64(b[7]) | uint64(b[6])<<8 | uint64(b[5])<<16 | uint64(b[4])<<24 | 377 uint64(b[3])<<32 | uint64(b[2])<<40 | uint64(b[1])<<48 | uint64(b[0])<<56 378 }