-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerator.java
106 lines (82 loc) · 3.54 KB
/
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
import java.lang.Math;
import java.util.ArrayList;
public class Generator {
// This is the array that stores every generated password in this session.
public ArrayList<String> generatedPasswords = new ArrayList<>();
StringBuilder str = new StringBuilder(); // This is where the generated passwords are assembled.
String modify;
char [] similarChars = {'i', 'I', 'l', 'L', '1', '0', 'o', 'O'};
// Modifiable conditions that the user can change in the settings menu.
boolean firstCharIsNumber = false, firstCharIsSymbol = false;
boolean generateWithSymbols = true, useSimilarChars = false;
int passwordLength = 16;
void generator() {
int randomValue;
char character;
if (firstCharIsNumber) {
if (!useSimilarChars) { // The randomValue equation below excludes 0 and 1.
randomValue = (int) (Math.random() * (57 + 1 - 50) + 50);
} else { // The randomValue equation below includes 0 and 1.
randomValue = (int) (Math.random() * (57 + 1 - 48) + 48);
}
character = (char) randomValue;
str.append(character);
} else if (firstCharIsSymbol) {
while (true) {
randomValue = (int) (Math.random() * (126 + 1 - 48) + 48);
character = (char) randomValue;
if (!(Character.isDigit(character) || Character.isLetter(character))) {
str.append(character);
break;
}
}
}
if (generateWithSymbols) {
while (str.length() < passwordLength) {
randomValue = (int) (Math.random() * (126 + 1 - 48) + 48);
character = (char) randomValue;
str.append(character);
if (!useSimilarChars) {
removeSimilarCharacters(character);
}
}
} else { // Generating without symbols. Only adds numbers and letters.
while (str.length() < passwordLength) {
randomValue = (int) (Math.random() * (126 + 1 - 48) + 48);
character = (char) randomValue;
if (Character.isDigit(character)) {
str.append(character);
} else if (Character.isLetter(character)) {
str.append(character);
}
if (!useSimilarChars) {
removeSimilarCharacters(character);
}
}
}
System.out.println("The generated password is " + str);
generatedPasswords.add(str.toString());
str.delete(0,passwordLength);
}
void history() {
System.out.println("\n---------------------------");
System.out.println(" Password History ");
System.out.println("---------------------------\n");
if (generatedPasswords.isEmpty()) {
System.out.println("Error: No passwords have been generated yet.");
}
int i = 1;
for (String str : generatedPasswords) {
System.out.println(i + ". " + str);
i++;
}
}
void removeSimilarCharacters(char character) { // Removes characters that look similar to other characters.
for (int i = 0; i < similarChars.length; i++) {
if (similarChars[i] == character) {
str.delete(str.length() - 1, character);
break;
}
}
}
}