< prev index next >

src/java.base/share/classes/sun/security/provider/SeedGenerator.java

Print this page




 148     abstract void getSeedBytes(byte[] result);
 149 
 150     /**
 151      * Retrieve some system information, hashed.
 152      */
 153     static byte[] getSystemEntropy() {
 154         final MessageDigest md;
 155 
 156         try {
 157             md = MessageDigest.getInstance("SHA");
 158         } catch (NoSuchAlgorithmException nsae) {
 159             throw new InternalError("internal error: SHA-1 not available.",
 160                     nsae);
 161         }
 162 
 163         // The current time in millis
 164         byte b =(byte)System.currentTimeMillis();
 165         md.update(b);
 166 
 167         java.security.AccessController.doPrivileged
 168             (new java.security.PrivilegedAction<Void>() {
 169                 @Override
 170                 public Void run() {
 171                     try {
 172                         // System properties can change from machine to machine
 173                         Properties p = System.getProperties();
 174                         for (String s: p.stringPropertyNames()) {
 175                             md.update(s.getBytes());
 176                             md.update(p.getProperty(s).getBytes());
 177                         }
 178 
 179                         // Include network adapter names (and a Mac address)
 180                         addNetworkAdapterInfo(md);
 181 
 182                         // The temporary dir
 183                         File f = new File(p.getProperty("java.io.tmpdir"));
 184                         int count = 0;
 185                         try (
 186                             DirectoryStream<Path> stream =
 187                                 Files.newDirectoryStream(f.toPath())) {
 188                             // We use a Random object to choose what file names


 278         /**
 279          * The constructor is only called once to construct the one
 280          * instance we actually use. It instantiates the message digest
 281          * and starts the thread going.
 282          */
 283         ThreadedSeedGenerator() {
 284             pool = new byte[20];
 285             start = end = 0;
 286 
 287             MessageDigest digest;
 288 
 289             try {
 290                 digest = MessageDigest.getInstance("SHA");
 291             } catch (NoSuchAlgorithmException e) {
 292                 throw new InternalError("internal error: SHA-1 not available."
 293                         , e);
 294             }
 295 
 296             final ThreadGroup[] finalsg = new ThreadGroup[1];
 297             Thread t = java.security.AccessController.doPrivileged
 298                 (new java.security.PrivilegedAction<Thread>() {
 299                         @Override
 300                         public Thread run() {
 301                             ThreadGroup parent, group =
 302                                 Thread.currentThread().getThreadGroup();
 303                             while ((parent = group.getParent()) != null) {
 304                                 group = parent;
 305                             }
 306                             finalsg[0] = new ThreadGroup
 307                                 (group, "SeedGenerator ThreadGroup");
 308                             Thread newT = new ManagedLocalsThread(finalsg[0],
 309                                 ThreadedSeedGenerator.this,
 310                                 "SeedGenerator Thread");
 311                             newT.setPriority(Thread.MIN_PRIORITY);
 312                             newT.setDaemon(true);
 313                             return newT;
 314                         }
 315                     });
 316             seedGroup = finalsg[0];
 317             t.start();
 318         }


 484         private InputStream seedStream;
 485 
 486         /**
 487          * The constructor is only called once to construct the one
 488          * instance we actually use. It opens the entropy gathering device
 489          * which will supply the randomness.
 490          */
 491 
 492         URLSeedGenerator(String egdurl) throws IOException {
 493         if (egdurl == null) {
 494                 throw new IOException("No random source specified");
 495             }
 496             deviceName = egdurl;
 497             init();
 498         }
 499 
 500         private void init() throws IOException {
 501             final URL device = new URL(deviceName);
 502             try {
 503                 seedStream = java.security.AccessController.doPrivileged
 504                     (new java.security.PrivilegedExceptionAction<InputStream>() {
 505                         @Override
 506                         public InputStream run() throws IOException {
 507                             /*
 508                              * return a shared InputStream for file URLs and
 509                              * avoid buffering.
 510                              * The URL.openStream() call wraps InputStream in a
 511                              * BufferedInputStream which
 512                              * can buffer up to 8K bytes. This read is a
 513                              * performance issue for entropy sources which
 514                              * can be slow to replenish.
 515                              */
 516                             if (device.getProtocol().equalsIgnoreCase("file")) {
 517                                 File deviceFile =
 518                                     SunEntries.getDeviceFile(device);
 519                                 return FileInputStreamPool
 520                                     .getInputStream(deviceFile);
 521                             } else {
 522                                 return device.openStream();
 523                             }
 524                         }




 148     abstract void getSeedBytes(byte[] result);
 149 
 150     /**
 151      * Retrieve some system information, hashed.
 152      */
 153     static byte[] getSystemEntropy() {
 154         final MessageDigest md;
 155 
 156         try {
 157             md = MessageDigest.getInstance("SHA");
 158         } catch (NoSuchAlgorithmException nsae) {
 159             throw new InternalError("internal error: SHA-1 not available.",
 160                     nsae);
 161         }
 162 
 163         // The current time in millis
 164         byte b =(byte)System.currentTimeMillis();
 165         md.update(b);
 166 
 167         java.security.AccessController.doPrivileged
 168             (new java.security.PrivilegedAction<>() {
 169                 @Override
 170                 public Void run() {
 171                     try {
 172                         // System properties can change from machine to machine
 173                         Properties p = System.getProperties();
 174                         for (String s: p.stringPropertyNames()) {
 175                             md.update(s.getBytes());
 176                             md.update(p.getProperty(s).getBytes());
 177                         }
 178 
 179                         // Include network adapter names (and a Mac address)
 180                         addNetworkAdapterInfo(md);
 181 
 182                         // The temporary dir
 183                         File f = new File(p.getProperty("java.io.tmpdir"));
 184                         int count = 0;
 185                         try (
 186                             DirectoryStream<Path> stream =
 187                                 Files.newDirectoryStream(f.toPath())) {
 188                             // We use a Random object to choose what file names


 278         /**
 279          * The constructor is only called once to construct the one
 280          * instance we actually use. It instantiates the message digest
 281          * and starts the thread going.
 282          */
 283         ThreadedSeedGenerator() {
 284             pool = new byte[20];
 285             start = end = 0;
 286 
 287             MessageDigest digest;
 288 
 289             try {
 290                 digest = MessageDigest.getInstance("SHA");
 291             } catch (NoSuchAlgorithmException e) {
 292                 throw new InternalError("internal error: SHA-1 not available."
 293                         , e);
 294             }
 295 
 296             final ThreadGroup[] finalsg = new ThreadGroup[1];
 297             Thread t = java.security.AccessController.doPrivileged
 298                 (new java.security.PrivilegedAction<>() {
 299                         @Override
 300                         public Thread run() {
 301                             ThreadGroup parent, group =
 302                                 Thread.currentThread().getThreadGroup();
 303                             while ((parent = group.getParent()) != null) {
 304                                 group = parent;
 305                             }
 306                             finalsg[0] = new ThreadGroup
 307                                 (group, "SeedGenerator ThreadGroup");
 308                             Thread newT = new ManagedLocalsThread(finalsg[0],
 309                                 ThreadedSeedGenerator.this,
 310                                 "SeedGenerator Thread");
 311                             newT.setPriority(Thread.MIN_PRIORITY);
 312                             newT.setDaemon(true);
 313                             return newT;
 314                         }
 315                     });
 316             seedGroup = finalsg[0];
 317             t.start();
 318         }


 484         private InputStream seedStream;
 485 
 486         /**
 487          * The constructor is only called once to construct the one
 488          * instance we actually use. It opens the entropy gathering device
 489          * which will supply the randomness.
 490          */
 491 
 492         URLSeedGenerator(String egdurl) throws IOException {
 493         if (egdurl == null) {
 494                 throw new IOException("No random source specified");
 495             }
 496             deviceName = egdurl;
 497             init();
 498         }
 499 
 500         private void init() throws IOException {
 501             final URL device = new URL(deviceName);
 502             try {
 503                 seedStream = java.security.AccessController.doPrivileged
 504                     (new java.security.PrivilegedExceptionAction<>() {
 505                         @Override
 506                         public InputStream run() throws IOException {
 507                             /*
 508                              * return a shared InputStream for file URLs and
 509                              * avoid buffering.
 510                              * The URL.openStream() call wraps InputStream in a
 511                              * BufferedInputStream which
 512                              * can buffer up to 8K bytes. This read is a
 513                              * performance issue for entropy sources which
 514                              * can be slow to replenish.
 515                              */
 516                             if (device.getProtocol().equalsIgnoreCase("file")) {
 517                                 File deviceFile =
 518                                     SunEntries.getDeviceFile(device);
 519                                 return FileInputStreamPool
 520                                     .getInputStream(deviceFile);
 521                             } else {
 522                                 return device.openStream();
 523                             }
 524                         }


< prev index next >