-
Notifications
You must be signed in to change notification settings - Fork 1
/
passwordGenerator.js
62 lines (57 loc) · 1.52 KB
/
passwordGenerator.js
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
// Set static variables
var abcUpper = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
var abcUpperArr = abcUpper.split("");
var abcLower = "abcdefghijklmnopqrstuvwxyz";
var abcLowerArr = abcLower.split("");
var num = "0123456789";
var numArr = num.split("");
var sym = "!#$%&\()*+,-./:;<=>?@^[\\]^_`{|}~";
var symArr = sym.split("");
var passCharsArr = [];
// Set variables to check user option
var passLength;
var isUpper;
var isLower;
var isNum;
var isSym;
// Validate user options
if (passLength < 8 || passLength > 128){
isLengthValid = false;
} else {
isLengthValid = true;
if (isUpper || isLower || isNum || isSym) {
isInputValid = true;
} else {
isInputValid = false;
}
}
// Create array for the range of password characters
if (isLengthValid && isInputValid) {
isReqValid = true; // requirement to generate password Valid
if (isUpper) {
Array.prototype.push.apply(passCharsArr,abcUpperArr);
}
if (isLower) {
Array.prototype.push.apply(passCharsArr,abcLowerArr);
}
if (isNum) {
Array.prototype.push.apply(passCharsArr,numArr);
}
if (isSym) {
Array.prototype.push.apply(passCharsArr,symArr);
}
} else {
isReqValid = false; // requirement to generate password NOT valid
}
// Generate the password
genPass(passLength,passCharsArr);
/**************************** FUNCTIONS ****************************/
function genPass(length,charArr) {
// Generate password
var result = "";
for (i = 0; i < length; i++) {
idx = Math.floor(Math.random()*charArr.length);
result += charArr[idx];
}
return result;
}