Skip to content

Instantly share code, notes, and snippets.

@kcelestine
Created April 17, 2014 19:25
Show Gist options
  • Save kcelestine/11006267 to your computer and use it in GitHub Desktop.
Save kcelestine/11006267 to your computer and use it in GitHub Desktop.
Code for Internship Consideration
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();
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("\nThis password will have at least one, uppercase letter, lower case letter and number");
System.out.println("How many characters would you like you password to have? (Minium: 6) ");
String password = generatePasswordAll();
System.out.println("Here is your password: " + 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(){
Scanner scan = new Scanner(System.in);
int length=0;
do {
System.out.print("Please enter a valid number: ");
try{
length = Integer.parseInt( scan.next() );
}
catch (NumberFormatException e ){
System.out.println("That's not a valid number");
}
} while (length < 6 );
scan.close();
return length;
}
//Original generator with no explicit forcing
private String generatePassword(){
int length = getNumber();
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();
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();
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();
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();
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