Class SecureRandoms

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

public final class SecureRandoms extends Object
Secure random number generator factory providing various SecureRandom instances. 安全随机数生成器工厂,提供各种 SecureRandom 实例。

Features | 主要功能:

  • Cryptographically secure random byte generation - 密码学安全的随机字节生成
  • Platform-optimized SecureRandom instances - 平台优化的 SecureRandom 实例

Usage Examples | 使用示例:

byte[] random = SecureRandoms.nextBytes(32);
SecureRandom sr = SecureRandoms.getDefault();

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

    • getDefault

      public static SecureRandom getDefault()
      Gets the default SecureRandom instance. 获取默认的 SecureRandom 实例。
      Returns:
      a new SecureRandom instance
    • getStrong

      public static SecureRandom getStrong()
      Gets a strong SecureRandom instance using the strongest available algorithm. 获取使用最强可用算法的 SecureRandom 实例。
      Returns:
      a strong SecureRandom instance
      Throws:
      IllegalStateException - if no strong random number generator is available
    • getDrbg

      public static SecureRandom getDrbg()
      Gets a DRBG (Deterministic Random Bit Generator) SecureRandom instance with default configuration. 获取默认配置的 DRBG(确定性随机位生成器)SecureRandom 实例。
      Returns:
      a DRBG SecureRandom instance
      Throws:
      IllegalStateException - if DRBG algorithm is not available
    • getDrbg

      public static SecureRandom getDrbg(int strength, DrbgParameters.Capability capability, byte[] personalizationString)
      Gets a DRBG SecureRandom instance with custom configuration. 获取自定义配置的 DRBG SecureRandom 实例。
      Parameters:
      strength - the security strength in bits (112, 128, 192, or 256)
      capability - the DRBG capability
      personalizationString - optional personalization string for DRBG (can be null)
      Returns:
      a configured DRBG SecureRandom instance
      Throws:
      IllegalStateException - if DRBG algorithm is not available
    • getInstance

      public static SecureRandom getInstance(String algorithm)
      Gets a SecureRandom instance for the specified algorithm. 获取指定算法的 SecureRandom 实例。
      Parameters:
      algorithm - the name of the RNG algorithm (e.g., "SHA1PRNG", "NativePRNG")
      Returns:
      a SecureRandom instance
      Throws:
      IllegalArgumentException - if the algorithm is not available
    • getInstance

      public static SecureRandom getInstance(String algorithm, String provider)
      Gets a SecureRandom instance for the specified algorithm and provider. 获取指定算法和提供者的 SecureRandom 实例。
      Parameters:
      algorithm - the name of the RNG algorithm
      provider - the name of the provider
      Returns:
      a SecureRandom instance
      Throws:
      IllegalArgumentException - if the algorithm or provider is not available
    • generateSeed

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

      public static byte[] nextBytes(int numBytes)
      Generates random bytes using a thread-local cached SecureRandom. 使用线程本地缓存的 SecureRandom 生成随机字节。
      Parameters:
      numBytes - the number of bytes to generate | 要生成的字节数
      Returns:
      array of random bytes | 随机字节数组
      Throws:
      IllegalArgumentException - if numBytes is not positive