Class RandomBytes

java.lang.Object
cloud.opencode.base.crypto.random.RandomBytes

public final class RandomBytes extends Object
Utility class for generating random bytes with various encoding options. 随机字节生成工具类,支持多种编码选项。

Features | 主要功能:

  • Random byte array generation utilities - 随机字节数组生成工具

Usage Examples | 使用示例:

byte[] random = RandomBytes.generate(32);

Security | 安全性:

  • Thread-safe: Yes - 线程安全: 是
  • Null-safe: Yes - 空值安全: 是
Since:
JDK 25, opencode-base-crypto V1.0.0
Author:
Leon Soo www.LeonSoo.com
See Also:
  • Method Details

    • generate

      public static byte[] generate(int length)
      Generates random bytes using the default SecureRandom instance. 使用默认 SecureRandom 实例生成随机字节。
      Parameters:
      length - the number of random bytes to generate
      Returns:
      array of random bytes
      Throws:
      IllegalArgumentException - if length is not positive
    • generate

      public static byte[] generate(int length, SecureRandom random)
      Generates random bytes using the specified SecureRandom instance. 使用指定的 SecureRandom 实例生成随机字节。
      Parameters:
      length - the number of random bytes to generate
      random - the SecureRandom instance to use
      Returns:
      array of random bytes
      Throws:
      IllegalArgumentException - if length is not positive or random is null
    • generateHex

      public static String generateHex(int length)
      Generates random bytes and returns them as a hexadecimal string. 生成随机字节并以十六进制字符串返回。
      Parameters:
      length - the number of random bytes to generate
      Returns:
      hexadecimal string representation of the random bytes
      Throws:
      IllegalArgumentException - if length is not positive
    • generateHex

      public static String generateHex(int length, SecureRandom random)
      Generates random bytes using the specified SecureRandom and returns them as a hexadecimal string. 使用指定的 SecureRandom 生成随机字节并以十六进制字符串返回。
      Parameters:
      length - the number of random bytes to generate
      random - the SecureRandom instance to use
      Returns:
      hexadecimal string representation of the random bytes
      Throws:
      IllegalArgumentException - if length is not positive or random is null
    • generateBase64

      public static String generateBase64(int length)
      Generates random bytes and returns them as a Base64 encoded string. 生成随机字节并以 Base64 编码字符串返回。
      Parameters:
      length - the number of random bytes to generate
      Returns:
      Base64 encoded string representation of the random bytes
      Throws:
      IllegalArgumentException - if length is not positive
    • generateBase64

      public static String generateBase64(int length, SecureRandom random)
      Generates random bytes using the specified SecureRandom and returns them as a Base64 encoded string. 使用指定的 SecureRandom 生成随机字节并以 Base64 编码字符串返回。
      Parameters:
      length - the number of random bytes to generate
      random - the SecureRandom instance to use
      Returns:
      Base64 encoded string representation of the random bytes
      Throws:
      IllegalArgumentException - if length is not positive or random is null
    • generateBase64Url

      public static String generateBase64Url(int length)
      Generates random bytes and returns them as a URL-safe Base64 encoded string. 生成随机字节并以 URL 安全的 Base64 编码字符串返回。
      Parameters:
      length - the number of random bytes to generate
      Returns:
      URL-safe Base64 encoded string representation of the random bytes
      Throws:
      IllegalArgumentException - if length is not positive
    • generateBase64Url

      public static String generateBase64Url(int length, SecureRandom random)
      Generates random bytes using the specified SecureRandom and returns them as a URL-safe Base64 encoded string. 使用指定的 SecureRandom 生成随机字节并以 URL 安全的 Base64 编码字符串返回。
      Parameters:
      length - the number of random bytes to generate
      random - the SecureRandom instance to use
      Returns:
      URL-safe Base64 encoded string representation of the random bytes
      Throws:
      IllegalArgumentException - if length is not positive or random is null