java.security.egd?作用詳解
SecureRandom
在java各種組件中使用廣泛,可以可靠的產(chǎn)生隨機數(shù)。但在大量產(chǎn)生隨機數(shù)的場景下,性能會較低。這時可以使用"-Djava.security.egd=file:/dev/./urandom"加快隨機數(shù)產(chǎn)生過程。
以產(chǎn)生uuid的時候使用nextBytes產(chǎn)生隨機數(shù)為入口,我們看一下SecureRandom的代碼邏輯。
public static UUID randomUUID() { SecureRandom ng =Holder.numberGenerator; byte[] randomBytes = new byte[16]; ng.nextBytes(randomBytes); randomBytes[6] &= 0x0f; /* clear version */ randomBytes[6] |=0x40; /* set to version 4 */ randomBytes[8] &= 0x3f; /* clear variant */ randomBytes[8] |=0x80; /* set to IETF variant */ return newUUID(randomBytes); }
使用SecureRandom.next*的方法
在使用SecureRandom產(chǎn)生下一個隨機數(shù)的時候調(diào)用nextLong或者nextBytes,最終會調(diào)用SecureRandom的nextBytes。
public long nextLong() { // it's okay that the bottom wordremains signed. return ((long)(next(32)) << 32)+ next(32); } final protected int next(int numBits) { int numBytes = (numBits+7)/8; byte b[] = new byte[numBytes]; int next = 0; nextBytes(b); for (int i = 0; i < numBytes; i++) next = (next << 8)+ (b[i] & 0xFF); return next >>> (numBytes*8 -numBits); }
而nextBytes是一個同步的方法,在多線程使用時,可能會產(chǎn)生性能瓶頸。
synchronized public void nextBytes(byte[] bytes) { secureRandomSpi.engineNextBytes(bytes); }
secureRandomSpi被初始化為sun.security.provider.SecureRandom
secureRandomSpi是SecureRandom.NativePRNG的一個實例。
使用jvm參數(shù)-Djava.security.debug=all
可以打印securityprovider列表,從中可以看出,SecureRandom.NativePRNG由sun.security.provider.NativePRNG提供服務(wù)。
Provider: Set SUN provider property[SecureRandom.NativePRNG/sun.security.provider.NativePRNG]
分析openjdk的源碼,NativePRNG.engineNextBytes調(diào)用了NativePRNG.RandomIO.ensureBufferValid,而ensureBufferValid直接從urandom讀取數(shù)據(jù):
private void ensureBufferValid() throws IOException { ... readFully(urandomIn, urandomBuffer); ... }
通過測試可以發(fā)現(xiàn)**,hotspot需要使用配置項"-Djava.security.egd=file:/dev/./urandom"才能從urandom讀取數(shù)據(jù),這里openjdk做了優(yōu)化,直接從urandom讀取數(shù)據(jù)**。
/dev/random在產(chǎn)生大量隨機數(shù)的時候比/dev/urandom慢,所以,建議在大量使用隨機數(shù)的時候,將隨機數(shù)發(fā)生器指定為/dev/./urandom。
注意:jvm參數(shù)值為/dev/./urandom而不是/dev/urandom,這里是jdk的一個bug引起。
bug產(chǎn)生的原因
bug產(chǎn)生的原因請注意下面第四行源碼,如果java.security.egd參數(shù)指定的是file:/dev/random或者file:/dev/urandom,則調(diào)用了無參的NativeSeedGenerator構(gòu)造函數(shù),而無參的構(gòu)造函數(shù)將默認使用file:/dev/random 。
openjdk的代碼和hotspot的代碼已經(jīng)不同,openjdk在后續(xù)產(chǎn)生隨機數(shù)的時候沒有使用這個變量。
abstract class SeedGenerator { ...... static { String egdSource = SunEntries.getSeedSource(); if (egdSource.equals(URL_DEV_RANDOM) || egdSource.equals(URL_DEV_URANDOM)) { try { instance = new NativeSeedGenerator(); if (debug != null) { debug.println("Using operating system seed generator"); } } catch (IOException e) { if (debug != null) { debug.println("Failed to use operating system seed " + "generator: " + e.toString()); } } } else if (egdSource.length() != 0) { try { instance = new URLSeedGenerator(egdSource); if (debug != null) { debug.println("Using URL seed generator reading from " + egdSource); } } catch (IOException e) { if (debug != null) debug.println("Failed to create seed generator with " + egdSource + ": " + e.toString()); } } ...... }
在啟動應(yīng)用時配置 -Djava.security.egd=file:/dev/./urandom 可以一定程度上加快應(yīng)用啟動。
以上就是java.security.egd 作用詳解的詳細內(nèi)容,更多關(guān)于java.security.egd 作用的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
使用springboot aop來實現(xiàn)讀寫分離和事物配置
這篇文章主要介紹了使用springboot aop來實現(xiàn)讀寫分離和事物配置,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-04-04SpringBoot解決@Component無法注入其他Bean的問題
這篇文章主要介紹了SpringBoot解決@Component無法注入其他Bean的問題,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-08-08java8 List<Object>去掉重復(fù)對象的幾種方法
本文主要介紹了java8 List<Object>去掉重復(fù)對象的幾種方法,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2022-04-04Spring Boot實現(xiàn)簡單的定時任務(wù)
這篇文章主要給大家介紹了關(guān)于利用Spring Boot實現(xiàn)簡單的定時任務(wù)的相關(guān)資料,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者使用Spring Boot具有一定的參考學(xué)習(xí)價值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07