add Text.randomPassword
This commit is contained in:
@@ -165,11 +165,31 @@ public class Text {
|
||||
SecureRandom r = new SecureRandom();
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (int i = 0; i < length; i++) {
|
||||
sb.append(pool.charAt(r.nextInt(pool.length() - 1)));
|
||||
sb.append(pool.charAt(r.nextInt(pool.length())));
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
public static String randomPassword(int length) {
|
||||
if (length < 6 || 32 < length) {
|
||||
throw new IllegalArgumentException("Password length must be between 6 and 32");
|
||||
}
|
||||
SecureRandom r = new SecureRandom();
|
||||
char[] chars = new char[length];
|
||||
chars[0] = LETTER_POOL.charAt(r.nextInt(LETTER_POOL.length()));
|
||||
chars[1] = NUMBER_POOL.charAt(r.nextInt(NUMBER_POOL.length()));
|
||||
for (int i = 2; i < length; i++) {
|
||||
chars[i] = PASSWORD_CHAR_POOL.charAt(r.nextInt(PASSWORD_CHAR_POOL.length()));
|
||||
}
|
||||
for (int i = chars.length - 1; 0 < i; i--) {
|
||||
int j = r.nextInt(i + 1);
|
||||
char temp = chars[i];
|
||||
chars[i] = chars[j];
|
||||
chars[j] = temp;
|
||||
}
|
||||
return new String(chars);
|
||||
}
|
||||
|
||||
/**
|
||||
* 较短的临时 UUID,如果使用在庞大的数据里,很可能会发生重复
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user