commons-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From trei...@apache.org
Subject cvs commit: jakarta-commons-sandbox/id/src/java/org/apache/commons/id/uuid VersionFourGenerator.java
Date Sun, 02 May 2004 08:07:23 GMT
treilly     2004/05/02 01:07:23

  Modified:    id/src/java/org/apache/commons/id/uuid
                        VersionFourGenerator.java
  Log:
  Adds flag for secure PRNG
  Removes singleton constructor
  
  Revision  Changes    Path
  1.4       +102 -38   jakarta-commons-sandbox/id/src/java/org/apache/commons/id/uuid/VersionFourGenerator.java
  
  Index: VersionFourGenerator.java
  ===================================================================
  RCS file: /home/cvs/jakarta-commons-sandbox/id/src/java/org/apache/commons/id/uuid/VersionFourGenerator.java,v
  retrieving revision 1.3
  retrieving revision 1.4
  diff -u -r1.3 -r1.4
  --- VersionFourGenerator.java	28 Apr 2004 03:13:15 -0000	1.3
  +++ VersionFourGenerator.java	2 May 2004 08:07:23 -0000	1.4
  @@ -15,6 +15,8 @@
    */
   package org.apache.commons.id.uuid;
   
  +import java.security.NoSuchAlgorithmException;
  +import java.security.NoSuchProviderException;
   import java.security.SecureRandom;
   import java.util.Random;
   
  @@ -44,48 +46,110 @@
       /** Byte position of the clock sequence and reserved field */
       private static final short CLOCK_SEQ_HI_AND_RESERVED_BYTE_8 = 8;
   
  -    /** SecureRandom used to generate UUID's */
  -    private static Random secureRandom;
  +        /** Random used to generate UUID's */
  +        private static final Random regularRandom = new Random();
   
  -    /** The singleton instance of this class */
  -    private static VersionFourGenerator instance;
  +        /** SecureRandom used to generate UUID's */
  +        private static Random secureRandom;
   
  -    /**
  -     * <p>Constructs a new VersionFourGenerator.</p>
  -     */
  -    private VersionFourGenerator() {
  -        super();
  -        try {
  -            secureRandom = SecureRandom.getInstance("SHA1PRNG", "SUN");
  +        /** The pseudo-random number generator to use */
  +        private static String usePRNG = "SHA1PRNG";
  +
  +        /** The pseudo-random number generator package name to use */
  +        private static String usePRNGPackage = "SUN";
  +
  +        /**
  +         * <p>Constructs a new VersionFourGenerator.</p>
  +         */
  +        public VersionFourGenerator() {
  +            super();
           }
  -        catch (Exception e) {
  -            secureRandom = new Random();
  +
  +        /**
  +         * <p>Returns a new version four UUID.</p>
  +         *
  +         * @return Object a new version 4 UUID.
  +         */
  +        public Object nextIdentifier() {
  +            return nextUUID(false);
           }
  -    }
   
  -    /**
  -     * <p>Returns the singleton instance of VersionFourGenerator.</p>
  -     *
  -     * @return the singleton instance of VersionFourGenerator.
  -     */
  -    public static VersionFourGenerator getInstance() {
  -        if (instance == null) {
  -            instance = new VersionFourGenerator();
  +        /**
  +         * <p>Returns a new version four UUID.</p>
  +         * <p>This overloaded method may produce both UUID's using a <code>SecureRandom</code>
as well as using normal
  +         * <code>Random</code>
  +         * </p>
  +         *
  +         * @param secure indicates whether or not to use <code>SecureRandom</code>
in generating the random bits.
  +         * @return a new version four UUID that was generated by either a <code>Random</code>
or <code>SecureRandom</code>.
  +         * @throws NoSuchAlgorithmException thrown if the secure option is true and the
PRNG algorithm is not available.
  +         * @throws NoSuchProviderException thrown if the secure option is true and the
PRNG provider package is not available.
  +         */
  +        public Object nextIdentifier(boolean secure) throws NoSuchAlgorithmException, NoSuchProviderException
{
  +            if (secure) {
  +                return nextUUID(true);
  +            }
  +            return nextUUID(false);
  +        }
  +
  +        /**
  +        * <p>Returns a new version four UUID.</p>
  +        *
  +        * @return Object a new version 4 UUID.
  +        */
  +        private UUID nextUUID() {
  +            //Call nextUUID with secure = false
  +            return nextUUID(false);
  +        }
  +
  +        /**
  +         * <p>Returns a new version four UUID using either <code>SecureRandom</code>
or <code>Random</code>.</p>
  +         *
  +         * @param secure boolean flag indicating whether to use <code>SecureRandom</code>
or <code>Random</code>.
  +         * @return a new version four UUID using either <code>SecureRandom</code>
or <code>Random</code>.
  +         */
  +        private UUID nextUUID(boolean secure) {
  +            byte[] raw = new byte[UUID.BYTE_LENGTH];
  +            if (secure) {
  +                //Initialize the secure random if null.
  +                if (secureRandom == null) {
  +                    try {
  +                        if (usePRNGPackage != null) {
  +                            secureRandom = SecureRandom.getInstance(usePRNG, usePRNGPackage);
  +                        } else {
  +                            secureRandom = SecureRandom.getInstance(usePRNG);
  +                        }
  +                    } catch (NoSuchAlgorithmException nsae) {
  +                        secure = false; //Fail back to default PRNG/Random
  +                    } catch (NoSuchProviderException nspe) {
  +                        secure = false; //Fail back to default PRNG/Random
  +                    }
  +                    secureRandom.nextBytes(raw);
  +                }
  +            }
  +
  +            if (!secure) {
  +                regularRandom.nextBytes(raw);
  +            }
  +
  +            raw[CLOCK_SEQ_HI_AND_RESERVED_BYTE_8] &= UUID.VARIANT_IETF_DRAFT;
  +            raw[TIME_HI_AND_VERSION_BYTE_6] &= 0x0F;
  +            raw[TIME_HI_AND_VERSION_BYTE_6] |= (UUID.VERSION_FOUR << 4);
  +
  +            return new UUID(raw);
           }
  -        return instance;
  -    }
  -    /**
  -     * Returns a new version 4 uuid.
  -     *
  -     * @return Object a new version 4 UUID.
  -     */
  -     public Object nextIdentifier() {
  -        byte[] raw = new byte[UUID.BYTE_LENGTH];
  -        secureRandom.nextBytes(raw);
  -        raw[CLOCK_SEQ_HI_AND_RESERVED_BYTE_8] &= UUID.VARIANT_IETF_DRAFT;
  -        raw[TIME_HI_AND_VERSION_BYTE_6] &= 0x0F;
  -        raw[TIME_HI_AND_VERSION_BYTE_6] |= (UUID.VERSION_FOUR << 4);
   
  -        return new UUID(raw);
  +       /**
  +        * <p>Allows clients to set the pseudo-random number generator implementation
used when generating a version four uuid with
  +        * the secure option. The secure option uses a <code>SecureRandom</code>.
The packageName string may be null to specify
  +        * no preferred package.</p>
  +        *
  +        * @param prngName the pseudo-random number generator implementation name. For example
"SHA1PRNG".
  +        * @param packageName the package name for the PRNG provider. For example "SUN".
  +        */
  +       public static void setPRNGProvider(String prngName, String packageName) {
  +           VersionFourGenerator.usePRNG = prngName;
  +           VersionFourGenerator.usePRNGPackage = packageName;
  +           VersionFourGenerator.secureRandom = null;
  +       }
       }
  -}
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org


Mime
View raw message