fix incorrect double-hash for ip
This commit is contained in:
+5
-3
@@ -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)
|
||||
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user