Class OpenSslCryptoRandom

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

    final class OpenSslCryptoRandom
    extends java.lang.Object
    implements CryptoRandom

    OpenSSL secure random using JNI. 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.

    This class is not public/protected so does not appear in the main Javadoc Please ensure that property use is documented in the enum CryptoRandomFactory.RandomProvider

    See Also:
    https://wiki.openssl.org/index.php/Random_Numbers, http://en.wikipedia.org/wiki/RdRand
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static java.lang.Throwable initException  
      private static boolean nativeEnabled  
    • Method Summary

      All Methods Static Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private static void checkNative()  
      void close()
      Overrides AutoCloseable.close().
      static boolean isNativeCodeEnabled()
      Judges whether native library was successfully loaded and initialized.
      void nextBytes​(byte[] bytes)
      Generates a user-specified number of random bytes.
      • Methods inherited from class java.lang.Object

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

      • nativeEnabled

        private static final boolean nativeEnabled
      • initException

        private static final java.lang.Throwable initException
    • Constructor Detail

      • OpenSslCryptoRandom

        public OpenSslCryptoRandom​(java.util.Properties props)
                            throws java.security.GeneralSecurityException
        Constructs a OpenSslCryptoRandom.
        Parameters:
        props - the configuration properties - not used
        Throws:
        java.security.GeneralSecurityException - if the native library could not be initialized successfully
    • Method Detail

      • checkNative

        private static void checkNative()
                                 throws java.security.GeneralSecurityException
        Throws:
        java.security.GeneralSecurityException
      • isNativeCodeEnabled

        public static boolean isNativeCodeEnabled()
        Judges whether native library was successfully loaded and initialized.
        Returns:
        true if library was loaded and initialized
      • close

        public void close()
        Overrides AutoCloseable.close(). Does nothing.
        Specified by:
        close in interface java.lang.AutoCloseable
        Specified by:
        close in interface java.io.Closeable
      • nextBytes

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