{"id":10008112,"date":"2023-08-18T10:43:49","date_gmt":"2023-08-18T02:43:49","guid":{"rendered":"https:\/\/blog.itmax.cn\/?p=10008112"},"modified":"2023-08-18T10:43:49","modified_gmt":"2023-08-18T02:43:49","slug":"%e8%81%8a%e8%81%8ajava%e7%9a%84java-security-egd","status":"publish","type":"post","link":"https:\/\/itmax.cn\/?p=10008112","title":{"rendered":"\u804a\u804ajava\u7684java.security.egd"},"content":{"rendered":"<h2>\u5e8f<\/h2>\n<p>\u672c\u6587\u4e3b\u8981\u7814\u7a76\u4e00\u4e0bjava\u7684java.security.egd<\/p>\n<h2>SunEntries<\/h2>\n<p>\/Library\/Java\/JavaVirtualMachines\/temurin-8.jdk\/Contents\/Home\/src.zip!\/sun\/security\/provider\/SunEntries.java<\/p>\n<pre><code>    \/\/ name of the *System* property, takes precedence over PROP_RNDSOURCE\n    private final static String PROP_EGD = &quot;java.security.egd&quot;;\n    \/\/ name of the *Security* property\n    private final static String PROP_RNDSOURCE = &quot;securerandom.source&quot;;\n\n    final static String URL_DEV_RANDOM = &quot;file:\/dev\/random&quot;;\n    final static String URL_DEV_URANDOM = &quot;file:\/dev\/urandom&quot;;\n\n    private static final String seedSource;\n\n    static {\n        seedSource = AccessController.doPrivileged(\n                new PrivilegedAction&lt;String&gt;() {\n\n            @Override\n            public String run() {\n                String egdSource = System.getProperty(PROP_EGD, &quot;&quot;);\n                if (egdSource.length() != 0) {\n                    return egdSource;\n                }\n                egdSource = Security.getProperty(PROP_RNDSOURCE);\n                if (egdSource == null) {\n                    return &quot;&quot;;\n                }\n                return egdSource;\n            }\n        });\n    }<\/code><\/pre>\n<blockquote><p>\u8fd9\u91cc\u4f18\u5148\u8bfb\u53d6<code>java.security.egd<\/code>\uff0c\u5982\u679c\u6ca1\u6709\u8bbe\u7f6e\u5219\u8bfb\u53d6<code>$JAVA_HOME\/jre\/lib\/security\/java.security<\/code>\u6587\u4ef6\u4e2d\u7684<code>securerandom.source<\/code>\u914d\u7f6e\uff0c\u9ed8\u8ba4\u503c\u4e3a<code>file:\/dev\/random<\/code><\/p><\/blockquote>\n<h2>SeedGenerator<\/h2>\n<p>\/Library\/Java\/JavaVirtualMachines\/temurin-8.jdk\/Contents\/Home\/src.zip!\/sun\/security\/provider\/SeedGenerator.java<\/p>\n<pre><code>    \/\/ Static initializer to hook in selected or best performing generator\n    static {\n        String egdSource = SunEntries.getSeedSource();\n\n        \/*\n         * Try the URL specifying the source (e.g. file:\/dev\/random)\n         *\n         * The URLs &quot;file:\/dev\/random&quot; or &quot;file:\/dev\/urandom&quot; are used to\n         * indicate the SeedGenerator should use OS support, if available.\n         *\n         * On Windows, this causes the MS CryptoAPI seeder to be used.\n         *\n         * On Solaris\/Linux\/MacOS, this is identical to using\n         * URLSeedGenerator to read from \/dev\/[u]random\n         *\/\n        if (egdSource.equals(SunEntries.URL_DEV_RANDOM) ||\n                egdSource.equals(SunEntries.URL_DEV_URANDOM)) {\n            try {\n                instance = new NativeSeedGenerator(egdSource);\n                if (debug != null) {\n                    debug.println(\n                        &quot;Using operating system seed generator&quot; + egdSource);\n                }\n            } catch (IOException e) {\n                if (debug != null) {\n                    debug.println(&quot;Failed to use operating system seed &quot;\n                                  + &quot;generator: &quot; + e.toString());\n                }\n            }\n        } else if (egdSource.length() != 0) {\n            try {\n                instance = new URLSeedGenerator(egdSource);\n                if (debug != null) {\n                    debug.println(&quot;Using URL seed generator reading from &quot;\n                                  + egdSource);\n                }\n            } catch (IOException e) {\n                if (debug != null) {\n                    debug.println(&quot;Failed to create seed generator with &quot;\n                                  + egdSource + &quot;: &quot; + e.toString());\n                }\n            }\n        }\n\n        \/\/ Fall back to ThreadedSeedGenerator\n        if (instance == null) {\n            if (debug != null) {\n                debug.println(&quot;Using default threaded seed generator&quot;);\n            }\n            instance = new ThreadedSeedGenerator();\n        }\n    }<\/code><\/pre>\n<blockquote><p>\u5982\u679c\u662f<code>file:\/dev\/random<\/code>\u53ca<code>file:\/dev\/urandom<\/code>\u5219\u8d70NativeSeedGenerator\uff0c\u4e0d\u662f\u5219\u8d70URLSeedGenerator\uff0c\u4e3a\u7a7a\u5219\u8d70ThreadedSeedGenerator<\/p><\/blockquote>\n<h2>NativeSeedGenerator<\/h2>\n<pre><code>\/**\n * Native seed generator for Unix systems. Inherit everything from\n * URLSeedGenerator.\n *\n *\/\nclass NativeSeedGenerator extends SeedGenerator.URLSeedGenerator {\n\n    NativeSeedGenerator(String seedFile) throws IOException {\n        super(seedFile);\n    }\n\n}<\/code><\/pre>\n<blockquote><p>NativeSeedGenerator\u7ee7\u627f\u4e86URLSeedGenerator<\/p><\/blockquote>\n<h2>SecureRandomSpi<\/h2>\n<h3>NativePRNG<\/h3>\n<p>\/Library\/Java\/JavaVirtualMachines\/temurin-8.jdk\/Contents\/Home\/src.zip!\/sun\/security\/provider\/NativePRNG.java<\/p>\n<pre><code>public final class NativePRNG extends SecureRandomSpi {\n\n    private static final long serialVersionUID = -6599091113397072932L;\n\n    private static final Debug debug = Debug.getInstance(&quot;provider&quot;);\n\n    \/\/ name of the pure random file (also used for setSeed())\n    private static final String NAME_RANDOM = &quot;\/dev\/random&quot;;\n    \/\/ name of the pseudo random file\n    private static final String NAME_URANDOM = &quot;\/dev\/urandom&quot;;\n\n    \/\/ which kind of RandomIO object are we creating?\n    private enum Variant {\n        MIXED, BLOCKING, NONBLOCKING\n    }\n\n    \/\/ singleton instance or null if not available\n    private static final RandomIO INSTANCE = initIO(Variant.MIXED);\n\n    \/**\n     * Get the System egd source (if defined).  We only allow &quot;file:&quot;\n     * URLs for now. If there is a egd value, parse it.\n     *\n     * @return the URL or null if not available.\n     *\/\n    private static URL getEgdUrl() {\n        \/\/ This will return &quot;&quot; if nothing was set.\n        String egdSource = SunEntries.getSeedSource();\n        URL egdUrl;\n\n        if (egdSource.length() != 0) {\n            if (debug != null) {\n                debug.println(&quot;NativePRNG egdUrl: &quot; + egdSource);\n            }\n            try {\n                egdUrl = new URL(egdSource);\n                if (!egdUrl.getProtocol().equalsIgnoreCase(&quot;file&quot;)) {\n                    return null;\n                }\n            } catch (MalformedURLException e) {\n                return null;\n            }\n        } else {\n            egdUrl = null;\n        }\n\n        return egdUrl;\n    }\n\n    \/\/......\n}    <\/code><\/pre>\n<blockquote><p>NativePRNG\u7684getEgdUrl\u5219\u901a\u8fc7egdSource\u6765\u6784\u5efaURL<\/p><\/blockquote>\n<h2>\u5c0f\u7ed3<\/h2>\n<ul>\n<li>SunEntries\u4f18\u5148\u8bfb\u53d6<code>java.security.egd<\/code>\uff0c\u5982\u679c\u6ca1\u6709\u8bbe\u7f6e\u5219\u8bfb\u53d6<code>$JAVA_HOME\/jre\/lib\/security\/java.security<\/code>\u6587\u4ef6\u4e2d\u7684<code>securerandom.source<\/code>\u914d\u7f6e\uff0c\u9ed8\u8ba4\u503c\u4e3a<code>file:\/dev\/random<\/code><\/li>\n<li>SeedGenerator\u5224\u65adegdSource\u5982\u679c\u662f<code>file:\/dev\/random<\/code>\u53ca<code>file:\/dev\/urandom<\/code>\u5219\u8d70NativeSeedGenerator\uff0c\u4e0d\u662f\u5219\u8d70URLSeedGenerator\uff0c\u4e3a\u7a7a\u5219\u8d70ThreadedSeedGenerator<\/li>\n<li>\u81f3\u4e8e<code>\/dev\/.\/urandom<\/code>\u8fd9\u79cd\u8868\u793a\u770b\u8d77\u6765\u6bd4\u8f83\u56f0\u60d1\uff0c\u7ffb\u8bd1\u8fc7\u6765\u5c31\u662f\u662f<code>\/dev<\/code>\u5f53\u524d\u76ee\u5f55\u4e0b\u7684<code>unrandom<\/code>\uff0c\u5176\u5b9e\u5c31\u662f<code>\/dev\/urandom<\/code>\uff0c\u4e4b\u6240\u4ee5\u6709\u8fd9\u79cd\u4f20\u53c2\u4e3b\u8981\u662f\u65e9\u671fjdk\u7248\u672c\u6709\u4e2abug\uff0c\u6ca1\u6709\u7ed9NativeSeedGenerator\u4f20\u53c2\uff0c\u6240\u4ee5\u901a\u8fc7<code>file:\/dev\/.\/urandom<\/code>\u7ed5\u8fc7\u8fd9\u4e2abug<\/li>\n<\/ul>\n<h2>doc<\/h2>\n<ul>\n<li><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=W2339YzeALgKHxZyEuY9mg%3D%3D.Zmgj29%2FcdeU3XLXmP8CtKkqvvZDuxu8FYnQpg4GyP2rH7KQ6UeQVupx9PuU16rD6\" rel=\"noopener noreferrer\">The java.security.egd JVM Option<\/a><\/li>\n<li><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=eCwWBLFe9rdI8pGMEpPXew%3D%3D.pu9jbSP8PKWkaipy1BzQMpAzZyDEyjQoLYscjUbw6WJ52DnEGxs5MLY3B6cCQEo75V6dNtQKI38163Onszq3SrC5GDQ06%2B8pR1UWpOZSi5bNxeqjerewL5KRuXovLeiX\" rel=\"noopener noreferrer\">Better Support for High Entropy Random Number Generation<\/a><\/li>\n<li><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=POWQ35Q6AhH2g82pEgKvsw%3D%3D.l6WBjfN%2Bg99btqNSOSV3NF4YdVX57mbFAtakGyhRXR8%3D\" rel=\"noopener noreferrer\">JEP 123: Configurable Secure Random-Number Generation<\/a><\/li>\n<li><a target=\"_blank\" href=\"https:\/\/link.segmentfault.com\/?enc=gIhh8JoiZWQdfBl%2BiPOeJw%3D%3D.5rJcUV2RBzqzmeeCBpIlptypMF0IHBJJr51EZTnUkzPYaBgtD5e6aIj82sVLv6TBZxzDOKGHDWp7x6z5lze2sA%3D%3D\" rel=\"noopener noreferrer\">java.security.egd \u4f5c\u7528<\/a><\/li>\n<\/ul>\n<p><a href=\"https:\/\/segmentfault.com\/a\/1190000044125684\">https:\/\/segmentfault.com\/a\/1190000044125684<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5e8f \u672c\u6587\u4e3b\u8981\u7814\u7a76\u4e00\u4e0bjava\u7684java.security.egd SunEntries \/Library\/Ja [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_jetpack_memberships_contains_paid_content":false,"footnotes":""},"categories":[4],"tags":[],"class_list":["post-10008112","post","type-post","status-publish","format-standard","hentry","category-share"],"jetpack_sharing_enabled":true,"jetpack_featured_media_url":"","_links":{"self":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008112","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=10008112"}],"version-history":[{"count":0,"href":"https:\/\/itmax.cn\/index.php?rest_route=\/wp\/v2\/posts\/10008112\/revisions"}],"wp:attachment":[{"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=10008112"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=10008112"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/itmax.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=10008112"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}