Package org.apache.commons.crypto.jna
Class OpenSslJnaCryptoRandom
- java.lang.Object
-
- org.apache.commons.crypto.jna.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.
-
-
Field Summary
Fields Modifier and Type Field Description private static intENGINE_METHOD_RANDprivate booleanrdrandEnabledprivate com.sun.jna.ptr.PointerByReferencerdrandEngine
-
Constructor Summary
Constructors Constructor Description OpenSslJnaCryptoRandom(java.util.Properties props)Constructs aOpenSslJnaCryptoRandom.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()OverridesAutoCloseable.close().private voidcloseRdrandEngine(boolean closing)Closes the rdrand engine.booleanisRdrandEnabled()Checks if rdrand engine is used to retrieve random bytesvoidnextBytes(byte[] bytes)Generates a user-specified number of random bytes.private voidthrowOnError(int retVal, boolean closing)
-
-
-
Field Detail
-
ENGINE_METHOD_RAND
private static final int ENGINE_METHOD_RAND
- See Also:
- Constant Field Values
-
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.GeneralSecurityExceptionConstructs aOpenSslJnaCryptoRandom.- Parameters:
props- the configuration properties (not used)- Throws:
java.security.GeneralSecurityException- if could not enable JNA access
-
-
Method Detail
-
close
public void close()
OverridesAutoCloseable.close(). Closes OpenSSL context if native enabled.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfacejava.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:
nextBytesin interfaceCryptoRandom- 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.
-
-