-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGenerator.java
More file actions
184 lines (145 loc) · 6.42 KB
/
Generator.java
File metadata and controls
184 lines (145 loc) · 6.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
import java.util.Objects;
import java.util.Scanner;
public class Generator {
Alphabet alphabet;
public static Scanner keyboard;
public Generator(Scanner scanner) {
keyboard = scanner;
}
public Generator(boolean IncludeUpper, boolean IncludeLower, boolean IncludeNum, boolean IncludeSym) {
alphabet = new Alphabet(IncludeUpper, IncludeLower, IncludeNum, IncludeSym);
}
public void mainLoop() {
System.out.println("Welcome to Ziz Password Services :)");
printMenu();
String userOption = "-1";
while (!userOption.equals("4")) {
userOption = keyboard.next();
switch (userOption) {
case "1" -> {
requestPassword();
printMenu();
}
case "2" -> {
checkPassword();
printMenu();
}
case "3" -> {
printUsefulInfo();
printMenu();
}
case "4" -> printQuitMessage();
default -> {
System.out.println();
System.out.println("Kindly select one of the available commands");
printMenu();
}
}
}
}
private Password GeneratePassword(int length) {
final StringBuilder pass = new StringBuilder("");
final int alphabetLength = alphabet.getAlphabet().length();
int max = alphabetLength - 1;
int min = 0;
int range = max - min + 1;
for (int i = 0; i < length; i++) {
int index = (int) (Math.random() * range) + min;
pass.append(alphabet.getAlphabet().charAt(index));
}
return new Password(pass.toString());
}
private void printUsefulInfo() {
System.out.println();
System.out.println("Use a minimum password length of 8 or more characters if permitted");
System.out.println("Include lowercase and uppercase alphabetic characters, numbers and symbols if permitted");
System.out.println("Generate passwords randomly where feasible");
System.out.println("Avoid using the same password twice (e.g., across multiple user accounts and/or software systems)");
System.out.println("Avoid character repetition, keyboard patterns, dictionary words, letter or number sequences," +
"\nusernames, relative or pet names, romantic links (current or past) " +
"and biographical information (e.g., ID numbers, ancestors' names or dates).");
System.out.println("Avoid using information that the user's colleagues and/or " +
"acquaintances might know to be associated with the user");
System.out.println("Do not use passwords which consist wholly of any simple combination of the aforementioned weak components");
}
private void requestPassword() {
boolean IncludeUpper = false;
boolean IncludeLower = false;
boolean IncludeNum = false;
boolean IncludeSym = false;
boolean correctParams;
System.out.println();
System.out.println("Hello, welcome to the Password Generator :) answer"
+ " the following questions by Yes or No \n");
do {
String input;
correctParams = false;
do {
System.out.println("Do you want Lowercase letters \"abcd...\" to be used? ");
input = keyboard.next();
PasswordRequestError(input);
} while (!input.equalsIgnoreCase("yes") && !input.equalsIgnoreCase("no"));
if (isInclude(input)) IncludeLower = true;
do {
System.out.println("Do you want Uppercase letters \"ABCD...\" to be used? ");
input = keyboard.next();
PasswordRequestError(input);
} while (!input.equalsIgnoreCase("yes") && !input.equalsIgnoreCase("no"));
if (isInclude(input)) IncludeUpper = true;
do {
System.out.println("Do you want Numbers \"1234...\" to be used? ");
input = keyboard.next();
PasswordRequestError(input);
} while (!input.equalsIgnoreCase("yes") && !input.equalsIgnoreCase("no"));
if (isInclude(input)) IncludeNum = true;
do {
System.out.println("Do you want Symbols \"!@#$...\" to be used? ");
input = keyboard.next();
PasswordRequestError(input);
} while (!input.equalsIgnoreCase("yes") && !input.equalsIgnoreCase("no"));
if (isInclude(input)) IncludeSym = true;
//No Pool Selected
if (!IncludeUpper && !IncludeLower && !IncludeNum && !IncludeSym) {
System.out.println("You have selected no characters to generate your " +
"password, at least one of your answers should be Yes\n");
correctParams = true;
}
} while (correctParams);
System.out.println("Great! Now enter the length of the password");
int length = keyboard.nextInt();
final Generator generator = new Generator(IncludeUpper, IncludeLower, IncludeNum, IncludeSym);
final Password password = generator.GeneratePassword(length);
System.err.println("Your generated password -> " + password);
}
private boolean isInclude(String Input) {
if (Input.equalsIgnoreCase("yes")) {
return true;
}
else {
return false;
}
}
private void PasswordRequestError(String i) {
if (!i.equalsIgnoreCase("yes") && !i.equalsIgnoreCase("no")) {
System.out.println("You have entered something incorrect let's go over it again \n");
}
}
private void checkPassword() {
String input;
System.out.print("\nEnter your password:");
input = keyboard.next();
final Password p = new Password(input);
System.out.println(p.calculateScore());
}
private void printMenu() {
System.out.println();
System.out.println("Enter 1 - Password Generator");
System.out.println("Enter 2 - Password Strength Check");
System.out.println("Enter 3 - Useful Information");
System.out.println("Enter 4 - Quit");
System.out.print("Choice:");
}
private void printQuitMessage() {
System.out.println("Closing the program bye bye!");
}
}