java.security.egd 作用详解

 更新时间:2023年08月22日 09:34:14   作者:明洋的生活分享  
这篇文章主要为大家介绍了java.security.egd作用详解,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

SecureRandom

在java各种组件中使用广泛,可以可靠的产生随机数。但在大量产生随机数的场景下,性能会较低。这时可以使用"-Djava.security.egd=file:/dev/./urandom"加快随机数产生过程。

以产生uuid的时候使用nextBytes产生随机数为入口,我们看一下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产生下一个随机数的时候调用nextLong或者nextBytes,最终会调用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是一个同步的方法,在多线程使用时,可能会产生性能瓶颈。

synchronized public void nextBytes(byte[] bytes) { 
       secureRandomSpi.engineNextBytes(bytes); 
    }

secureRandomSpi被初始化为sun.security.provider.SecureRandom

secureRandomSpi是SecureRandom.NativePRNG的一个实例。

使用jvm参数-Djava.security.debug=all

可以打印securityprovider列表,从中可以看出,SecureRandom.NativePRNG由sun.security.provider.NativePRNG提供服务。

Provider: Set SUN provider property[SecureRandom.NativePRNG/sun.security.provider.NativePRNG]

分析openjdk的源码,NativePRNG.engineNextBytes调用了NativePRNG.RandomIO.ensureBufferValid,而ensureBufferValid直接从urandom读取数据:

private void ensureBufferValid() throws IOException {
            ...
            readFully(urandomIn, urandomBuffer);
            ...
        }

通过测试可以发现**,hotspot需要使用配置项"-Djava.security.egd=file:/dev/./urandom"才能从urandom读取数据,这里openjdk做了优化,直接从urandom读取数据**。

/dev/random在产生大量随机数的时候比/dev/urandom慢,所以,建议在大量使用随机数的时候,将随机数发生器指定为/dev/./urandom。

注意:jvm参数值为/dev/./urandom而不是/dev/urandom,这里是jdk的一个bug引起。

bug产生的原因

bug产生的原因请注意下面第四行源码,如果java.security.egd参数指定的是file:/dev/random或者file:/dev/urandom,则调用了无参的NativeSeedGenerator构造函数,而无参的构造函数将默认使用file:/dev/random 。

openjdk的代码和hotspot的代码已经不同,openjdk在后续产生随机数的时候没有使用这个变量。

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());
            }
        }
......
    }

在启动应用时配置 -Djava.security.egd=file:/dev/./urandom 可以一定程度上加快应用启动。

以上就是java.security.egd 作用详解的详细内容,更多关于java.security.egd 作用的资料请关注脚本之家其它相关文章!

相关文章

  • 浅谈Spring boot cache使用和原理

    浅谈Spring boot cache使用和原理

    这篇文章主要介绍了浅谈Spring boot cache使用和原理,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-09-09
  • 使用springboot aop来实现读写分离和事物配置

    使用springboot aop来实现读写分离和事物配置

    这篇文章主要介绍了使用springboot aop来实现读写分离和事物配置,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
    2020-04-04
  • SpringBoot解决@Component无法注入其他Bean的问题

    SpringBoot解决@Component无法注入其他Bean的问题

    这篇文章主要介绍了SpringBoot解决@Component无法注入其他Bean的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2021-08-08
  • Java本地缓存的实现代码

    Java本地缓存的实现代码

    本篇文章主要介绍了Java本地缓存的实现代码,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-05-05
  • java8 List<Object>去掉重复对象的几种方法

    java8 List<Object>去掉重复对象的几种方法

    本文主要介绍了java8 List<Object>去掉重复对象的几种方法,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04
  • Java中如何编写一个数的n次方(幂运算)?

    Java中如何编写一个数的n次方(幂运算)?

    本文介绍了使用pow函数和自定义for循环计算幂的O(n)时间复杂度方法,然后重点讲解了快速幂算法的分治思想,以及从二进制角度的解释,包括如何通过位运算和循环迭代实现高效计算,给出了Java代码实现
    2024-07-07
  • Spring Boot实现简单的定时任务

    Spring Boot实现简单的定时任务

    这篇文章主要给大家介绍了关于利用Spring Boot实现简单的定时任务的相关资料,文中通过示例代码介绍的非常详细,对大家的学习或者使用Spring Boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧
    2020-07-07
  • 详解Spring依赖注入的三种方式以及优缺点

    详解Spring依赖注入的三种方式以及优缺点

    IoC 和 DI 是 Spring 中最重要的两个概念,其中 IoC(Inversion of Control)为控制反转的思想,而 DI(Dependency Injection)依赖注入为其(IoC)具体实现。那么 DI 实现依赖注入的方式有几种?这些注入方式又有什么不同?本文就来和大家一起详细聊聊
    2022-08-08
  • java中continue和break区别详细解析

    java中continue和break区别详细解析

    break和continue都是跳转语句,它们将程序的控制权转移到程序的另一部分,下面这篇文章主要给大家介绍了关于java中continue和break区别的相关资料,需要的朋友可以参考下
    2022-11-11
  • SpringBoot实现日志链路追踪的项目实践

    SpringBoot实现日志链路追踪的项目实践

    在分布式系统中,由于请求的处理过程可能会跨越多个服务,因此,对请求的追踪变得尤为重要,本文主要介绍了SpringBoot实现日志链路追踪的项目实践,感兴趣的可以了解一下
    2024-03-03

最新评论