Created
May 10, 2013 20:41
-
-
Save Chase-san/5557228 to your computer and use it in GitHub Desktop.
Jenkins hash function
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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