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

Paul Sandoz paul.sandoz at oracle.com
Mon Sep 16 14:10:03 UTC 2013


Hi Doug,

Perhaps it would be conservative to find the first non-loopback interface, if any, just in case the first one in the enumeration is a loopback (which will return a null byte array)?

e.g.:

           while (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];
                   break;
               }
           }

Paul.

On Sep 16, 2013, at 3:54 PM, Doug Lea <dl at cs.oswego.edu> wrote:

> 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