Skip to main content

Password Generator, generate password of desired length

While I do know that there are many questions regarding "password generators," I have a different approach and would like to know if it is effective.

  1. The below password generator takes on a different approach that other basic password generators, creating an empty string of x length, x being the desired length of the final password, and inserting characters and sub-stringing. I would like to know if this fashion is more effective than adding random characters one-by-one

  2. Have I ensured the most random-ness of the characters in the password by using this character set?

  3. I also seek general reminders on java conventions that I do not know.

    public static final String CHARACTER_SET = ("aA1bB2cC3dD4eE5fF6gG8hH9iI0jJkKlLmMnNoOpP&qQrRsStT_uUvV-wWxX+yYzZ");

Or if I rearrange the characters the password will theoretically be more random?

Here is my full class for reference.

public class PasswordGenerator {

  public static final String VERSION = ("1.0");

  public static final String CHARACTER_SET = ("aA1bB2cC3dD4eE5fF6gG8hH9iI0jJkKlLmMnNoOpP&qQrRsStT_uUvV-wWxX+yYzZ");

  public static void main(String[] args) {
    System.out.println("Welcome to password generator, version: " + VERSION);
    System.out.println("Copyright 2015. All rights reserved.");
    System.out.println(generatePassword(20));
  }

  private static String generatePassword(int passwordLength) {
    String password = "";
    for (int i = 0; i < passwordLength; i++) {
      password = password + " ";
    }
    Random random = new Random();
    for (int i = 0; i < password.length(); i++) {
      if (i == 0) {
        password = String.valueOf(CHARACTER_SET.charAt(random.nextInt(65))) + password.substring(1, password.length());
      } else {
        password = password.substring(0, i) + String.valueOf(CHARACTER_SET.charAt(random.nextInt(65))) + password.substring(i + 1, password.length());
      }
    }
    return password.toString();
  }
}

Note: The 65 comes from the CHARACTER_SET length, which I have found out before.

Mario Ishac
  • 957
  • 6
  • 15