-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathentropy.js
66 lines (54 loc) · 1.46 KB
/
entropy.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
63
64
65
66
const constants = require("./const");
const getLength = require("./length");
const getEntropy = (password) => {
const base = getBase(password);
const length = getLength(password);
return Math.log2(Math.pow(base, length));
};
const getBase = (password) => {
let isLowercase = false;
let isUppercase = false;
let isDigits = false;
let isReplacements = false;
let isSeparators = false;
let isSpecials = false;
let baseCount = 0;
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;
} else {
baseCount++;
}
}
if (isLowercase) {
baseCount += constants.lowercase.length;
}
if (isUppercase) {
baseCount += constants.uppercase.length;
}
if (isDigits) {
baseCount += constants.digits.length;
}
if (isReplacements) {
baseCount += constants.replacements.length;
}
if (isSeparators) {
baseCount += constants.separators.length;
}
if (isSpecials) {
baseCount += constants.specials.length;
}
return baseCount;
};
module.exports = getEntropy;