Generate a random String suitable for use as a temporary password. - Java Algorithm

Java examples for Algorithm:String

Description

Generate a random String suitable for use as a temporary password.

Demo Code



import java.security.SecureRandom;
import java.util.Random;

public class Main {

  private static final Random RANDOM = new SecureRandom();
  /** Length of password. @see #generateRandomPassword() */
  public static final int PASSWORD_LENGTH = 12;

  /**/*from w w  w . ja  v  a2  s  . co m*/
   * Generate a random String suitable for use as a temporary password.
   *
   * @return String suitable for use as a temporary password
   * @since 2.4
   */
  public String generateCode() {
    // Pick from some letters that won't be easily mistaken for each
    // other. So, for example, omit o O and 0, 1 l and L.
    String letters = "abcdefghjkmnpqrstuvwxyzABCDEFGHJKMNPQRSTUVWXYZ23456789@$*";

    String pw = "";
    for (int i = 0; i < PASSWORD_LENGTH; i++) {
      int index = (int) (RANDOM.nextDouble() * letters.length());
      pw += letters.substring(index, index + 1);
    }
    return pw;
  }
}

Related Tutorials