Java7在MacOSX上的SecureRandom性能问题

3
我正在MacOSX Mavericks DP3上运行带有jdk7的tomcat7。 一切都很顺利,启动只需要500毫秒。 但突然间变慢了35秒。
日志消息显示SecureRandom是根本原因。 谷歌告诉我这是一个jre bug,并提供以下代码进行验证:
import java.security.SecureRandom;
class JRand {
     public static void main(String args[]) throws Exception {
        System.out.println("Ok: " +
           SecureRandom.getInstance("SHA1PRNG").nextLong());
     }
}

是的,最简单的代码也需要35秒。但似乎所有相关的解决方案都对我无效。在Mac上,/dev/random和/dev/urandom都不是块设备。

cat /dev/random | hexdump -C

输出非常快!

当切换回jre6时,生成随机数非常快。下载最新的jdk8-ea,问题仍然存在。

事实上,不仅是tomcat明显变慢,Netbeans、glassfish也都受到影响。 经过几个小时的奋斗,我最终放弃了。

今天早上,当我来到办公室,插上以太网,猜猜发生了什么? 它恢复了!

所以我的问题是,背后发生了什么?这真的很奇怪。

谢谢。


现在它又出现了。 当调用以下代码时,配置文件工具显示已被阻止:java.net.Inet6AddressImpl.lookupAllHostAddr(hostname)来自java.net.InetAddress.getLocalHost() :'( - outersky
这是一个本地方法。 - outersky
2个回答

1
笑死,问题解决了。 获取 InetAddress.java 源码(可以从 IDE 复制); 修改 getLocalHost 方法。
String local = impl.getLocalHostName();

到:

String local = "localhost"; // impl.getLocalHostName();

重新编译它,并将java.net.InetAddress.class添加回JDK/jre/lib/rt.jar。
已解决。

0

不要更改InetAddress,其他代码可能依赖它。相反,更改sun.security.provider.SeedGenerator :: getSystemEntropy()以不使用本地IP地址。(那到底有多安全呢?)作为额外的奖励,您现在通过模糊性变得略微更加安全 :)


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接