public class Main {
public static void main(String[] args) {
byte passwordLength = 15;
System.out.println(new PasswordGenerator().generatePassword(passwordLength));
}
}
import java.security.SecureRandom;
/**
* A class whose instances generate random passwords to be stored in a password manager!
*
* @author orochi-100
* @version 03.04.2020
*/
public class PasswordGenerator {
private final static char[] PASSWORD_CHARACTERS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ ".toCharArray();
private final static char[] REGULAR_PASSWORD_CHARACTERS = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789".toCharArray();
/**
* Basic constructor
*/
public PasswordGenerator() {}
/**
* Generates a random password in ASCII characters.
*
* @param length the password-length to specify
* @return the password-<code>String</code>
* @throws IllegalArgumentException if length is < 0
*/
public String generatePassword(byte length) {
if (length < 0) throw new IllegalArgumentException("length is < 0");
StringBuilder password = new StringBuilder();
SecureRandom secureRandom = new SecureRandom();
for (int i = 0; i < length; i++) password.append(REGULAR_PASSWORD_CHARACTERS[secureRandom.nextInt(REGULAR_PASSWORD_CHARACTERS.length)]);
return password.toString();
}
}