2023-07-18 15:29:07 +08:00
|
|
|
package xyz.zhouxy.plusone.commons.util;
|
|
|
|
|
|
|
|
import java.security.SecureRandom;
|
|
|
|
|
2024-04-03 16:20:41 +08:00
|
|
|
public final class RandomTools {
|
|
|
|
private RandomTools() {
|
2023-07-18 15:29:07 +08:00
|
|
|
throw new IllegalStateException("Utility class");
|
|
|
|
}
|
|
|
|
|
|
|
|
public static String secureRandomStr(char[] sourceCharacters, int length) {
|
|
|
|
SecureRandom random = new SecureRandom();
|
|
|
|
char[] result = new char[length];
|
|
|
|
for (int i = 0; i < length; i++) {
|
|
|
|
result[i] = sourceCharacters[random.nextInt(sourceCharacters.length)];
|
|
|
|
}
|
|
|
|
return String.valueOf(result);
|
|
|
|
}
|
|
|
|
}
|