public static Keyboard of(final String graph) { for (Keyboard keyboard : ALL_KEYBOARDS) { if (keyboard.getName().equals(graph)) { return keyboard; } } throw new IllegalArgumentException("Illegal graph " + graph); }
private Keyboard(final String name, final AdjacentGraphBuilder adjacentGraphBuilder) { this.name = name; this.adjacencyGraph = adjacentGraphBuilder.build(); this.slanted = adjacentGraphBuilder.isSlanted(); this.startingPositions = adjacencyGraph.size(); this.averageDegree = calcAverageDegree(adjacencyGraph); }
@Override public double exec(Match match) { Keyboard keyboard = Keyboard.of(match.graph); int s = keyboard.getStartingPositions(); double d = keyboard.getAverageDegree(); double guesses = 0; int l = match.token.length(); int t = match.turns; for (int i = 2; i <= l; i++) { int possibleTurns = Math.min(t, i - 1); for (int j = 1; j <= possibleTurns; j++) { guesses += nCk(i - 1, j - 1) * s * Math.pow(d, j); } } if (match.shiftedCount != null && match.shiftedCount > 0) { int shiftedCount = match.shiftedCount; int unshiftedCount = match.token.length() - match.shiftedCount; if (shiftedCount == 0 || unshiftedCount == 0) { guesses *= 2; } else { int shiftedVariations = 0; for (int i = 1; i <= Math.min(shiftedCount, unshiftedCount); i++) { shiftedVariations += nCk(shiftedCount + unshiftedCount, i); } guesses *= shiftedVariations; } } return guesses; } }
int turns = 0; int shiftedCount; if (keyboard.isSlanted() && shiftedRx.matcher(String.valueOf(password.charAt(i))).find()) { shiftedCount = 1; } else { shiftedCount = 0; final Map<Character, List<String>> graph = keyboard.getAdjacencyGraph(); while(true) { Character prevChar = password.charAt(j-1); j - 1, password.substring(i, j), keyboard.getName(), turns, shiftedCount));
int turns = 0; int shiftedCount; if (keyboard.isSlanted() && shiftedRx.matcher(String.valueOf(password.charAt(i))).find()) { shiftedCount = 1; } else { shiftedCount = 0; final Map<Character, List<String>> graph = keyboard.getAdjacencyGraph(); while(true) { Character prevChar = password.charAt(j-1); j - 1, password.substring(i, j), keyboard.getName(), turns, shiftedCount));
@Override public double exec(Match match) { Keyboard keyboard = Keyboard.of(match.graph); int s = keyboard.getStartingPositions(); double d = keyboard.getAverageDegree(); double guesses = 0; int l = match.token.length(); int t = match.turns; for (int i = 2; i <= l; i++) { int possibleTurns = Math.min(t, i - 1); for (int j = 1; j <= possibleTurns; j++) { guesses += nCk(i - 1, j - 1) * s * Math.pow(d, j); } } if (match.shiftedCount != null && match.shiftedCount > 0) { int shiftedCount = match.shiftedCount; int unshiftedCount = match.token.length() - match.shiftedCount; if (shiftedCount == 0 || unshiftedCount == 0) { guesses *= 2; } else { int shiftedVariations = 0; for (int i = 1; i <= Math.min(shiftedCount, unshiftedCount); i++) { shiftedVariations += nCk(shiftedCount + unshiftedCount, i); } guesses *= shiftedVariations; } } return guesses; } }
public static Keyboard of(final String graph) { for (Keyboard keyboard : ALL_KEYBOARDS) { if (keyboard.getName().equals(graph)) { return keyboard; } } throw new IllegalArgumentException("Illegal graph " + graph); }
private Keyboard(final String name, final AdjacentGraphBuilder adjacentGraphBuilder) { this.name = name; this.adjacencyGraph = adjacentGraphBuilder.build(); this.slanted = adjacentGraphBuilder.isSlanted(); this.startingPositions = adjacencyGraph.size(); this.averageDegree = calcAverageDegree(adjacencyGraph); }
@Test public void testSpatialMatching() throws Exception { final Keyboard keyboard = Keyboard.QWERTY; final String token = "6tfGHJ"; List<Match> actualMatches = new SpatialMatcher(Collections.singletonList(keyboard)) .execute("rz!" + token + "%z"); String msg = "matches against spatial patterns surrounded by non-spatial patterns"; ExpectedMatch[] expectedMatches = new ExpectedMatch[]{ new ExpectedMatch(token, 3, 3 + token.length() - 1).graph(keyboard.getName()).turns(2).shiftedCount(3) }; assertMatches(msg, Pattern.Spatial, expectedMatches, actualMatches); }
@Test public void testSpatialMatching() throws Exception { List<Match> actualMatches = new SpatialMatcher(Collections.singletonList(keyboard)).execute(token); String msg = String.format("matches %s as a %s token", token, keyboard); assertMatches(msg, Pattern.Spatial, new ExpectedMatch[]{ new ExpectedMatch(token).graph(keyboard.getName()).turns(turns).shiftedCount(shifts)}, actualMatches); }