-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
43 lines (35 loc) · 1.12 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
exports.generate = (lenght, special) => {
var retVal = "";
var length = parseInt(lenght, 10);
var charset = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
if (special === true) {
charset += "!@#$%^&*()_+~`|}{[]:;?><,./-=";
}
for (var i = 0, n = charset.length; i < length; ++i) {
retVal += charset.charAt(Math.floor(Math.random() * n));
}
// Vérifier que le mot de passe ait bien la longueur demandée
if (retVal.length != length) {
retVal = generate(length, special);
}
return retVal;
}
exports.strenght = (password) => {
let strength = 0;
if (password.length >= 8 && password.length <= 12) {
strength += 10;
} else if (password.length > 12) {
strength += 25;
}
let regex = /^(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[@$!%*?&])[A-Za-z\d@$!%*?&]+$/;
if (regex.test(password)) {
strength += 25;
}
if (!/(\w)\1{2,}/.test(password)) {
strength += 25;
}
if (!/(012|123|234|345|456|567|678|789)/.test(password)) {
strength += 25;
}
return (strength/75*100).toFixed(2);
}