Skip to content

Commit c9aa0cc

Browse files
authored
Add files via upload
1 parent 9897ad4 commit c9aa0cc

16 files changed

+393
-0
lines changed
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
2+
3+
4+
public class Alphabet {
5+
6+
public static final String UPPERCASE_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
7+
public static final String LOWERCASE_LETTERS = "abcdefghijklmnopqrstuvwxyz";
8+
public static final String NUMBERS = "1234567890";
9+
public static final String SYMBOLS = "!@#$%^&*()-_=+\\/~?";
10+
11+
private final StringBuilder pool;
12+
13+
14+
public Alphabet(boolean uppercaseIncluded, boolean lowercaseIncluded, boolean numbersIncluded, boolean specialCharactersIncluded) {
15+
16+
pool = new StringBuilder();
17+
18+
if (uppercaseIncluded) pool.append(UPPERCASE_LETTERS);
19+
20+
if (lowercaseIncluded) pool.append(LOWERCASE_LETTERS);
21+
22+
if (numbersIncluded) pool.append(NUMBERS);
23+
24+
if (specialCharactersIncluded) pool.append(SYMBOLS);
25+
26+
}
27+
28+
public String getAlphabet() {
29+
return pool.toString();
30+
}
31+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
import java.util.Objects;
2+
import java.util.Scanner;
3+
4+
public class Generator {
5+
Alphabet alphabet;
6+
public static Scanner keyboard;
7+
8+
public Generator(Scanner scanner) {
9+
keyboard = scanner;
10+
}
11+
12+
public Generator(boolean IncludeUpper, boolean IncludeLower, boolean IncludeNum, boolean IncludeSym) {
13+
alphabet = new Alphabet(IncludeUpper, IncludeLower, IncludeNum, IncludeSym);
14+
}
15+
16+
public void mainLoop() {
17+
System.out.println("Welcome to Ziz Password Services :)");
18+
printMenu();
19+
20+
String userOption = "-1";
21+
22+
while (!userOption.equals("4")) {
23+
24+
userOption = keyboard.next();
25+
26+
switch (userOption) {
27+
case "1" -> {
28+
requestPassword();
29+
printMenu();
30+
}
31+
case "2" -> {
32+
checkPassword();
33+
printMenu();
34+
}
35+
case "3" -> {
36+
printUsefulInfo();
37+
printMenu();
38+
}
39+
case "4" -> printQuitMessage();
40+
default -> {
41+
System.out.println();
42+
System.out.println("Kindly select one of the available commands");
43+
printMenu();
44+
}
45+
}
46+
}
47+
}
48+
49+
private Password GeneratePassword(int length) {
50+
final StringBuilder pass = new StringBuilder("");
51+
52+
final int alphabetLength = alphabet.getAlphabet().length();
53+
54+
int max = alphabetLength - 1;
55+
int min = 0;
56+
int range = max - min + 1;
57+
58+
for (int i = 0; i < length; i++) {
59+
int index = (int) (Math.random() * range) + min;
60+
pass.append(alphabet.getAlphabet().charAt(index));
61+
}
62+
63+
return new Password(pass.toString());
64+
}
65+
66+
private void printUsefulInfo() {
67+
System.out.println();
68+
System.out.println("Use a minimum password length of 8 or more characters if permitted");
69+
System.out.println("Include lowercase and uppercase alphabetic characters, numbers and symbols if permitted");
70+
System.out.println("Generate passwords randomly where feasible");
71+
System.out.println("Avoid using the same password twice (e.g., across multiple user accounts and/or software systems)");
72+
System.out.println("Avoid character repetition, keyboard patterns, dictionary words, letter or number sequences," +
73+
"\nusernames, relative or pet names, romantic links (current or past) " +
74+
"and biographical information (e.g., ID numbers, ancestors' names or dates).");
75+
System.out.println("Avoid using information that the user's colleagues and/or " +
76+
"acquaintances might know to be associated with the user");
77+
System.out.println("Do not use passwords which consist wholly of any simple combination of the aforementioned weak components");
78+
}
79+
80+
private void requestPassword() {
81+
boolean IncludeUpper = false;
82+
boolean IncludeLower = false;
83+
boolean IncludeNum = false;
84+
boolean IncludeSym = false;
85+
86+
boolean correctParams = false;
87+
88+
System.out.println();
89+
System.out.println("Hello, welcome to the Password Generator :) answer"
90+
+ " the following questions by Yes or No \n");
91+
92+
do {
93+
System.out.println("Do you want Lowercase letters \"abcd...\" to be used? ");
94+
String input = keyboard.nextLine();
95+
96+
if (isInclude(input)) IncludeLower = true;
97+
98+
System.out.println("Do you want Uppercase letters \"ABCD...\" to be used? ");
99+
input = keyboard.nextLine();
100+
101+
if (isInclude(input)) IncludeUpper = true;
102+
103+
System.out.println("Do you want Numbers \"1234...\" to be used? ");
104+
input = keyboard.nextLine();
105+
106+
if (isInclude(input)) IncludeNum = true;
107+
108+
System.out.println("Do you want Symbols \"!@#$...\" to be used? ");
109+
input = keyboard.nextLine();
110+
111+
if (isInclude(input)) IncludeSym = true;
112+
113+
//No Pool Selected
114+
if (!IncludeUpper && !IncludeLower && !IncludeNum && !IncludeSym) {
115+
System.out.println("You have selected no characters to generate your " +
116+
"password at least one of your answers should be Yes");
117+
correctParams = true;
118+
}
119+
120+
System.out.println("Great! Now enter the length of the password");
121+
int length = keyboard.nextInt();
122+
123+
final Generator generator = new Generator(IncludeUpper, IncludeLower, IncludeNum, IncludeSym);
124+
final Password password = generator.GeneratePassword(length);
125+
126+
System.err.println("Your generated password -> " + password);
127+
128+
} while (correctParams);
129+
}
130+
131+
private boolean isInclude(String Input) {
132+
if (Input.equalsIgnoreCase("yes")) {
133+
return true;
134+
} else {
135+
if (!Input.equalsIgnoreCase("no")) {
136+
PasswordRequestError();
137+
}
138+
return false;
139+
}
140+
}
141+
142+
private void PasswordRequestError() {
143+
System.out.println("You have entered something incorrect let's go over it again \n");
144+
}
145+
146+
private void checkPassword() {
147+
String input;
148+
final Scanner in = new Scanner(System.in);
149+
150+
System.out.print("\nEnter your password:");
151+
input = in.nextLine();
152+
153+
final Password p = new Password(input);
154+
155+
System.out.println(p.calculateScore());
156+
157+
in.close();
158+
}
159+
160+
private void printMenu() {
161+
System.out.println();
162+
System.out.println("Enter 1 - Password Generator");
163+
System.out.println("Enter 2 - Password Strength Check");
164+
System.out.println("Enter 3 - Useful Information");
165+
System.out.println("Enter 4 - Quit");
166+
System.out.print("Choice:");
167+
}
168+
169+
private void printQuitMessage() {
170+
System.out.println("Closing the program bye bye!");
171+
}
172+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import static org.junit.jupiter.api.Assertions.*;
2+
3+
import org.junit.jupiter.api.Test;
4+
5+
class GeneratorTest {
6+
7+
private final Password password= new Password("Secret");
8+
private final Alphabet firstAlphabet = new Alphabet(true,false,false,false);
9+
private final Alphabet secondAlphabet = new Alphabet(false,true,true,true);
10+
private final Generator generator = new Generator(true,false,false,false);
11+
// private final Password generatedPassword = generator.GeneratePassword(4);
12+
13+
@Test
14+
void test1() {
15+
assertEquals("Secret", password.toString());
16+
}
17+
18+
@Test
19+
void test2() {
20+
assertEquals(firstAlphabet.getAlphabet(), Alphabet.UPPERCASE_LETTERS);
21+
}
22+
23+
@Test
24+
void test3() {
25+
assertEquals(secondAlphabet.getAlphabet(), Alphabet.LOWERCASE_LETTERS + Alphabet.NUMBERS + Alphabet.SYMBOLS);
26+
}
27+
28+
@Test
29+
void test4() {
30+
assertEquals(generator.alphabet.getAlphabet(), Alphabet.UPPERCASE_LETTERS);
31+
}
32+
33+
@Test
34+
void test5() {
35+
assertEquals(generator.alphabet.getAlphabet().length(), 26);
36+
}
37+
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import java.util.Scanner;
2+
3+
public class Main {
4+
5+
public static final Scanner keyboard = new Scanner(System.in);
6+
7+
public static void main(String[] args) {
8+
Generator generator = new Generator(keyboard);
9+
generator.mainLoop();
10+
keyboard.close();
11+
}
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
2+
public class Password {
3+
String Value;
4+
int Length;
5+
6+
public Password(String s) {
7+
Value = s;
8+
Length = s.length();
9+
}
10+
11+
public int CharType(char C) {
12+
int val;
13+
14+
// Char is Uppercase Letter
15+
if ((int) C >= 65 && (int) C <= 90)
16+
val = 1;
17+
18+
// Char is Lowercase Letter
19+
else if ((int) C >= 97 && (int) C <= 122) {
20+
val = 2;
21+
}
22+
23+
// Char is Digit
24+
else if ((int) C >= 60 && (int) C <= 71) {
25+
val = 3;
26+
}
27+
28+
// Char is Symbol
29+
else {
30+
val = 4;
31+
}
32+
33+
return val;
34+
}
35+
36+
public int PasswordStrength() {
37+
String s = this.Value;
38+
boolean UsedUpper = false;
39+
boolean UsedLower = false;
40+
boolean UsedNum = false;
41+
boolean UsedSym = false;
42+
int type;
43+
int Score = 0;
44+
45+
for (int i = 0; i < s.length(); i++) {
46+
char c = s.charAt(i);
47+
type = CharType(c);
48+
49+
if (type == 1) UsedUpper = true;
50+
if (type == 2) UsedLower = true;
51+
if (type == 3) UsedNum = true;
52+
if (type == 4) UsedSym = true;
53+
}
54+
55+
if (UsedUpper) Score += 1;
56+
if (UsedLower) Score += 1;
57+
if (UsedNum) Score += 1;
58+
if (UsedSym) Score += 1;
59+
60+
if (s.length() >= 8) Score += 1;
61+
if (s.length() >= 16) Score += 1;
62+
63+
return Score;
64+
}
65+
66+
public String calculateScore() {
67+
int Score = this.PasswordStrength();
68+
69+
if (Score == 6) {
70+
return "This is a very good password :D check the Useful Information section to make sure it satisfies the guidelines";
71+
} else if (Score >= 4) {
72+
return "This is a good password :) but you can still do better";
73+
} else if (Score >= 3) {
74+
return "This is a medium password :/ try making it better";
75+
} else {
76+
return "This is a weak password :( definitely find a new one";
77+
}
78+
}
79+
80+
@Override
81+
public String toString() {
82+
return Value;
83+
}
84+
}
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" inherit-compiler-output="true">
4+
<exclude-output />
5+
<content url="file://$MODULE_DIR$">
6+
<sourceFolder url="file://$MODULE_DIR$/Password Generator/src" isTestSource="false" />
7+
</content>
8+
<orderEntry type="inheritedJdk" />
9+
<orderEntry type="sourceFolder" forTests="false" />
10+
<orderEntry type="module-library">
11+
<library name="JUnit5.8.1">
12+
<CLASSES>
13+
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter/5.8.1/junit-jupiter-5.8.1.jar!/" />
14+
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-api/5.8.1/junit-jupiter-api-5.8.1.jar!/" />
15+
<root url="jar://$MAVEN_REPOSITORY$/org/opentest4j/opentest4j/1.2.0/opentest4j-1.2.0.jar!/" />
16+
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-commons/1.8.1/junit-platform-commons-1.8.1.jar!/" />
17+
<root url="jar://$MAVEN_REPOSITORY$/org/apiguardian/apiguardian-api/1.1.2/apiguardian-api-1.1.2.jar!/" />
18+
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-params/5.8.1/junit-jupiter-params-5.8.1.jar!/" />
19+
<root url="jar://$MAVEN_REPOSITORY$/org/junit/jupiter/junit-jupiter-engine/5.8.1/junit-jupiter-engine-5.8.1.jar!/" />
20+
<root url="jar://$MAVEN_REPOSITORY$/org/junit/platform/junit-platform-engine/1.8.1/junit-platform-engine-1.8.1.jar!/" />
21+
</CLASSES>
22+
<JAVADOC />
23+
<SOURCES />
24+
</library>
25+
</orderEntry>
26+
</component>
27+
</module>

0 commit comments

Comments
 (0)