So I'm building an app that generates a strong password per the specifications of the user. The spec part of the UI looks like this.
The following is the onCreate from the MainActivity.java class. I tried creating some of the logic, such as if statements when a certain radio button is checked and adding allowed characters to the String variable whenever a checkbox is checked. The View objects are all global btw but I couldn't figure out how to create a random String using at least one of each allowed character, within the character limit set by the user.
Here is the code:
private static int MAX_LENGTH;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
capitalLetter = (CheckBox) findViewById(R.id.capital_letter);
lowercaseLetter = (CheckBox) findViewById(R.id.lowercase_letter);
numbers = (CheckBox) findViewById(R.id.numbers);
symbols = (CheckBox) findViewById(R.id.symbols);
passGroup = (RadioGroup) findViewById(R.id.passRadioGroup);
sizeFour = (RadioButton) findViewById(R.id.size_four);
sizeEight = (RadioButton) findViewById(R.id.size_eight);
sizeTwelve = (RadioButton) findViewById(R.id.size_twelve);
sizeSixteen = (RadioButton) findViewById(R.id.size_sixteen);
passHint = (EditText) findViewById(R.id.passwordHint);
passShow = (TextView) findViewById(R.id.passwordDisplay);
passGenerate = (Button) findViewById(R.id.passwordGenerate);
passClear = (Button) findViewById(R.id.passwordClear);
String allowedCharacters = "";
// Determines the types of characters permitted when a check box is checked.
if (capitalLetter.isChecked()) {allowedCharacters += "ABCDEFGHIJKLMNOPQRSTUVWXYZ";}
if (lowercaseLetter.isChecked()) {allowedCharacters += "abcdefghijklmnopqrstuvwxyz";}
if (numbers.isChecked()) {allowedCharacters += "0123456789";}
if (symbols.isChecked()) {allowedCharacters += "!@#$%^&*()_-+=<>?/{}~|";}
//Determines the length of the string based on which radio button the user has selected.
int checkedRadioButtonId = passGroup.getCheckedRadioButtonId();
if (checkedRadioButtonId == 1) {MAX_LENGTH = 4;}
if (checkedRadioButtonId == 2) {MAX_LENGTH = 8;}
if (checkedRadioButtonId == 3) {MAX_LENGTH = 12;}
if (checkedRadioButtonId == 4) {MAX_LENGTH = 16;}
}
Basic Idea about generate random String:
private static final String ALLOWED_CHARACTERS ="0123456789qwertyuiopasdfghjklzxcvbnm";
private static String getRandomString(final int sizeOfPasswordString){
final Random random=new Random();
final StringBuilder sb=new StringBuilder(sizeOfPasswordString);
for(int i=0;i<sizeOfRandomString;++i){
sb.append(ALLOWED_CHARACTERS.charAt(random.nextInt(ALLOWED_CHARACTERS.length())));
}
return sb.toString();
}
Here is what you are looking for. First you have to add required characters according to the checkboxes checked and then you have to fill the lefover length with random characters from the whole string of allowed characters.
private static String generateRandomPassword(int max_length, boolean upperCase, boolean lowerCase, boolean numbers, boolean specialCharacters)
{
String upperCaseChars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
String lowerCaseChars = "abcdefghijklmnopqrstuvwxyz";
String numberChars = "0123456789";
String specialChars = "!@#$%^&*()_-+=<>?/{}~|";
String allowedChars = "";
Random rn = new Random();
StringBuilder sb = new StringBuilder(max_length);
//this will fulfill the requirements of atleast one character of a type.
if(upperCase) {
allowedChars += upperCaseChars;
sb.append(upperCaseChars.charAt(rn.nextInt(upperCaseChars.length()-1)));
}
if(lowerCase) {
allowedChars += lowerCaseChars;
sb.append(lowerCaseChars.charAt(rn.nextInt(lowerCaseChars.length()-1)));
}
if(numbers) {
allowedChars += numberChars;
sb.append(numberChars.charAt(rn.nextInt(numberChars.length()-1)));
}
if(specialCharacters) {
allowedChars += specialChars;
sb.append(specialChars.charAt(rn.nextInt(specialChars.length()-1)));
}
//fill the allowed length from different chars now.
for(int i=sb.length();i < max_length;++i){
sb.append(allowedChars.charAt(rn.nextInt(allowedChars.length())));
}
return sb.toString();
}
Usage : In the implementation of onclick of button for which you are generating password add the following:
String pass = generateRandomPassword(MAX_LENGTH, capitalLetter.isChecked(), lowercaseLetter.isChecked(), numbers.isChecked(),symbols.isChecked());
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With