From 7208aa5aae9a22a7ee10a3fdb94e1d97c19a0873 Mon Sep 17 00:00:00 2001 From: ZhouXY108 Date: Tue, 18 Jul 2023 15:29:07 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E9=9A=8F=E6=9C=BA=E5=B7=A5?= =?UTF-8?q?=E5=85=B7=E7=B1=BB=E3=80=82?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../plusone/commons/util/RandomUtil.java | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 src/main/java/xyz/zhouxy/plusone/commons/util/RandomUtil.java 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); + } +}