This commit is contained in:
Looly 2023-05-19 22:28:39 +08:00
parent 586b29f3d1
commit ea1c49a93b
23 changed files with 53 additions and 41 deletions

View File

@ -48,9 +48,13 @@ public class RandomUtil {
*/ */
public static final String BASE_CHAR = "abcdefghijklmnopqrstuvwxyz"; public static final String BASE_CHAR = "abcdefghijklmnopqrstuvwxyz";
/** /**
* 用于随机选的字符和数字 * 用于随机选的字符和数字小写
*/ */
public static final String BASE_CHAR_NUMBER = BASE_CHAR + BASE_NUMBER; public static final String BASE_CHAR_NUMBER_LOWER = BASE_CHAR + BASE_NUMBER;
/**
* 用于随机选的字符和数字包括大写和小写字母
*/
public static final String BASE_CHAR_NUMBER = BASE_CHAR.toUpperCase() + BASE_CHAR_NUMBER_LOWER;
/** /**
* 获取随机数生成器对象<br> * 获取随机数生成器对象<br>
@ -528,13 +532,23 @@ public class RandomUtil {
} }
/** /**
* 获得一个随机的字符串只包含数字和字符 * 获得一个随机的字符串只包含数字和大小写字母
* *
* @param length 字符串的长度 * @param length 字符串的长度
* @return 随机字符串 * @return 随机字符串
*/ */
public static String randomString(final int length) { public static String randomString(final int length) {
return randomString(BASE_CHAR_NUMBER, length); return randomStringLower(BASE_CHAR_NUMBER, length);
}
/**
* 获得一个随机的字符串只包含数字和小写字母
*
* @param length 字符串的长度
* @return 随机字符串
*/
public static String randomStringLower(final int length) {
return randomStringLower(BASE_CHAR_NUMBER_LOWER, length);
} }
/** /**
@ -545,7 +559,7 @@ public class RandomUtil {
* @since 4.0.13 * @since 4.0.13
*/ */
public static String randomStringUpper(final int length) { public static String randomStringUpper(final int length) {
return randomString(BASE_CHAR_NUMBER, length).toUpperCase(); return randomStringLower(BASE_CHAR_NUMBER_LOWER, length).toUpperCase();
} }
/** /**
@ -556,9 +570,9 @@ public class RandomUtil {
* @return 随机字符串 * @return 随机字符串
*/ */
public static String randomStringWithoutStr(final int length, final String elemData) { public static String randomStringWithoutStr(final int length, final String elemData) {
String baseStr = BASE_CHAR_NUMBER; String baseStr = BASE_CHAR_NUMBER_LOWER;
baseStr = StrUtil.removeAll(baseStr, elemData.toLowerCase().toCharArray()); baseStr = StrUtil.removeAll(baseStr, elemData.toLowerCase().toCharArray());
return randomString(baseStr, length); return randomStringLower(baseStr, length);
} }
/** /**
@ -568,7 +582,7 @@ public class RandomUtil {
* @return 随机字符串 * @return 随机字符串
*/ */
public static String randomNumbers(final int length) { public static String randomNumbers(final int length) {
return randomString(BASE_NUMBER, length); return randomStringLower(BASE_NUMBER, length);
} }
/** /**
@ -578,7 +592,7 @@ public class RandomUtil {
* @param length 字符串的长度 * @param length 字符串的长度
* @return 随机字符串 * @return 随机字符串
*/ */
public static String randomString(final String baseString, int length) { public static String randomStringLower(final String baseString, int length) {
if (StrUtil.isEmpty(baseString)) { if (StrUtil.isEmpty(baseString)) {
return StrUtil.EMPTY; return StrUtil.EMPTY;
} }
@ -612,7 +626,7 @@ public class RandomUtil {
* @since 3.1.2 * @since 3.1.2
*/ */
public static char randomChar() { public static char randomChar() {
return randomChar(BASE_CHAR_NUMBER); return randomChar(BASE_CHAR_NUMBER_LOWER);
} }
/** /**

View File

@ -25,7 +25,7 @@ public class LRUCacheTest {
final LRUCache<String, String> cache = CacheUtil.newLRUCache(100, 10); final LRUCache<String, String> cache = CacheUtil.newLRUCache(100, 10);
for (int i = 0; i < 10000; i++) { for (int i = 0; i < 10000; i++) {
//ThreadUtil.execute(()-> cache.put(RandomUtil.randomString(5), "1243", 10)); //ThreadUtil.execute(()-> cache.put(RandomUtil.randomString(5), "1243", 10));
ThreadUtil.execute(()-> cache.get(RandomUtil.randomString(5), ()->RandomUtil.randomString(10))); ThreadUtil.execute(()-> cache.get(RandomUtil.randomStringLower(5), ()->RandomUtil.randomStringLower(10)));
} }
ThreadUtil.sleep(3000); ThreadUtil.sleep(3000);
} }

View File

@ -38,7 +38,7 @@ public class Base32Test {
@Test @Test
public void encodeAndDecodeRandomTest(){ public void encodeAndDecodeRandomTest(){
final String a = RandomUtil.randomString(RandomUtil.randomInt(1000)); final String a = RandomUtil.randomStringLower(RandomUtil.randomInt(1000));
final String encode = Base32.encode(a); final String encode = Base32.encode(a);
final String decodeStr = Base32.decodeStr(encode); final String decodeStr = Base32.decodeStr(encode);
Assertions.assertEquals(a, decodeStr); Assertions.assertEquals(a, decodeStr);

View File

@ -35,7 +35,7 @@ public class Base62Test {
@Test @Test
public void encodeAndDecodeRandomTest() { public void encodeAndDecodeRandomTest() {
final String a = RandomUtil.randomString(RandomUtil.randomInt(1000)); final String a = RandomUtil.randomStringLower(RandomUtil.randomInt(1000));
final String encode = Base62.encode(a); final String encode = Base62.encode(a);
final String decodeStr = Base62.decodeStr(encode); final String decodeStr = Base62.decodeStr(encode);
Assertions.assertEquals(a, decodeStr); Assertions.assertEquals(a, decodeStr);
@ -43,7 +43,7 @@ public class Base62Test {
@Test @Test
public void encodeAndDecodeInvertedRandomTest() { public void encodeAndDecodeInvertedRandomTest() {
final String a = RandomUtil.randomString(RandomUtil.randomInt(1000)); final String a = RandomUtil.randomStringLower(RandomUtil.randomInt(1000));
final String encode = Base62.encodeInverted(a); final String encode = Base62.encodeInverted(a);
final String decodeStr = Base62.decodeStrInverted(encode); final String decodeStr = Base62.decodeStrInverted(encode);
Assertions.assertEquals(a, decodeStr); Assertions.assertEquals(a, decodeStr);

View File

@ -17,7 +17,7 @@ public class Base64Test {
@Test @Test
public void isTypeBase64Test(){ public void isTypeBase64Test(){
Assertions.assertTrue(Base64.isTypeBase64(Base64.encode(RandomUtil.randomString(1000)))); Assertions.assertTrue(Base64.isTypeBase64(Base64.encode(RandomUtil.randomStringLower(1000))));
} }
@Test @Test

View File

@ -97,7 +97,7 @@ public class MetroHashTest {
final String[] result = new String[10000000]; final String[] result = new String[10000000];
int index = 0; int index = 0;
while (index < 10000000) { while (index < 10000000) {
result[index++] = RandomUtil.randomString(RandomUtil.randomInt(64)); result[index++] = RandomUtil.randomStringLower(RandomUtil.randomInt(64));
} }
return result; return result;
} }

View File

@ -11,10 +11,10 @@ public class MemorySafeLinkedBlockingQueueTest {
public void offerTest(){ public void offerTest(){
// 设置初始值达到最大这样任何时候元素都无法加入队列 // 设置初始值达到最大这样任何时候元素都无法加入队列
final MemorySafeLinkedBlockingQueue<String> queue = new MemorySafeLinkedBlockingQueue<>(Long.MAX_VALUE); final MemorySafeLinkedBlockingQueue<String> queue = new MemorySafeLinkedBlockingQueue<>(Long.MAX_VALUE);
Assertions.assertFalse(queue.offer(RandomUtil.randomString(RandomUtil.randomInt(100)))); Assertions.assertFalse(queue.offer(RandomUtil.randomStringLower(RandomUtil.randomInt(100))));
// 设定一个很小的值可以成功加入 // 设定一个很小的值可以成功加入
queue.setMaxFreeMemory(10); queue.setMaxFreeMemory(10);
Assertions.assertTrue(queue.offer(RandomUtil.randomString(RandomUtil.randomInt(100)))); Assertions.assertTrue(queue.offer(RandomUtil.randomStringLower(RandomUtil.randomInt(100))));
} }
} }

View File

@ -15,7 +15,6 @@ package org.dromara.hutool.core.func;
import org.dromara.hutool.core.collection.ListUtil; import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.date.DateUtil; import org.dromara.hutool.core.date.DateUtil;
import org.dromara.hutool.core.date.StopWatch; import org.dromara.hutool.core.date.StopWatch;
import org.dromara.hutool.core.func.FunctionPool;
import org.dromara.hutool.core.util.RandomUtil; import org.dromara.hutool.core.util.RandomUtil;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -31,7 +30,7 @@ public class FunctionPoolTest {
// 测试数据 // 测试数据
final ArrayList<char[]> list = ListUtil.of(); final ArrayList<char[]> list = ListUtil.of();
for (int i = 0; i < 100000; i++) { for (int i = 0; i < 100000; i++) {
list.add(RandomUtil.randomString(100).toCharArray()); list.add(RandomUtil.randomStringLower(100).toCharArray());
} }
final StopWatch stopWatch = DateUtil.createStopWatch(); final StopWatch stopWatch = DateUtil.createStopWatch();

View File

@ -13,7 +13,7 @@ public class InternUtilTest {
@Test @Test
public void weakTest(){ public void weakTest(){
final Intern<String> intern = InternUtil.ofWeak(); final Intern<String> intern = InternUtil.ofWeak();
final String a1 = RandomUtil.randomString(RandomUtil.randomInt(100)); final String a1 = RandomUtil.randomStringLower(RandomUtil.randomInt(100));
final String a2 = new String(a1); final String a2 = new String(a1);
Assertions.assertNotSame(a1, a2); Assertions.assertNotSame(a1, a2);

View File

@ -39,6 +39,6 @@ public class TolerantMapTest {
map.put("tuesday", "星期二"); map.put("tuesday", "星期二");
assert "星期二".equals(map.get("tuesday")); assert "星期二".equals(map.get("tuesday"));
assert "default".equals(map.get(RandomUtil.randomString(6))); assert "default".equals(map.get(RandomUtil.randomStringLower(6)));
} }
} }

View File

@ -461,7 +461,7 @@ public class StrUtilTest {
@Test @Test
public void briefTest() { public void briefTest() {
// case: 1 str.length - 1 // case: 1 str.length - 1
final String str = RandomUtil.randomString(RandomUtil.randomInt(1, 100)); final String str = RandomUtil.randomStringLower(RandomUtil.randomInt(1, 100));
for (int maxLength = 1; maxLength < str.length(); maxLength++) { for (int maxLength = 1; maxLength < str.length(); maxLength++) {
final String brief = StrUtil.brief(str, maxLength); final String brief = StrUtil.brief(str, maxLength);
Assertions.assertEquals(brief.length(), maxLength); Assertions.assertEquals(brief.length(), maxLength);

View File

@ -47,9 +47,9 @@ public class TextSimilarityTest {
@Disabled @Disabled
void longestCommonSubstringLengthTest() { void longestCommonSubstringLengthTest() {
// https://github.com/dromara/hutool/issues/3045 // https://github.com/dromara/hutool/issues/3045
final String strCommon = RandomUtil.randomString(1024 * 32); final String strCommon = RandomUtil.randomStringLower(1024 * 32);
final String strA = RandomUtil.randomString(1024 * 32) + strCommon; final String strA = RandomUtil.randomStringLower(1024 * 32) + strCommon;
final String strB = RandomUtil.randomString(1024 * 32) + strCommon; final String strB = RandomUtil.randomStringLower(1024 * 32) + strCommon;
final int i = TextSimilarity.longestCommonSubstringLength(strA, strB); final int i = TextSimilarity.longestCommonSubstringLength(strA, strB);
Console.log(i); Console.log(i);

View File

@ -16,7 +16,6 @@ import org.dromara.hutool.core.collection.ListUtil;
import org.dromara.hutool.core.convert.Convert; import org.dromara.hutool.core.convert.Convert;
import org.dromara.hutool.core.lang.Console; import org.dromara.hutool.core.lang.Console;
import org.dromara.hutool.core.math.NumberUtil; import org.dromara.hutool.core.math.NumberUtil;
import org.dromara.hutool.core.util.RandomUtil;
import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Disabled; import org.junit.jupiter.api.Disabled;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -90,7 +89,7 @@ public class RandomUtilTest {
@Test @Test
public void randomStringOfLengthTest(){ public void randomStringOfLengthTest(){
final String s = RandomUtil.randomString("123", -1); final String s = RandomUtil.randomStringLower("123", -1);
Assertions.assertNotNull(s); Assertions.assertNotNull(s);
} }

View File

@ -174,7 +174,7 @@ public class KeyUtil {
} }
if (null == password) { if (null == password) {
password = RandomUtil.randomString(32).toCharArray(); password = RandomUtil.randomStringLower(32).toCharArray();
} }
return generateKey(algorithm, SpecUtil.createPBEKeySpec(password)); return generateKey(algorithm, SpecUtil.createPBEKeySpec(password));
} }

View File

@ -72,7 +72,7 @@ public class SpecUtil {
*/ */
public static PBEKeySpec createPBEKeySpec(char[] password) { public static PBEKeySpec createPBEKeySpec(char[] password) {
if (null == password) { if (null == password) {
password = RandomUtil.randomString(32).toCharArray(); password = RandomUtil.randomStringLower(32).toCharArray();
} }
return new PBEKeySpec(password); return new PBEKeySpec(password);
} }

View File

@ -193,7 +193,7 @@ public class RSATest {
final byte[] keyBytes = Base64.decode(publicKeyStr); final byte[] keyBytes = Base64.decode(publicKeyStr);
final PublicKey publicKey = KeyUtil.generateRSAPublicKey(keyBytes); final PublicKey publicKey = KeyUtil.generateRSAPublicKey(keyBytes);
final byte[] data = RandomUtil.randomString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 16).getBytes(); final byte[] data = RandomUtil.randomStringLower("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", 16).getBytes();
//长度不满足128补0 //长度不满足128补0
final byte[] finalData = ArrayUtil.resize(data, 128); final byte[] finalData = ArrayUtil.resize(data, 128);

View File

@ -134,7 +134,7 @@ public class SymmetricTest {
@Test @Test
public void aesZeroPaddingTest() { public void aesZeroPaddingTest() {
final String content = RandomUtil.randomString(RandomUtil.randomInt(200)); final String content = RandomUtil.randomStringLower(RandomUtil.randomInt(200));
final AES aes = new AES(Mode.CBC, Padding.ZeroPadding, "0123456789ABHAEQ".getBytes(), "DYgjCEIMVrj2W9xN".getBytes()); final AES aes = new AES(Mode.CBC, Padding.ZeroPadding, "0123456789ABHAEQ".getBytes(), "DYgjCEIMVrj2W9xN".getBytes());
// 加密为16进制表示 // 加密为16进制表示
@ -160,7 +160,7 @@ public class SymmetricTest {
@Test @Test
public void aesPkcs7PaddingTest() { public void aesPkcs7PaddingTest() {
final String content = RandomUtil.randomString(RandomUtil.randomInt(200)); final String content = RandomUtil.randomStringLower(RandomUtil.randomInt(200));
final AES aes = new AES("CBC", "PKCS7Padding", final AES aes = new AES("CBC", "PKCS7Padding",
RandomUtil.randomBytes(32), RandomUtil.randomBytes(32),
"DYgjCEIMVrj2W9xN".getBytes()); "DYgjCEIMVrj2W9xN".getBytes());

View File

@ -13,7 +13,7 @@ public class ZucTest {
final byte[] iv = RandomUtil.randomBytes(16); final byte[] iv = RandomUtil.randomBytes(16);
final ZUC zuc = new ZUC(ZUC.ZUCAlgorithm.ZUC_128, secretKey, iv); final ZUC zuc = new ZUC(ZUC.ZUCAlgorithm.ZUC_128, secretKey, iv);
final String msg = RandomUtil.randomString(500); final String msg = RandomUtil.randomStringLower(500);
final byte[] crypt2 = zuc.encrypt(msg); final byte[] crypt2 = zuc.encrypt(msg);
final String msg2 = zuc.decryptStr(crypt2, CharsetUtil.UTF_8); final String msg2 = zuc.decryptStr(crypt2, CharsetUtil.UTF_8);
Assertions.assertEquals(msg, msg2); Assertions.assertEquals(msg, msg2);
@ -25,7 +25,7 @@ public class ZucTest {
final byte[] iv = RandomUtil.randomBytes(25); final byte[] iv = RandomUtil.randomBytes(25);
final ZUC zuc = new ZUC(ZUC.ZUCAlgorithm.ZUC_256, secretKey, iv); final ZUC zuc = new ZUC(ZUC.ZUCAlgorithm.ZUC_256, secretKey, iv);
final String msg = RandomUtil.randomString(500); final String msg = RandomUtil.randomStringLower(500);
final byte[] crypt2 = zuc.encrypt(msg); final byte[] crypt2 = zuc.encrypt(msg);
final String msg2 = zuc.decryptStr(crypt2, CharsetUtil.UTF_8); final String msg2 = zuc.decryptStr(crypt2, CharsetUtil.UTF_8);
Assertions.assertEquals(msg, msg2); Assertions.assertEquals(msg, msg2);

View File

@ -18,7 +18,7 @@ public class FPETest {
final FPE fpe = new FPE(FPE.FPEMode.FF1, keyBytes, numberMapper, null); final FPE fpe = new FPE(FPE.FPEMode.FF1, keyBytes, numberMapper, null);
// 原始数据 // 原始数据
final String phone = RandomUtil.randomString("A0123456789", 13); final String phone = RandomUtil.randomStringLower("A0123456789", 13);
final String encrypt = fpe.encrypt(phone); final String encrypt = fpe.encrypt(phone);
// 加密后与原密文长度一致 // 加密后与原密文长度一致
Assertions.assertEquals(phone.length(), encrypt.length()); Assertions.assertEquals(phone.length(), encrypt.length());
@ -37,7 +37,7 @@ public class FPETest {
final FPE fpe = new FPE(FPE.FPEMode.FF3_1, keyBytes, numberMapper, null); final FPE fpe = new FPE(FPE.FPEMode.FF3_1, keyBytes, numberMapper, null);
// 原始数据 // 原始数据
final String phone = RandomUtil.randomString("A0123456789", 13); final String phone = RandomUtil.randomStringLower("A0123456789", 13);
final String encrypt = fpe.encrypt(phone); final String encrypt = fpe.encrypt(phone);
// 加密后与原密文长度一致 // 加密后与原密文长度一致
Assertions.assertEquals(phone.length(), encrypt.length()); Assertions.assertEquals(phone.length(), encrypt.length());

View File

@ -43,7 +43,7 @@ public class DialectFactoryTest {
//map.put("hive",DRIVER_HIVE); //map.put("hive",DRIVER_HIVE);
map.forEach((k,v) -> Assertions.assertEquals(v, map.forEach((k,v) -> Assertions.assertEquals(v,
DialectFactory.identifyDriver(k+ RandomUtil.randomString(2),null) )); DialectFactory.identifyDriver(k+ RandomUtil.randomStringLower(2),null) ));
} }
} }

View File

@ -37,7 +37,7 @@ public class HttpGlobalConfig implements Serializable {
* 底层调用{@link HttpURLConnection#setConnectTimeout(int)} 同时设置: 连接超时 * 底层调用{@link HttpURLConnection#setConnectTimeout(int)} 同时设置: 连接超时
*/ */
private static int timeout = -1; private static int timeout = -1;
private static String boundary = "--------------------Hutool_" + RandomUtil.randomString(16); private static String boundary = "--------------------Hutool_" + RandomUtil.randomStringLower(16);
private static int maxRedirectCount = 0; private static int maxRedirectCount = 0;
private static boolean ignoreEOFError = true; private static boolean ignoreEOFError = true;
private static boolean decodeUrl = false; private static boolean decodeUrl = false;

View File

@ -35,7 +35,7 @@ public abstract class AbstractGenerator implements CodeGenerator {
* @param count 生成验证码长度 * @param count 生成验证码长度
*/ */
public AbstractGenerator(final int count) { public AbstractGenerator(final int count) {
this(RandomUtil.BASE_CHAR_NUMBER, count); this(RandomUtil.BASE_CHAR_NUMBER_LOWER, count);
} }
/** /**

View File

@ -46,7 +46,7 @@ public class RandomGenerator extends AbstractGenerator {
@Override @Override
public String generate() { public String generate() {
return RandomUtil.randomString(this.baseStr, this.length); return RandomUtil.randomStringLower(this.baseStr, this.length);
} }
@Override @Override