Skip to content

Commit 82b8d76

Browse files
committed
Fix DecodeHex: check that decoded ints are in range; faster impl.
- check that decoded numbers are in range - much much faster implementation
1 parent 16ad349 commit 82b8d76

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

hashids.go

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -335,13 +335,15 @@ func (h *HashID) DecodeHex(hash string) (string, error) {
335335
return "", err
336336
}
337337

338-
ret := ""
339-
for _, n := range numbers {
340-
nHex := fmt.Sprintf("%X", n)
341-
ret = strings.ToLower(fmt.Sprintf("%s%s", ret, nHex[1:len(nHex)]))
338+
const hex = "0123456789abcdef"
339+
b := make([]byte, len(numbers))
340+
for i, n := range numbers {
341+
if n < 0x10 || n > 0x1f {
342+
return "", errors.New("invalid number")
343+
}
344+
b[i] = hex[n-0x10]
342345
}
343-
344-
return ret, nil
346+
return string(b), nil
345347
}
346348

347349
func splitRunes(input, seps []rune) [][]rune {

0 commit comments

Comments
 (0)