Class OpenSslJnaCryptoRandom

  • All Implemented Interfaces:
    java.io.Closeable, java.lang.AutoCloseable, CryptoRandom

    final class OpenSslJnaCryptoRandom
    extends java.lang.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:
    https://wiki.openssl.org/index.php/Random_Numbers, http://en.wikipedia.org/wiki/RdRand
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      void close()
      Overrides AutoCloseable.close().
      private void closeRdrandEngine​(boolean closing)
      Closes the rdrand engine.
      boolean isRdrandEnabled()
      Checks if rdrand engine is used to retrieve random bytes
      void nextBytes​(byte[] bytes)
      Generates a user-specified number of random bytes.
      private void throwOnError​(int retVal, boolean closing)  
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • rdrandEnabled

        private final boolean rdrandEnabled
      • rdrandEngine

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

      • OpenSslJnaCryptoRandom

        public OpenSslJnaCryptoRandom​(java.util.Properties props)
                               throws java.security.GeneralSecurityException
        Parameters:
        props - the configuration properties (not used)
        Throws:
        java.security.GeneralSecurityException - if could not enable JNA access
    • Method Detail

      • close

        public void close()
        Overrides AutoCloseable.close(). Closes OpenSSL context if native enabled.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.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.