You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
67 lines
1.7 KiB
67 lines
1.7 KiB
import java.security.SecureRandom;
|
|
|
|
public class PasswordGenerator {
|
|
|
|
final String lowercaseCharacters = "abcdefghjkmnpqrstuvwxyz";
|
|
final String uppercaseCharacters = "ABCDEFGHJKMNPQRSTUVWXYZ";
|
|
final String digits = "0123456789";
|
|
|
|
private int length = 12;
|
|
private boolean useUppercase = true;
|
|
private boolean useLowercase = true;
|
|
private boolean useDigits = true;
|
|
|
|
|
|
public String generateRandomPassword() {
|
|
StringBuilder generatedPassword = new StringBuilder();
|
|
SecureRandom rand = new SecureRandom();
|
|
String characterPool = "";
|
|
|
|
if (isUseLowercase()) {
|
|
characterPool += lowercaseCharacters;
|
|
}
|
|
if (isUseUppercase()) {
|
|
characterPool += uppercaseCharacters;
|
|
}
|
|
if (isUseDigits()) {
|
|
characterPool += digits;
|
|
}
|
|
|
|
for (int generatorPosition = 0; generatorPosition < getLength(); generatorPosition++) {
|
|
generatedPassword.append(characterPool.charAt(rand.nextInt(characterPool.length())));
|
|
}
|
|
return generatedPassword.toString();
|
|
}
|
|
|
|
public boolean isUseUppercase() {
|
|
return useUppercase;
|
|
}
|
|
|
|
public void setUseUppercase(boolean useUppercase) {
|
|
this.useUppercase = useUppercase;
|
|
}
|
|
|
|
public boolean isUseLowercase() {
|
|
return useLowercase;
|
|
}
|
|
|
|
public void setUseLowercase(boolean useLowercase) {
|
|
this.useLowercase = useLowercase;
|
|
}
|
|
|
|
public boolean isUseDigits() {
|
|
return useDigits;
|
|
}
|
|
|
|
public void setUseDigits(boolean useDigits) {
|
|
this.useDigits = useDigits;
|
|
}
|
|
|
|
public int getLength() {
|
|
return length;
|
|
}
|
|
|
|
public void setLength(int length) {
|
|
this.length = length;
|
|
}
|
|
}
|