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

com.appslandia.common.crypto.PasswordGenerator Maven / Gradle / Ivy

The newest version!
// The MIT License (MIT)
// Copyright © 2015 AppsLandia. All rights reserved.

// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

package com.appslandia.common.crypto;

import java.security.SecureRandom;
import java.util.Random;

import com.appslandia.common.base.InitializeObject;
import com.appslandia.common.base.Permutation;
import com.appslandia.common.base.WordsGenerator;
import com.appslandia.common.utils.AssertUtils;
import com.appslandia.common.utils.CharUtils;
import com.appslandia.common.utils.RandomUtils;

/**
 *
 * @author Loc Ha
 *
 */
public class PasswordGenerator extends InitializeObject {

	private static final char[] __upperCases = CharUtils.toCharRange("A-Z");
	private static final char[] __lowerCases = CharUtils.toCharRange("a-z");
	private static final char[] __digits = CharUtils.toCharRange("0-9");
	private static final char[] __symbols = "@#$%".toCharArray();

	private int upperCases;
	private int lowerCases;
	private int digits;
	private int symbols;

	final Random random = new SecureRandom();

	@Override
	protected void init() throws Exception {
		this.upperCases = Math.max(1, this.upperCases);
		this.lowerCases = Math.max(1, this.lowerCases);
		this.digits = Math.max(1, this.digits);
		this.symbols = Math.max(1, this.symbols);
	}

	public char[] generatePassword(int length) {
		AssertUtils.assertTrue(length >= 8, "length >= 8");
		return generatePassword(length, length);
	}

	public char[] generatePassword(int minLength, int maxLength) {
		this.initialize();
		AssertUtils.assertTrue(minLength <= maxLength, "maxLength >= minLength.");
		AssertUtils.assertTrue(minLength >= 8, "minLength >= 8");

		int length = RandomUtils.nextInt(this.random, minLength, maxLength);
		char[] pwdChars = new char[length];
		int emptyCount = length;

		Permutation perm = new Permutation(4);
		while (perm.hasNext()) {
			int next = perm.next();
			switch (next) {
			case 0:
				emptyCount = WordsGenerator.randomChars(this.random, __upperCases, pwdChars, this.upperCases, emptyCount);
				break;
			case 1:
				emptyCount = WordsGenerator.randomChars(this.random, __lowerCases, pwdChars, this.lowerCases, emptyCount);
				break;
			case 2:
				emptyCount = WordsGenerator.randomChars(this.random, __digits, pwdChars, this.digits, emptyCount);
				break;
			default:
				emptyCount = WordsGenerator.randomChars(this.random, __symbols, pwdChars, this.symbols, emptyCount);
				break;
			}
		}

		for (int i = 0; i < length; i++) {
			if (pwdChars[i] == 0) {
				int src = this.random.nextInt(4);
				switch (src) {
				case 0:
					pwdChars[i] = __upperCases[this.random.nextInt(__upperCases.length)];
					break;
				case 1:
					pwdChars[i] = __lowerCases[this.random.nextInt(__lowerCases.length)];
					break;
				case 2:
					pwdChars[i] = __digits[this.random.nextInt(__digits.length)];
					break;
				default:
					pwdChars[i] = __symbols[this.random.nextInt(__symbols.length)];
					break;
				}
			}
		}
		return pwdChars;
	}

	public PasswordGenerator setUpperCases(int upperCases) {
		this.assertNotInitialized();
		this.upperCases = upperCases;
		return this;
	}

	public PasswordGenerator setLowerCases(int lowerCases) {
		this.assertNotInitialized();
		this.lowerCases = lowerCases;
		return this;
	}

	public PasswordGenerator setDigits(int digits) {
		this.assertNotInitialized();
		this.digits = digits;
		return this;
	}

	public PasswordGenerator setSymbols(int symbols) {
		this.assertNotInitialized();
		this.symbols = symbols;
		return this;
	}
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy