File tree 1 file changed +10
-3
lines changed
1 file changed +10
-3
lines changed Original file line number Diff line number Diff line change @@ -22,10 +22,17 @@ func IsHex(b []byte) bool {
22
22
// DecodeHexWithLength takes a string and a length in bytes,
23
23
// and validates whether the string is a hex and has the correct length.
24
24
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 )
27
27
}
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
29
36
}
30
37
31
38
// DecodeHexWithMaxLength takes a string and a length in bytes,
You can’t perform that action at this time.
0 commit comments