mirror of
https://github.com/taigrr/colorhash.git
synced 2026-04-01 18:58:45 -07:00
Adds hash_test code
This commit is contained in:
35
hash.go
Normal file
35
hash.go
Normal file
@@ -0,0 +1,35 @@
|
||||
package go_colorhash
|
||||
|
||||
import (
|
||||
"crypto/md5"
|
||||
"encoding/binary"
|
||||
"io"
|
||||
)
|
||||
|
||||
const MaxUint = ^uint(0)
|
||||
const MaxInt = int(MaxUint >> 1)
|
||||
|
||||
func HashString(s string) int {
|
||||
h := md5.New()
|
||||
io.WriteString(h, s)
|
||||
hashb := h.Sum(nil)
|
||||
hashb = hashb[len(hashb)-8:]
|
||||
lsb := binary.BigEndian.Uint64(hashb)
|
||||
sint := int(lsb)
|
||||
if sint < 0 {
|
||||
sint = sint + MaxInt
|
||||
}
|
||||
return sint
|
||||
}
|
||||
func HashBytes(r io.Reader) int {
|
||||
h := md5.New()
|
||||
io.Copy(h, r)
|
||||
hashb := h.Sum(nil)
|
||||
hashb = hashb[len(hashb)-8:]
|
||||
lsb := binary.BigEndian.Uint64(hashb)
|
||||
sint := int(lsb)
|
||||
if sint < 0 {
|
||||
sint = sint + MaxInt
|
||||
}
|
||||
return sint
|
||||
}
|
||||
Reference in New Issue
Block a user