diff --git a/src/main/java/xyz/zhouxy/plusone/commons/util/RandomUtil.java b/src/main/java/xyz/zhouxy/plusone/commons/util/RandomUtil.java new file mode 100644 index 0000000..9375728 --- /dev/null +++ b/src/main/java/xyz/zhouxy/plusone/commons/util/RandomUtil.java @@ -0,0 +1,18 @@ +package xyz.zhouxy.plusone.commons.util; + +import java.security.SecureRandom; + +public final class RandomUtil { + private RandomUtil() { + 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); + } +}