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.
119 lines
3.8 KiB
119 lines
3.8 KiB
import java.security.SecureRandom;
|
|
|
|
public class PasswordGenerator {
|
|
|
|
final String lowercaseCharacters = "abcdefghjkmnpqrstuvwxyz";
|
|
final String uppercaseCharacters = "ABCDEFGHJKMNPQRSTUVWXYZ";
|
|
final String digits = "0123456789";
|
|
final String specialCharacters = ".!?=@#$()%^&/*_-+";
|
|
final SecureRandom rand = new SecureRandom();
|
|
|
|
private int length = 12;
|
|
private boolean useUppercase = true;
|
|
private boolean useLowercase = true;
|
|
private boolean useDigits = true;
|
|
private boolean useSpecialCharacters = true;
|
|
private boolean requireEveryConfiguredCharacterType = true;
|
|
|
|
|
|
public String generateRandomPassword() {
|
|
StringBuilder generatedPassword = new StringBuilder();
|
|
String characterPool = "";
|
|
|
|
if (isUseLowercase()) {
|
|
characterPool += lowercaseCharacters;
|
|
}
|
|
if (isUseUppercase()) {
|
|
characterPool += uppercaseCharacters;
|
|
}
|
|
if (isUseDigits()) {
|
|
characterPool += digits;
|
|
}
|
|
if (isUseSpecialCharacters()) {
|
|
characterPool += specialCharacters;
|
|
}
|
|
|
|
for (int generatorPosition = 0; generatorPosition < getLength(); generatorPosition++) {
|
|
String customCharacterPool = characterPool;
|
|
|
|
if (isRequireEveryConfiguredCharacterType()) {
|
|
if (generatorPosition == 0 && isUseLowercase()) {
|
|
customCharacterPool = lowercaseCharacters;
|
|
} else if (generatorPosition == 1 && isUseUppercase()) {
|
|
customCharacterPool = uppercaseCharacters;
|
|
} else if (generatorPosition == 2 && isUseDigits()) {
|
|
customCharacterPool = digits;
|
|
} else if (generatorPosition == 3 && isUseSpecialCharacters()) {
|
|
customCharacterPool = specialCharacters;
|
|
} else {
|
|
customCharacterPool = characterPool;
|
|
}
|
|
}
|
|
|
|
generatedPassword.append(customCharacterPool.charAt(rand.nextInt(customCharacterPool.length())));
|
|
}
|
|
|
|
return shufflePassword(generatedPassword.toString());
|
|
}
|
|
|
|
public String shufflePassword(String password) {
|
|
StringBuilder shuffledPassword = new StringBuilder();
|
|
StringBuilder passwordCopy = new StringBuilder(password);
|
|
|
|
while (passwordCopy.length() != 0) {
|
|
int index = rand.nextInt(passwordCopy.length());
|
|
char c = passwordCopy.charAt(index);
|
|
shuffledPassword.append(c);
|
|
passwordCopy.deleteCharAt(index);
|
|
}
|
|
return shuffledPassword.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 boolean isUseSpecialCharacters() {
|
|
return useSpecialCharacters;
|
|
}
|
|
|
|
public void setUseSpecialCharacters(boolean useSpecialCharacters) {
|
|
this.useSpecialCharacters = useSpecialCharacters;
|
|
}
|
|
|
|
public boolean isRequireEveryConfiguredCharacterType() {
|
|
return requireEveryConfiguredCharacterType;
|
|
}
|
|
|
|
public void setRequireEveryConfiguredCharacterType(boolean requireEveryConfiguredCharacterType) {
|
|
this.requireEveryConfiguredCharacterType = requireEveryConfiguredCharacterType;
|
|
}
|
|
|
|
public int getLength() {
|
|
return length;
|
|
}
|
|
|
|
public void setLength(int length) {
|
|
this.length = length;
|
|
}
|
|
}
|