Apache Commons logo Commons Crypto? logo

Frequently asked questions

How to use a custom secret generator?

Commons Crypto provides the CryptoRandom interface for defining secret generators. The RandomProvider enum in the CryptoRandomFactory defines some sensible default implementations:
OPENSSL
OpenSSL based JNI implementation shipped with Commons Crypto.
JAVA
The SecureRandom implementation from the JVM.
OS
The OS random device implementation. May not be available on some operation systems.
When calling CryptoRandomFactory.getCryptoRandom(), Commons Crypto tries to use the OpenSSL CryptoRandom implementation first. If this fails, the Java implementation is used. In order use a different CryptoRandom implementation (e.g. OS), the CryptoRandomFactory.getCryptoRandom(Properties) method can be used, passing in the desired implementation class names:
Properties props = new Properties();
props.setProperty(CryptoRandomFactory.CLASSES_KEY, CryptoRandomFactory.RandomProvider.OS.getClassName());
CryptoRandom random = CryptoRandomFactory.getCryptoRandom(props);