java的java.security.egd源码解读

 更新时间:2023年08月22日 09:38:45   作者:codecraft  
这篇文章主要为大家介绍了java的java.security.egd源码解析,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

本文主要研究一下java的java.security.egd

SunEntries

/Library/Java/JavaVirtualMachines/temurin-8.jdk/Contents/Home/src.zip!/sun/security/provider/SunEntries.java

// name of the *System* property, takes precedence over PROP_RNDSOURCE
    private final static String PROP_EGD = "java.security.egd";
    // name of the *Security* property
    private final static String PROP_RNDSOURCE = "securerandom.source";
    final static String URL_DEV_RANDOM = "file:/dev/random";
    final static String URL_DEV_URANDOM = "file:/dev/urandom";
    private static final String seedSource;
    static {
        seedSource = AccessController.doPrivileged(
                new PrivilegedAction<String>() {
            @Override
            public String run() {
                String egdSource = System.getProperty(PROP_EGD, "");
                if (egdSource.length() != 0) {
                    return egdSource;
                }
                egdSource = Security.getProperty(PROP_RNDSOURCE);
                if (egdSource == null) {
                    return "";
                }
                return egdSource;
            }
        });
    }

 这里优先读取java.security.egd,如果没有设置则读取$JAVA_HOME/jre/lib/security/java.security文件中的securerandom.source配置,默认值为file:/dev/random

SeedGenerator

/Library/Java/JavaVirtualMachines/temurin-8.jdk/Contents/Home/src.zip!/sun/security/provider/SeedGenerator.java

// Static initializer to hook in selected or best performing generator
    static {
        String egdSource = SunEntries.getSeedSource();
        /*
         * Try the URL specifying the source (e.g. file:/dev/random)
         *
         * The URLs "file:/dev/random" or "file:/dev/urandom" are used to
         * indicate the SeedGenerator should use OS support, if available.
         *
         * On Windows, this causes the MS CryptoAPI seeder to be used.
         *
         * On Solaris/Linux/MacOS, this is identical to using
         * URLSeedGenerator to read from /dev/[u]random
         */
        if (egdSource.equals(SunEntries.URL_DEV_RANDOM) ||
                egdSource.equals(SunEntries.URL_DEV_URANDOM)) {
            try {
                instance = new NativeSeedGenerator(egdSource);
                if (debug != null) {
                    debug.println(
                        "Using operating system seed generator" + egdSource);
                }
            } 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());
                }
            }
        }
        // Fall back to ThreadedSeedGenerator
        if (instance == null) {
            if (debug != null) {
                debug.println("Using default threaded seed generator");
            }
            instance = new ThreadedSeedGenerator();
        }
    }

 如果是file:/dev/randomfile:/dev/urandom则走NativeSeedGenerator,不是则走URLSeedGenerator,为空则走ThreadedSeedGenerator

NativeSeedGenerator

/**
 * Native seed generator for Unix systems. Inherit everything from
 * URLSeedGenerator.
 *
 */
class NativeSeedGenerator extends SeedGenerator.URLSeedGenerator {
    NativeSeedGenerator(String seedFile) throws IOException {
        super(seedFile);
    }
}
NativeSeedGenerator继承了URLSeedGenerator

SecureRandomSpi

NativePRNG

/Library/Java/JavaVirtualMachines/temurin-8.jdk/Contents/Home/src.zip!/sun/security/provider/NativePRNG.java

public final class NativePRNG extends SecureRandomSpi {
    private static final long serialVersionUID = -6599091113397072932L;
    private static final Debug debug = Debug.getInstance("provider");
    // name of the pure random file (also used for setSeed())
    private static final String NAME_RANDOM = "/dev/random";
    // name of the pseudo random file
    private static final String NAME_URANDOM = "/dev/urandom";
    // which kind of RandomIO object are we creating?
    private enum Variant {
        MIXED, BLOCKING, NONBLOCKING
    }
    // singleton instance or null if not available
    private static final RandomIO INSTANCE = initIO(Variant.MIXED);
    /**
     * Get the System egd source (if defined).  We only allow "file:"
     * URLs for now. If there is a egd value, parse it.
     *
     * @return the URL or null if not available.
     */
    private static URL getEgdUrl() {
        // This will return "" if nothing was set.
        String egdSource = SunEntries.getSeedSource();
        URL egdUrl;
        if (egdSource.length() != 0) {
            if (debug != null) {
                debug.println("NativePRNG egdUrl: " + egdSource);
            }
            try {
                egdUrl = new URL(egdSource);
                if (!egdUrl.getProtocol().equalsIgnoreCase("file")) {
                    return null;
                }
            } catch (MalformedURLException e) {
                return null;
            }
        } else {
            egdUrl = null;
        }
        return egdUrl;
    }
    //......
}

 NativePRNG的getEgdUrl则通过egdSource来构建URL

