github.com/oweisse/u-root@v0.0.0-20181109060735-d005ad25fef1/cmds/elvish/hash/hash.go (about)

     1  // Copyright 2018 the u-root 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  // Package hash contains some common hash functions suitable for use in hash
     5  // maps.
     6  package hash
     7  
     8  import "fmt"
     9  
    10  const DJBInit uint32 = 5381
    11  
    12  func DJBCombine(acc, h uint32) uint32 {
    13  	return (acc<<5 + acc) + h
    14  }
    15  
    16  func DJB(hs ...uint32) uint32 {
    17  	acc := DJBInit
    18  	for _, h := range hs {
    19  		acc = DJBCombine(acc, h)
    20  	}
    21  	return acc
    22  }
    23  
    24  func Hash(i interface{}) uint32 {
    25  	s := fmt.Sprintf("%x", i)
    26  	h := DJBInit
    27  	for i := 0; i < len(s); i++ {
    28  		h = DJBCombine(h, uint32(s[i]))
    29  	}
    30  	return h
    31  }