Java Securerandom
##Java Random Number Generation
In the Linux operating system, there is a special device file that can be used as a random number generator or pseudo-random number generator.
/dev/random
When reading, the/dev/random device will return random bytes that are less than the total noise in the entropy pool. /Dev/random can generate highly random public keys or one-time codebooks. If the entropy pool is empty, read operations to/dev/random will be blocked until sufficient environmental noise is collected from other devices.
Of course, you can also set it to not block. When you open, you can set the parameter ONONBLOCK. However, when you read, if the entropy pool is empty, it will return -1
/dev/urandom
/A copy of dev/random is/dev/urandom (“unlocked”, non blocking random number generator [4]), which repeatedly uses data from the entropy pool to generate pseudo-random data. This means that the read operation on/dev/urandom will not block, but its output entropy may be smaller than that of/dev/random. It can be used as a pseudo-random number generator to generate lower strength passwords and is not recommended for generating high-strength long-term passwords.
##Set the method for generating random numbers
There are two ways to set a specified random number generator in JAVA
- DAva. security. egd=file:/dev/random or - DAva. security. egd=file:/dev/urandom
- Modify the configuration file ‘java. security’ in jvm \ home \ jre \ lib \ security
Parameter securerandom. source=file:/dev/urandom
Even if - DAva. security. egd=file:/dev/urandom is set, the final result is still to read file:/dev/random
1 | if (egdSource.equals(URL_DEV_RANDOM) || egdSource.equals(URL_DEV_URANDOM)){ //走此分支都读/dev/random文件 |
因此要这样设置:-Djava.security.egd=file:/dev/./urandom