Class OpenSslJnaCryptoRandom

java.lang.Object
org.apache.commons.crypto.jna.OpenSslJnaCryptoRandom
All Implemented Interfaces:
Closeable, AutoCloseable, CryptoRandom

final class OpenSslJnaCryptoRandom extends Object implements CryptoRandom

OpenSSL secure random using JNA. This implementation is thread-safe.

If using an Intel chipset with RDRAND, the high-performance hardware random number generator will be used and it's much faster than SecureRandom. If RDRAND is unavailable, default OpenSSL secure random generator will be used. It's still faster and can generate strong random bytes.

See Also:
  • Field Details

    • ENGINE_METHOD_RAND

      private static final int ENGINE_METHOD_RAND
      See Also:
    • rdrandEnabled

      private final boolean rdrandEnabled
    • rdrandEngine

      private final transient com.sun.jna.ptr.PointerByReference rdrandEngine
  • Constructor Details

  • Method Details

    • close

      public void close()
      Overrides AutoCloseable.close(). Closes OpenSSL context if native enabled.
      Specified by:
      close in interface AutoCloseable
      Specified by:
      close in interface Closeable
    • closeRdrandEngine

      private void closeRdrandEngine(boolean closing)
      Closes the rdrand engine.
      Parameters:
      closing - true when called while closing.
    • isRdrandEnabled

      public boolean isRdrandEnabled()
      Checks if rdrand engine is used to retrieve random bytes
      Returns:
      true if rdrand is used, false if default engine is used
    • nextBytes

      public void nextBytes(byte[] bytes)
      Generates a user-specified number of random bytes. It's thread-safe.
      Specified by:
      nextBytes in interface CryptoRandom
      Parameters:
      bytes - the array to be filled in with random bytes.
    • throwOnError

      private void throwOnError(int retVal, boolean closing)
      Parameters:
      retVal - the result value of error.
      closing - true when called while closing.