All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.nulabinc.zxcvbn.Matching Maven / Gradle / Ivy

Go to download

This is a java port of zxcvbn, which is a JavaScript password strength generator.

There is a newer version: 1.9.0
Show newest version
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;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy