Skip to content

Instantly share code, notes, and snippets.

@kcelestine
Created April 17, 2014 19:14
Show Gist options
  • Save kcelestine/11005624 to your computer and use it in GitHub Desktop.
Save kcelestine/11005624 to your computer and use it in GitHub Desktop.
import java.util.Random;
import java.util.Scanner;
public class PasswordGenerator {
Random rand = new Random();
char numbers[] = "1234567890".toCharArray();
char lower[] = "abcdefghijklmnopqrstuvwxyz".toCharArray();
char upper[] = "abcdefghijklmnopqrstuvwxyz".toUpperCase().toCharArray();
final int MIN_NUMBER = 5;
final int MAX_NUMBER = 20;
String choices = generateChoices();
enum CharType {NUMBER, LOWER_LETTERS, UPPER_LETTERS}
PasswordGenerator(){
System.out.println("**************************************");
System.out.println("* Welcome to the Password Generator! *");
System.out.println("**************************************");
System.out.println("Here are your options");
System.out.println("1 : A general password");
System.out.println("2 : A password with at least 1 number");
System.out.println("3 : A password with at least 1 upper case letter");
System.out.println("4 : A password with at lease 1 lower case letter");
System.out.println("5 : A password with all three");
System.out.println("\nEach Password must have between 5 and 20 characters");
System.out.println("How many characters do you want in your password?");
int option = getNumber(1,5);
// String password;
// switch(option){
// case 0:
// password = generatePassword();
// break;
// case 1:
// password = generatePasswordNumber();
// break;
// case 2:
// password = generatePasswordLower();
// break;
// case 3:
// password = generatePasswordUpper();
// break;
// case 4:
// password = generatePasswordAll();
// break;
// default:
// password = generatePassword();
// break;
// }
//
// System.out.println("Your password is" + password);
}
//Force number, lower case letter or upper case letter
private String force(String password, int type) {
CharType charType = CharType.values()[type]; //do your own bounds checking
int index = rand.nextInt(password.length() + 1);
char theChar;
switch (charType) {
case NUMBER:
theChar = numbers[rand.nextInt(numbers.length)];
break;
case LOWER_LETTERS:
theChar = lower[rand.nextInt(lower.length)];
break;
case UPPER_LETTERS:
theChar = upper[rand.nextInt(upper.length)];
break;
default:
return password;
}
return password.substring(0, index-1) + theChar + password.substring(index);
}
//Create string of characters to use in password creation
private String generateChoices(){
String choices = lower.toString() + upper.toString();
choices = choices + "~!@$%&*_+?";
for (int i = 0; i < 2; i++) {choices = choices + numbers.toString();}
return choices;
}
//Recieve input from user and ensure that it is a number no greater than max
private int getNumber(int min, int max){
Scanner scan = new Scanner(System.in);
int length=min-1;
// do {
// try{
// length = Integer.parseInt( scan.next() );
// }
// catch (NumberFormatException e ){
// System.out.println("That's not a number");
// }
// if (length > max) System.out.print("That number is too large. ");
// if (length < max) System.out.print("That number is too small. ");
// } while ((length >= min && length <= max));
//
System.out.println(min);
System.out.println(max);
System.out.println(length);
while ((length >= min & length <= max)){
try{
length = Integer.parseInt( scan.next() );
}
catch (NumberFormatException e ){
System.out.println("That's not a number");
}
if (length > max) System.out.print("That number is too large. ");
if (length < max) System.out.print("That number is too small. ");
}
System.out.println(length <= max);
System.out.println(length <= max);
//scan.close();
return Math.abs(length);
}
//Original generator with no explicit forcing
public String generatePassword(){
int length = getNumber(MIN_NUMBER, MAX_NUMBER);
String password = "";
int k = 0;
while (k < length){
password = password + choices.charAt(rand.nextInt(choices.length()));
k++;
}
return password;
}
//Require the password has at least one digit
//After generation, if there is no digit, return edited version of password
private String generatePasswordNumber(){
int length = getNumber(MIN_NUMBER, MAX_NUMBER);
String password = "";
int k = 0;
while (k < length){
password = password + choices.charAt(rand.nextInt(choices.length()));
k++;
}
if(!password.matches(".*\\d.*")){
//Use of ordinal method is not recommended
return force(password, CharType.NUMBER.ordinal());
}
return password;
}
//force at least one lower case letter and at least one upper case letter
private String generatePasswordLower(){
int length = getNumber(MIN_NUMBER, MAX_NUMBER);
String password = "";
int k = 0;
while (k < length){
password = password + choices.charAt(rand.nextInt(choices.length()));
k++;
}
if(!password.matches(".*[a-z].*")){
//Use of ordinal method is not recommended
return force(password, CharType.LOWER_LETTERS.ordinal());
}
return password;
}
//force at least one lower case letter and at least one upper case letter
private String generatePasswordUpper(){
int length = getNumber(MIN_NUMBER, MAX_NUMBER);
String password = "";
int k = 0;
while (k < length){
password = password + choices.charAt(rand.nextInt(choices.length()));
k++;
}
if(!password.matches(".*[A-Z].*")){
//Use of ordinal method is not recommended
return force(password, CharType.UPPER_LETTERS.ordinal());
}
return password;
}
private String generatePasswordAll(){
int length = getNumber(MIN_NUMBER, MAX_NUMBER);
String password = "";
int k = 0;
while (k < length){
password = password + choices.charAt(rand.nextInt(choices.length()));
k++;
}
password = force(password, CharType.NUMBER.ordinal());
password = force(password, CharType.LOWER_LETTERS.ordinal());
password = force(password, CharType.UPPER_LETTERS.ordinal());
return password;
}
private boolean isLowerConsonant(char c){
return c=='b' || c=='c' || c=='d' || c=='f' || c=='g' || c=='h' || c=='j' || c=='k' || c=='l'|| c=='m' || c=='n' || c=='p' || c=='q'|| c=='r' || c=='s' || c=='t' || c=='v'|| c=='w' || c=='x' || c=='y' || c=='z';
}
private boolean isUpperConsonant(char c){
return c=='B' || c=='C' || c=='D' || c=='F' || c=='G' || c=='H' || c=='J' || c=='K' || c=='L'|| c=='M' || c=='N' || c=='P' || c=='Q'|| c=='R' || c=='S' || c=='T' || c=='V'|| c=='W' || c=='X' || c=='Y' || c=='Z';
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment