1 package org.cacert.gigi.util;
3 import java.util.TreeSet;
4 import java.util.regex.Pattern;
6 import org.cacert.gigi.GigiApiException;
7 import org.cacert.gigi.dbObjects.Name;
8 import org.cacert.gigi.dbObjects.NamePart;
10 public class PasswordStrengthChecker {
12 private static Pattern digits = Pattern.compile("\\d");
14 private static Pattern lower = Pattern.compile("[a-z]");
16 private static Pattern upper = Pattern.compile("[A-Z]");
18 private static Pattern whitespace = Pattern.compile("\\s");
20 private static Pattern special = Pattern.compile("(?!\\s)\\W");
22 private PasswordStrengthChecker() {}
24 private static int checkpwlight(String pw) {
26 if (pw.length() > 15) {
29 if (pw.length() > 20) {
32 if (pw.length() > 25) {
35 if (pw.length() > 30) {
38 if (digits.matcher(pw).find()) {
41 if (lower.matcher(pw).find()) {
44 if (upper.matcher(pw).find()) {
47 if (special.matcher(pw).find()) {
50 if (whitespace.matcher(pw).find()) {
56 public static int checkpw(String pw, String[] nameParts, String email) {
60 int light = checkpwlight(pw);
61 if (contained(pw, email)) {
64 for (int i = 0; i < nameParts.length; i++) {
65 if (contained(pw, nameParts[i])) {
69 // TODO dictionary check
73 public static void assertStrongPassword(String pw, Name[] names, String email) throws GigiApiException {
74 TreeSet<String> parts = new TreeSet<>();
75 for (int i = 0; i < names.length; i++) {
76 for (NamePart string : names[i].getParts()) {
77 parts.add(string.getValue());
80 if (checkpw(pw, parts.toArray(new String[parts.size()]), email) < 3) {
81 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.");
85 private static boolean contained(String pw, String check) {
86 if (check == null || check.equals("")) {
89 if (pw.contains(check)) {
92 if (check.contains(pw)) {