Class ByteUtil

java.lang.Object
cloud.opencode.base.crypto.util.ByteUtil

public final class ByteUtil extends Object
Byte array manipulation utilities for cryptographic operations - Provides operations for concatenation, splitting, XOR, and padding 加密操作的字节数组操作工具 - 提供连接、拆分、异或和填充操作

Features | 主要功能:

  • Byte array manipulation utilities - 字节数组操作工具
  • Concatenation, splitting, comparison - 连接、分割、比较

Usage Examples | 使用示例:

byte[] combined = ByteUtil.concat(part1, part2);
boolean equal = ByteUtil.constantTimeEquals(a, b);

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Yes - 空值安全: 是

Performance | 性能特性:

  • Time complexity: O(n) - 时间复杂度: O(n),n为数据长度
  • Space complexity: O(n) - 空间复杂度: O(n)
Since:
JDK 25, opencode-base-crypto V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Summary

    Modifier and Type
    Method
    Description
    static int
    bytesToInt(byte[] bytes)
    Convert byte array to int (big-endian) 将字节数组转换为int(大端序)
    static long
    bytesToLong(byte[] bytes)
    Convert byte array to long (big-endian) 将字节数组转换为long(大端序)
    static byte[]
    concat(byte[]... arrays)
    Concatenate multiple byte arrays into a single array 将多个字节数组连接成单个数组
    static byte[]
    intToBytes(int value)
    Convert int to byte array (big-endian) 将int转换为字节数组(大端序)
    static byte[]
    longToBytes(long value)
    Convert long to byte array (big-endian) 将long转换为字节数组(大端序)
    static byte[]
    padPkcs7(byte[] data, int blockSize)
    Apply PKCS#7 padding to data 对数据应用PKCS#7填充
    static byte[]
    reverse(byte[] array)
    Reverse byte array 反转字节数组
    static byte[][]
    split(byte[] array, int... lengths)
    Split byte array into multiple arrays of specified lengths 将字节数组拆分为指定长度的多个数组
    static byte[]
    unpadPkcs7(byte[] data)
    Remove PKCS#7 padding from data 从数据中移除PKCS#7填充
    static byte[]
    xor(byte[] a, byte[] b)
    XOR two byte arrays 对两个字节数组执行异或操作

    Methods inherited from class Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Method Details

    • concat

      public static byte[] concat(byte[]... arrays)
      Concatenate multiple byte arrays into a single array 将多个字节数组连接成单个数组
      Parameters:
      arrays - byte arrays to concatenate
      Returns:
      concatenated byte array
      Throws:
      NullPointerException - if arrays is null or contains null elements
    • split

      public static byte[][] split(byte[] array, int... lengths)
      Split byte array into multiple arrays of specified lengths 将字节数组拆分为指定长度的多个数组
      Parameters:
      array - byte array to split
      lengths - lengths of each resulting array
      Returns:
      array of byte arrays
      Throws:
      NullPointerException - if array or lengths is null
      IllegalArgumentException - if sum of lengths does not equal array length or any length is negative
    • xor

      public static byte[] xor(byte[] a, byte[] b)
      XOR two byte arrays 对两个字节数组执行异或操作
      Parameters:
      a - first byte array
      b - second byte array
      Returns:
      XOR result
      Throws:
      NullPointerException - if either array is null
      IllegalArgumentException - if arrays have different lengths
    • reverse

      public static byte[] reverse(byte[] array)
      Reverse byte array 反转字节数组
      Parameters:
      array - byte array to reverse
      Returns:
      reversed byte array
      Throws:
      NullPointerException - if array is null
    • intToBytes

      public static byte[] intToBytes(int value)
      Convert int to byte array (big-endian) 将int转换为字节数组(大端序)
      Parameters:
      value - integer value
      Returns:
      4-byte array
    • bytesToInt

      public static int bytesToInt(byte[] bytes)
      Convert byte array to int (big-endian) 将字节数组转换为int(大端序)
      Parameters:
      bytes - byte array (must be at least 4 bytes)
      Returns:
      integer value
      Throws:
      NullPointerException - if bytes is null
      IllegalArgumentException - if bytes length is less than 4
    • longToBytes

      public static byte[] longToBytes(long value)
      Convert long to byte array (big-endian) 将long转换为字节数组(大端序)
      Parameters:
      value - long value
      Returns:
      8-byte array
    • bytesToLong

      public static long bytesToLong(byte[] bytes)
      Convert byte array to long (big-endian) 将字节数组转换为long(大端序)
      Parameters:
      bytes - byte array (must be at least 8 bytes)
      Returns:
      long value
      Throws:
      NullPointerException - if bytes is null
      IllegalArgumentException - if bytes length is less than 8
    • padPkcs7

      public static byte[] padPkcs7(byte[] data, int blockSize)
      Apply PKCS#7 padding to data 对数据应用PKCS#7填充

      PKCS#7 padding adds N bytes of value N, where N is the number of bytes required to reach the next block boundary. PKCS#7填充添加N个值为N的字节,其中N是到达下一个块边界所需的字节数。

      Parameters:
      data - data to pad
      blockSize - block size in bytes
      Returns:
      padded data
      Throws:
      NullPointerException - if data is null
      IllegalArgumentException - if blockSize is not between 1 and 255
    • unpadPkcs7

      public static byte[] unpadPkcs7(byte[] data)
      Remove PKCS#7 padding from data 从数据中移除PKCS#7填充
      Parameters:
      data - padded data
      Returns:
      unpadded data
      Throws:
      NullPointerException - if data is null
      IllegalArgumentException - if data is empty or padding is invalid