facundop facundop - 1 month ago 11
Java Question

How to generate random passwords with options in Java?

I know there is

nextInt
,
nextDouble
, etc for numbers and even a
nextBoolean
for
true
or
false
, but I haven't found anything for
Strings
with the following:


  • Ability to create a minimum / maximum or even specific longitudes. For instance, create a random string between 6 or 8 characters, or create a random string with 12 characters

  • Ability to specify a schema. For instance, only numbers, only letters, only uppercase letters, only special signs like
    !@#$%^&
    , etc



Basically, what I'm trying to create, is a Crunch tool, a pen testing tool able to generate random passwords, but for Java, using only the JDK (it can be JDK8, but I'm not interested in third party libraries), making use of the maximum amount of available built in tools in my code, just for the sake of not reinventing the wheel. I would like to know the available methods before starting to code.

I'm not looking for something able to give me everything I want. I just would like to know the function to call to retrieve, for instance, a random char, or a random uppercase char, etc. I have no issues gluing everything together.

I'm looking for OOTB tools to help me accomplish this. This also implies, the ability to ask Java for specifics characters, like only letters, only signs, only upper case letters, only numbers, etc. I know I can play with bytes and converting them to chars, but perhaps there is an already efficient and built in functionality to make use of.

DimaSan was able to provide part of what I was looking for. I will be able to use a mix of his code and the
Character
comparison methods, like:

isDigit
isLetter
isLetterOrDigit
isLowerCase
isUpperCase
isSpaceChar
isDefined


I took those methods from the Oracle docs, where I was able to find a good example:


Developers who aren't used to writing global software might determine
a character's properties by comparing it with character constants. For
instance, they might write code like this:


char ch;
//...

// This code is WRONG!

// check if ch is a letter
if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))
// ...

// check if ch is a digit
if (ch >= '0' && ch <= '9')
// ...

// check if ch is a whitespace
if ((ch == ' ') || (ch =='\n') || (ch == '\t'))
// ...



The preceding code is wrong because it works only with English and a
few other languages. To internationalize the previous example, replace
it with the following statements:


char ch;
// ...

// This code is OK!

if (Character.isLetter(ch))
// ...

if (Character.isDigit(ch))
// ...

if (Character.isSpaceChar(ch))
// ...


Gluing everything together I will be able to make what I was asking for. Thank you.

Answer

There are no OOTB random string generator in Java.

But I'd like to recommend you a generator I wrote and use in my project:

public class RandomGenerator {
    private static final String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

    public static String generateRandom(int length) {
        if (length <= 0) {
            throw new IllegalArgumentException("String length must be a positive integer");
        }

        Random random = new SecureRandom();
        StringBuilder sb = new StringBuilder(length);
        for (int i = 0; i < length; i++) {
            sb.append(characters.charAt(random.nextInt(characters.length())));
        }

        return sb.toString();
    }
}
  • in characters string you can put any characters you want to be included
  • int length parameter is the length of generated random string.