Class ByteArrays

java.lang.Object
org.bluezoo.util.ByteArrays

public final class ByteArrays extends Object
Utility methods for byte array operations.

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 Details

    • toByteArray

      public static byte[] toByteArray(String hexString)
      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

      public static String toHexString(byte[] byteArray)
      Encodes a byte array as a lowercase hexadecimal string.

      This implementation is optimized for performance, using direct character array manipulation rather than String.format() or StringBuilder.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 false as 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), use equalsConstantTime(byte[], byte[]) instead.

      Parameters:
      b1 - the first byte array
      b2 - the second byte array
      Returns:
      true if both arrays are non-null, have the same length, and contain identical bytes; false otherwise
    • 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 false immediately 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 array
      b2 - the second byte array
      Returns:
      true if both arrays are non-null, have the same length, and contain identical bytes; false otherwise