]> WPIA git - gigi.git/blob - src/org/cacert/gigi/util/PasswordStrengthChecker.java
Empty: rename fname,mname,lname,dob
[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.GigiApiException;
6 import org.cacert.gigi.dbObjects.User;
7
8 public class PasswordStrengthChecker {
9
10     private static Pattern digits = Pattern.compile("\\d");
11
12     private static Pattern lower = Pattern.compile("[a-z]");
13
14     private static Pattern upper = Pattern.compile("[A-Z]");
15
16     private static Pattern whitespace = Pattern.compile("\\s");
17
18     private static Pattern special = Pattern.compile("(?!\\s)\\W");
19
20     private PasswordStrengthChecker() {}
21
22     private static int checkpwlight(String pw) {
23         int points = 0;
24         if (pw.length() > 15) {
25             points++;
26         }
27         if (pw.length() > 20) {
28             points++;
29         }
30         if (pw.length() > 25) {
31             points++;
32         }
33         if (pw.length() > 30) {
34             points++;
35         }
36         if (digits.matcher(pw).find()) {
37             points++;
38         }
39         if (lower.matcher(pw).find()) {
40             points++;
41         }
42         if (upper.matcher(pw).find()) {
43             points++;
44         }
45         if (special.matcher(pw).find()) {
46             points++;
47         }
48         if (whitespace.matcher(pw).find()) {
49             points++;
50         }
51         return points;
52     }
53
54     public static int checkpw(String pw, User u) {
55         if (pw == null) {
56             return 0;
57         }
58         int light = checkpwlight(pw);
59         if (contained(pw, u.getEmail())) {
60             light -= 2;
61         }
62         if (contained(pw, u.getFName())) {
63             light -= 2;
64         }
65         if (contained(pw, u.getLName())) {
66             light -= 2;
67         }
68         if (contained(pw, u.getMName())) {
69             light -= 2;
70         }
71         if (contained(pw, u.getSuffix())) {
72             light -= 2;
73         }
74         // TODO dictionary check
75         return light;
76     }
77
78     public static void assertStrongPassword(String pw, User u) throws GigiApiException {
79         if (checkpw(pw, u) < 3) {
80             throw new GigiApiException("The Pass Phrase you submitted failed to contain enough" + " differing characters and/or contained words from" + " your name and/or email address.");
81         }
82     }
83
84     private static boolean contained(String pw, String check) {
85         if (check == null || check.equals("")) {
86             return false;
87         }
88         if (pw.contains(check)) {
89             return true;
90         }
91         if (check.contains(pw)) {
92             return true;
93         }
94         return false;
95     }
96 }