If you want a cryptographically strong random numbers in Java, you use SecureRandom
. Unfortunately, SecureRandom
can be very slow. If it uses
Many Linux distros (mostly Debian-based) configure OpenJDK to use /dev/random
for entropy.
/dev/random
is by definition slow (and can even block).
From here you have two options on how to unblock it:
Option 1, Improve entropy
To get more entropy into /dev/random
, try the haveged daemon. It's a daemon that continuously collects HAVEGE entropy, and works also in a virtualized environment because it doesn't require any special hardware, only the CPU itself and a clock.
On Ubuntu/Debian:
apt-get install haveged
update-rc.d haveged defaults
service haveged start
On RHEL/CentOS:
yum install haveged
systemctl enable haveged
systemctl start haveged
Option 2. Reduce randomness requirements
If for some reason the solution above doesn't help or you don't care about cryptographically strong randomness, you can switch to /dev/urandom
instead, which is guaranteed not to block.
To do it globally, edit the file jre/lib/security/java.security
in your default Java installation to use /dev/urandom
(due to another bug it needs to be specified as /dev/./urandom
).
Like this:
#securerandom.source=file:/dev/random
securerandom.source=file:/dev/./urandom
Then you won't ever have to specify it on the command line.
Note: If you do cryptography, you need good entropy. Case in point - android PRNG issue reduced the security of Bitcoin wallets.