-
Notifications
You must be signed in to change notification settings - Fork 54
/
SeparatorMatcher.java
69 lines (56 loc) · 1.83 KB
/
SeparatorMatcher.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package me.gosimple.nbvcxz.matching;
import me.gosimple.nbvcxz.matching.match.Match;
import me.gosimple.nbvcxz.matching.match.SeparatorMatch;
import me.gosimple.nbvcxz.resources.Configuration;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
* Extract any likely separator within the password.
*
* @author Adam Brusselback
*/
public final class SeparatorMatcher implements PasswordMatcher
{
private static final Pattern NON_ALPHA_NUMERIC = Pattern.compile("[^a-zA-Z\\d]");
public List<Match> match(final Configuration configuration, final String password)
{
Matcher matcher = NON_ALPHA_NUMERIC.matcher(password);
List<Match> matches = new ArrayList<>();
if (password.length() <= 5)
{
return matches;
}
List<String> occurrences = new ArrayList<>();
while (matcher.find())
{
if (matcher.start() != 0 && matcher.end() != password.length() - 1)
{
occurrences.add(matcher.group());
}
}
int count = 0;
String token = null;
for (String new_token : new HashSet<>(occurrences))
{
int new_count = Collections.frequency(occurrences, new_token);
if (new_count > count)
{
count = new_count;
token = new_token;
}
}
matcher.reset();
while (matcher.find())
{
if (matcher.group().equals(token) && matcher.start() != 0 && matcher.end() != password.length() - 1)
{
matches.add(new SeparatorMatch(matcher.group(), configuration, matcher.start(), matcher.end() - 1));
}
}
return matches;
}
}