com.nulabinc.zxcvbn.Matching Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of zxcvbn Show documentation
Show all versions of zxcvbn Show documentation
This is a java port of zxcvbn, which is a JavaScript password strength generator.
package com.nulabinc.zxcvbn;
import com.nulabinc.zxcvbn.matchers.*;
import com.nulabinc.zxcvbn.matchers.Dictionary;
import java.util.*;
public class Matching {
private static final Map> BASE_RANKED_DICTIONARIES = new HashMap<>();
static {
for (Map.Entry frequencyListRef : Dictionary.FREQUENCY_LISTS.entrySet()) {
String name = frequencyListRef.getKey();
String[] ls = frequencyListRef.getValue();
BASE_RANKED_DICTIONARIES.put(name, buildRankedDict(ls));
}
}
private final Map> rankedDictionaries;
public Matching() {
this(new ArrayList());
}
public Matching(List orderedList) {
if (orderedList == null) orderedList = new ArrayList<>();
this.rankedDictionaries = new HashMap<>();
for (Map.Entry> baseRankedDictionary : BASE_RANKED_DICTIONARIES.entrySet()) {
this.rankedDictionaries.put(
baseRankedDictionary.getKey(),
baseRankedDictionary.getValue());
}
this.rankedDictionaries.put("user_inputs", buildRankedDict(orderedList.toArray(new String[]{})));
}
public List omnimatch(String password) {
return new OmnibusMatcher(rankedDictionaries).execute(password);
}
private static Map buildRankedDict(String[] orderedList) {
HashMap result = new HashMap<>();
int i = 1; // rank starts at 1, not 0
for(String word: orderedList) {
result.put(word, i);
i++;
}
return result;
}
}