Class ByteArrays
This class provides efficient implementations for common byte array operations including hexadecimal encoding/decoding and comparison.
Hexadecimal Conversion
The toHexString(byte[]) and toByteArray(String) methods
provide efficient conversion between byte arrays and hexadecimal strings.
These are optimized for performance, avoiding the overhead of
String.format() or Integer.toHexString().
Usage Examples
// Convert MD5 hash to hex string
MessageDigest md = MessageDigest.getInstance("MD5");
md.update(data);
String hashHex = ByteArrays.toHexString(md.digest());
// Parse hex string back to bytes
byte[] bytes = ByteArrays.toByteArray("48656c6c6f");
// Constant-time comparison for security-sensitive contexts
boolean match = ByteArrays.equals(computed, expected);
Thread Safety
All methods in this class are stateless and thread-safe.
- Author:
- Chris Burdess
-
Method Summary
Modifier and TypeMethodDescriptionstatic booleanequals(byte[] b1, byte[] b2) Compares two byte arrays for equality.static booleanequalsConstantTime(byte[] b1, byte[] b2) Compares two byte arrays for equality in constant time.static byte[]toByteArray(String hexString) Decodes a hexadecimal string into a byte array.static StringtoHexString(byte[] byteArray) Encodes a byte array as a lowercase hexadecimal string.
-
Method Details
-
toByteArray
Decodes a hexadecimal string into a byte array.Each pair of hexadecimal characters is converted to a single byte. Both uppercase and lowercase hex digits are accepted.
- Parameters:
hexString- the hexadecimal string to decode (must have even length)- Returns:
- the decoded byte array
- Throws:
IllegalArgumentException- if the string is null, has odd length, or contains non-hexadecimal characters
-
toHexString
Encodes a byte array as a lowercase hexadecimal string.This implementation is optimized for performance, using direct character array manipulation rather than
String.format()orStringBuilder.append().The output uses lowercase hexadecimal digits (0-9, a-f).
- Parameters:
byteArray- the byte array to encode- Returns:
- the hexadecimal string representation
- Throws:
IllegalArgumentException- if the byte array is null
-
equals
public static boolean equals(byte[] b1, byte[] b2) Compares two byte arrays for equality.This method returns
falseas soon as a mismatch is found, making it efficient for general-purpose comparison. For security-sensitive comparisons where timing attacks are a concern (e.g., comparing MACs), useequalsConstantTime(byte[], byte[])instead.- Parameters:
b1- the first byte arrayb2- the second byte array- Returns:
trueif both arrays are non-null, have the same length, and contain identical bytes;falseotherwise
-
equalsConstantTime
public static boolean equalsConstantTime(byte[] b1, byte[] b2) Compares two byte arrays for equality in constant time.This method always iterates through all bytes of equal-length arrays, preventing timing attacks that could reveal information about the position of mismatched bytes. Use this for security-sensitive comparisons such as MAC verification or password hash comparison.
Returns
falseimmediately if either array is null or if the arrays have different lengths (length comparison is not constant-time as it is typically not security-sensitive).- Parameters:
b1- the first byte arrayb2- the second byte array- Returns:
trueif both arrays are non-null, have the same length, and contain identical bytes;falseotherwise
-