-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
57 lines (54 loc) · 1.61 KB
/
index.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
const constants = require("./const");
const getEntropy = require("./entropy");
exports.passwordValidator = (password, minEntropy) => {
const entropy = getEntropy(password);
const res = {};
if (entropy >= minEntropy) {
res.strong = true;
} else {
res.strong = false;
const messages = [];
let isLowercase = false;
let isUppercase = false;
let isDigits = false;
let isReplacements = false;
let isSeparators = false;
let isSpecials = false;
for (let i = 0; i < password.length; i++) {
let c = password[i];
if (constants.lowercase.includes(c)) {
isLowercase = true;
} else if (constants.uppercase.includes(c)) {
isUppercase = true;
} else if (constants.digits.includes(c)) {
isDigits = true;
} else if (constants.replacements.includes(c)) {
isReplacements = true;
} else if (constants.separators.includes(c)) {
isSeparators = true;
} else if (constants.specials.includes(c)) {
isSpecials = true;
}
}
if (!isSpecials || !isReplacements || !isSeparators) {
messages.push("including more special characters");
}
if (!isDigits) {
messages.push("including more digits");
}
if (!isLowercase) {
messages.push("using lowercase alphabets");
}
if (!isUppercase) {
messages.push("using uppercase alphabets");
}
if (messages.length > 0) {
res.error = `insecure password try ${messages.join(
" "
)} or using a longer password`;
} else {
res.error = "insecure password, try using a longer password";
}
}
return res;
};