0

I want to generate alpha and numeric random password, i have two switches, one for alpha and one for numeric when both switches are on at that time generated string in alpha and numeric also but when my password length will be 4 so some time it's showing only in alpha but i want in digit and also in alpha.

my code is look like below

if (switchLetters.isChecked() && switchDigits.isChecked()) {
    randomString(mProgress, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", txtPassword);
}

String randomString(int len, String mPassword, TextView mTextView) {
    SecureRandom rnd = new SecureRandom();
    StringBuilder sb = new StringBuilder(len);
    for (int i = 0; i < len; i++)
    sb.append(mPassword.charAt(rnd.nextInt(mPassword.length())));
    mTextView.setText(sb.toString());

    return sb.toString();
}
YakuZa
  • 516
  • 7
  • 12

3 Answers3

0

Here is good example of generating secure random alpha and numeric string using SecureRandom

import java.security.SecureRandom; import java.math.BigInteger;

public final class SessionIdentifierGenerator {
  private SecureRandom random = new SecureRandom();

  public String nextSessionId() {
    return new BigInteger(130, random).toString(32);
  }
}

Here is example how to generate string with custom length.

Community
  • 1
  • 1
thealeksandr
  • 1,686
  • 12
  • 17
  • but here how can i define string length because string length it's depend on seekbar progress! –  Feb 08 '17 at 10:56
  • sorry but when my string length is 3 or 4 at that some time it's showing in alpha. –  Feb 08 '17 at 11:10
  • All the time? It's random. It can generate only letters sometimes – thealeksandr Feb 08 '17 at 11:12
  • It's because it's random. It can be letters only or numbers only or both. Because there is more different letters then numbers there is chance that for small string it will be only letters. – thealeksandr Feb 08 '17 at 11:21
  • yeah but when user select only 4 string length at that time arise this issue on sometime –  Feb 08 '17 at 11:49
0

Try following code:

String randomString(int len, String mPassword, TextView mTextView) {
        SecureRandom rnd = new SecureRandom();
        StringBuilder sb = new StringBuilder(len);
        for (int i = 0; i < len; i++)
            sb.append(mPassword.charAt(rnd.nextInt(mPassword.length())));

        mTextView.setText(sb.toString());
        return sb.toString();
    }


    private String generateAlphaNumericString(int len, String mPassword, TextView mTextView) {

        String regexDigit = "\\d+";
        String regexAlphabets = "[a-zA-Z]+";
        String randomAlphaNumString;

        do {
            randomAlphaNumString = randomString(mProgress, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", txtPassword);

        } while (while ((randomAlphaNumString.matches(regexDigit) || randomAlphaNumString.matches(regexAlphabets)));
        return randomAlphaNumString;
    }
nnn
  • 980
  • 6
  • 13
  • My Bad. Updated the code. Please check now. Make call to generateAlphaNumericString() function. It should work. – nnn Feb 08 '17 at 11:57
  • below vote label(+1) you will get right icon, select that. – nnn Feb 08 '17 at 12:12
0

You pass in the pool of letters to select from without distinguishing between letters and digits. If you want to ensure both are included, then you need to keep them separate for longer.

The TextView parameter is a needless complication and belongs somewhere in the calling GUI code, rather than in this utility method. You can add it back in if you have to.

This version picks a letter if switchLetters is checked and picks a digit if switchNumbers is checked, ensuring at least one of each selected type is picked. The rest of the characters are random.

Because the first two characters are in a specific order, the result is securely shuffled. That is why I used an array to hold the result, so it was easier to shuffle.

I have put in a lot of comments to help explain things.

final String alpha = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

final String digits = "0123456789";

Random secRand = new SecureRandom();


String randomString(int len) {

    if (len < 2) {
        throw new IllegalArgumentException("randomString - length too short: " + len);
    }

    // Pool of characters to select from.
    String pool = "";

    // Array to hold random characters.
    Character[] result = new Character[len];

    // Index into result.
    int index = 0;

    if (switchLetters.isChecked()) {
        // Put letters in pool.
        pool = pool + alpha;

        // Ensure at least one letter.
        result[index] = alpha.charAt(secRand.nextInt(alpha.length()));
        index++;
    }

    if (switchDigits.isChecked()) {
        // Put digits in pool.
        pool = pool + digits;

        // Ensure at least one digit.
        result[index] = digits.charAt(secRand.nextInt(digits.length()));
        index++;
    }

    // Fill rest of result array from pool.
    for ( ; index < len; index++) {
        result[index] = pool.charAt(secRand.nextInt(pool.length()));
    }

    // Shuffle result array with secRand to hide ordering.
    Collections.shuffle(Arrays.asList(result), secRand);

    // Assemble return string.
    StringBuilder sb = new StringBuilder(len);
    for (Character c : result) {
        sb.append(c);
    }
    return sb.toString();

}  // end randomString()
rossum
  • 15,344
  • 1
  • 24
  • 38