1 // Password strength meter |
1 // Password strength meter |
2 function passwordStrength(password,username) { |
2 function passwordStrength(password1, username, password2) { |
3 var shortPass = 1, badPass = 2, goodPass = 3, strongPass = 4, symbolSize = 0, natLog, score; |
3 var shortPass = 1, badPass = 2, goodPass = 3, strongPass = 4, mismatch = 5, symbolSize = 0, natLog, score; |
|
4 |
|
5 // password 1 != password 2 |
|
6 if ( (password1 != password2) && password2.length > 0) |
|
7 return mismatch |
4 |
8 |
5 //password < 4 |
9 //password < 4 |
6 if (password.length < 4 ) { return shortPass }; |
10 if ( password1.length < 4 ) |
|
11 return shortPass |
7 |
12 |
8 //password == username |
13 //password1 == username |
9 if (password.toLowerCase()==username.toLowerCase()) return badPass; |
14 if ( password1.toLowerCase() == username.toLowerCase() ) |
|
15 return badPass; |
10 |
16 |
11 if (password.match(/[0-9]/)) symbolSize +=10; |
17 if ( password1.match(/[0-9]/) ) |
12 if (password.match(/[a-z]/)) symbolSize +=26; |
18 symbolSize +=10; |
13 if (password.match(/[A-Z]/)) symbolSize +=26; |
19 if ( password1.match(/[a-z]/) ) |
14 if (password.match(/[^a-zA-Z0-9]/)) symbolSize +=31; |
20 symbolSize +=26; |
|
21 if ( password1.match(/[A-Z]/) ) |
|
22 symbolSize +=26; |
|
23 if ( password1.match(/[^a-zA-Z0-9]/) ) |
|
24 symbolSize +=31; |
15 |
25 |
16 natLog = Math.log( Math.pow(symbolSize,password.length) ); |
26 natLog = Math.log( Math.pow(symbolSize, password1.length) ); |
17 score = natLog / Math.LN2; |
27 score = natLog / Math.LN2; |
18 if (score < 40 ) return badPass |
28 |
19 if (score < 56 ) return goodPass |
29 if (score < 40 ) |
|
30 return badPass |
|
31 |
|
32 if (score < 56 ) |
|
33 return goodPass |
|
34 |
20 return strongPass; |
35 return strongPass; |
21 } |
36 } |