@Override public double exec(Match match) { if (match.guesses != null) return match.guesses; int minGuesses = 1; if (match.token.length() < password.length()) { minGuesses = match.token.length() == 1 ? MIN_SUBMATCH_GUESSES_SINGLE_CHAR : MIN_SUBMATCH_GUESSES_MULTI_CHAR; } final Guess guess; switch (match.pattern) { case Bruteforce: guess = new BruteforceGuess(); break; case Dictionary: guess = new DictionaryGuess(); break; case Spatial: guess = new SpatialGuess(); break; case Repeat: guess = new RepeatGuess(); break; case Sequence: guess = new SequenceGuess(); break; case Regex: guess = new RegexGuess(); break; case Date: guess = new DateGuess(); break; default: guess = null; break; } double guesses = guess != null ? guess.exec(match) : 0; match.guesses = Math.max(guesses, minGuesses); match.guessesLog10 = Scoring.log10(match.guesses); return match.guesses; } }
@Override public double exec(Match match) { if (match.guesses != null) return match.guesses; int minGuesses = 1; if (match.token.length() < password.length()) { minGuesses = match.token.length() == 1 ? MIN_SUBMATCH_GUESSES_SINGLE_CHAR : MIN_SUBMATCH_GUESSES_MULTI_CHAR; } final Guess guess; switch (match.pattern) { case Bruteforce: guess = new BruteforceGuess(); break; case Dictionary: guess = new DictionaryGuess(); break; case Spatial: guess = new SpatialGuess(); break; case Repeat: guess = new RepeatGuess(); break; case Sequence: guess = new SequenceGuess(); break; case Regex: guess = new RegexGuess(); break; case Date: guess = new DateGuess(); break; default: guess = null; break; } double guesses = guess != null ? guess.exec(match) : 0; match.guesses = Math.max(guesses, minGuesses); match.guessesLog10 = Scoring.log10(match.guesses); return match.guesses; } }
@Test public void testUppercaseVariants() throws Exception { DictionaryGuess dictionaryGuess = new DictionaryGuess(); Method uppercaseVariationsMethod = DictionaryGuess.class.getDeclaredMethod("uppercaseVariations", Match.class); uppercaseVariationsMethod.setAccessible(true); Match match = new Match.Builder(Pattern.Dictionary, 0, 0, word).sub(new HashMap<Character, Character>()).l33t(true).build(); String msg = String.format("guess multiplier of %s is %s", word, variants); assertEquals(msg, variants, uppercaseVariationsMethod.invoke(dictionaryGuess, match)); }
@Test public void testDictionaryGuessesMixed() throws Exception { Map<Character, Character> sub = new HashMap<>(); sub.put('@', 'a'); Match match = new Match.Builder(Pattern.Dictionary, 0, 0, "AaA@@@").sub(sub).l33t(true).rank(32).build(); String msg = "extra guesses are added for both capitalization and common l33t substitutions"; int expected = 32 * new DictionaryGuess().l33tVariations(match) * new DictionaryGuess().uppercaseVariations(match); assertEquals(msg, expected, new DictionaryGuess().exec(match), 0.0); }
@Test public void testDictionaryGuesses133t() throws Exception { Map<Character, Character> sub = new HashMap<>(); sub.put('@', 'a'); Match match = new Match.Builder(Pattern.Dictionary, 0, 0, "aaa@@@").sub(sub).l33t(true).rank(32).build(); String msg = "extra guesses are added for common l33t substitutions"; assertEquals(msg, 32 * new DictionaryGuess().l33tVariations(match), new DictionaryGuess().exec(match), 0.0); }
@Test public void testDictionaryGuessesCapitalization() throws Exception { Match match = new Match.Builder(Pattern.Dictionary, 0, 0, "AAAaaa").rank(32).build(); String msg = "extra guesses are added for capitalization"; assertEquals(msg, 32 * new DictionaryGuess().uppercaseVariations(match), new DictionaryGuess().exec(match), 0.0); }
@Test public void testL33tVariants() throws Exception { Match match = MatchFactory.createDictionaryMatch(0, 0, "", "", 0, ""); assertEquals("1 variant for non-l33t matches", 1.0, new DictionaryGuess().l33tVariations(match), 0.0); } }
@Test public void testL33tVariants() throws Exception { Match match = new Match.Builder(Pattern.Dictionary, 0, 0, word).sub(sub).l33t(!sub.isEmpty()).build(); String msg = String.format("extra l33t guesses of %s is %s", word, variants); assertEquals(msg, variants, new DictionaryGuess().l33tVariations(match)); }
@Test public void testDictionaryGuessesSameWithRank() throws Exception { Match match = new Match.Builder(Pattern.Dictionary, 0, 0, "aaaa").rank(32).build(); String msg = "base guesses == the rank"; assertEquals(msg, 32, new DictionaryGuess().exec(match), 0.0); }
@Test public void testDictionaryGuessesReverse() throws Exception { Match match = new Match.Builder(Pattern.Dictionary, 0, 0, "aaa").reversed(true).rank(32).build(); String msg = "guesses are doubled when word is reversed"; assertEquals(msg, 32 * 2, new DictionaryGuess().exec(match), 0.0); }