fix incorrect double-hash for ip

This commit is contained in:
Umputun
2018-05-23 14:41:31 -05:00
parent 3f22b919c9
commit ebe3fb8234
2 changed files with 7 additions and 3 deletions
+5 -3
View File
@@ -6,7 +6,7 @@ import (
"fmt"
"hash/crc64"
"log"
"strconv"
"regexp"
)
// User holds user-related info
@@ -19,12 +19,14 @@ type User struct {
IP string `json:"ip,omitempty"`
}
var reValidSha = regexp.MustCompile("^[a-fA-F0-9]{40}$")
// HashIP replace IP field with hashed hmac
func (u *User) HashIP(secret string) {
hashVal := func(val string) string {
if _, err := strconv.ParseUint(val, 16, 64); err == nil || val == "" {
return val // already hashed
if val == "" || reValidSha.Match([]byte(val)) {
return val // already hashed or empty
}
key := []byte(secret)
h := hmac.New(sha1.New, key)
+2
View File
@@ -28,6 +28,8 @@ func TestUser_HashIP(t *testing.T) {
}{
{"127.0.0.1", "ae12fe3b5f129b5cc4cdd2b136b7b7947c4d2741", "dbc7c999343f003f189f70aaf52cc04443f90790"},
{"8.8.8.8", "8cee77c27e32a2b5aec95c29888ac9946618d9a2", "70a46afce9633f010b06e129b8ad08243a1c4da9"},
{"8cee77c27e32a2b5aec95c29888ac9946618d9a2", "8cee77c27e32a2b5aec95c29888ac9946618d9a2", "8cee77c27e32a2b5aec95c29888ac9946618d9a2"},
{"", "", ""},
}
for i, tt := range tbl {