-
Notifications
You must be signed in to change notification settings - Fork 11
/
password_generator.java
172 lines (133 loc) · 5.89 KB
/
password_generator.java
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
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 = false;
System.out.println();
System.out.println("Hello, welcome to the Password Generator :) answer"
+ " the following questions by Yes or No \n");
do {
System.out.println("Do you want Lowercase letters \"abcd...\" to be used? ");
String input = keyboard.nextLine();
if (isInclude(input)) IncludeLower = true;
System.out.println("Do you want Uppercase letters \"ABCD...\" to be used? ");
input = keyboard.nextLine();
if (isInclude(input)) IncludeUpper = true;
System.out.println("Do you want Numbers \"1234...\" to be used? ");
input = keyboard.nextLine();
if (isInclude(input)) IncludeNum = true;
System.out.println("Do you want Symbols \"!@#$...\" to be used? ");
input = keyboard.nextLine();
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");
correctParams = true;
}
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);
} while (correctParams);
}
private boolean isInclude(String Input) {
if (Input.equalsIgnoreCase("yes")) {
return true;
} else {
if (!Input.equalsIgnoreCase("no")) {
PasswordRequestError();
}
return false;
}
}
private void PasswordRequestError() {
System.out.println("You have entered something incorrect let's go over it again \n");
}
private void checkPassword() {
String input;
final Scanner in = new Scanner(System.in);
System.out.print("\nEnter your password:");
input = in.nextLine();
final Password p = new Password(input);
System.out.println(p.calculateScore());
in.close();
}
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!");
}
}