[Webfunds-commits] java/webfunds/client Core.java

Ian Grigg iang@cypherpunks.ai
Tue, 3 Apr 2001 09:56:08 -0400 (AST)


iang        01/04/03 09:56:07

  Modified:    webfunds/client Core.java
  Added:       webfunds/util SecureRandomHack.java
  Log:
  moved SecureRandomHack to util and added comment about Unix for user

Revision  Changes    Path
1.1                  java/webfunds/util/SecureRandomHack.java

Index: SecureRandomHack.java
===================================================================
/* $Id: SecureRandomHack.java,v 1.1 2001/04/03 13:56:07 iang Exp $
 *
 * Copyright (c) 2000 Systemics Inc. on behalf of
 * The WebFunds Development Team. All rights reserved.
 */
package webfunds.util;


import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.security.SecureRandom;


/**
 * This class is a hack to speed up SecureRandom initialization, 
 * working around JDK 1.1 brain damage. This class should disappear
 * when we drop JDK 1.1.x support.
 *
 * Just call it before you call your first SecureRandom() constructor.
 *
 * This will only improve speed on UN*X, Windows and Mac don't have 
 * a /dev/random nor something equivalent.
 *
 * @version $Revision: 1.1 $
 * @author Jeroen C. van Gelderen (gelderen@systemics.com)
 */
public final class SecureRandomHack
{
    /** Name of randomness device. */
    private static final File RANDOM_DEV = new File("/dev/random");


    /** Length of seed in bytes. */
    private static final int SEED_LEN = 20;


    /** Only static methods. */
    private SecureRandomHack() {}


    /**
     * Get a SecureRandom object that has been initialized a quickly as
     * possible. On Mac and Windows this uses the default SecureRandom
     * emtpy constructor but on UNIX it quickly initialized from /dev/random.
     */
    public static SecureRandom getInstance()
    {
        byte[] seed = getSeedFast();
        if( seed != null )
            return new SecureRandom(seed);
        else
            return new SecureRandom();
    }


    /**
     * Obtain a seed quickly by reading from /dev/random.
     *
     * @return a 20-byte seed or null
     */
    private static byte[] getSeedFast()
    {
        try
        {
            FileInputStream fis = new FileInputStream(RANDOM_DEV);
            byte[] seed = new byte[SEED_LEN];
            int todo  = seed.length;
            int off   = 0;
            int count = 0;
            while( todo > 0 )
            {
                if( (count = fis.read(seed, off, todo)) == -1 )
                    throw new IOException("EOF");

                off  += count;
                todo -= count;
            }
            System.err.println(RANDOM_DEV + " appears successful.");
            return seed;
        }
        catch(IOException e)
        {
            // e.printStackTrace();
            System.err.println("No " + RANDOM_DEV + " available (not Unix?!).");
            return null;
        }
    }
}



1.72      +3 -2      java/webfunds/client/Core.java

Index: Core.java
===================================================================
RCS file: /home/webfunds/cvsroot/java/webfunds/client/Core.java,v
retrieving revision 1.71
retrieving revision 1.72
diff -u -r1.71 -r1.72
--- Core.java	2001/03/20 23:33:07	1.71
+++ Core.java	2001/04/03 13:56:07	1.72
@@ -1,4 +1,4 @@
-/* $Id: Core.java,v 1.71 2001/03/20 23:33:07 iang Exp $
+/* $Id: Core.java,v 1.72 2001/04/03 13:56:07 iang Exp $
  *
  * Copyright (c) Systemics Inc. 1995-2000 on behalf of
  * The WebFunds Development Team.  All rights reserved.
@@ -14,10 +14,11 @@
 
 import webfunds.openpgp.provider.CryptixOpenPGP;
 
+import webfunds.util.SecureRandomHack;
+
 import webfunds.utils.Debug;
 import webfunds.utils.MultiPrintStream;
 import webfunds.utils.NetWatcher;
-import webfunds.utils.SecureRandomHack;
 import webfunds.utils.VersionNumbers;
 
 import webfunds.ricardian.ContractStore;