From 6d82a1be93088e7e79a85d3109cdd8ac5229b902 Mon Sep 17 00:00:00 2001 From: Umputun Date: Wed, 25 Jul 2018 13:28:15 -0400 Subject: [PATCH] extract common hasing with failback --- backend/app/store/user.go | 19 ++++++++++--------- 1 file changed, 10 insertions(+), 9 deletions(-) diff --git a/backend/app/store/user.go b/backend/app/store/user.go index a1d032ed..eec073e0 100644 --- a/backend/app/store/user.go +++ b/backend/app/store/user.go @@ -5,6 +5,7 @@ import ( "crypto/sha1" "encoding/hex" "fmt" + "hash" "hash/crc64" "io" "log" @@ -36,12 +37,7 @@ func HashValue(val string, secret string) string { } key := []byte(secret) h := hmac.New(sha1.New, key) - if _, err := io.WriteString(h, val); err != nil { - // fail back to crc64 - log.Printf("[WARN] can't hash ip, %s", err) - return fmt.Sprintf("%x", crc64.Checksum([]byte(val), crc64.MakeTable(crc64.ECMA))) - } - return hex.EncodeToString(h.Sum(nil)) + return hashWithFailback(h, val) } // EncodeID hashes id to sha1. The function intentionally left outside of User struct because in some cases @@ -51,10 +47,15 @@ func EncodeID(id string) string { return id // already hashed or empty } h := sha1.New() - if _, err := io.WriteString(h, id); err != nil { + return hashWithFailback(h, id) +} + +// hashWithFailback tries to has val with hash.Hash and failback to crc if needed +func hashWithFailback(h hash.Hash, val string) string { + if _, err := io.WriteString(h, val); err != nil { // fail back to crc64 - log.Printf("[WARN] can't hash id %s, %s", id, err) - return fmt.Sprintf("%x", crc64.Checksum([]byte(id), crc64.MakeTable(crc64.ECMA))) + log.Printf("[WARN] can't hash id %s, %s", val, err) + return fmt.Sprintf("%x", crc64.Checksum([]byte(val), crc64.MakeTable(crc64.ECMA))) } return hex.EncodeToString(h.Sum(nil)) }