Skip to content

Commit

Permalink
Adding Password Generator in Java (#63)
Browse files Browse the repository at this point in the history
  • Loading branch information
shivu2002a authored Oct 19, 2022
1 parent 71a3c1f commit 024b464
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 0 deletions.
4 changes: 4 additions & 0 deletions Java/Random Password Generator Java/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
This is a password generator program that allows you to generate passwords as well as mention length of each password.

Here is a screenshot of the welcome screen of the application.

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
86 changes: 86 additions & 0 deletions Java/Random Password Generator Java/src/App.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* This program generates a password of specified length
*/

import java.util.Random;
class App {

// All the capital letters
static final char ALPHA_CAPS[] = { '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'
};

// All the small letters
static final char ALPHA_LOWER[] = { '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'
};

//All the numeric characters
static final char NUMERIC[] = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'};

//All the special characters
static final char SPECIAL[] = { '!', '@', '#', '$', '%', '^', '&', '*', '|', ';', ':' };

// All characters
static final char ALL_CHARS[] = { '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', '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', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9',
'!', '@', '#', '$', '%', '^', '&', '*', '|', ';', ':'
};

public static void main(String[] args) {
System.out.println("Random password generator in Java");
System.out.println(generatePassword(8));
System.out.println(generatePassword(14));
System.out.println(generatePassword(3));
System.out.println(generatePassword(10));
System.out.println(generatePassword(9));

}

/*
* Password generating function
* Input: Password length
* Output: Password string
*/

private static String generatePassword(int length) {
// Assert that given length is that greater than 3
if (length < 4) {
System.out.print("Length of Password to generate must be greater than 3");
return "";
}

char password[] = new char[length];

// Random number generator
Random random = new Random();

// Generate at least one character of each type
password[0] = ALPHA_CAPS[random.nextInt(ALPHA_CAPS.length)];
password[1] = ALPHA_LOWER[random.nextInt(ALPHA_LOWER.length)];
password[2] = NUMERIC[random.nextInt(NUMERIC.length)];
password[3] = SPECIAL[random.nextInt(SPECIAL.length)];

// Generate remaining number of characters
for (int i = 4; i < length; i++) {
password[i] = ALL_CHARS[random.nextInt(ALL_CHARS.length)];
}

// Shuffling all the characters
for (int i = 0; i < password.length; i++) {
int r = random.nextInt(password.length);
char temp = password[r];
password[r] = password[i];
password[i] = temp;
}

// Create the string from password arr
return new String(password);

}
}

0 comments on commit 024b464

Please sign in to comment.