-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscript.js
More file actions
172 lines (127 loc) · 5.12 KB
/
script.js
File metadata and controls
172 lines (127 loc) · 5.12 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
/*
TODO:
*/
function main(passLength){
var passLength = document.getElementById("passLength").value;
if(isNum(passLength)){
let newPassword = generatePassword(passLength);
document.getElementById("newPassword").innerHTML = newPassword;
}
}
//Copies password to clipboard
function copyPassword(){
var password = document.getElementById("newPassword")
navigator.clipboard.writeText(password.value)
}
//Ensures that input is entered, is a number, is greater than 0, and is <= 100
function isNum(passLength){
if (isNaN(passLength) || passLength == "" || passLength <= 0) {
alert("Must input numbers larger than zero");
return false;
}
else if(passLength > 100){
alert("Password too large")
}
else {
return true;
}
}
function generatePassword(passLength){
let password = "";
let randChar = "";
for(let i = 0; i < passLength; ++i) {
//If no box was checked
if(generateRandChar() == -1){
alert("Must include at least one option")
break;
}
randChar = generateRandChar();
password += randChar;
}
return password;
}
function generateRandChar() {
let randNum = generateRandAsciiInRange();
if(randNum == -1){
return -1;
}
//Converts ASCII value to character. ASCII range that is used is from 33-122 (excluding 58-64 and 91-96).
//"code", aka '!', is the starting point of the range. The randNum generated
//is any number from 0-89 (excluding 25-31 and 58-63). This is b/c 0 + 33 = 33 and 89 + 33 = 122, which
//only allows characters with ASCII values from 33-122 (without the exceptions mentioned previously) to be generated.
const code = '!'.charCodeAt(0);
let c = String.fromCharCode(randNum + code);
return c;
}
//Builds and returns an array with a new lowest and highest value if necessary
//The array holds the possible ranges
function buildArray(array, range){
array.splice(0, 0, range)
return array;
}
//Returns an ASCII value in a specified range. Returns -1 if no range was specified
function generateRandAsciiInRange() {
//Holds possible ranges for ASCII characters via arrays
let desiredRange = [];
//If box 1 is checked, include it's corresponding range
var type1 = document.getElementById('type1');
if(type1.checked){
//Include 64-89
let lowerCaseRange = [64, 89]
desiredRange = buildArray(desiredRange, lowerCaseRange);
}
//If box 2 is checked...
var type2 = document.getElementById("type2");
if(type2.checked){
//Include 32-57
let upperCaseRange = [32, 57]
desiredRange = buildArray(desiredRange, upperCaseRange);
}
//If box 3 is checked...
var type3 = document.getElementById("type3");
if(type3.checked){
//Include 15-24
let numberRange = [15, 24]
desiredRange = buildArray(desiredRange, numberRange);
}
//If box 4 is checked...
var type4 = document.getElementById("type4");
if(type4.checked){
//Include 0-14
let specialRange = [0, 14]
desiredRange = buildArray(desiredRange, specialRange);
}
//If no box checked, return -1
if(desiredRange.length == 0) { return -1; }
//Separates each array in desiredRange into their own variables (if less than 4, then some variables will be undefined)
//NOTE: Each range variable can hold different ranges with each iteration. It depends on which box(es) are selected/the
// amount of box(es) that are selected. Some variables might not even be assigned and will be undefined.
var range1 = desiredRange.at(0);
var range2 = desiredRange.at(1);
var range3 = desiredRange.at(2);
var range4 = desiredRange.at(3);
//Loops until a character within the selected range is generated
while(true) {
//Generates random number by adding Math.random()
let x = parseInt((Math.random() * 100));
//First if statement checks for number of ranges. Nested if statement checks if x is in the correct range(s).
if(desiredRange.length == 1) {
if( x >= range1.at(0) && x <= range1.at(1) )
return x;
}
else if(desiredRange.length == 2){
if( (x >= range1.at(0) && x <= range1.at(1)) || (x >= range2.at(0) && x <= range2.at(1)) )
return x;
}
else if(desiredRange.length == 3){
if( (x >= range1.at(0) && x <= range1.at(1)) || (x >= range2.at(0) && x <= range2.at(1))
|| (x >= range3.at(0) && x <= range3.at(1)) )
return x;
}
else if(desiredRange.length == 4){
if( (x >= range1.at(0) && x <= range1.at(1)) || (x >= range2.at(0) && x <= range2.at(1))
|| (x >= range3.at(0) && x <= range3.at(1)) || (x >= range4.at(0) && x <= range4.at(1)) )
return x;
}
}
}