RFR 8024253: ThreadLocal random can use SecureRandom for the initial seed

Doug Lea dl at cs.oswego.edu
Mon Sep 16 13:54:08 UTC 2013


On 09/16/2013 08:46 AM, Peter Levart wrote:

> What worries me is that InetAddress.getLocalHost() involves name
> service look-up. It depends on configuration, but on my computer, it takes about
> 5s to evaluate the hostname -> IP mapping the first time the program is run.
>
> NetworkInterface also has one method called getHardwareAddress(). This might me
> interesting too...

Using NetworkInterface.getHardwareAddress() is a good idea; thanks!
Using only one of them should suffice. And just giving up on
SecurityException seems fine.

Could you check that this performs reasonably on your
unusually-configured machine?

  private static long initialSeed() {
         String pp = java.security.AccessController.doPrivileged(
                 new sun.security.action.GetPropertyAction(
                         "java.util.secureRandomSeed"));
         if (pp != null && pp.equalsIgnoreCase("true")) {
             byte[] seedBytes = java.security.SecureRandom.getSeed(8);
             long s = (long)(seedBytes[0]) & 0xffL;
             for (int i = 1; i < 8; ++i)
                 s = (s << 8) | ((long)(seedBytes[i]) & 0xffL);
             return s;
         }
         long h = 0L;
         try {
             Enumeration<NetworkInterface> ifcs =
                 NetworkInterface.getNetworkInterfaces();
             if (ifcs.hasMoreElements()) {
                 byte[] bs = ifcs.nextElement().getHardwareAddress();
                 if (bs != null) {
                     for (int i = 0; i < 8 && i < bs.length; ++i)
                         h = (h << 8) ^ bs[i];
                 }
             }
         } catch (Exception ignore) {
         }
         return (mix64(h ^ System.currentTimeMillis()) ^
                 mix64(System.nanoTime()));






More information about the core-libs-dev mailing list