小结

  • SunEntries优先读取java.security.egd,如果没有设置则读取$JAVA_HOME/jre/lib/security/java.security文件中的securerandom.source配置,默认值为file:/dev/random
  • SeedGenerator判断egdSource如果是file:/dev/randomfile:/dev/urandom则走NativeSeedGenerator,不是则走URLSeedGenerator,为空则走ThreadedSeedGenerator
  • 至于/dev/./urandom这种表示看起来比较困惑,翻译过来就是是/dev当前目录下的unrandom,其实就是/dev/urandom,之所以有这种传参主要是早期jdk版本有个bug,没有给NativeSeedGenerator传参,所以通过file:/dev/./urandom绕过这个bug

doc

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

相关文章

  • 使用Spring Cache和Redis实现查询数据缓存

    使用Spring Cache和Redis实现查询数据缓存

    在现代应用程序中,查询缓存的使用已经变得越来越普遍,它不仅能够显著提高系统的性能,还能提升用户体验,在这篇文章中,我们将探讨缓存的基本概念、重要性以及如何使用Spring Cache和Redis实现查询数据缓存,需要的朋友可以参考下
    2024-07-07
  • Java的方法和this关键字如何理解与应用

    Java的方法和this关键字如何理解与应用

    Java语言中的“方法”(Method)在其他语言当中也可能被称为“函数”(Function)。对于一些复杂的代码逻辑,如果希望重复使用这些代码,并且做到“随时任意使用”,那么就可以将这些代码放在一个大括号{}当中,并且起一个名字。使用代码的时候,直接找到名字调用即可
    2021-10-10
  • Elasticsearch term 查询之精确值搜索功能实现

    Elasticsearch term 查询之精确值搜索功能实现

    term查询是Elasticsearch中用于精确值搜索的一种基本方式,通过了解 term 查询的工作原理和使用方法,你可以更好地利用 Elasticsearch 进行结构化数据的搜索和分析,本文将详细介绍 term 查询的工作原理、使用场景以及如何在 Elasticsearch 中应用它,感兴趣的朋友一起看看吧
    2024-06-06
  • 记一次Feign中实现传实体Bean的问题

    记一次Feign中实现传实体Bean的问题

    这篇文章主要介绍了记一次Feign中如何传实体Bean的问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-03-03
  • 聊一聊new对象与Spring对bean的初始化的差别

    聊一聊new对象与Spring对bean的初始化的差别

    这篇文章主要介绍了聊一聊new对象与Spring对bean的初始化的差别,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-02-02
  • Java 泛型(Generic)简介及用法详解

    Java 泛型(Generic)简介及用法详解

    泛型是一种把类型明确的工作推迟到创建对象或者调用方法的时候才去明确的特殊的类型,参数化类型,把类型当作参数一样的传递,本文给大家介绍Java 泛型(Generic)概述及使用,感兴趣的朋友跟随小编一起看看吧
    2023-10-10
  • Java实现拖拽列表项的排序功能

    Java实现拖拽列表项的排序功能

    这篇文章主要介绍了Java实现拖拽列表项的排序功能,非常不错,具有参考借鉴价值,需要的朋友可以参考下
    2017-02-02
  • Java实现JDBC连接数据库简单案例

    Java实现JDBC连接数据库简单案例

    这篇文章主要介绍了Java实现JDBC连接数据库简单案例,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2018-08-08
  • java判断某个点是否在所画多边形/圆形内

    java判断某个点是否在所画多边形/圆形内

    这篇文章主要为大家详细介绍了java判断某个点是否在所画多边形或圆形内的方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
    2018-05-05
  • Feign实现多文件上传,Open Feign多文件上传问题及解决

    Feign实现多文件上传,Open Feign多文件上传问题及解决

    这篇文章主要介绍了Feign实现多文件上传,Open Feign多文件上传问题及解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-11-11

最新评论