From c2db54354d46d27a04de49fcdb4658c18ee30c32 Mon Sep 17 00:00:00 2001 From: vvatanabe Date: Tue, 29 Aug 2023 09:45:11 +0900 Subject: [PATCH 01/11] fix: fix the code smell warnings in Feedback --- src/main/java/com/nulabinc/zxcvbn/Feedback.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/com/nulabinc/zxcvbn/Feedback.java b/src/main/java/com/nulabinc/zxcvbn/Feedback.java index a0ae07a..70b0d4c 100644 --- a/src/main/java/com/nulabinc/zxcvbn/Feedback.java +++ b/src/main/java/com/nulabinc/zxcvbn/Feedback.java @@ -125,7 +125,7 @@ private String l10n(ResourceBundle messages, String messageId) { } static Feedback getFeedback(int score, List sequence) { - if (sequence.size() == 0) { + if (sequence.isEmpty()) { return FeedbackFactory.getFeedbackWithoutWarnings( DEFAULT_SUGGESTIONS_USE_FEW_WORDS, DEFAULT_SUGGESTIONS_NO_NEED_SYMBOLS); } From 347396941ef0c48f39169264c17b4400079e7203 Mon Sep 17 00:00:00 2001 From: vvatanabe Date: Tue, 29 Aug 2023 09:45:40 +0900 Subject: [PATCH 02/11] fix: fix the code smell warnings in FeedbackFactory --- src/main/java/com/nulabinc/zxcvbn/FeedbackFactory.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/com/nulabinc/zxcvbn/FeedbackFactory.java b/src/main/java/com/nulabinc/zxcvbn/FeedbackFactory.java index de921c7..e280cfd 100644 --- a/src/main/java/com/nulabinc/zxcvbn/FeedbackFactory.java +++ b/src/main/java/com/nulabinc/zxcvbn/FeedbackFactory.java @@ -11,6 +11,10 @@ class FeedbackFactory { private static final List NAME_DICTIONARIES = Arrays.asList("surnames", "male_names", "female_names"); + private FeedbackFactory() { + throw new IllegalStateException("FeedbackFactory should not be instantiated"); + } + static Feedback getFeedbackWithoutWarnings(String... suggestions) { return new Feedback(null, suggestions); } From c05aefebc69370c0031efb30064f1463e3aebd11 Mon Sep 17 00:00:00 2001 From: vvatanabe Date: Tue, 29 Aug 2023 09:45:59 +0900 Subject: [PATCH 03/11] fix: fix the code smell warnings in StandardContext --- src/main/java/com/nulabinc/zxcvbn/StandardContext.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/com/nulabinc/zxcvbn/StandardContext.java b/src/main/java/com/nulabinc/zxcvbn/StandardContext.java index d06d41a..9869659 100644 --- a/src/main/java/com/nulabinc/zxcvbn/StandardContext.java +++ b/src/main/java/com/nulabinc/zxcvbn/StandardContext.java @@ -8,6 +8,10 @@ class StandardContext { + private StandardContext() { + throw new IllegalStateException("StandardContext should not be instantiated"); + } + static Context build() throws IOException { Map dictionaryMap = new LinkedHashMap<>(); for (Dictionary dictionary : StandardDictionaries.loadAllDictionaries()) { From 31baf6473a93c13540756256ff3bbe50473921f8 Mon Sep 17 00:00:00 2001 From: vvatanabe Date: Tue, 29 Aug 2023 09:46:43 +0900 Subject: [PATCH 04/11] fix: fix the code smell warnings in TimeEstimates --- src/main/java/com/nulabinc/zxcvbn/TimeEstimates.java | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/main/java/com/nulabinc/zxcvbn/TimeEstimates.java b/src/main/java/com/nulabinc/zxcvbn/TimeEstimates.java index c5a6baf..1b7405e 100644 --- a/src/main/java/com/nulabinc/zxcvbn/TimeEstimates.java +++ b/src/main/java/com/nulabinc/zxcvbn/TimeEstimates.java @@ -17,6 +17,10 @@ public class TimeEstimates { private static final double YEAR = MONTH * 12; private static final double CENTURY = YEAR * 100; + private TimeEstimates() { + throw new IllegalStateException("TimeEstimates should not be instantiated"); + } + public static AttackTimes estimateAttackTimes(double guesses) { AttackTimes.CrackTimeSeconds crackTimeSeconds = new AttackTimes.CrackTimeSeconds( From 3d03ecfacdd9269656fd333b229bc342af06b6e5 Mon Sep 17 00:00:00 2001 From: vvatanabe Date: Tue, 29 Aug 2023 09:47:20 +0900 Subject: [PATCH 05/11] fix: fix the code smell warnings in WipeableString --- .../com/nulabinc/zxcvbn/WipeableString.java | 34 ++++++++++++------- 1 file changed, 21 insertions(+), 13 deletions(-) diff --git a/src/main/java/com/nulabinc/zxcvbn/WipeableString.java b/src/main/java/com/nulabinc/zxcvbn/WipeableString.java index 16957c4..a15b4f6 100644 --- a/src/main/java/com/nulabinc/zxcvbn/WipeableString.java +++ b/src/main/java/com/nulabinc/zxcvbn/WipeableString.java @@ -52,7 +52,10 @@ public void wipe() { /** Returns a new wipeable string with the specified content forced into lower case. */ public static WipeableString lowerCase(CharSequence source) { - assert source != null; + if (source == null) { + throw new IllegalArgumentException("source is null"); + } + char[] chars = new char[source.length()]; for (int n = 0; n < source.length(); n++) { chars[n] = Character.toLowerCase(source.charAt(n)); @@ -65,7 +68,9 @@ public static WipeableString lowerCase(CharSequence source) { * reversed. */ public static WipeableString reversed(CharSequence source) { - assert source != null; + if (source == null) { + throw new IllegalArgumentException("source is null"); + } int length = source.length(); char[] chars = new char[length]; for (int n = 0; n < source.length(); n++) { @@ -135,6 +140,7 @@ public static int parseInt(CharSequence s) throws NumberFormatException { } /** A version of Integer.parse(String) that accepts CharSequence as parameter. */ + @SuppressWarnings("squid:S3776") public static int parseInt(CharSequence s, int radix) throws NumberFormatException { if (s == null) { throw new NumberFormatException("null"); @@ -165,10 +171,10 @@ public static int parseInt(CharSequence s, int radix) throws NumberFormatExcepti negative = true; limit = Integer.MIN_VALUE; } else if (firstChar != '+') { - throw new NumberFormatException("For input string: \"" + s + "\""); + throw numberFormatException(s); } if (len == 1) { // Cannot have lone "+" or "-" - throw new NumberFormatException("For input string: \"" + s + "\""); + throw numberFormatException(s); } i++; } @@ -177,23 +183,27 @@ public static int parseInt(CharSequence s, int radix) throws NumberFormatExcepti // Accumulating negatively avoids surprises near MAX_VALUE digit = Character.digit(s.charAt(i++), radix); if (digit < 0) { - throw new NumberFormatException("For input string: \"" + s + "\""); + throw numberFormatException(s); } if (result < multmin) { - throw new NumberFormatException("For input string: \"" + s + "\""); + throw numberFormatException(s); } result *= radix; if (result < limit + digit) { - throw new NumberFormatException("For input string: \"" + s + "\""); + throw numberFormatException(s); } result -= digit; } } else { - throw new NumberFormatException("For input string: \"" + s + "\""); + throw numberFormatException(s); } return negative ? result : -result; } + private static NumberFormatException numberFormatException(CharSequence s) { + return new NumberFormatException("For input string: \"" + s + "\""); + } + @Override public String toString() { return new String(content); @@ -260,11 +270,9 @@ public static void wipeIfPossible(CharSequence text) { ((StringBuffer) text).setCharAt(n, ' '); } ((StringBuffer) text).setLength(0); - } else if (text instanceof CharBuffer) { - if (!((CharBuffer) text).isReadOnly()) { - for (int n = 0; n < text.length(); n++) { - ((CharBuffer) text).put(n, ' '); - } + } else if (text instanceof CharBuffer && !((CharBuffer) text).isReadOnly()) { + for (int n = 0; n < text.length(); n++) { + ((CharBuffer) text).put(n, ' '); } } } From b416c2968ffc6768528fb9d96c9fa910a36a8dc6 Mon Sep 17 00:00:00 2001 From: vvatanabe Date: Tue, 29 Aug 2023 09:47:44 +0900 Subject: [PATCH 06/11] fix: fix the code smell warnings in DictionaryGuess --- .../zxcvbn/guesses/DictionaryGuess.java | 60 ++++++++++++------- 1 file changed, 39 insertions(+), 21 deletions(-) diff --git a/src/main/java/com/nulabinc/zxcvbn/guesses/DictionaryGuess.java b/src/main/java/com/nulabinc/zxcvbn/guesses/DictionaryGuess.java index f7fc603..6db6101 100644 --- a/src/main/java/com/nulabinc/zxcvbn/guesses/DictionaryGuess.java +++ b/src/main/java/com/nulabinc/zxcvbn/guesses/DictionaryGuess.java @@ -3,6 +3,7 @@ import com.nulabinc.zxcvbn.Context; import com.nulabinc.zxcvbn.WipeableString; import com.nulabinc.zxcvbn.matchers.Match; +import java.util.AbstractMap; import java.util.Map; import java.util.regex.Pattern; @@ -59,29 +60,46 @@ public int l33tVariations(Match match) { int totalVariations = 1; WipeableString lowercaseToken = WipeableString.lowerCase(match.token); for (Map.Entry substitution : match.sub.entrySet()) { - Character substitutedChar = substitution.getKey(); - Character originalChar = substitution.getValue(); - int substitutedCount = 0; - int originalCount = 0; - for (char currentChar : lowercaseToken.charArray()) { - if (currentChar == substitutedChar) { - substitutedCount++; - } - if (currentChar == originalChar) { - originalCount++; - } + totalVariations *= calculateSubstitutionVariation(substitution, lowercaseToken); + } + return totalVariations; + } + + private static int calculateSubstitutionVariation( + Map.Entry substitution, WipeableString token) { + Character substitutedChar = substitution.getKey(); + Character originalChar = substitution.getValue(); + AbstractMap.SimpleImmutableEntry counts = + countCharOccurrences(token, substitutedChar, originalChar); + int substitutedCount = counts.getKey(); + int originalCount = counts.getValue(); + if (substitutedCount == 0 || originalCount == 0) { + return 2; + } + return calculatePossibleCombinations(originalCount, substitutedCount); + } + + private static AbstractMap.SimpleImmutableEntry countCharOccurrences( + WipeableString str, char char1, char char2) { + int count1 = 0; + int count2 = 0; + for (char currentChar : str.charArray()) { + if (currentChar == char1) { + count1++; } - if (substitutedCount == 0 || originalCount == 0) { - totalVariations *= 2; - } else { - int minCount = Math.min(originalCount, substitutedCount); - int possibleCombinations = 0; - for (int i = 1; i <= minCount; i++) { - possibleCombinations += calculateBinomialCoefficient(originalCount + substitutedCount, i); - } - totalVariations *= possibleCombinations; + if (currentChar == char2) { + count2++; } } - return totalVariations; + return new AbstractMap.SimpleImmutableEntry<>(count1, count2); + } + + private static int calculatePossibleCombinations(int originalCount, int substitutedCount) { + int minCount = Math.min(originalCount, substitutedCount); + int possibleCombinations = 0; + for (int i = 1; i <= minCount; i++) { + possibleCombinations += calculateBinomialCoefficient(originalCount + substitutedCount, i); + } + return possibleCombinations; } } From 8cf49656e604106a69e7c9140f81e23b2f82fd3b Mon Sep 17 00:00:00 2001 From: vvatanabe Date: Tue, 29 Aug 2023 09:48:24 +0900 Subject: [PATCH 07/11] fix: fix the code smell warnings in EstimateGuess --- src/main/java/com/nulabinc/zxcvbn/guesses/EstimateGuess.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/main/java/com/nulabinc/zxcvbn/guesses/EstimateGuess.java b/src/main/java/com/nulabinc/zxcvbn/guesses/EstimateGuess.java index bf81f55..5666ae9 100644 --- a/src/main/java/com/nulabinc/zxcvbn/guesses/EstimateGuess.java +++ b/src/main/java/com/nulabinc/zxcvbn/guesses/EstimateGuess.java @@ -5,13 +5,13 @@ import com.nulabinc.zxcvbn.Pattern; import com.nulabinc.zxcvbn.Scoring; import com.nulabinc.zxcvbn.matchers.Match; -import java.util.HashMap; +import java.util.EnumMap; import java.util.Map; public class EstimateGuess extends BaseGuess { private final CharSequence password; - private final Map patternGuessMap = new HashMap<>(); + private final Map patternGuessMap = new EnumMap<>(Pattern.class); public EstimateGuess(Context context, CharSequence password) { super(context); From c3408e9c755e833030e7ff11dd0fe558077ce5ba Mon Sep 17 00:00:00 2001 From: vvatanabe Date: Tue, 29 Aug 2023 09:48:53 +0900 Subject: [PATCH 08/11] fix: fix the code smell warnings in Pattern --- src/main/java/com/nulabinc/zxcvbn/Pattern.java | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/main/java/com/nulabinc/zxcvbn/Pattern.java b/src/main/java/com/nulabinc/zxcvbn/Pattern.java index bab81b5..a6b0d16 100644 --- a/src/main/java/com/nulabinc/zxcvbn/Pattern.java +++ b/src/main/java/com/nulabinc/zxcvbn/Pattern.java @@ -1,12 +1,19 @@ package com.nulabinc.zxcvbn; public enum Pattern { + @SuppressWarnings("squid:S115") Bruteforce("bruteforce"), + @SuppressWarnings("squid:S115") Dictionary("dictionary"), + @SuppressWarnings("squid:S115") Spatial("spatial"), + @SuppressWarnings("squid:S115") Repeat("repeat"), + @SuppressWarnings("squid:S115") Sequence("sequence"), + @SuppressWarnings("squid:S115") Regex("regex"), + @SuppressWarnings("squid:S115") Date("date"); private final String value; From 36d5045079569909cde7bdcda2b152e40d91e4f9 Mon Sep 17 00:00:00 2001 From: vvatanabe Date: Tue, 29 Aug 2023 09:49:09 +0900 Subject: [PATCH 09/11] fix: fix the code smell warnings in StandardDictionaries --- src/main/java/com/nulabinc/zxcvbn/StandardDictionaries.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/com/nulabinc/zxcvbn/StandardDictionaries.java b/src/main/java/com/nulabinc/zxcvbn/StandardDictionaries.java index 191ee7b..92a7174 100644 --- a/src/main/java/com/nulabinc/zxcvbn/StandardDictionaries.java +++ b/src/main/java/com/nulabinc/zxcvbn/StandardDictionaries.java @@ -9,6 +9,7 @@ public class StandardDictionaries { + @SuppressWarnings("squid:S1075") private static final String BASE_PATH = "/com/nulabinc/zxcvbn/matchers/dictionaries/"; public static final String US_TV_AND_FILM = "us_tv_and_film"; @@ -42,6 +43,10 @@ public class StandardDictionaries { public static final DictionaryLoader FEMALE_NAMES_LOADER = new DictionaryLoader(FEMALE_NAMES, new ClasspathResource(BASE_PATH + "female_names.txt")); + private StandardDictionaries() { + throw new IllegalStateException("StandardDictionaries should not be instantiated"); + } + private static final DictionaryLoader[] ALL_LOADERS = { US_TV_AND_FILM_LOADER, ENGLISH_WIKIPEDIA_LOADER, From be8ce1723596001ae5bba102cec41c7b36086a3c Mon Sep 17 00:00:00 2001 From: vvatanabe Date: Tue, 29 Aug 2023 09:49:23 +0900 Subject: [PATCH 10/11] fix: fix the code smell warnings in StandardKeyboards --- src/main/java/com/nulabinc/zxcvbn/StandardKeyboards.java | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/src/main/java/com/nulabinc/zxcvbn/StandardKeyboards.java b/src/main/java/com/nulabinc/zxcvbn/StandardKeyboards.java index cbaf1f0..e3c21e1 100644 --- a/src/main/java/com/nulabinc/zxcvbn/StandardKeyboards.java +++ b/src/main/java/com/nulabinc/zxcvbn/StandardKeyboards.java @@ -11,6 +11,7 @@ public class StandardKeyboards { + @SuppressWarnings("squid:S1075") private static final String RESOURCES_PACKAGE_PATH = "/com/nulabinc/zxcvbn/matchers/keyboards/"; public static final String QWERTY = "qwerty"; @@ -23,6 +24,10 @@ public class StandardKeyboards { public static final String MAC_KEYPAD = "mac_keypad"; + private StandardKeyboards() { + throw new IllegalStateException("StandardKeyboards should not be instantiated"); + } + public static final KeyboardLoader QWERTY_LOADER = new SlantedKeyboardLoader( QWERTY, new ClasspathResource(RESOURCES_PACKAGE_PATH + "qwerty.txt")); From d47055288ec20fb70876e253ceb8539a4cbb5511 Mon Sep 17 00:00:00 2001 From: vvatanabe Date: Tue, 29 Aug 2023 09:51:22 +0900 Subject: [PATCH 11/11] fix: fix the code smell warnings in ClasspathResource --- src/main/java/com/nulabinc/zxcvbn/io/ClasspathResource.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/com/nulabinc/zxcvbn/io/ClasspathResource.java b/src/main/java/com/nulabinc/zxcvbn/io/ClasspathResource.java index 0cb292c..a7ee4fd 100644 --- a/src/main/java/com/nulabinc/zxcvbn/io/ClasspathResource.java +++ b/src/main/java/com/nulabinc/zxcvbn/io/ClasspathResource.java @@ -29,6 +29,7 @@ public InputStream getInputStream() throws IOException { * available; Next, the ClassLoader that loaded the ResourceLoader class will be used as fallback. * Finally, if even the system ClassLoader could not access resource as stream, return null. */ + @SuppressWarnings("squid:S1181") private InputStream getResourceAsStreamWithFallback(String path) { // Try loading the resource from the same artifact as this class InputStream in = getClass().getResourceAsStream(path);