public class Main {
public static void main(String[] args) {
System.out.println(new PasswordGenerator().generatePassword((byte) 50));
}
}
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 String PASSWORD_CHARACTERS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~ ";
/**
* 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();
StringBuilder password = new StringBuilder();
SecureRandom secureRandom = new SecureRandom();
for (int i = 0; i < length; i++) password.append(PASSWORD_CHARACTERS.charAt(secureRandom.nextInt(PASSWORD_CHARACTERS.length())));
return password.toString();
}
}