]> WPIA git - gigi.git/blob - src/org/cacert/gigi/util/PasswordStrengthChecker.java
Merge branch 'libs/jetty/upstream' into libs/jetty/local
[gigi.git] / src / org / cacert / gigi / util / PasswordStrengthChecker.java
1 package org.cacert.gigi.util;
2
3 import java.util.regex.Pattern;
4
5 import org.cacert.gigi.User;
6
7 public class PasswordStrengthChecker {
8         static Pattern digits = Pattern.compile("\\d");
9         static Pattern lower = Pattern.compile("[a-z]");
10         static Pattern upper = Pattern.compile("[A-Z]");
11         static Pattern whitespace = Pattern.compile("\\s");
12         static Pattern special = Pattern.compile("(?!\\s)\\W");
13         private PasswordStrengthChecker() {
14         }
15         private static int checkpwlight(String pw) {
16                 int points = 0;
17                 if (pw.length() > 15) {
18                         points++;
19                 }
20                 if (pw.length() > 20) {
21                         points++;
22                 }
23                 if (pw.length() > 25) {
24                         points++;
25                 }
26                 if (pw.length() > 30) {
27                         points++;
28                 }
29                 if (digits.matcher(pw).find()) {
30                         points++;
31                 }
32                 if (lower.matcher(pw).find()) {
33                         points++;
34                 }
35                 if (upper.matcher(pw).find()) {
36                         points++;
37                 }
38                 if (special.matcher(pw).find()) {
39                         points++;
40                 }
41                 if (whitespace.matcher(pw).find()) {
42                         points++;
43                 }
44                 return points;
45         }
46         public static int checkpw(String pw, User u) {
47                 if (pw == null) {
48                         return 0;
49                 }
50                 int light = checkpwlight(pw);
51                 if (contained(pw, u.getEmail())) {
52                         light -= 2;
53                 }
54                 if (contained(pw, u.getFname())) {
55                         light -= 2;
56                 }
57                 if (contained(pw, u.getLname())) {
58                         light -= 2;
59                 }
60                 if (contained(pw, u.getMname())) {
61                         light -= 2;
62                 }
63                 if (contained(pw, u.getSuffix())) {
64                         light -= 2;
65                 }
66                 // TODO dictionary check
67                 return light;
68         }
69         private static boolean contained(String pw, String check) {
70                 if (check == null || check.equals("")) {
71                         return false;
72                 }
73                 if (pw.contains(check)) {
74                         return true;
75                 }
76                 if (check.contains(pw)) {
77                         return true;
78                 }
79                 return false;
80         }
81 }