From: Roland Häder Date: Fri, 2 Sep 2016 15:55:55 +0000 (+0200) Subject: Continued a bit: X-Git-Url: https://git.mxchange.org/?a=commitdiff_plain;h=840d2980ab5e69c8472089bd47281e06501662d9;p=juser-core.git Continued a bit: - introduced ifRegExFoundInString() which uses find() instead of matches() - added slash to password "alphabet" --- diff --git a/src/org/mxchange/jusercore/model/user/UserUtils.java b/src/org/mxchange/jusercore/model/user/UserUtils.java index b4484a1..52dd218 100644 --- a/src/org/mxchange/jusercore/model/user/UserUtils.java +++ b/src/org/mxchange/jusercore/model/user/UserUtils.java @@ -21,6 +21,7 @@ import java.security.SecureRandom; 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; @@ -37,7 +38,7 @@ public class UserUtils implements Serializable { /** * Password alphabet */ - private static String PASSWORD_ALPHABET; + private static final String PASSWORD_ALPHABET; /** * Password alphabet parts @@ -119,7 +120,7 @@ public class UserUtils implements Serializable { */ 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) { @@ -137,69 +138,69 @@ public class UserUtils implements Serializable { 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; @@ -536,6 +537,37 @@ public class UserUtils implements Serializable { return ifPasswordMatches(container.getUserPassword(), container.getUser()); } + /** + * Checks if the regular expression is found in given string + *

+ * @param pattern Regular expression + * @param str String + *

+ * @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 */