Skip to content

Instantly share code, notes, and snippets.

@Chase-san
Created May 10, 2013 20:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Chase-san/5557228 to your computer and use it in GitHub Desktop.
Save Chase-san/5557228 to your computer and use it in GitHub Desktop.
Jenkins hash function
package jenkins
import "hash"
// Jenkins hash function
type jenkins uint32
func New32() hash.Hash32 {
var j jenkins = 0
return &j;
}
func (j *jenkins) Write(data []byte) (int, error) {
hash := *j
for _, e := range data {
hash += jenkins(e)
hash += (hash << 10)
hash ^= (hash >> 6)
}
*j = hash
return len(data), nil
}
func (j *jenkins) Reset() {
*j = 0
}
func (j *jenkins) Size() int {
return 4
}
func (j *jenkins) BlockSize() int {
return 1
}
func (j *jenkins) Sum32() uint32 {
sum := uint32(*j)
sum += sum << 3
sum ^= sum >> 11
sum += sum << 15
return sum
}
func (j *jenkins) Sum(b []byte) []byte {
s := j.Sum32()
b = append(b, byte(s>>24))
b = append(b, byte(s>>16))
b = append(b, byte(s>>8))
b = append(b, byte(s))
return b
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment