import java.text.MessageFormat;
import java.util.Properties;
import java.util.Random;
+import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.commons.codec.digest.Crypt;
import org.apache.commons.codec.digest.DigestUtils;
/**
* Password alphabet
*/
- private static String PASSWORD_ALPHABET;
+ private static final String PASSWORD_ALPHABET;
/**
* Password alphabet parts
*/
public static double calculatePasswordScore (final String password) {
// Log message
- System.out.println(UserUtils.class.getSimpleName() + ".calculatePasswordScore: password=" + password + " - CALLED!");
+ System.out.println(UserUtils.class.getSimpleName() + ".calculatePasswordScore: password=" + password + " - CALLED!"); //NOI18N
// Should not be null
if (null == password) {
score += password.length() * calculateEntropyFactor(password) / 100;
// Log message
- System.out.println(UserUtils.class.getSimpleName() + ".calculatePasswordScore: score=" + score + " - after length");
+ System.out.println(UserUtils.class.getSimpleName() + ".calculatePasswordScore: score=" + score + " - after length"); //NOI18N
//password has 3 numbers
- if (Pattern.matches("(.*[0-9].*[0-9].*[0-9].*)+", password)) { //NOI18N
+ if (ifRegExFoundInString("(.*[0-9].*[0-9].*[0-9].*)+", password)) { //NOI18N
score += 5;
}
// Log message
- System.out.println(UserUtils.class.getSimpleName() + ".calculatePasswordScore: score=" + score + " - after 3 numbers");
+ System.out.println(UserUtils.class.getSimpleName() + ".calculatePasswordScore: score=" + score + " - after 3 numbers"); //NOI18N
//password has 2 symbols
- if (Pattern.matches("(.*[!,@,#,$,%,^,&,*,?,_,~,=,.,-,;,:].*[!,@,#,$,%,^,&,*,?,_,~,=,.,-,;,:].*)+", password)) { //NOI18N
+ if (ifRegExFoundInString("(.*[!,@,#,$,%,^,&,*,/,?,_,~,=,.,-,;,:].*[!,@,#,$,%,^,&,*,/,?,_,~,=,.,-,;,:].*)+", password)) { //NOI18N
score += 5;
}
// Log message
- System.out.println(UserUtils.class.getSimpleName() + ".calculatePasswordScore: score=" + score + " - after 2 symbols");
+ System.out.println(UserUtils.class.getSimpleName() + ".calculatePasswordScore: score=" + score + " - after 2 symbols"); //NOI18N
//password has Upper and Lower chars
- if (Pattern.matches("(.*[a-z].*[A-Z])|([A-Z].*[a-z].*)+", password)) { //NOI18N
+ if (ifRegExFoundInString("(.*[a-z].*[A-Z])|([A-Z].*[a-z].*)+", password)) { //NOI18N
score += 10;
}
// Log message
- System.out.println(UserUtils.class.getSimpleName() + ".calculatePasswordScore: score=" + score + " - after upper/lower");
+ System.out.println(UserUtils.class.getSimpleName() + ".calculatePasswordScore: score=" + score + " - after upper/lower"); //NOI18N
//password has number and chars
- if (Pattern.matches("(.*[a-zA-Z].*)+", password) && Pattern.matches("(.*[0-9].*)+", password)) { //NOI18N
+ if (ifRegExFoundInString("(.*[a-zA-Z].*)+", password) && ifRegExFoundInString("(.*[0-9].*)+", password)) { //NOI18N
score += 15;
}
// Log message
- System.out.println(UserUtils.class.getSimpleName() + ".calculatePasswordScore: score=" + score + " - after number+chars");
+ System.out.println(UserUtils.class.getSimpleName() + ".calculatePasswordScore: score=" + score + " - after number+chars"); //NOI18N
//password has number and symbol
- if (Pattern.matches("(.*[!,@,#,$,%,^,&,*,?,_,~,=,.,-,;,:].*)+", password) && Pattern.matches("(.*[0-9].*)+", password)) { //NOI18N
+ if (ifRegExFoundInString("(.*[!,@,#,$,%,^,&,*,/,?,_,~,=,.,-,;,:].*)+", password) && ifRegExFoundInString("(.*[0-9].*)+", password)) { //NOI18N
score += 15;
}
// Log message
- System.out.println(UserUtils.class.getSimpleName() + ".calculatePasswordScore: score=" + score + " - after number+symbol");
+ System.out.println(UserUtils.class.getSimpleName() + ".calculatePasswordScore: score=" + score + " - after number+symbol"); //NOI18N
//password has char and symbol
- if (Pattern.matches("(.*[!,@,#,$,%,^,&,*,?,_,~,=,.,-,;,:].*)+", password) && Pattern.matches("(.*[a-zA-Z].*)+", password)) { //NOI18N
+ if (ifRegExFoundInString("(.*[!,@,#,$,%,^,&,*,/,?,_,~,=,.,-,;,:].*)+", password) && ifRegExFoundInString("(.*[a-zA-Z].*)+", password)) { //NOI18N
score += 15;
}
// Log message
- System.out.println(UserUtils.class.getSimpleName() + ".calculatePasswordScore: score=" + score + " - after char+symbol");
+ System.out.println(UserUtils.class.getSimpleName() + ".calculatePasswordScore: score=" + score + " - after char+symbol"); //NOI18N
//password is just numbers or chars
- if (Pattern.matches("^[a-zA-Z]+$", password) || Pattern.matches("^[0-9]+$", password)) { //NOI18N
+ if (ifRegExFoundInString("^[a-zA-Z]+$", password) || ifRegExFoundInString("^[0-9]+$", password)) { //NOI18N
score -= 10;
}
// Log message
- System.out.println(UserUtils.class.getSimpleName() + ".calculatePasswordScore: score=" + score + " - after number/char");
+ System.out.println(UserUtils.class.getSimpleName() + ".calculatePasswordScore: score=" + score + " - after number/char"); //NOI18N
// Larger than 100 is not allowed
score = Math.max(Math.min(score, 100.0f), 0.0f);
// Log message
- System.out.println(UserUtils.class.getSimpleName() + ".calculatePasswordScore: score=" + score + " - EXIT!");
+ System.out.println(UserUtils.class.getSimpleName() + ".calculatePasswordScore: score=" + score + " - EXIT!"); //NOI18N
// Return it
return score;
return ifPasswordMatches(container.getUserPassword(), container.getUser());
}
+ /**
+ * Checks if the regular expression is found in given string
+ * <p>
+ * @param pattern Regular expression
+ * @param str String
+ * <p>
+ * @return Whether it is found
+ */
+ private static boolean ifRegExFoundInString (final String pattern, final String str) {
+ // Mus be valid parameters
+ if (null == pattern) {
+ // Throw NPE
+ throw new NullPointerException("pattern is null"); //NOI18N
+ } else if (pattern.isEmpty()) {
+ // Is empty
+ throw new IllegalArgumentException("pattern is empty"); //NOI18N
+ } else if (null == str) {
+ // Throw NPE
+ throw new NullPointerException("str is null"); //NOI18N
+ }
+
+ // Compile pattern
+ Pattern r = Pattern.compile(pattern);
+
+ // Get matcher
+ Matcher m = r.matcher(str);
+
+ // Check if it is found
+ return m.find();
+ }
+
/**
* No instance from this class
*/