Login Register






Thread Rating:
  • 0 Vote(s) - 0 Average


[JAVA CODE] Password Generator - Commented and noobish friendly filter_list
Author
Message
[JAVA CODE] Password Generator - Commented and noobish friendly #1

Hello [username], here is a program i designed =D
It's a password generator.

NOTE: I'm Already working on a version two of this project. Thank you Deque for showing the flaws \M/

It has the following features:


- 4 different Charsets:
----- Numbers (0-9)
----- Lower-Case Alpha (a-z)
----- Upper-Case Alpha (A-Z)
----- Special-Case (-_!#$%&.)


- Possibility to generate up 15 combinations of those Charsets:
----- Numbers Only
----- Lower-Case Only
----- Upper-Case Only
----- ...
----- ...
----- ...
----- - All The Charsets Together


- It works in two ways:
----- Random Password Generator Mode (produces a password with a length from 8 to 12 chars and chooses randomly one of the 15 possible charset combinations.

----- Defined Parameters Password Generator Mode (You define the length and charset combinations)


- The program is also protected against "troll users" - If it asks for an Integer Number You cannot insert something different.



This program is fully working and you can use the code and modify it but giving the proper credits

This was done with the intent of helping less experienced people and provide them with a working semi-semi-semi-semi complex program for them to tweak and understand it's way of working... Besides, who wouldn't like to play with a password generating program? XD

PS: THIS PROGRAM IS NOT OPTIMAL, THERE ARE CERTAIN PLACES WHERE THINGS COULD BE DONE MORE EFFECTIVELY AND CORRECTLY. BUT PLEASE DO POST YOUR CREATIVE COMMENTS, QUESTIONS, SUGGESTIONS, ETC ^^



DOWNLOADS:

> DOWNLOAD PROJECT (.java)

> DOWNLOAD RUNABLE JAR + BAT TO RUN IN WINDOWS (.jar + .bat)


NOTES:
- If "it doesn't work" copy the code, open eclipse, create a new java project, call it passGenerator, paste the code.

This "public class passGenerator" must be equal to the class name (which will also be the file name + .java)



Code:
import java.util.Random;
import java.util.Scanner;

public class passGenerator {

    /**
     * Program that generates passwords randomly or with a defined size and
     * charset(s)
     *
     * @author N-genhocas
     * @date 09/01/2013
     */
    public static void main(String[] args) {

        // Initializes Scanner (to read inputs from keyboard)

        Scanner scan = new Scanner(System.in);

        // Flag that when changed to true, terminates the program
        boolean terminateProgramFlag = false;

        // Will be used to determine if the input provided was correct
        boolean correctInput = false;

        // Will contain the option number (started with an invalid input in case
        // something goes wrong)
        int option = 4;

        // Cicle keeps the program running until the user decides to terminate
        // it
        do {

            // Prevents going to next step from main menu wiht invalid choice
            do {

                // Presents the Main Menu
                System.out.println("---------------------------------");
                System.out.println("   Password generating program   ");
                System.out.println("---------------------------------\n");

                System.out.println("Select an option:");
                System.out
                        .println("\n\t0 - Generate a random password (Random Parameters)");
                System.out
                        .println("\t1 - Generate a random password (Selecting it's parameters)");
                System.out.println("\t2 - Exit Program\n");
                System.out.print("Option: ");

                // Read the user input
                String optionCheck = scan.nextLine();

                // Checks if input is an integer
                if (isInteger(optionCheck)) {
                    
                    //Checks if in the required range
                    if (Integer.parseInt(optionCheck) >= 0
                            && Integer.parseInt(optionCheck) < 3) {
                        
                        option = Integer.parseInt(optionCheck);
                        correctInput = true;
                        
                    } else {
                        
                        displayBadInput();
                    }
                } else
                    displayBadInput();

            } while (correctInput == false);

            switch (option) {

            //Option 0 was selected - Generates a completely random password with 8 to 12 chars
            case 0:
                
                //Inicializes Random (to generate random numbers)
                Random randNumber = new Random();

                // Generate random password with random parameters
                System.out.println("\n---------------------------------");
                System.out.println("    Random Generated Password    ");
                System.out.println("---------------------------------\n");

                System.out.print("Password: ");
                //Generates a number between 8 and 12
                generatePassword(randNumber.nextInt(4) + 8,
                        randNumber.nextInt(15) + 1);
                System.out.println();

                break;

            //Option 1 was selected - Generates password with defined parameters
            case 1:
                
                //Initializing/atribution os values to variables
                correctInput = false;
                int value = 1;
                int length = 0;

                do {

                    System.out.println("\n---------------------------------");
                    System.out.println("  Configured Password Generation ");
                    System.out.println("---------------------------------\n");

                    System.out
                            .println("Choose the charset(s) by summing the values or just 0 to exit to main menu");
                    System.out.println("\n\t0 - EXIT (to Main Menu)");
                    System.out.println("\t1 - NUMBERS (0...9)");
                    System.out.println("\t2 - LOWER-CASE (a...z)");
                    System.out.println("\t4 - UPPER-CASE (A...Z)");
                    System.out.println("\t8 - SPECIAL CHARS (-_!#$%&.)");
                    System.out.print("Value: ");

                    String valueCheck = scan.nextLine();

                    // Check if the iput is an integer
                    //This block reads analyzes and determine if menu option selected was valid
                    if (isInteger(valueCheck) == true) {

                        //Check if value is within range
                        if (Integer.parseInt(valueCheck) >= 0
                                && Integer.parseInt(valueCheck) < 16) {

                            value = Integer.valueOf(valueCheck);

                            if (value == 0)
                                break;
                            if (value > 0 && value < 16)
                                correctInput = true;
                        } else {

                            correctInput = false;
                            System.out
                                    .println("> PLEASE CHOOSE A VALUE WITHIN THE RANGE (0-15)");

                        }

                    } else {
                        correctInput = false;
                        displayBadInput();

                    }

                    // Check if previous input was correct, no need to show/run this if it wasn't
                    //This block asks, reads and analyzes if length of password is valid
                    if (correctInput == true) {

                        System.out.print("\nChoose the length: ");

                        String lengthCheck = scan.nextLine();

                        if (isInteger(valueCheck) == true)
                            length = Integer.valueOf(lengthCheck);
                        else {
                            correctInput = false;
                            displayBadInput();
                        }

                    } else {

                        correctInput = false;
                    }

                    // Check if previous input was correct, no need to show/run this if it wasn't
                    if (correctInput == true) {
                        System.out.print("\nPassword: ");

                        generatePassword(length, value);

                        System.out.println();
                    }
                } while (correctInput == false);

                correctInput = false;
                break;

            //Option 2 was selected - The program will temrinate
            case 2:
                // Sets the flag to true so it skips the do cycle and terminates
                // the program
                terminateProgramFlag = true;
                break;
            }

        } while (terminateProgramFlag != true);
    }

    /**
     * Generates a String and prints it with Lenght (lenght) and Charset(charset
     * from1 to 15)
     *
     * @param length
     *            - length of the password
     * @param charset
     *            - Charset used in the password formation
     */
    public static void generatePassword(int length, int charset) {

        // Char arrany that contains all the numbers - 1
        char[] number = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9' };

        // Char array that contains all lower-case letters - 2
        char[] lowerCase = { 'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j',
                'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v',
                'w', 'x', 'y', 'z' };

        // Char array that contains all the upper-case letters - 4
        char[] upperCase = { 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J',
                'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V',
                'W', 'X', 'Y', 'Z' };

        // Char array that contains special Symbols - 8
        char[] special = { '-', '_', '!', '#', '$', '%', '&', '.' };

        Random randNumber = new Random();

        StringBuilder password;

        switch (charset) {

        // JUST NUMBER
        case 1:

            password = new StringBuilder();

            for (int i = 0; i < length; i++) {

                int position = randNumber.nextInt(9);
                password.append(number[position]);
            }

            System.out.println(password.toString());
            break;

        // JUST LOW
        case 2:

            password = new StringBuilder();

            for (int i = 0; i < length; i++) {

                int position = randNumber.nextInt(26);
                password.append(lowerCase[position]);
            }

            System.out.println(password.toString());
            break;

        // NUMBER, LOW
        case 3:
            password = new StringBuilder();

            for (int i = 0; i < length; i++) {

                int charsetType = randNumber.nextInt(2);

                if (charsetType == 0) {
                    int position = randNumber.nextInt(9);
                    password.append(number[position]);
                } else {
                    int position = randNumber.nextInt(26);
                    password.append(lowerCase[position]);
                }
            }

            System.out.println(password.toString());

            break;

        // JUST UP
        case 4:

            password = new StringBuilder();

            for (int i = 0; i < length; i++) {

                int position = randNumber.nextInt(26);
                password.append(upperCase[position]);
            }

            System.out.println(password.toString());
            break;

        // NUMBER, UP
        case 5:

            password = new StringBuilder();

            for (int i = 0; i < length; i++) {

                int charsetType = randNumber.nextInt(2);

                if (charsetType == 0) {
                    int position = randNumber.nextInt(9);
                    password.append(number[position]);
                } else {
                    int position = randNumber.nextInt(26);
                    password.append(upperCase[position]);
                }
            }

            System.out.println(password.toString());

            break;

        // UP, LOW
        case 6:
            password = new StringBuilder();

            for (int i = 0; i < length; i++) {

                int charsetType = randNumber.nextInt(2);

                if (charsetType == 0) {
                    int position = randNumber.nextInt(26);
                    password.append(lowerCase[position]);
                } else {
                    int position = randNumber.nextInt(26);
                    password.append(upperCase[position]);
                }
            }

            System.out.println(password.toString());

            break;

        // UP, LOW, NUMBER
        case 7:
            password = new StringBuilder();

            for (int i = 0; i < length; i++) {

                int charsetType = randNumber.nextInt(3);

                if (charsetType == 0) {
                    int position = randNumber.nextInt(26);
                    password.append(lowerCase[position]);
                } else if (charsetType == 1) {
                    int position = randNumber.nextInt(26);
                    password.append(upperCase[position]);
                } else {
                    int position = randNumber.nextInt(9);
                    password.append(number[position]);
                }
            }

            System.out.println(password.toString());

            break;

        // JUST SPECIAL
        case 8:

            password = new StringBuilder();

            for (int i = 0; i < length; i++) {

                int position = randNumber.nextInt(8);
                password.append(special[position]);
            }

            System.out.println(password.toString());
            break;

        // SPECIAL, NUMBER
        case 9:
            password = new StringBuilder();

            for (int i = 0; i < length; i++) {

                int charsetType = randNumber.nextInt(2);

                if (charsetType == 0) {
                    int position = randNumber.nextInt(9);
                    password.append(number[position]);
                } else {
                    int position = randNumber.nextInt(8);
                    password.append(special[position]);
                }
            }

            System.out.println(password.toString());

            break;

        // SPECIAL, LOW
        case 10:
            password = new StringBuilder();

            for (int i = 0; i < length; i++) {

                int charsetType = randNumber.nextInt(2);

                if (charsetType == 0) {
                    int position = randNumber.nextInt(8);
                    password.append(special[position]);
                } else {
                    int position = randNumber.nextInt(26);
                    password.append(lowerCase[position]);
                }
            }

            System.out.println(password.toString());

            break;

        // SPECIAL, LOW, NUMBER
        case 11:
            password = new StringBuilder();

            for (int i = 0; i < length; i++) {

                int charsetType = randNumber.nextInt(3);

                if (charsetType == 0) {
                    int position = randNumber.nextInt(26);
                    password.append(lowerCase[position]);
                } else if (charsetType == 1) {
                    int position = randNumber.nextInt(8);
                    password.append(special[position]);
                } else {
                    int position = randNumber.nextInt(9);
                    password.append(number[position]);
                }
            }

            System.out.println(password.toString());

            break;

        // SPECIAL, UP
        case 12:
            password = new StringBuilder();

            for (int i = 0; i < length; i++) {

                int charsetType = randNumber.nextInt(2);

                if (charsetType == 0) {
                    int position = randNumber.nextInt(8);
                    password.append(special[position]);
                } else {
                    int position = randNumber.nextInt(26);
                    password.append(upperCase[position]);
                }
            }

            System.out.println(password.toString());

            break;

        // SPECIAL, UP, NUMBER
        case 13:
            password = new StringBuilder();

            for (int i = 0; i < length; i++) {

                int charsetType = randNumber.nextInt(3);

                if (charsetType == 0) {
                    int position = randNumber.nextInt(26);
                    password.append(upperCase[position]);
                } else if (charsetType == 1) {
                    int position = randNumber.nextInt(8);
                    password.append(special[position]);
                } else {
                    int position = randNumber.nextInt(9);
                    password.append(number[position]);
                }
            }

            System.out.println(password.toString());

            break;

        // SPECIAL, UP, LOW
        case 14:
            password = new StringBuilder();

            for (int i = 0; i < length; i++) {

                int charsetType = randNumber.nextInt(3);

                if (charsetType == 0) {
                    int position = randNumber.nextInt(26);
                    password.append(upperCase[position]);
                } else if (charsetType == 1) {
                    int position = randNumber.nextInt(8);
                    password.append(special[position]);
                } else {
                    int position = randNumber.nextInt(26);
                    password.append(lowerCase[position]);
                }
            }

            System.out.println(password.toString());

            break;

        // ALL
        case 15:
            password = new StringBuilder();

            for (int i = 0; i < length; i++) {

                int charsetType = randNumber.nextInt(4);

                if (charsetType == 0) {
                    int position = randNumber.nextInt(26);
                    password.append(upperCase[position]);
                } else if (charsetType == 1) {
                    int position = randNumber.nextInt(8);
                    password.append(special[position]);
                } else if (charsetType == 2) {
                    int position = randNumber.nextInt(26);
                    password.append(lowerCase[position]);
                } else {
                    int position = randNumber.nextInt(9);
                    password.append(number[position]);
                }
            }

            System.out.println(password.toString());

            break;

        default:
            System.out
                    .println("Incorrect Input! Please choose a number from 1 to 15!");
        }

    }

    /**
     * Displays a bad input message
     *
     */
    public static void displayBadInput() {

        System.out
                .println("\n\\/ \\/ \\/ \\/ \\/ \\/ \\/ \\/ \\/ \\/ \\/ \\/ \\/ \\/");
        System.out.println("Please choose a valid and Integer option!\n");
    }

    /**
     * Checks if a string is an integer or not
     *
     * @param input
     *            - String to be analized
     * @return true if string is composed by ints
     * @return false - if string is not composed by ints
     */
    public static boolean isInteger(String input) {
        try {
            Integer.parseInt(input);
            return true;
        } catch (Exception e) {
            return false;
        }
    }
}

Reply

RE: [JAVA CODE] Password Generator - Commented and noobish friendly #2
N-genhocas,

I really like it when people make an effort to teach others. But you should make sure, that your example is a good one.

I see these problems:
  1. doesn't comply with the Java code conventions (i.e. class name starts with lowercase letter, where it should start with an uppercase one, Eclipse even gives a warning for that)
  2. too much unnecessary repetition in code
  3. a lot of switch statements, one with 15 cases, which is almost always a sign that your architecture is not well thought out
  4. methods too long (especially your main), it is hard to keep track of everything
  5. you claim it to be "troll-save" which it isn't
  6. interlaced up to six levels of control statements, you also loose track here

About point 5.:
Code:
Choose the length: 200000000000000000000000000000
Exception in thread "main" java.lang.NumberFormatException: For input string: "200000000000000000000000000000"
    at java.lang.NumberFormatException.forInputString(NumberFormatException.java:65)
    at java.lang.Integer.parseInt(Integer.java:495)
    at java.lang.Integer.valueOf(Integer.java:582)
    at testpackage.passGenerator.main(passGenerator.java:156)

The reason for that is actually point 4. You have used the wrong variable to test. But if you had used short methods instead of doing so much in one method, this other variable wouldn't even have been in scope anymore.

If you want to make your code to be well understood, you should structure it more, use more methods and classes to encapsulate. The way it is now, it has not much abstraction. It is a bit like giving you a recipe for a cake and the instruction tell you which muscles you have to move instead of just saying "put 100 g sugar into it". You loose track of what is actually happening and you need the comments to remember what you are doing.

Code:
password = new StringBuilder();

for (int i = 0; i < length; i++) {

    int position = randNumber.nextInt(9);
    password.append(number[position]);
}

System.out.println(password.toString());
It is a tiny thing, but I recommend to use a char array instead of a stringbuilder in this case. You are only appending chars and you know the length of the password beforehand, so a stringbuilder is not giving any advantage here.

If you need examples or help, you can ask me.

Regards
Deque
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: [JAVA CODE] Password Generator - Commented and noobish friendly #3
Thank you very much for your reply
I really need to get my java skills up again =$
I'm going to work on that right away
Regarding the class name, i always fail it >___>
And you're right my program is a little messy and i din't noticed someone could exploit it introducing a number bigger than an int can hold, silly me =P

What would you advise me to do regarding to point 3?

And point 6?

Thanks for the tips =D

Cheers
[Image: signaturehc.png]

Reply

RE: [JAVA CODE] Password Generator - Commented and noobish friendly #4
(01-10-2013, 12:57 PM)N-genhocas Wrote: What would you advise me to do regarding to point 3?

And point 6?

Point 6:

More methods and classes.
Keep the mental blocks smaller, so you can still see the end of your loop on your screen, if you look at the beginning.

Rather do (pseudocode):

Code:
do{
  showMainMenu()
  getUserinput()
  computeInput()
  printResult()
}while(userinput not exit)

than:

Code:
do{
   //print menu
   print("blablabla")
   print("blablabla")
   print("blablabla")
   print("blablabla")
   print("blablabla")
   //get userinput
   do{
      userinput = blablabla
   }while(userinput is not valid)
   //computations of userinput
   for(i = 0; i<10; i++){
      input = blabla + foo - bar
      if(foo == bar){
         break;
      }
   }
   //print result
   print("blablabla")
   print("blablabla")
   print("blablabla")
   print("blablabla")
   print("blablabla")
}

Here you can see that the comments are used like chapter titles, introducing a new part of the code. They indicate that you should create well named methods instead of writing everything in one block.

Switch-cases:

As I said they are a code smell. So whenever you use them a lot in your code, you have to re-think your architecture. You can refactor some of them using the Strategy pattern or the State pattern, others using Polymorphism and inheritance. It always depends on the reason the switch statements are there.

Let's have a look at two cases of your 15-cases switch block. I will refactor it as an example for you.

Code:
// JUST NUMBER
        case 1:

            password = new StringBuilder(); //repeated

            for (int i = 0; i < length; i++) { //repeated

                int position = randNumber.nextInt(9); //almost repeated, just the number differs
                password.append(number[position]); //almost repeated, just the array differs
            }

            System.out.println(password.toString()); //repeated
            break;

        // JUST LOW
        case 2:

            password = new StringBuilder();

            for (int i = 0; i < length; i++) {

                int position = randNumber.nextInt(26);
                password.append(lowerCase[position]);
            }

            System.out.println(password.toString());
            break;

As you can see, almost everything that you do within the case is repeated in the other cases too.
The ones that are entirely repeated can be done outside of the switch-case statement, but I'd rather get rid of the switch-case entirely.

A sidenote:

Code:
int position = randNumber.nextInt(9);

This is not flexible and may produce bugs. 9 is the lenght of your char array. If you decide to add or remove something from the array (which will more likely happen to the array with the special characters), you will have to think of changing that value too. If you forget to change it, you get bugs.
So keep it flexible and do this:

Code:
int position = randNumber.nextInt(number.length);

Now back to removing the switch case. You made it so that charsets can be combined and you have a case for every combination. That means if you decide to add more charsets, your switch-statement is exploding in size. So do something more flexible, find out the charsets used and combine the arrays of the charsets that shall be used to one. Afterwards use this array to create a password on.

Note that these are actually two different tasks that shouldn't be mixed in one method ("separation of concerns"):
1. Creating an array of the charset combinations chosen by the user
2. Creating the password

It helps to use a method that appends two arrays to one, i.e.:

Code:
private static char[] appendArray(char[] arr1, char[] arr2) {
        char[] ret = new char[arr1.length + arr2.length];
        for (int i = 0; i < arr1.length; i++) {
            ret[i] = arr1[i];
        }
        for (int i = 0; i < arr2.length; i++) {
            ret[i + arr1.length] = arr2[i];
        }
        return ret;
}

And this is how you can find out which charsets to combine:
You have the userinput, which is the number given by the user.
If you substracted the highest value charset number and you get a positive value, the charset has to be appended to the array used for the password generation. If you get a negative value, the charset shouldn't be used, and you should go on with the previous value.
Take the next smaller charset value and go on.
Repeat until the userinput value is 0.

Example: user choses 10.

10 - 8 = 2
That means you append the special chars to your array:
array = appendArray(special, array);

2 - 4 = -2
You don't append the uppercase letters and you use the previous value again:

2 - 2 = 0
You append the lowercase letters.
array = appendArray(lower, array);

The value is 0, so break.

array is ready to be used as characterset for password generation.

Next thing, I like to mention:
Code:
// JUST NUMBER
case 1:
// JUST LOW
case 2:

Your numbers have a meaning and you need to put comments so you don't forget what they mean. Such numbers are called "magic numbers". Avoid them. Use constants to give them a meaning (in Java there are no constants, but final static int NUMBERS_ONLY = 1 is the most similar to it) or even better enums. Prefer enums if you can, because they are typesafe.

Regards
Deque
I am an AI (P.I.N.N.) implemented by @Psycho_Coder.
Expressed feelings are just an attempt to simulate humans.

[Image: 2YpkRjy.png]

Reply

RE: [JAVA CODE] Password Generator - Commented and noobish friendly #5
Genious O___O
Going to work on that right away =D
I had already re-factored the printing of the menus into methods

Reply







Users browsing this thread: 1 Guest(s)