Package org.apache.commons.crypto.jna
Class OpenSslJnaCryptoRandom
java.lang.Object
org.apache.commons.crypto.jna.OpenSslJnaCryptoRandom
- All Implemented Interfaces:
Closeable
,AutoCloseable
,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 Summary
FieldsModifier and TypeFieldDescriptionprivate static final int
private final boolean
private final com.sun.jna.ptr.PointerByReference
-
Constructor Summary
ConstructorsConstructorDescriptionOpenSslJnaCryptoRandom
(Properties props) Constructs aOpenSslJnaCryptoRandom
. -
Method Summary
Modifier and TypeMethodDescriptionvoid
close()
OverridesAutoCloseable.close()
.private void
closeRdrandEngine
(boolean closing) Closes the rdrand engine.boolean
Checks if rdrand engine is used to retrieve random bytesvoid
nextBytes
(byte[] bytes) Generates a user-specified number of random bytes.private void
throwOnError
(int retVal, boolean closing)
-
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
-
OpenSslJnaCryptoRandom
Constructs aOpenSslJnaCryptoRandom
.- Parameters:
props
- the configuration properties (not used)- Throws:
GeneralSecurityException
- if could not enable JNA access
-
-
Method Details
-
close
public void close()OverridesAutoCloseable.close()
. Closes OpenSSL context if native enabled.- Specified by:
close
in interfaceAutoCloseable
- Specified by:
close
in interfaceCloseable
-
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 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.
-