All Downloads are FREE. Search and download functionalities are using the official Maven repository.

common.crypto.CryptoRandom Maven / Gradle / Ivy

package com.unbound.common.crypto;

import java.math.BigInteger;
import java.security.SecureRandom;

public final class CryptoRandom
{
  private CryptoRandom() {}

  public static SecureRandom instance = new SecureRandom();

  public static byte[] generate(int length)
  {
    byte[] out = new byte[length];
    instance.nextBytes(out);
    return out;
  }

  public static BigInteger generateBigInteger(int bits)
  {
    return new BigInteger(bits, instance);
  }

  public static BigInteger generateBigInteger(BigInteger bound)
  {
    int bits = bound.bitLength();
    for (;;)
    {
      BigInteger result = generateBigInteger(bits);
      if (result.compareTo(bound) < 0) return result;
    }
  }

  public static int generateInt() { return instance.nextInt(); }
  public static int generateInt(int bound) { return instance.nextInt(bound); }
  public static long generateLong() { return instance.nextInt(); }
  public static long generateLong(int bound) { return instance.nextInt(bound); }
  public static boolean generateBoolean() { return instance.nextBoolean(); }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy