mirror of
https://github.com/taigrr/colorhash.git
synced 2026-04-02 03:08:48 -07:00
use fnv instead of md5
This commit is contained in:
@@ -1,5 +1,13 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"fmt"
|
||||||
|
|
||||||
|
go_colorhash "github.com/taigrr/go-colorhash"
|
||||||
|
)
|
||||||
|
|
||||||
func main() {
|
func main() {
|
||||||
|
|
||||||
|
x := go_colorhash.HashString("asdasd")
|
||||||
|
fmt.Println(x)
|
||||||
}
|
}
|
||||||
|
|||||||
3
hash.go
3
hash.go
@@ -3,6 +3,7 @@ package go_colorhash
|
|||||||
import (
|
import (
|
||||||
"crypto/md5"
|
"crypto/md5"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"hash/fnv"
|
||||||
"io"
|
"io"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -10,7 +11,7 @@ const MaxUint = ^uint(0)
|
|||||||
const MaxInt = int(MaxUint >> 1)
|
const MaxInt = int(MaxUint >> 1)
|
||||||
|
|
||||||
func HashString(s string) int {
|
func HashString(s string) int {
|
||||||
h := md5.New()
|
h := fnv.New64()
|
||||||
io.WriteString(h, s)
|
io.WriteString(h, s)
|
||||||
hashb := h.Sum(nil)
|
hashb := h.Sum(nil)
|
||||||
hashb = hashb[len(hashb)-8:]
|
hashb = hashb[len(hashb)-8:]
|
||||||
|
|||||||
@@ -15,10 +15,10 @@ func TestHashString(t *testing.T) {
|
|||||||
String string
|
String string
|
||||||
Value int
|
Value int
|
||||||
ID string
|
ID string
|
||||||
}{{String: "", Value: 7602086723416769149, ID: "Empty string"},
|
}{{String: "", Value: 5472609002491880228, ID: "Empty string"},
|
||||||
{String: "123", Value: 1606385479620709231, ID: "123"},
|
{String: "123", Value: 6449148174219763898, ID: "123"},
|
||||||
{String: "it's as easy as", Value: 5377981271559288604, ID: "easy"},
|
{String: "it's as easy as", Value: 5908178111834329190, ID: "easy"},
|
||||||
{String: "hello colorhash", Value: 4155814819593785823, ID: "hello"}}
|
{String: "hello colorhash", Value: 893132354324239557, ID: "hello"}}
|
||||||
for _, tc := range testStrings {
|
for _, tc := range testStrings {
|
||||||
t.Run(tc.ID, func(t *testing.T) {
|
t.Run(tc.ID, func(t *testing.T) {
|
||||||
hash := HashString(tc.String)
|
hash := HashString(tc.String)
|
||||||
|
|||||||
Reference in New Issue
Block a user