From f101db6d58cd29a4f6251b078b1a218c9bf741b0 Mon Sep 17 00:00:00 2001 From: Tai Groot Date: Fri, 25 Mar 2022 20:40:57 -0700 Subject: [PATCH] use fnv instead of md5 --- cmd/main.go | 8 ++++++++ hash.go | 3 ++- hash_test.go | 8 ++++---- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/cmd/main.go b/cmd/main.go index 7905807..586abd8 100644 --- a/cmd/main.go +++ b/cmd/main.go @@ -1,5 +1,13 @@ package main +import ( + "fmt" + + go_colorhash "github.com/taigrr/go-colorhash" +) + func main() { + x := go_colorhash.HashString("asdasd") + fmt.Println(x) } diff --git a/hash.go b/hash.go index 4976e2d..a32d61d 100644 --- a/hash.go +++ b/hash.go @@ -3,6 +3,7 @@ package go_colorhash import ( "crypto/md5" "encoding/binary" + "hash/fnv" "io" ) @@ -10,7 +11,7 @@ const MaxUint = ^uint(0) const MaxInt = int(MaxUint >> 1) func HashString(s string) int { - h := md5.New() + h := fnv.New64() io.WriteString(h, s) hashb := h.Sum(nil) hashb = hashb[len(hashb)-8:] diff --git a/hash_test.go b/hash_test.go index a454227..b346bc4 100644 --- a/hash_test.go +++ b/hash_test.go @@ -15,10 +15,10 @@ func TestHashString(t *testing.T) { String string Value int ID string - }{{String: "", Value: 7602086723416769149, ID: "Empty string"}, - {String: "123", Value: 1606385479620709231, ID: "123"}, - {String: "it's as easy as", Value: 5377981271559288604, ID: "easy"}, - {String: "hello colorhash", Value: 4155814819593785823, ID: "hello"}} + }{{String: "", Value: 5472609002491880228, ID: "Empty string"}, + {String: "123", Value: 6449148174219763898, ID: "123"}, + {String: "it's as easy as", Value: 5908178111834329190, ID: "easy"}, + {String: "hello colorhash", Value: 893132354324239557, ID: "hello"}} for _, tc := range testStrings { t.Run(tc.ID, func(t *testing.T) { hash := HashString(tc.String)