From 738b2455d2c503c829d1043a780f8df7f20b7edf Mon Sep 17 00:00:00 2001 From: "A. Mashmooli" Date: Tue, 21 May 2019 16:52:33 +0430 Subject: [PATCH 1/3] rewrite encode and decode hex from original idea --- hashids.go | 39 +++++++++++++-------------------------- 1 file changed, 13 insertions(+), 26 deletions(-) diff --git a/hashids.go b/hashids.go index 0c133b6..7f2472e 100644 --- a/hashids.go +++ b/hashids.go @@ -10,6 +10,8 @@ import ( "errors" "fmt" "math" + "regexp" + "strconv" "strings" ) @@ -226,26 +228,15 @@ func (h *HashID) EncodeInt64(numbers []int64) (string, error) { // // Each hex nibble is encoded as an integer in range [16, 31]. func (h *HashID) EncodeHex(hex string) (string, error) { - nums := make([]int, len(hex)) - - for i := 0; i < len(hex); i++ { - b := hex[i] - switch { - case (b >= '0') && (b <= '9'): - b -= '0' - case (b >= 'a') && (b <= 'f'): - b -= 'a' - 'A' - fallthrough - case (b >= 'A') && (b <= 'F'): - b -= ('A' - 0xA) - default: - return "", errors.New("invalid hex digit") - } - // Each int is in range [16, 31] - nums[i] = 0x10 + int(b) - } + reg := regexp.MustCompile(`[\w\W]{1,12}`) + numbers := reg.FindAllString(hex, -1) - return h.Encode(nums) + nums := make([]int64, len(numbers)) + for i, number := range numbers { + num, _ := strconv.ParseInt("1"+number, 16, 0) + nums[i] = num + } + return h.EncodeInt64(nums) } // DEPRECATED: Use DecodeWithError instead @@ -339,13 +330,9 @@ func (h *HashID) DecodeHex(hash string) (string, error) { return "", err } - const hex = "0123456789abcdef" - b := make([]byte, len(numbers)) - for i, n := range numbers { - if n < 0x10 || n > 0x1f { - return "", errors.New("invalid number") - } - b[i] = hex[n-0x10] + var b string + for _, n := range numbers { + b += strconv.FormatInt(n, 16)[1:] } return string(b), nil } From 8a1afd2039cc2b6a1280def25b2f70e2ed0b1754 Mon Sep 17 00:00:00 2001 From: "A. Mashmooli" Date: Tue, 21 May 2019 16:58:00 +0430 Subject: [PATCH 2/3] update test for new encode and decode hex method --- hashids_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hashids_test.go b/hashids_test.go index e63d091..998d979 100644 --- a/hashids_test.go +++ b/hashids_test.go @@ -73,7 +73,7 @@ func TestEncodeDecodeEpoch(t *testing.T) { func TestEncodeDecodeHex(t *testing.T) { hdata := NewData() - hdata.MinLength = 30 + hdata.MinLength = 8 hdata.Salt = "this is my salt" hid, _ := NewWithData(hdata) @@ -84,7 +84,7 @@ func TestEncodeDecodeHex(t *testing.T) { t.Fatal(err) } - const expected = "qmTqfesOIqHrsoCYf9UkFZixSKuBT4umuruXuMiDsVsbSrfV" + const expected = "bv89jEY45DslgBOeD2Qg" if hash != expected { t.Fatalf("got %q, expected %q", hash, expected) } From 7c9e54c709a28b482f5f113a2e221f2d78fd0d46 Mon Sep 17 00:00:00 2001 From: A Mashmooli Date: Tue, 21 May 2019 21:21:41 +0430 Subject: [PATCH 3/3] rollback encode/decode hex test min length --- hashids_test.go | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/hashids_test.go b/hashids_test.go index 998d979..c0a3dec 100644 --- a/hashids_test.go +++ b/hashids_test.go @@ -73,7 +73,7 @@ func TestEncodeDecodeEpoch(t *testing.T) { func TestEncodeDecodeHex(t *testing.T) { hdata := NewData() - hdata.MinLength = 8 + hdata.MinLength = 30 hdata.Salt = "this is my salt" hid, _ := NewWithData(hdata) @@ -84,7 +84,7 @@ func TestEncodeDecodeHex(t *testing.T) { t.Fatal(err) } - const expected = "bv89jEY45DslgBOeD2Qg" + const expected = "81OP0bv89jEY45DslgBOeD2Qg0kmb7" if hash != expected { t.Fatalf("got %q, expected %q", hash, expected) }