Skip to content

Commit 028b666

Browse files
authored
fix: use commons-codec for hex encoding/decoding in Util class (#758)
- Throws IllegalArgumentException for non-hex characters (previously silently converted to incorrect bytes) - Throws IllegalArgumentException for odd-length strings (previously threw IndexOutOfBoundsException)
1 parent a589c2a commit 028b666

2 files changed

Lines changed: 10 additions & 16 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
- fix: add validation for `ManageDataOperation` value length to ensure it does not exceed 64 bytes.
1010
- fix: use `StandardCharsets.UTF_8` explicitly when converting byte arrays to strings to ensure consistent behavior across different platforms.
1111
- refactor: use static initialization for `GsonSingleton` to ensure thread safety.
12+
- fix: use `commons-codec` for hex encoding/decoding in `Util` class to properly validate input and throw clear exceptions for invalid hex strings.
1213

1314
## 2.2.1
1415

src/main/java/org/stellar/sdk/Util.java

Lines changed: 9 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@
66
import java.security.MessageDigest;
77
import java.security.NoSuchAlgorithmException;
88
import java.util.Arrays;
9+
import org.apache.commons.codec.DecoderException;
10+
import org.apache.commons.codec.binary.Hex;
911
import org.stellar.sdk.exception.UnexpectedException;
1012

1113
/**
@@ -14,38 +16,29 @@
1416
* <p>note: For some reason we need to make it public, but I don't recommend to use it directly.
1517
*/
1618
public class Util {
17-
private static final char[] HEX_ARRAY = "0123456789ABCDEF".toCharArray();
18-
1919
/**
2020
* Returns hex representation of <code>bytes</code> array.
2121
*
2222
* @param bytes byte array to convert to hex
23-
* @return hex representation of the byte array
23+
* @return hex representation of the byte array (uppercase)
2424
*/
2525
public static String bytesToHex(byte[] bytes) {
26-
char[] hexChars = new char[bytes.length * 2];
27-
for (int j = 0; j < bytes.length; j++) {
28-
int v = bytes[j] & 0xFF;
29-
hexChars[j * 2] = HEX_ARRAY[v >>> 4];
30-
hexChars[j * 2 + 1] = HEX_ARRAY[v & 0x0F];
31-
}
32-
return new String(hexChars);
26+
return Hex.encodeHexString(bytes, false);
3327
}
3428

3529
/**
3630
* Returns byte array representation of <code>hex</code> string.
3731
*
3832
* @param s hex string to convert to byte array
3933
* @return byte array representation of the hex string
34+
* @throws IllegalArgumentException if the string contains non-hex characters or has odd length
4035
*/
4136
public static byte[] hexToBytes(String s) {
42-
int len = s.length();
43-
byte[] data = new byte[len / 2];
44-
for (int i = 0; i < len; i += 2) {
45-
data[i / 2] =
46-
(byte) ((Character.digit(s.charAt(i), 16) << 4) + Character.digit(s.charAt(i + 1), 16));
37+
try {
38+
return Hex.decodeHex(s);
39+
} catch (DecoderException e) {
40+
throw new IllegalArgumentException("Invalid hex string: " + s, e);
4741
}
48-
return data;
4942
}
5043

5144
/**

0 commit comments

Comments
 (0)