2024-10-12 00:57:35 +08:00
|
|
|
|
/*
|
|
|
|
|
* Copyright 2023-2024 the original author or authors.
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
2023-07-18 15:29:07 +08:00
|
|
|
|
package xyz.zhouxy.plusone.commons.util;
|
|
|
|
|
|
2024-05-28 09:38:59 +08:00
|
|
|
|
import java.security.SecureRandom;
|
2024-04-16 21:04:08 +08:00
|
|
|
|
import java.util.Random;
|
|
|
|
|
import java.util.concurrent.ThreadLocalRandom;
|
2023-07-18 15:29:07 +08:00
|
|
|
|
|
2024-04-03 16:20:41 +08:00
|
|
|
|
public final class RandomTools {
|
2024-04-16 21:04:08 +08:00
|
|
|
|
|
2024-05-28 09:38:59 +08:00
|
|
|
|
public static final SecureRandom DEFAULT_SECURE_RANDOM = new SecureRandom();
|
|
|
|
|
|
|
|
|
|
public static final String CAPITAL_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
|
|
|
|
|
public static final String LOWERCASE_LETTERS = "abcdefghijklmnopqrstuvwxyz";
|
|
|
|
|
public static final String NUMBERS = "0123456789";
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* 使用传入的随机数生成器,生成指定长度的字符串
|
|
|
|
|
*
|
|
|
|
|
* @param random 随机数生成器。根据需要可以传入
|
|
|
|
|
* {@link java.util.concurrent.ThreadLocalRandom}、{@link java.security.SecureRandom}
|
|
|
|
|
* 等,不为空
|
|
|
|
|
* @param sourceCharacters 字符池。字符串的字符将在数组中选,不为空
|
|
|
|
|
* @param length 字符串长度
|
|
|
|
|
* @return 随机字符串
|
|
|
|
|
*/
|
|
|
|
|
public static String randomStr(Random random, char[] sourceCharacters, int length) {
|
|
|
|
|
final char[] result = new char[length];
|
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
|
result[i] = sourceCharacters[random.nextInt(sourceCharacters.length)];
|
|
|
|
|
}
|
|
|
|
|
return String.valueOf(result);
|
2023-07-18 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
|
2024-04-16 21:04:08 +08:00
|
|
|
|
public static String randomStr(char[] sourceCharacters, int length) {
|
|
|
|
|
return randomStr(ThreadLocalRandom.current(), sourceCharacters, length);
|
|
|
|
|
}
|
|
|
|
|
|
2024-05-28 09:38:59 +08:00
|
|
|
|
public static String secureRandomStr(char[] sourceCharacters, int length) {
|
|
|
|
|
return randomStr(DEFAULT_SECURE_RANDOM, sourceCharacters, length);
|
|
|
|
|
}
|
|
|
|
|
|
2024-04-16 21:04:08 +08:00
|
|
|
|
/**
|
|
|
|
|
* 使用传入的随机数生成器,生成指定长度的字符串
|
|
|
|
|
*
|
|
|
|
|
* @param random 随机数生成器。根据需要可以传入
|
|
|
|
|
* {@link java.util.concurrent.ThreadLocalRandom}、{@link java.security.SecureRandom}
|
|
|
|
|
* 等,不为空
|
|
|
|
|
* @param sourceCharacters 字符池。字符串的字符将在数组中选,不为空
|
|
|
|
|
* @param length 字符串长度
|
|
|
|
|
* @return 随机字符串
|
|
|
|
|
*/
|
2024-05-28 09:38:59 +08:00
|
|
|
|
public static String randomStr(Random random, String sourceCharacters, int length) {
|
|
|
|
|
final char[] result = new char[length];
|
2023-07-18 15:29:07 +08:00
|
|
|
|
for (int i = 0; i < length; i++) {
|
2024-05-28 09:38:59 +08:00
|
|
|
|
result[i] = sourceCharacters.charAt(random.nextInt(sourceCharacters.length()));
|
2023-07-18 15:29:07 +08:00
|
|
|
|
}
|
|
|
|
|
return String.valueOf(result);
|
|
|
|
|
}
|
2024-05-28 09:38:59 +08:00
|
|
|
|
|
|
|
|
|
public static String randomStr(String sourceCharacters, int length) {
|
|
|
|
|
return randomStr(ThreadLocalRandom.current(), sourceCharacters, length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public static String secureRandomStr(String sourceCharacters, int length) {
|
|
|
|
|
return randomStr(DEFAULT_SECURE_RANDOM, sourceCharacters, length);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private RandomTools() {
|
|
|
|
|
throw new IllegalStateException("Utility class");
|
|
|
|
|
}
|
2023-07-18 15:29:07 +08:00
|
|
|
|
}
|