Skip to content

Commit

Permalink
common: Let Hex2Bytes and Hex2BytesFixed panic for invalid hex strings
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianst committed Jan 26, 2024
1 parent 4e134b2 commit ff3219a
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions common/bytes.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,14 +76,24 @@ func Bytes2Hex(d []byte) string {
}

// Hex2Bytes returns the bytes represented by the hexadecimal string str.
// The string must not be prefixed with `0x`.
// It panics if str contains a non-hex char.
func Hex2Bytes(str string) []byte {
h, _ := hex.DecodeString(str)
h, err := hex.DecodeString(str)
if err != nil {
panic("error decoding string: " + err.Error())
}
return h
}

// Hex2BytesFixed returns bytes of a specified fixed length flen.
// The string must not be prefixed with `0x`.
// It panics if str contains a non-hex char.
func Hex2BytesFixed(str string, flen int) []byte {
h, _ := hex.DecodeString(str)
h, err := hex.DecodeString(str)
if err != nil {
panic("error decoding string: " + err.Error())
}
if len(h) == flen {
return h
}
Expand Down

0 comments on commit ff3219a

Please sign in to comment.