Skip to content

Commit f0ec608

Browse files
committed
Kasey's review #2
1 parent 1dd6ba8 commit f0ec608

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

encoding/bytesutil/hex.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,17 @@ func IsHex(b []byte) bool {
2222
// DecodeHexWithLength takes a string and a length in bytes,
2323
// and validates whether the string is a hex and has the correct length.
2424
func DecodeHexWithLength(s string, length int) ([]byte, error) {
25-
if len(s) != 2*length+2 {
26-
return nil, fmt.Errorf("%s is not length %d bytes", s, length)
25+
if len(s) > 2*length+2 {
26+
return nil, fmt.Errorf("%s is greather than length %d bytes", s, length)
2727
}
28-
return hexutil.Decode(s)
28+
bytes, err := hexutil.Decode(s)
29+
if err != nil {
30+
return nil, errors.Wrap(err, fmt.Sprintf("%s is not a valid hex", s))
31+
}
32+
if len(bytes) != length {
33+
return nil, fmt.Errorf("length of %s is not %d bytes", s, length)
34+
}
35+
return bytes, nil
2936
}
3037

3138
// DecodeHexWithMaxLength takes a string and a length in bytes,

0 commit comments

Comments
 (0)