passwords.PwGen.java Source code

Java tutorial

Introduction

Here is the source code for passwords.PwGen.java

Source

/*  
 *  Copyright 2016 by Lorenz Wellmer <lorwel@mailbox.org>
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *    http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 */

package passwords;

import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import org.apache.commons.lang3.ArrayUtils;

public class PwGen {
    private String pattern = "";
    private String output = "";
    private int length = 0;
    //Arrays containing the possible characters
    private char[] vows = { 'a', 'e', 'i', 'o', 'u', 'y' };
    private char[] cons = { 'b', 'c', 'd', 'f', 'g', 'h', 'j', 'k', 'l', 'm', 'n', 'p', 'q', 'r', 's', 't', 'v',
            'w', 'x', 'z' };
    private char[] uvows = { 'A', 'E', 'I', 'O', 'U', 'Y' };
    private char[] ucons = { 'B', 'C', 'D', 'F', 'G', 'H', 'J', 'K', 'L', 'M', 'N', 'P', 'Q', 'R', 'S', 'T', 'V',
            'W', 'X', 'Z' };
    private char[] nums = { '1', '2', '3', '4', '5', '6', '7', '8', '9', '0' };
    private char[] specs = { '!', '?', '$', '%', '&', '#', '+' };
    //Array list for creating random pattern
    private List<String> pats = new ArrayList<String>();
    //Random Numbers generator
    Random r = new Random();

    //Main Constructor
    public PwGen() {
    }

    public void setLength(int length) {
        this.length = length;
    }

    public void setPattern(String pattern) {
        this.pattern = pattern;
    }

    //Check if the inputs are valid
    //If not: throw exception
    public void checkPattern() throws IllegalArgumentException {
        if (!pattern.matches("[CVcvds]*") || pattern.equals("")) {
            throw new IllegalArgumentException("Invalid Input");
        }
    }

    public void checkRandom() throws IllegalArgumentException {
        if (length <= 0) {
            throw new IllegalArgumentException("Invalid Input");
        }
    }

    //Method for removing ambiguous characters
    public void noAmbiguous() {
        cons = ArrayUtils.removeElement(cons, 'l');
        uvows = ArrayUtils.removeElements(uvows, 'I', 'O');
    }

    //Method to generate the password using the provided pattern
    private void createPw() {
        for (int i = 0; i < pattern.length(); i++) {
            char patchar = pattern.charAt(i);
            if (patchar == 'C')
                output = output + ucons[r.nextInt(ucons.length)];
            if (patchar == 'V')
                output = output + uvows[r.nextInt(uvows.length)];
            if (patchar == 'c')
                output = output + cons[r.nextInt(cons.length)];
            if (patchar == 'v')
                output = output + vows[r.nextInt(vows.length)];
            if (patchar == 'd')
                output = output + nums[r.nextInt(nums.length)];
            if (patchar == 's')
                output = output + specs[r.nextInt(specs.length)];
        }
    }

    //method generating passwords using the user provided pattern
    public String getPatternPw() {
        createPw();
        return output;
    }

    //Method to generate random pattern for password creation
    public String getRandomPw(String mode) throws IllegalArgumentException {
        String ranpat = "";
        for (int x = 0; x < length; x++) {
            if (mode.contains("upcase")) {
                pats.add("C");
                pats.add("V");
            }
            if (mode.contains("lowcase")) {
                pats.add("c");
                pats.add("v");
            }
            if (mode.contains("digits")) {
                pats.add("d");
            }
            if (mode.contains("specials")) {
                pats.add("s");
            }
            if (mode.equals(""))
                throw new IllegalArgumentException();

            ranpat = ranpat + pats.get(r.nextInt(pats.size()));
        }
        this.pattern = ranpat;

        createPw();
        return output;
    }

    //Method to calculate the entropy of the generated password
    public int getEntropy(String i) {
        double c = i.chars().distinct().count();
        double l = i.length();
        int e = (int) Math.round(l * Math.log(c) / Math.log(2));
        return e;
    }
}