Generates a secure random word with the given length consisting of uppercase and lowercase letters and numbers. : Random « Development Class « Java






Generates a secure random word with the given length consisting of uppercase and lowercase letters and numbers.

  
//package org.boticelli.util;

import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.util.Arrays;

//import org.apache.log4j.Logger;

public class RandomUtil
{
  //  protected static Logger log = Logger.getLogger(RandomUtil.class);

    private static String[] secureRndNames = new String[] {
        System.getProperty("boticelli.securerandom"), "SHA1PRNG", "IBMSecureRandom" };

    private RandomUtil()
    {

    }

    /**
     * Creates a new secure random number generator. The following secure random
     * algorithm names are tried:
     * <ul>
     * <li>The value of system property "boticelli.securerandom", if set. </li>
     * <li> "SHA1PRNG" </li>
     * <li> "IBMSecureRandom" (available if running in the IBM JRE) </li>
     * </ul>
     */
    private static SecureRandom createSecureRandom()
    {
        SecureRandom secureRnd = null;
        try
        {

            for (int i = 0; i < secureRndNames.length; i++)
            {
                try
                {
                    if (secureRndNames[i] != null)
                    {
                        secureRnd = SecureRandom.getInstance(secureRndNames[i]);
                        break;
                    }
                }
                catch (NoSuchAlgorithmException nsae)
                {
                  //  log.debug("no secure random algorithm named \"" + secureRndNames[i] + "\"",
                  //      nsae);
                }
            }
            if (secureRnd == null)
            {
                throw new IllegalStateException("no secure random algorithm found. (tried " +
                    Arrays.asList(secureRndNames) + ")");
            }
            secureRnd.setSeed(System.currentTimeMillis());
        }
        catch (Exception e)
        {
           // log.fatal("error initializing secure random", e);
        }

        return secureRnd;
    }

    /**
     * Generates a secure random word with the given length consisting of
     * uppercase and lowercase letters and numbers.
     * 
     * @param len Amount of random characters to generate
     * @return random Word containing letters and numbers.
     */
    public static String createWord(int len)
    {
        return createWord(len, null);
    }

    /**
     * Generates a secure random word with the given length.
     * 
     * @param len Amount of random characters to generate
     * @param alphabet Alphabet to generate from.
     * @return random Word containing letters and numbers.
     */
    public static String createWord(int len, char[] alphabet)
    {
        SecureRandom random = createSecureRandom();

        if (alphabet == null)
        {
            alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890"
                .toCharArray();
        }

        StringBuffer out = new StringBuffer(len);
        for (int i = 0; i < len; i++)
        {
            out.append(alphabet[random.nextInt(alphabet.length)]);
        }

        return out.toString();
    }
}

   
    
  








Related examples in the same category

1.Create random number
2.Create two random number generators with the same seed
3.Does Math.random() produce 0.0 and 1.0
4.Random numbers between 1 and 100
5.Random number between 0 AND 10
6.Random integers that range from from 0 to n
7.Random.nextInt(n) returns a distributed int value between 0 (inclusive) and n (exclusive).
8.Round Java float and double numbers using Math.round
9.Randomizer
10.nextDouble() and nextGaussian() in java.util.Random
11.Generating random numbers
12.Generate random ints by asking Random() for
13.Getting random numbers: nextGaussian
14.Generate a random array of numbers
15.Next double and next int
16.Random bytes
17.Random boolean
18.Random long type number
19.Random float type number
20.Random double type number
21.Math.random
22.A wrapper that supports all possible Random methods via the java.lang.Math#random() method and its system-wide {@link Random} object.
23.Operations for random Strings
24.Random Util with ReentrantLock
25.A Java implementation of the MT19937 (Mersenne Twister) pseudo random number generator algorithm
26.Randomizer
27.Random: Properly synchronized and can be used in a multithreaded environment
28.A predictable random number generator.
29.Random GUID
30.A random.org seeded random generator.
31.A random choice maker
32.Memory-efficient map of keys to values with list-style random-access semantics.
33.Generates a random integer inside the lo and hi interval
34.Randomizer
35.RandomGUID generates truly random GUIDs
36.A random access text file allows a text file to be accessed randomly starting at any position.
37.A garbage-free randomised quicksort
38.A linear random method based on xor shifts
39.Mersenne Twister Random
40.A GaussianHat is a random number generator that will give you numbers based on a population mean and standard deviation.
41.Randomly permutes the elements of the specified array
42.generate string consists of random characters.
43.Atomic Pseudo Random
44.Atomic Simple Random
45.Mersenne
46.Mersenne Twister
47.Mersenne Twister Fast
48.Mersenne Twister algorithm
49.Emulates a dice