Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

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 =='
') || (ch == '	'))
    // ...

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...