Here is a simple password generator in Java:
public class PasswordGenerator {
public static final String LOWER_CASE = "abcdefghijklmnopqrstuvwxyz";
public static final String UPPER_CASE = LOWER_CASE.toUpperCase();
public static final String DIGITS = "0123456789";
public static final String PUNCTUATION_MARKS = "!,.:;@#$%^*()_-+={}\"<>?/\\№";
public static final String SIMILAR_CHARACTERS = "Ll1ioO0";
static class CharacterSet {
private String include;
private String exclude;
public CharacterSet() {
this.include = "";
this.exclude = "";
}
public void include(String str) {
include += str;
}
public void exclude(String str) {
exclude += str;
}
public char randomCharacter(Random random) {
int randomIndex;
char randomCharacter;
do {
randomIndex = random.nextInt(include.length());
randomCharacter = include.charAt(randomIndex);
} while (exclude.contains(String.valueOf(randomCharacter)));
return randomCharacter;
}
}
public String generatePassword(PasswordSettings passwordSettings) {
CharacterSet characterSet = characterSetFromSettings(passwordSettings);
StringBuilder passwordBuilder = new StringBuilder();
Random random = new Random(System.currentTimeMillis());
for (int i = 0; i < passwordSettings.getLength(); ++i) {
passwordBuilder.append(characterSet.randomCharacter(random));
}
return passwordBuilder.toString();
}
private CharacterSet characterSetFromSettings(PasswordSettings passwordSettings) {
CharacterSet characterSet = new CharacterSet();
if (passwordSettings.isLowerCase()) {
characterSet.include(LOWER_CASE);
}
if (passwordSettings.isUpperCase()) {
characterSet.include(UPPER_CASE);
}
if (passwordSettings.isDigits()) {
characterSet.include(DIGITS);
}
if (passwordSettings.isPunctuationMarks()) {
characterSet.include(PUNCTUATION_MARKS);
}
if (passwordSettings.isExcludeSimilarCharacters()) {
characterSet.exclude(SIMILAR_CHARACTERS);
}
return characterSet;
}
}
PasswordSettings
is just a POJO class.
public class PasswordSettings implements Parcelable {
public static final Parcelable.Creator<PasswordSettings> CREATOR = new Parcelable.Creator<PasswordSettings>() {
@Override
public PasswordSettings createFromParcel(Parcel source) {
return new PasswordSettings(source);
}
@Override
public PasswordSettings[] newArray(int size) {
return new PasswordSettings[size];
}
};
private int length;
private boolean lowerCase;
private boolean upperCase;
private boolean digits;
private boolean punctuationMarks;
private boolean excludeSimilarCharacters;
public int getLength() {
return length;
}
public void setLength(int length) {
this.length = length;
}
public boolean isLowerCase() {
return lowerCase;
}
public void setLowerCase(boolean lowerCase) {
this.lowerCase = lowerCase;
}
public boolean isUpperCase() {
return upperCase;
}
public void setUpperCase(boolean upperCase) {
this.upperCase = upperCase;
}
public boolean isDigits() {
return digits;
}
public void setDigits(boolean digits) {
this.digits = digits;
}
public boolean isPunctuationMarks() {
return punctuationMarks;
}
public void setPunctuationMarks(boolean punctuationMarks) {
this.punctuationMarks = punctuationMarks;
}
public boolean isExcludeSimilarCharacters() {
return excludeSimilarCharacters;
}
public void setExcludeSimilarCharacters(boolean excludeSimilarCharacters) {
this.excludeSimilarCharacters = excludeSimilarCharacters;
}
public PasswordSettings() {
// empty constructor
}
public PasswordSettings(Parcel in) {
length = in.readInt();
lowerCase = ParcelHelper.readBoolean(in);
upperCase = ParcelHelper.readBoolean(in);
digits = ParcelHelper.readBoolean(in);
punctuationMarks = ParcelHelper.readBoolean(in);
excludeSimilarCharacters = ParcelHelper.readBoolean(in);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(length);
ParcelHelper.writeBoolean(dest, lowerCase);
ParcelHelper.writeBoolean(dest, upperCase);
ParcelHelper.writeBoolean(dest, digits);
ParcelHelper.writeBoolean(dest, punctuationMarks);
ParcelHelper.writeBoolean(dest, excludeSimilarCharacters);
}
}