如何使用 Java 中的选项生成随机密码? [英] How to generate random passwords with options in Java?

查看:45
本文介绍了如何使用 Java 中的选项生成随机密码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道有用于数字的 nextIntnextDouble 等,甚至还有用于 truenextBooleannextBooleancode>false,但我没有找到任何关于 Strings 的内容:

  • 能够创建最小/最大甚至特定经度.例如,创建一个 6 或 8 个字符之间的随机字符串,或创建一个 12 个字符的随机字符串

  • 能够指定架构.例如,只有数字,只有字母,只有大写字母,只有像 !@#$%^& 等特殊符号

基本上,我想要创建的是一个紧缩工具,一个能够生成随机密码的笔测试工具,但对于 Java,仅使用 JDK(可以是 JDK8,但我对第三方库不感兴趣),在我的代码中使用最大数量的可用内置工具,只是为了不重新发明轮子.在开始编码之前,我想知道可用的方法.

我不是在寻找能够给我我想要的一切的东西.我只想知道要调用的函数来检索,例如,随机字符或随机大写字符等.将所有内容粘合在一起没有问题.

我正在寻找 OOTB 工具来帮助我实现这一目标.这也意味着,能够向 Java 询问特定字符,例如仅字母、仅符号、仅大写字母、仅数字等.我知道我可以使用字节并将它们转换为字符,但也许已经有一个有效的和内置功能可以使用.

DimaSan 能够提供我正在寻找的部分内容.我将能够混合使用他的代码和 Character 比较方法,例如:

isDigit是字母是字母或数字是小写大写isSpaceChar被定义为

我从 Oracle 文档中获取了这些方法,在那里我找到了一个很好的例子:

<块引用>

不习惯编写全球软件的开发人员可能会决定通过将字符与字符常量进行比较来确定字符的属性.为了例如,他们可能会编写这样的代码:

char ch;//...//这段代码是错误的!//检查 ch 是否为字母if ((ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z'))//...//检查 ch 是否为数字如果 (ch >='0' && ch <='9')//...//检查 ch 是否为空格if ((ch == ' ') || (ch =='\n') || (ch == '\t'))//...

<块引用>

前面的代码是错误的,因为它只适用于英语和其他语言很少.要将前面的示例国际化,请替换它带有以下语句:

char ch;//...//这段代码没问题!如果(Character.isLetter(ch))//...如果(Character.isDigit(ch))//...如果(Character.isSpaceChar(ch))//...

将所有东西粘合在一起,我就能做出我想要的东西.谢谢.

解决方案

Java 中没有 OOTB 随机字符串生成器.

但我想向您推荐一个我在我的项目中编写并使用的生成器:

public class RandomGenerator {private static final String characters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";公共静态字符串 generateRandom(int length) {如果(长度<= 0){throw new IllegalArgumentException("字符串长度必须是正整数");}随机随机 = new SecureRandom();StringBuilder sb = new StringBuilder(length);for (int i = 0; i < length; i++) {sb.append(characters.charAt(random.nextInt(characters.length())));}返回 sb.toString();}}

  • characters 字符串中,您可以输入您想要包含的任何字符
  • int length 参数是生成的随机字符串的长度.

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.

解决方案

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.

这篇关于如何使用 Java 中的选项生成随机密码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