6 Commits
dev ... v0.0.7

Author SHA1 Message Date
b9d47baa1a Merge pull request 'v0.0.7' (#6) from dev into master
Reviewed-on: #6
2026-06-02 16:11:14 +00:00
a4bcc3bf57 Merge pull request 'v0.0.6' (#5) from dev into master
Reviewed-on: #5
2026-05-22 11:15:51 +00:00
6361c70f37 Merge pull request 'v0.0.5' (#4) from dev into master
Reviewed-on: #4
2026-05-07 06:42:52 +00:00
e793a260be Merge pull request 'v0.0.4' (#3) from dev into master
Reviewed-on: #3
2026-05-06 08:38:39 +00:00
2020cb9111 Merge pull request 'v0.0.3' (#2) from dev into master
Reviewed-on: #2
2026-03-21 10:01:09 +00:00
b686679014 Merge pull request 'v0.0.2' (#1) from dev into master
Reviewed-on: #1
2026-01-19 09:39:33 +00:00
4 changed files with 9 additions and 73 deletions

View File

@@ -6,7 +6,7 @@
<groupId>com.imyeyu.utils</groupId>
<artifactId>timi-utils</artifactId>
<version>0.0.11</version>
<version>0.0.7</version>
<properties>
<maven.test.skip>true</maven.test.skip>
@@ -49,7 +49,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.46</version>
<version>1.18.36</version>
</dependency>
</dependencies>
</plugin>
@@ -91,7 +91,7 @@
<dependency>
<groupId>com.imyeyu.java</groupId>
<artifactId>timi-java</artifactId>
<version>0.0.5</version>
<version>0.0.4</version>
</dependency>
<dependency>
<groupId>de.mkammerer</groupId>
@@ -108,7 +108,7 @@
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.46</version>
<version>1.18.34</version>
</dependency>
</dependencies>
</project>

View File

@@ -52,8 +52,8 @@ public class Regex {
/** 用户名 (4-16 位字母、数字、下划线) */
public static final String USERNAME = "^[a-zA-Z0-9_]{4,16}$";
/** 密码 (6-18 位,包含字母和数字,允许特殊字符) */
public static final String PASSWORD = "^(?=.*[a-zA-Z])(?=.*\\d)[a-zA-Z0-9!#$%&()*+,\\-./:;<=>?@\\[\\]\\^_`{|}~]{6,32}$";
/** 密码 (6-18 位,包含字母和数字) */
public static final String PASSWORD = "^(?=.*[a-zA-Z])(?=.*\\d)[a-zA-Z0-9]{6,18}$";
/** 中文字符 */
public static final String CHINESE_CHAR = "^[\\u4e00-\\u9fa5]+$";

View File

@@ -15,16 +15,8 @@ import java.util.UUID;
*/
public class Text {
public static String LETTER_POOL = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
public static String NUMBER_POOL = "0123456789";
public static String LETTER_NUMBER_POOL = LETTER_POOL + NUMBER_POOL;
public static String PASSWORD_CHAR_POOL = LETTER_NUMBER_POOL + "!#$%&()*+,-./:;<=>?@[]^_`{|}~";
/** 十六进制小写 */
public static char[] HEX_DIGITS_LOWER = (NUMBER_POOL + "abcdef").toCharArray();
public static char[] HEX_DIGITS_LOWER = "0123456789abcdef".toCharArray();
/**
* 是否为半角字符
@@ -158,38 +150,18 @@ public class Text {
}
public static String randomString(int length) {
return randomString(LETTER_NUMBER_POOL, length);
return randomString("abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789", length);
}
public static String randomString(String pool, int length) {
SecureRandom r = new SecureRandom();
StringBuilder sb = new StringBuilder();
for (int i = 0; i < length; i++) {
sb.append(pool.charAt(r.nextInt(pool.length())));
sb.append(pool.charAt(r.nextInt(pool.length() - 1)));
}
return sb.toString();
}
public static String randomPassword(int length) {
if (length < 6 || 32 < length) {
throw new IllegalArgumentException("Password length must be between 6 and 32");
}
SecureRandom r = new SecureRandom();
char[] chars = new char[length];
chars[0] = LETTER_POOL.charAt(r.nextInt(LETTER_POOL.length()));
chars[1] = NUMBER_POOL.charAt(r.nextInt(NUMBER_POOL.length()));
for (int i = 2; i < length; i++) {
chars[i] = PASSWORD_CHAR_POOL.charAt(r.nextInt(PASSWORD_CHAR_POOL.length()));
}
for (int i = chars.length - 1; 0 < i; i--) {
int j = r.nextInt(i + 1);
char temp = chars[i];
chars[i] = chars[j];
chars[j] = temp;
}
return new String(chars);
}
/**
* 较短的临时 UUID如果使用在庞大的数据里很可能会发生重复
*

View File

@@ -1,36 +0,0 @@
package test;
import com.imyeyu.utils.Regex;
import com.imyeyu.utils.Text;
import org.junit.jupiter.api.Test;
import static org.junit.jupiter.api.Assertions.assertThrows;
/**
* @author 夜雨
* @since 2026-06-11 14:00
*/
public class TestRegex {
@Test
public void testPassword() {
assert Regex.isMatch(Regex.PASSWORD, "abc123");
assert Regex.isMatch(Regex.PASSWORD, "abc123!");
assert Regex.isMatch(Regex.PASSWORD, "Abc123[]^_`{|}~");
assert Regex.isMatch(Regex.PASSWORD, "A1!#$%&*+,-./:@?");
for (int i = 0; i < 20; i++) {
assert Regex.isMatch(Regex.PASSWORD, Text.randomPassword(20));
}
assert Regex.isNotMatch(Regex.PASSWORD, "abcdef!");
assert Regex.isNotMatch(Regex.PASSWORD, "123456!");
assert Regex.isNotMatch(Regex.PASSWORD, "Ab1!");
assert Regex.isNotMatch(Regex.PASSWORD, "Abcdefghijklmnopqrstuvwxzy123456!");
assert Regex.isNotMatch(Regex.PASSWORD, "Abc123\\");
assert Regex.isNotMatch(Regex.PASSWORD, "Abc123\"");
assert Regex.isNotMatch(Regex.PASSWORD, "Abc123'");
assert Regex.isNotMatch(Regex.PASSWORD, "Abc123 ");
assertThrows(IllegalArgumentException.class, () -> Text.randomPassword(5));
assertThrows(IllegalArgumentException.class, () -> Text.randomPassword(33));
}
}