catlett     01/08/14 11:32:28

  Modified:    random/src/org/apache/taglibs/random RandomStrg.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       +99 -16    
jakarta-taglibs/random/src/org/apache/taglibs/random/RandomStrg.java
  
  Index: RandomStrg.java
  ===================================================================
  RCS file: 
/home/cvs/jakarta-taglibs/random/src/org/apache/taglibs/random/RandomStrg.java,v
  retrieving revision 1.1
  retrieving revision 1.2
  diff -u -r1.1 -r1.2
  --- RandomStrg.java   2001/05/21 20:57:09     1.1
  +++ RandomStrg.java   2001/08/14 18:32:28     1.2
  @@ -1,7 +1,7 @@
   /*
  - * $Header: 
/home/cvs/jakarta-taglibs/random/src/org/apache/taglibs/random/RandomStrg.java,v 1.1 
2001/05/21 20:57:09 catlett Exp $
  - * $Revision: 1.1 $
  - * $Date: 2001/05/21 20:57:09 $
  + * $Header: 
/home/cvs/jakarta-taglibs/random/src/org/apache/taglibs/random/RandomStrg.java,v 1.2 
2001/08/14 18:32:28 catlett Exp $
  + * $Revision: 1.2 $
  + * $Date: 2001/08/14 18:32:28 $
    *
    * ====================================================================
    *
  @@ -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;
   
   /**
    * RandomStrg class will produce a variable set of random characters.
  @@ -99,7 +103,7 @@
        */
       private ArrayList upper = null;
       /**
  -     * ArrayL for the set of chars that aren't part of a range
  +     * Array for the set of chars that aren't part of a range
        */
       private char[] single = null;
       /**
  @@ -111,6 +115,64 @@
        * single chars
        */
       private boolean singles = false;
  +    /**
  +     * 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
  +                 random = SecureRandom.getInstance(algorithm, provider);
  +             else
  +                 random = 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 string
  @@ -122,7 +184,7 @@
        if (allchars)
            for (int i = 0; i < length.intValue(); i++)
                randomstr = randomstr + new Character((char)((int) 34 + 
  -                                     ((int)(Math.random() * 93)))).toString();
  +                                  ((int)(getFloat() * 93)))).toString();
        else if (singles) {
            // check if there are single chars to be included
   
  @@ -135,10 +197,10 @@
                    // the choice a little more random select a number out of 100
   
                    // get a random number even or odd
  -                 if (((int) (Math.random() * 100)) % 2 == 0) {
  +                 if (((int) (getFloat() * 100)) % 2 == 0) {
   
                        // the number was even get another number even or odd
  -                     if (((int) (Math.random() * 100)) % 2 == 0)
  +                     if (((int) (getFloat() * 100)) % 2 == 0)
                            // choose a random char from the single char group
                            randomstr = randomstr + randomSingle().toString();
                        else
  @@ -148,7 +210,7 @@
                    } else {
                        // the number was odd
   
  -                     if (((int) (Math.random() * 100)) % 2 == 0)
  +                     if (((int) (getFloat() * 100)) % 2 == 0)
                            // choose a random char from the second range
                            randomstr = randomstr + randomChar((Character)lower.get(1),
                                               (Character)upper.get(1)).toString();
  @@ -167,11 +229,11 @@
                    // select the single chars or a range to get each random char
                    // from
   
  -                 if (((int)(Math.random() * 100)) % 2 == 0) {
  +                 if (((int)(getFloat() * 100)) % 2 == 0) {
   
                        // get random char from the single chars
                        randomstr = randomstr + randomSingle().toString();
  -                 } else if (((int) (Math.random() * 100)) % 2 == 0) {
  +                 } else if (((int) (getFloat() * 100)) % 2 == 0) {
   
                        // get the random char from the first range
                        randomstr = randomstr + randomChar((Character)lower.get(1),
  @@ -187,7 +249,7 @@
   
                // build the random string from single chars and one range
                for (int i = 0; i < length.intValue(); i++) {
  -                 if (((int) Math.random() * 100) % 2 == 0)
  +                 if (((int) getFloat() * 100) % 2 == 0)
                        // get a random single char
                        randomstr = randomstr + randomSingle().toString();
                    else
  @@ -208,12 +270,12 @@
                // build random strng from three ranges
                for (int i = 0; i < length.intValue(); i++) {
   
  -                 if (((int) (Math.random() * 100)) % 2 == 0) {
  +                 if (((int) (getFloat() * 100)) % 2 == 0) {
   
                        // get random char from first range
                        randomstr = randomstr + randomChar((Character)lower.get(2),
                                               (Character)upper.get(2)).toString();
  -                 } else if (((int) (Math.random() * 100)) % 2 == 0) {
  +                 } else if (((int) (getFloat() * 100)) % 2 == 0) {
   
                        // get random char form second range
                        randomstr = randomstr + randomChar((Character)lower.get(1),
  @@ -229,7 +291,7 @@
   
                // build random string from two ranges
                for (int i = 0; i < length.intValue(); i++) {
  -                 if (((int) (Math.random() * 100)) % 2 == 0)
  +                 if (((int) (getFloat() * 100)) % 2 == 0)
                        // get random char from first range
                        randomstr = randomstr + randomChar((Character)lower.get(1),
                                               (Character)upper.get(1)).toString();
  @@ -256,7 +318,7 @@
        */
       private final Character randomSingle() {
   
  -     return (new Character(single[(int)((Math.random() * singlecount) - 1)]));
  +     return (new Character(single[(int)((getFloat() * singlecount) - 1)]));
       }
   
       /**
  @@ -274,7 +336,7 @@
        char up = upper.charValue();
   
        // get a random number in the range lowlow - lowup
  -     tempval = (int)((int)low + (Math.random() * ((int)(up - low))));
  +     tempval = (int)((int)low + (getFloat() * ((int)(up - low))));
   
        // return the random char
        return (new Character((char) tempval));
  @@ -337,6 +399,27 @@
        */
       public final void setLength(Integer value) {
        length = value;
  +    }
  +
  +    /**
  +     * 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