snippetjavaMinor
Password Generator, generate password of desired length
Viewed 0 times
lengthdesiredgeneratorpasswordgenerate
Problem
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? Or should I rearrange the characters differently?
3) I also seek general reminders on java conventions that I do not know.
Here is my full class for reference.
Note: The 65 comes from the CHARACTER_SET length, which I have found out before.
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? Or should I rearrange the characters differently?
public static final String CHARACTER_SET = ("aA1bB2cC3dD4eE5fF6gG8hH9iI0jJkKlLmMnNoOpP&qQrRsStT_uUvV-wWxX+yYzZ");3) I also seek general reminders on java conventions that I do not know.
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(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.
Solution
Note: The 65 comes from the CHARACTER_SET length, which I have found out before.
It's
Can be replaced by:
There is one more hard-coded value in your code.
Extract the
It's
65 at the moment. But what if you want to support more or less characters? A different character set? You'd have to change those hard-coded values every time.CHARACTER_SET.charAt(random.nextInt(65)Can be replaced by:
CHARACTER_SET.charAt(random.nextInt(CHARACTER_SET.length())There is one more hard-coded value in your code.
System.out.println(generatePassword(20));Extract the
20 to it's own variable. This allows you to extend your program to, for example, ask for user input and use that value instead. Perhaps the user prefers 16 or 28.Code Snippets
CHARACTER_SET.charAt(random.nextInt(65)CHARACTER_SET.charAt(random.nextInt(CHARACTER_SET.length())System.out.println(generatePassword(20));Context
StackExchange Code Review Q#114834, answer score: 3
Revisions (0)
No revisions yet.