randomstrg.java

来自「jakarta-taglibs」· Java 代码 · 共 407 行

JAVA
407
字号
/*
 * Copyright 1999,2004 The Apache Software Foundation.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.apache.taglibs.random;

import java.security.NoSuchAlgorithmException;
import java.security.NoSuchProviderException;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;

import javax.servlet.jsp.JspException;

/**
 * RandomStrg class will produce a variable set of random characters.
 *
 * @author Rich Catlett
 *
 * @version 1.0
 *
 */

public class RandomStrg {

    /**
     * generated password
     */
    private String randomstr;
    /**
     * flag determines if all chars ar to be used
     */
    private boolean allchars = false;
    /**
     * length of random string defaults to 8
     */
    private Integer length = new Integer(8);
    /**
     * list of all generated strings, list is stored at the application level
     */
    private HashMap hmap;
    /**
     * ArrayList for the lowerbound of the char sets
     */
    private ArrayList lower = null;
    /**
     * ArrayList for the upperbound of the char sets
     */
    private ArrayList upper = null;
    /**
     * Array for the set of chars that aren't part of a range
     */
    private char[] single = null;
    /**
     * counter for position in the array single
     */
    private int singlecount = 0;
    /**
     * boolean flat that tells the random char generator if there is a list of
     * 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
     *
     */
    private final void generaterandom() {

	// use all chars in the string
	if (allchars)
	    for (int i = 0; i < length.intValue(); i++)
		randomstr = randomstr + new Character((char)((int) 34 + 
				     ((int)(getFloat() * 93)))).toString();
	else if (singles) {
	    // check if there are single chars to be included

	    if (upper.size() == 3) {
		// check for the number of ranges max 3 uppercase lowercase digits

		// build the random string
		for (int i = 0; i < length.intValue(); i++) {
		    // you have four groups to choose a random number from, to make
		    // the choice a little more random select a number out of 100

		    // get a random number even or odd
		    if (((int) (getFloat() * 100)) % 2 == 0) {

			// the number was even get another number even or odd
			if (((int) (getFloat() * 100)) % 2 == 0)
			    // choose a random char from the single char group
			    randomstr = randomstr + randomSingle().toString();
			else
			    // get a random char from the first range
			    randomstr = randomstr + randomChar((Character)lower.get(2),
						(Character)upper.get(2)).toString();
		    } else {
			// the number was odd

			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();
			else
			    // choose a random char from the third range
			    randomstr = randomstr + randomChar((Character)lower.get(0),  
					       (Character)upper.get(0)).toString();
		    }
		}
	    } else if (upper.size() == 2) {
		// single chars are to be included choose a random char from
		// two different ranges

		// build the random char from single chars and two ranges
		for (int i = 0; i < length.intValue(); i++) {
		    // select the single chars or a range to get each random char
		    // from

		    if (((int)(getFloat() * 100)) % 2 == 0) {

			// get random char from the single chars
			randomstr = randomstr + randomSingle().toString();
		    } else if (((int) (getFloat() * 100)) % 2 == 0) {

			// get the random char from the first range
			randomstr = randomstr + randomChar((Character)lower.get(1),
					       (Character)upper.get(1)).toString();
		    } else {

			// get the random char from the second range
			randomstr = randomstr + randomChar((Character)lower.get(0),
					       (Character)upper.get(0)).toString();
		    }
		}
	    } else if (upper.size() == 1) {

		// build the random string from single chars and one range
		for (int i = 0; i < length.intValue(); i++) {
		    if (((int) getFloat() * 100) % 2 == 0)
			// get a random single char
			randomstr = randomstr + randomSingle().toString();
		    else
			// get a random char from the range
			randomstr = randomstr + randomChar((Character)lower.get(0),
					       (Character)upper.get(0)).toString();
		}
	    } else {
		// build the rand string from single chars
		for (int i = 0; i < length.intValue(); i++)
		    randomstr = randomstr + randomSingle().toString();
	    }
	} else {

	    // no single chars are to be included in the random string
	    if (upper.size() == 3) {

		// build random strng from three ranges
		for (int i = 0; i < length.intValue(); i++) {

		    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) (getFloat() * 100)) % 2 == 0) {

			// get random char form second range
			randomstr = randomstr + randomChar((Character)lower.get(1),
					       (Character)upper.get(1)).toString();
		    } else {

			// get random char from third range
			randomstr = randomstr + randomChar((Character)lower.get(0),
					       (Character)upper.get(0)).toString();
		    }
		}
	    } else if (upper.size() == 2) {

		// build random string from two ranges
		for (int i = 0; i < length.intValue(); i++) {
		    if (((int) (getFloat() * 100)) % 2 == 0)
			// get random char from first range
			randomstr = randomstr + randomChar((Character)lower.get(1),
					       (Character)upper.get(1)).toString();
		    else
			// get random char from second range
			randomstr = randomstr + randomChar((Character)lower.get(0),
					     (Character)upper.get(0)).toString();
		}
	    } else

		// build random string
		for (int i = 0; i < length.intValue(); i++)
		    // get random char from only range
		    randomstr = randomstr + randomChar((Character)lower.get(0), 
					       (Character)upper.get(0)).toString();
	}
    }

    /**
     * generate a random char from the single char list
     *
     * @returns - a randomly selscted character from the single char list
     *
     */
    private final Character randomSingle() {

	return (new Character(single[(int)((getFloat() * singlecount) - 1)]));
    }

    /**
     * generate a random character
     *
     * @param lower  lower bound from which to get a random char
     * @param upper  upper bound from which to get a random char
     *
     * @returns - a randomly generated character
     *
     */
    private final Character randomChar(Character lower, Character upper) {
	int tempval;
	char low = lower.charValue();
	char up = upper.charValue();

	// get a random number in the range lowlow - lowup
	tempval = (int)((int)low + (getFloat() * ((int)(up - low))));

	// return the random char
	return (new Character((char) tempval));
    }

    /**
     * get the randomly created string for use with the 
     * &lt;jsp:getProperty name=<i>"id"</i> property="randomstr"/&gt;
     *
     * @return - randomly created string
     *
     */
    public final String getRandom() {

	randomstr = new String();

	generaterandom(); // generate the first random string

	if (hmap != null) {

	    while (hmap.containsKey(randomstr)) {
		// random string has already been created generate a different one
		generaterandom();
	    }

	    hmap.put(randomstr, null);  // add the new random string
	}

	return randomstr;
    }

    /**
     * set the ranges from which to choose the characters for the random string
     *
     * @param low  set of lower ranges
     * @param up  set of upper ranges
     *
     */
    public final void setRanges(ArrayList low, ArrayList up) {
	lower = low;
	upper = up;
    }


    /**
     * set the hashmap that is used to check the uniqueness of random strings
     *
     * @param map  hashmap whose keys are used to insure uniqueness of random strgs
     *
     */
    public final void setHmap(HashMap map) {
	hmap = map;
    }

    /**
     * set the length of the random string
     *
     * @param value  length of the random string
     *
     */
    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;
    }

    /**
     * set the allchars flag
     *
     * @param value  boolean value of the allchars flag
     *
     */
    public final void setAllchars(boolean value) {
	allchars = value;
    }

    /**
     * set the array of single chars to choose from for this random string and the
     * number of chars in the array
     *
     * @param chars  the array of single chars
     * @param value  the number of single chars
     *
     */
    public final void setSingle(char[] chars, int value) {
	single = chars;  // set the array of chars
	singlecount = value;  // set the number of chars in array single
	singles = true;  // set flag that single chars are in use
    }
}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?