catlett     01/08/14 11:32:17

  Modified:    random/src/org/apache/taglibs/random RandomNum.java
  Log:
  the random taglib can now use either a SecureRandom or a Random object to create the 
required random number or string
  
  Revision  Changes    Path
  1.2       +88 -5     
jakarta-taglibs/random/src/org/apache/taglibs/random/RandomNum.java
  
  Index: RandomNum.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-taglibs/random/src/org/apache/taglibs/random/RandomNum.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- RandomNum.java    2001/05/21 20:55:21     1.1
  +++ RandomNum.java    2001/08/14 18:32:17     1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-taglibs/random/src/org/apache/taglibs/random/RandomNum.java,v 1.1 
2001/05/21 20:55:21 catlett Exp $
  - * $Revision: 1.1 $
  - * $Date: 2001/05/21 20:55:21 $
  + * $Header: 
/home/cvs/jakarta-taglibs/random/src/org/apache/taglibs/random/RandomNum.java,v 1.2 
2001/08/14 18:32:17 catlett Exp $
  + * $Revision: 1.2 $
  + * $Date: 2001/08/14 18:32:17 $
    *
    * ====================================================================
    *
  @@ -62,6 +62,10 @@
   package org.apache.taglibs.random;
   
   import java.util.*;
  +import java.security.SecureRandom;
  +import java.security.NoSuchAlgorithmException;
  +import java.security.NoSuchProviderException;
  +import javax.servlet.jsp.JspException;
   
   /**
    * The RandomNum will produce a variable set of random numbers.
  @@ -94,11 +98,69 @@
        * lower bound in which to search for a number defaults to 0
        */
       private long lower = 0;
  +    /**
  +     * the algorithm to use for a SecureRandom object
  +     */
  +    private String algorithm = null;
  +    /**
  +     * the provider package to check for the algorithm
  +     */
  +    private String provider = null;
  +    /**
  +     * boolean value that marks if a Random or SecureRandom object is to be used,
  +     * default value of false says that a Random object will be used
  +     */
  +    private boolean secure = false;
  +    /**
  +     * random object that could be used
  +     */
  +    private Random random = null;
  +    /**
  +     * SecureRandom object that could be used
  +     */
  +    private SecureRandom secrandom = null;
   
       /**
  +     * nethod determines if a Random or SecureRandom object is to be used to
        * generate the random number
        *
        */
  +    private final float getFloat() {
  +     if (random == null)
  +         return secrandom.nextFloat();
  +     else
  +         return random.nextFloat();
  +    }
  +
  +    /**
  +     * generate the Random object that will be used for this random number 
  +     * generator
  +     *
  +     */
  +    public final void generateRandomObject() throws JspException {
  +
  +     // check to see if the object is a SecureRandom object
  +     if (secure) {
  +         try {
  +             // get an instance of a SecureRandom object
  +             if (provider != null)
  +                 // search for algorithm in package provider
  +                 secrandom = SecureRandom.getInstance(algorithm, provider);
  +             else
  +                 secrandom = SecureRandom.getInstance(algorithm);
  +         } catch (NoSuchAlgorithmException ne) {
  +             throw new JspException(ne.getMessage());
  +         } catch (NoSuchProviderException pe) {
  +             throw new JspException(pe.getMessage());
  +         }
  +        } else
  +         random = new Random();
  +    }
  +
  +    /**
  +     * generate the random number
  +     *
  +     */
       private final void generaterandom() {
   
        int tmprandom = 0;  // temp storage for random generated number
  @@ -106,9 +168,9 @@
   
        // check to see if float value is expected
        if (floatvalue)
  -         randomfloat = new Float(Math.random());
  +         randomfloat = new Float(getFloat());
           else
  -         randomnum = new Long(lower + (long) ((Math.random() * (upper - lower))));
  +         randomnum = new Long(lower + (long) ((getFloat() * (upper - lower))));
       }
   
       /**
  @@ -146,5 +208,26 @@
        // check to see if a float value is expected
        if ((lower == 0) && (upper == 1))
            floatvalue = true;
  +    }
  +
  +    /**
  +     * set the algorithm name
  +     *
  +     * @param value  name of the algorithm to use for a SecureRandom object
  +     *
  +     */
  +    public final void setAlgorithm(String value) {
  +     algorithm = value;
  +     secure = true;  // a SecureRandom object is to be used
  +    }
  +
  +    /**
  +     * set the provider name
  +     *
  +     * @param value  name of the package to check for the algorithm
  +     *
  +     */
  +    public final void setProvider(String value) {
  +     provider = value;
       }
   }
  
  
  

Reply via email to