Compare commits
12 Commits
ea9e669d71
...
dev
| Author | SHA1 | Date | |
|---|---|---|---|
| 1afc59dd12 | |||
| 97e07db838 | |||
| b070950ed6 | |||
| e3e9f81f33 | |||
| 3153afad5a | |||
| 42f0212a40 | |||
| 7378078299 | |||
| 256d9d5937 | |||
| 8f161590c1 | |||
| 53985cd358 | |||
| 4a633765e8 | |||
| 9e6b97632a |
111
.gitea/workflows/ci.yml
Normal file
111
.gitea/workflows/ci.yml
Normal file
@ -0,0 +1,111 @@
|
||||
name: CI/CD
|
||||
|
||||
on:
|
||||
pull_request:
|
||||
branches:
|
||||
- master
|
||||
types:
|
||||
- closed
|
||||
|
||||
jobs:
|
||||
build-deploy:
|
||||
runs-on: act_runner_java
|
||||
if: ${{ github.event.pull_request.merged == true }}
|
||||
env:
|
||||
JAVA_HOME: /usr/lib/jvm/java-21-openjdk
|
||||
steps:
|
||||
- name: Checkout code
|
||||
run: |
|
||||
git clone ${{ github.server_url }}/${{ github.repository }}.git .
|
||||
git checkout ${{ github.sha }}
|
||||
- name: Set up environment
|
||||
run: |
|
||||
echo "PR #${{ github.event.number }} merged into master"
|
||||
echo "Source branch: ${{ github.event.pull_request.head.ref }}"
|
||||
echo "Target branch: ${{ github.event.pull_request.base.ref }}"
|
||||
- name: Run tests
|
||||
run: |
|
||||
echo "Running test suite..."
|
||||
- name: Build project
|
||||
run: |
|
||||
mvn -B -DskipTests clean package source:jar javadoc:jar
|
||||
- name: Deploy to Nexus
|
||||
if: success()
|
||||
run: |
|
||||
if [ -z "${{ secrets.NEXUS_USERNAME }}" ] || [ -z "${{ secrets.NEXUS_PASSWORD }}" ]; then
|
||||
echo "Missing secrets.NEXUS_USERNAME or secrets.NEXUS_PASSWORD"
|
||||
exit 1
|
||||
fi
|
||||
mkdir -p ~/.m2
|
||||
cat > ~/.m2/settings.xml <<EOF
|
||||
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 http://maven.apache.org/xsd/settings-1.0.0.xsd">
|
||||
<servers>
|
||||
<server>
|
||||
<id>timi-nexus</id>
|
||||
<username>${{ secrets.NEXUS_USERNAME }}</username>
|
||||
<password>${{ secrets.NEXUS_PASSWORD }}</password>
|
||||
</server>
|
||||
</servers>
|
||||
</settings>
|
||||
EOF
|
||||
version=$(mvn -q -DforceStdout help:evaluate -Dexpression=project.version)
|
||||
artifact_id=$(mvn -q -DforceStdout help:evaluate -Dexpression=project.artifactId)
|
||||
main_jar="target/${artifact_id}-${version}.jar"
|
||||
sources_jar="target/${artifact_id}-${version}-sources.jar"
|
||||
javadoc_jar="target/${artifact_id}-${version}-javadoc.jar"
|
||||
if [ ! -f "$main_jar" ] || [ ! -f "$sources_jar" ] || [ ! -f "$javadoc_jar" ]; then
|
||||
echo "Missing build artifacts in target"
|
||||
exit 1
|
||||
fi
|
||||
mvn -B deploy:deploy-file \
|
||||
-Dfile="$main_jar" \
|
||||
-Dsources="$sources_jar" \
|
||||
-Djavadoc="$javadoc_jar" \
|
||||
-DpomFile="./pom.xml" \
|
||||
-Durl="https://nexus.imyeyu.com/repository/maven-releases/" \
|
||||
-DrepositoryId="timi-nexus" \
|
||||
-Dhttps.protocols=TLSv1.2 \
|
||||
-Djdk.tls.client.protocols=TLSv1.2
|
||||
- name: Create release
|
||||
if: ${{ success() && startsWith(github.event.pull_request.title, 'v') }}
|
||||
env:
|
||||
GITEA_TOKEN: ${{ secrets.RUNNER_TOKEN }}
|
||||
GITEA_SERVER_URL: ${{ github.server_url }}
|
||||
GITEA_REPOSITORY: ${{ github.repository }}
|
||||
RELEASE_TAG: ${{ github.event.pull_request.title }}
|
||||
RELEASE_TARGET: ${{ github.sha }}
|
||||
run: |
|
||||
if [ -z "$GITEA_TOKEN" ]; then
|
||||
echo "Missing secrets.RUNNER_TOKEN"
|
||||
exit 1
|
||||
fi
|
||||
api_url="$GITEA_SERVER_URL/api/v1/repos/$GITEA_REPOSITORY/releases"
|
||||
payload=$(cat <<EOF
|
||||
{
|
||||
"tag_name": "$RELEASE_TAG",
|
||||
"name": "$RELEASE_TAG",
|
||||
"target_commitish": "$RELEASE_TARGET",
|
||||
"draft": false,
|
||||
"prerelease": false
|
||||
}
|
||||
EOF
|
||||
)
|
||||
response=$(curl -sS -X POST "$api_url" \
|
||||
-H "Authorization: token $GITEA_TOKEN" \
|
||||
-H "Content-Type: application/json" \
|
||||
-d "$payload")
|
||||
release_id=$(echo "$response" | grep -o '"id":[0-9]*' | head -n 1 | grep -o '[0-9]*')
|
||||
if [ -z "$release_id" ] || echo "$response" | grep -q '"message"'; then
|
||||
echo "Create release failed: $response"
|
||||
exit 1
|
||||
fi
|
||||
echo "Release created: id=$release_id"
|
||||
for asset_path in target/*.jar; do
|
||||
asset_name=$(basename "$asset_path")
|
||||
curl -sS -X POST "$api_url/$release_id/assets?name=$asset_name" \
|
||||
-H "Authorization: token $GITEA_TOKEN" \
|
||||
-H "Content-Type: application/octet-stream" \
|
||||
--data-binary @"$asset_path"
|
||||
done
|
||||
28
pom.xml
28
pom.xml
@ -6,7 +6,7 @@
|
||||
|
||||
<groupId>com.imyeyu.utils</groupId>
|
||||
<artifactId>timi-utils</artifactId>
|
||||
<version>0.0.1</version>
|
||||
<version>0.0.2</version>
|
||||
|
||||
<properties>
|
||||
<maven.compiler.source>21</maven.compiler.source>
|
||||
@ -26,29 +26,11 @@
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-source-plugin</artifactId>
|
||||
<version>3.3.1</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-sources</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar-no-fork</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-javadoc-plugin</artifactId>
|
||||
<version>3.11.2</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>attach-javadocs</id>
|
||||
<phase>package</phase>
|
||||
<goals>
|
||||
<goal>jar</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
@ -77,7 +59,13 @@
|
||||
<dependency>
|
||||
<groupId>com.imyeyu.java</groupId>
|
||||
<artifactId>timi-java</artifactId>
|
||||
<version>0.0.1</version>
|
||||
<version>0.0.2</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>de.mkammerer</groupId>
|
||||
<artifactId>argon2-jvm</artifactId>
|
||||
<version>2.12</version>
|
||||
<scope>compile</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.junit.jupiter</groupId>
|
||||
|
||||
@ -4,6 +4,7 @@ import com.imyeyu.java.bean.timi.TimiCode;
|
||||
import com.imyeyu.java.bean.timi.TimiException;
|
||||
|
||||
import java.io.StringWriter;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.net.URLDecoder;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.util.Base64;
|
||||
@ -102,4 +103,42 @@ public class Decoder {
|
||||
}
|
||||
return URLDecoder.decode(url, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
/**
|
||||
* 16 进制字符串转字节数据
|
||||
*
|
||||
* @param hex 16 进制字符串
|
||||
* @return 字节数据
|
||||
* @throws UnsupportedEncodingException 不支持的编码
|
||||
*/
|
||||
public static byte[] hex(String hex) throws UnsupportedEncodingException {
|
||||
final char[] c = hex.toCharArray();
|
||||
final byte[] b = new byte[c.length >> 1];
|
||||
|
||||
final int len = c.length;
|
||||
if ((len & 0x01) != 0) {
|
||||
throw new UnsupportedEncodingException("Odd number of characters.");
|
||||
}
|
||||
|
||||
final int outLen = len >> 1;
|
||||
if (c.length < outLen) {
|
||||
throw new UnsupportedEncodingException("Output array is not large enough to accommodate decoded data.");
|
||||
}
|
||||
for (int i = 0, j = 0; j < len; i++) {
|
||||
int f = toDigit(c[j], j) << 4;
|
||||
j++;
|
||||
f = f | toDigit(c[j], j);
|
||||
j++;
|
||||
b[i] = (byte) (f & 0xFF);
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
private static int toDigit(final char ch, final int index) throws UnsupportedEncodingException {
|
||||
final int digit = Character.digit(ch, 16);
|
||||
if (digit == -1) {
|
||||
throw new UnsupportedEncodingException("Illegal hexadecimal character " + ch + " at index " + index);
|
||||
}
|
||||
return digit;
|
||||
}
|
||||
}
|
||||
|
||||
50
src/main/java/com/imyeyu/utils/Decryptor.java
Normal file
50
src/main/java/com/imyeyu/utils/Decryptor.java
Normal file
@ -0,0 +1,50 @@
|
||||
package com.imyeyu.utils;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
/**
|
||||
* @author 夜雨
|
||||
* @since 2025-10-29 16:11
|
||||
*/
|
||||
public class Decryptor {
|
||||
|
||||
/**
|
||||
* 解密字符串
|
||||
*
|
||||
* @param data 待解密字节数据
|
||||
* @param key 密钥
|
||||
* @return 解密结果
|
||||
*/
|
||||
public static byte[] aes(String data, byte[] key) {
|
||||
return aes(data.getBytes(), key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 解密
|
||||
*
|
||||
* @param data 待解密字节数据
|
||||
* @param key 密钥
|
||||
* @return 解密结果
|
||||
*/
|
||||
public static byte[] aes(byte[] data, byte[] key) {
|
||||
try {
|
||||
SecretKey secretKey = new SecretKeySpec(key, "AES");
|
||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||
cipher.init(Cipher.DECRYPT_MODE, secretKey);
|
||||
return cipher.doFinal(data);
|
||||
} catch (IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException e) {
|
||||
throw new RuntimeException("invalid data");
|
||||
} catch (InvalidKeyException e) {
|
||||
throw new RuntimeException("invalid key");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException("not support AES encrypt");
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -1,5 +1,8 @@
|
||||
package com.imyeyu.utils;
|
||||
|
||||
import de.mkammerer.argon2.Argon2;
|
||||
import de.mkammerer.argon2.Argon2Factory;
|
||||
|
||||
import java.math.BigInteger;
|
||||
import java.nio.charset.Charset;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
@ -12,72 +15,102 @@ import java.security.NoSuchAlgorithmException;
|
||||
*/
|
||||
public class Digest {
|
||||
|
||||
public static String md5(String data) throws NoSuchAlgorithmException {
|
||||
public static String md5(String data) {
|
||||
return md5(data, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public static String md5(String data, Charset charset) throws NoSuchAlgorithmException {
|
||||
public static String md5(String data, Charset charset) {
|
||||
return md5(data.getBytes(charset));
|
||||
}
|
||||
|
||||
public static String md5(byte[] data) throws NoSuchAlgorithmException {
|
||||
if (data == null || data.length == 0) {
|
||||
return null;
|
||||
public static String md5(byte[] data) {
|
||||
try {
|
||||
if (data == null || data.length == 0) {
|
||||
return null;
|
||||
}
|
||||
MessageDigest md5 = MessageDigest.getInstance("MD5");
|
||||
md5.update(data);
|
||||
byte[] bytes = md5.digest();
|
||||
char[] chars = new char[bytes.length * 2];
|
||||
for (int i = 0, j = 0; i < bytes.length; i++) {
|
||||
chars[j++] = Text.HEX_DIGITS_LOWER[bytes[i] >>> 4 & 0xF];
|
||||
chars[j++] = Text.HEX_DIGITS_LOWER[bytes[i] & 0xF];
|
||||
}
|
||||
return new String(chars);
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new UnsupportedOperationException("unsupported md5 digest");
|
||||
}
|
||||
MessageDigest md5 = MessageDigest.getInstance("MD5");
|
||||
md5.update(data);
|
||||
byte[] bytes = md5.digest();
|
||||
char[] chars = new char[bytes.length * 2];
|
||||
for (int i = 0, j = 0; i < bytes.length; i++) {
|
||||
chars[j++] = Text.HEX_DIGITS_LOWER[bytes[i] >>> 4 & 0xF];
|
||||
chars[j++] = Text.HEX_DIGITS_LOWER[bytes[i] & 0xF];
|
||||
}
|
||||
return new String(chars);
|
||||
}
|
||||
|
||||
public static String sha1(String data) throws NoSuchAlgorithmException {
|
||||
public static String sha1(String data) {
|
||||
return sha1(data, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public static String sha1(String data, Charset charset) throws NoSuchAlgorithmException {
|
||||
public static String sha1(String data, Charset charset) {
|
||||
return sha1(data.getBytes(charset));
|
||||
}
|
||||
|
||||
public static String sha1(byte[] bytes) throws NoSuchAlgorithmException {
|
||||
MessageDigest sha = MessageDigest.getInstance("SHA");
|
||||
sha.update(bytes);
|
||||
return Text.byteToHex(sha.digest());
|
||||
public static String sha1(byte[] bytes) {
|
||||
try {
|
||||
MessageDigest sha = MessageDigest.getInstance("SHA");
|
||||
sha.update(bytes);
|
||||
return Encoder.hex(sha.digest());
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new UnsupportedOperationException("unsupported sha1 digest");
|
||||
}
|
||||
}
|
||||
|
||||
public static String sha256(String data) throws NoSuchAlgorithmException {
|
||||
public static String sha256(String data) {
|
||||
return sha256(data, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public static String sha256(String data, Charset charset) throws NoSuchAlgorithmException {
|
||||
public static String sha256(String data, Charset charset) {
|
||||
return sha256(data.getBytes(charset));
|
||||
}
|
||||
|
||||
public static String sha256(byte[] bytes) throws NoSuchAlgorithmException {
|
||||
MessageDigest sha = MessageDigest.getInstance("SHA-256");
|
||||
sha.update(bytes);
|
||||
return Text.byteToHex(sha.digest());
|
||||
public static String sha256(byte[] bytes) {
|
||||
try {
|
||||
MessageDigest sha = MessageDigest.getInstance("SHA-256");
|
||||
sha.update(bytes);
|
||||
return Encoder.hex(sha.digest());
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new UnsupportedOperationException("unsupported sha256 digest");
|
||||
}
|
||||
}
|
||||
|
||||
public static String sha512(String data) throws NoSuchAlgorithmException {
|
||||
public static String sha512(String data) {
|
||||
return sha512(data, StandardCharsets.UTF_8);
|
||||
}
|
||||
|
||||
public static String sha512(String data, Charset charset) throws NoSuchAlgorithmException {
|
||||
public static String sha512(String data, Charset charset) {
|
||||
return sha512(data.getBytes(charset));
|
||||
}
|
||||
|
||||
public static String sha512(byte[] bytes) throws NoSuchAlgorithmException {
|
||||
MessageDigest sha = MessageDigest.getInstance("SHA-512");
|
||||
BigInteger number = new BigInteger(1, sha.digest(bytes));
|
||||
StringBuilder result = new StringBuilder(number.toString(16));
|
||||
while (result.length() < 64) {
|
||||
result.insert(0, "0");
|
||||
public static String sha512(byte[] bytes) {
|
||||
try {
|
||||
MessageDigest sha = MessageDigest.getInstance("SHA-512");
|
||||
BigInteger number = new BigInteger(1, sha.digest(bytes));
|
||||
StringBuilder result = new StringBuilder(number.toString(16));
|
||||
while (result.length() < 64) {
|
||||
result.insert(0, "0");
|
||||
}
|
||||
return result.toString();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new UnsupportedOperationException("unsupported sha512 digest");
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
public static String argon2id(String password) {
|
||||
return argon2id(10, 65536, 1, password);
|
||||
}
|
||||
|
||||
public static String argon2id(int iterations, int memory, int parallelism, String password) {
|
||||
Argon2 argon2id = Argon2Factory.create(Argon2Factory.Argon2Types.ARGON2id);
|
||||
return argon2id.hash(iterations, memory, parallelism, password.toCharArray());
|
||||
}
|
||||
|
||||
public static boolean argon2idVerify(String passwordHash, String password) {
|
||||
Argon2 argon2id = Argon2Factory.create(Argon2Factory.Argon2Types.ARGON2id);
|
||||
return argon2id.verify(passwordHash, password.toCharArray());
|
||||
}
|
||||
}
|
||||
|
||||
@ -159,4 +159,20 @@ public class Encoder {
|
||||
throw new TimiException(TimiCode.ERROR, e.getMessage());
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 字节数据转 16 进制字符串
|
||||
*
|
||||
* @param bytes 字节数据
|
||||
* @return 16 进制字符串
|
||||
*/
|
||||
public static String hex(byte[] bytes) {
|
||||
final int l = bytes.length;
|
||||
final char[] c = new char[l << 1];
|
||||
for (int i = 0, j = 0; i < l; i++) {
|
||||
c[j++] = Text.HEX_DIGITS_LOWER[(0xF0 & bytes[i]) >>> 4];
|
||||
c[j++] = Text.HEX_DIGITS_LOWER[0x0F & bytes[i]];
|
||||
}
|
||||
return new String(c);
|
||||
}
|
||||
}
|
||||
|
||||
65
src/main/java/com/imyeyu/utils/Encryptor.java
Normal file
65
src/main/java/com/imyeyu/utils/Encryptor.java
Normal file
@ -0,0 +1,65 @@
|
||||
package com.imyeyu.utils;
|
||||
|
||||
import javax.crypto.BadPaddingException;
|
||||
import javax.crypto.Cipher;
|
||||
import javax.crypto.IllegalBlockSizeException;
|
||||
import javax.crypto.KeyGenerator;
|
||||
import javax.crypto.NoSuchPaddingException;
|
||||
import javax.crypto.SecretKey;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.security.InvalidKeyException;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
|
||||
/**
|
||||
* @author 夜雨
|
||||
* @since 2025-10-29 16:02
|
||||
*/
|
||||
public class Encryptor {
|
||||
|
||||
public static byte[] aesKey() {
|
||||
return aesKey(256);
|
||||
}
|
||||
|
||||
public static byte[] aesKey(int size) {
|
||||
try {
|
||||
KeyGenerator keyGen = KeyGenerator.getInstance("AES");
|
||||
keyGen.init(size);
|
||||
return keyGen.generateKey().getEncoded();
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException("not support AES encrypt");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密字符串
|
||||
*
|
||||
* @param data 待加密字符串
|
||||
* @param key 密钥
|
||||
* @return 加密结果
|
||||
*/
|
||||
public static byte[] aes(String data, byte[] key) {
|
||||
return aes(data.getBytes(), key);
|
||||
}
|
||||
|
||||
/**
|
||||
* 加密
|
||||
*
|
||||
* @param data 待加密字节数据
|
||||
* @param key 密钥
|
||||
* @return 加密结果
|
||||
*/
|
||||
public static byte[] aes(byte[] data, byte[] key) {
|
||||
try {
|
||||
SecretKey secretKey = new SecretKeySpec(key, "AES");
|
||||
Cipher cipher = Cipher.getInstance("AES/ECB/PKCS5Padding");
|
||||
cipher.init(Cipher.ENCRYPT_MODE, secretKey);
|
||||
return cipher.doFinal(data);
|
||||
} catch (IllegalBlockSizeException | BadPaddingException | NoSuchPaddingException e) {
|
||||
throw new RuntimeException("invalid data");
|
||||
} catch (InvalidKeyException e) {
|
||||
throw new RuntimeException("invalid key");
|
||||
} catch (NoSuchAlgorithmException e) {
|
||||
throw new RuntimeException("not support AES encrypt");
|
||||
}
|
||||
}
|
||||
}
|
||||
102
src/main/java/com/imyeyu/utils/Regex.java
Normal file
102
src/main/java/com/imyeyu/utils/Regex.java
Normal file
@ -0,0 +1,102 @@
|
||||
package com.imyeyu.utils;
|
||||
|
||||
import com.imyeyu.java.TimiJava;
|
||||
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* @author 夜雨
|
||||
* @since 2025-10-30 17:16
|
||||
*/
|
||||
public class Regex {
|
||||
|
||||
/** 手机号码 (11 位,以 1 开头) */
|
||||
public static final String MOBILE_PHONE = "^1[3-9]\\d{9}$";
|
||||
|
||||
/** 固定电话号码 (带区号,如:010-12345678 或 0512-1234567) */
|
||||
public static final String FIXED_PHONE = "^(0\\d{2,3}-)?[1-9]\\d{6,7}$";
|
||||
|
||||
/** 身份证号码 (15 位或 18 位) */
|
||||
public static final String ID_CARD = "(^\\d{15}$)|(^\\d{17}([0-9]|X|x)$)";
|
||||
|
||||
/** 邮箱地址 */
|
||||
public static final String EMAIL = "^[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}$";
|
||||
|
||||
/** 中文姓名 (2-10 个中文字符) */
|
||||
public static final String CHINESE_NAME = "^[\\u4e00-\\u9fa5]{2,10}$";
|
||||
|
||||
/** 邮政编码 (6 位数字) */
|
||||
public static final String POSTAL_CODE = "^[1-9]\\d{5}$";
|
||||
|
||||
/** 银行卡号 (16-19 位数字) */
|
||||
public static final String BANK_CARD = "^[1-9]\\d{15,18}$";
|
||||
|
||||
/** QQ号码 (5-12 位数字) */
|
||||
public static final String QQ_NUMBER = "^[1-9]\\d{4,11}$";
|
||||
|
||||
/** 微信号 (6-20 位字母、数字、下划线、减号) */
|
||||
public static final String WECHAT_ID = "^[a-zA-Z][-_a-zA-Z0-9]{5,19}$";
|
||||
|
||||
/** 车牌号码 (包含新能源车牌) */
|
||||
public static final String LICENSE_PLATE = "^[京津沪渝冀豫云辽黑湘皖鲁新苏浙赣鄂桂甘晋蒙陕吉闽贵粤青藏川宁琼]{1}[A-Z]{1}[A-Z0-9]{4,5}[A-Z0-9挂学警港澳]{1}$";
|
||||
|
||||
/** 日期格式 (yyyy-MM-dd) */
|
||||
public static final String DATE = "^\\d{4}-(0?[1-9]|1[0-2])-(0?[1-9]|[12]\\d|3[01])$";
|
||||
|
||||
/** 时间格式 (HH:mm:ss) */
|
||||
public static final String TIME = "^([01]\\d|2[0-3]):[0-5]\\d:[0-5]\\d$";
|
||||
|
||||
/** 金额 (支持小数,最多两位小数) */
|
||||
public static final String MONEY = "^[0-9]+(\\.[0-9]{1,2})?$";
|
||||
|
||||
/** 用户名 (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,18}$";
|
||||
|
||||
/** 中文字符 */
|
||||
public static final String CHINESE_CHAR = "^[\\u4e00-\\u9fa5]+$";
|
||||
|
||||
/** 网址 */
|
||||
public static final String URL = "^(https?://)?([\\w-]+\\.)+[\\w-]+(/[\\w-./?%&=]*)?$";
|
||||
|
||||
/** IP 地址 */
|
||||
public static final String IP_ADDRESS = "^((25[0-5]|2[0-4]\\d|[01]?\\d\\d?)\\.){3}(25[0-5]|2[0-4]\\d|[01]?\\d\\d?)$";
|
||||
|
||||
/** 统一社会信用代码 */
|
||||
public static final String UNIFIED_SOCIAL_CREDIT_CODE = "^[0-9A-HJ-NPQRTUWXY]{2}\\d{6}[0-9A-HJ-NPQRTUWXY]{10}$";
|
||||
|
||||
/** 港澳居民来往内地通行证 */
|
||||
public static final String HONG_KONG_MACAO_PASS = "^[HMhm]{1}([0-9]{10}|[0-9]{8})$";
|
||||
|
||||
/** 台湾居民来往大陆通行证 */
|
||||
public static final String TAIWAN_PASS = "^[0-9]{8}|[0-9]{10}$";
|
||||
|
||||
/** 护照 */
|
||||
public static final String PASSPORT = "^[a-zA-Z0-9]{5,17}$";
|
||||
|
||||
// IPv4 正则表达式
|
||||
public static final String IPv4 = "^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$";
|
||||
|
||||
// IPv6 正则表达式(简化版)
|
||||
public static final String IPv6 = "^([0-9a-fA-F]{0,4}:){7}[0-9a-fA-F]{0,4}$|^::1$|^::$|^([0-9a-fA-F]{0,4}:){1,7}:$";
|
||||
|
||||
// 域名正则表达式(RFC 1123)
|
||||
public static final String DOMAIN = "^([a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?\\.)*[a-zA-Z0-9]([a-zA-Z0-9\\-]{0,61}[a-zA-Z0-9])?$";
|
||||
|
||||
public static boolean isMatch(String regex, String text) {
|
||||
if (TimiJava.isEmpty(regex) || TimiJava.isEmpty(text)) {
|
||||
return false;
|
||||
}
|
||||
try {
|
||||
return Pattern.matches(regex, text);
|
||||
} catch (Exception e) {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isNotMatch(String regex, String text) {
|
||||
return !isMatch(regex, text);
|
||||
}
|
||||
}
|
||||
@ -6,6 +6,8 @@ import com.imyeyu.java.ref.Ref;
|
||||
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
@ -16,26 +18,21 @@ import java.util.regex.Pattern;
|
||||
*/
|
||||
public class StringInterpolator {
|
||||
|
||||
/** ${} 插值正则 */
|
||||
public static final String DOLLAR_OBJ = "\\$\\{(.+?)\\}";
|
||||
|
||||
/** {} 插值正则 */
|
||||
public static final String SIMPLE_OBJ = "\\{(.+?)\\}";
|
||||
|
||||
/** 缓存解析过的嵌套属性路径 */
|
||||
private final Map<String, String[]> deepKeyCache = new ConcurrentHashMap<>();
|
||||
|
||||
/** 正则匹配器 */
|
||||
private final Pattern pattern;
|
||||
|
||||
/** 为 true 时允许安全插值空,变量可以插值 null 而不抛出异常 */
|
||||
/** 过滤器映射 */
|
||||
private final Map<String, CallbackArgReturn<String, String>> filterMap = new HashMap<>();
|
||||
|
||||
/** 空值处理策略 */
|
||||
private boolean nullable = false;
|
||||
|
||||
/** 过滤器 */
|
||||
private Map<String, CallbackArgReturn<String, String>> filterMap;
|
||||
|
||||
/**
|
||||
* 默认构造
|
||||
*
|
||||
* @param regex 插槽正则
|
||||
*/
|
||||
public StringInterpolator(String regex) {
|
||||
this.pattern = Pattern.compile(regex);
|
||||
}
|
||||
@ -43,79 +40,114 @@ public class StringInterpolator {
|
||||
/**
|
||||
* 注入变量
|
||||
*
|
||||
* @param string 字符串
|
||||
* @param argsMap 变量表
|
||||
* @param template 字符串
|
||||
* @param argsMap 变量表
|
||||
* @return 插值结果
|
||||
*/
|
||||
public String inject(String string, Map<String, Object> argsMap) {
|
||||
return pattern.matcher(string).replaceAll(result -> {
|
||||
String group = result.group(1);
|
||||
String key = group.trim();
|
||||
String[] filters = null;
|
||||
if (group.contains("|")) {
|
||||
// 过滤器
|
||||
String[] groups = group.split("\\|");
|
||||
key = groups[0].trim();
|
||||
filters = new String[groups.length - 1];
|
||||
System.arraycopy(groups, 1, filters, 0, filters.length);
|
||||
public String inject(String template, Map<String, Object> argsMap) {
|
||||
if (TimiJava.isEmpty(template) || TimiJava.isEmpty(argsMap)) {
|
||||
return template;
|
||||
}
|
||||
Matcher matcher = pattern.matcher(template);
|
||||
StringBuilder result = new StringBuilder();
|
||||
while (matcher.find()) {
|
||||
String replacement = processReplacement(matcher.group(1), argsMap);
|
||||
matcher.appendReplacement(result, Matcher.quoteReplacement(replacement));
|
||||
}
|
||||
matcher.appendTail(result);
|
||||
return result.toString();
|
||||
}
|
||||
|
||||
/** 处理单个替换项 */
|
||||
private String processReplacement(String expression, Map<String, Object> argsMap) {
|
||||
// 解析表达式:变量名和过滤器
|
||||
ExpressionParts parts = parseExpression(expression);
|
||||
// 获取变量值
|
||||
Object value = getVariableValue(parts.variable, argsMap);
|
||||
// 应用过滤器
|
||||
value = applyFilters(value, parts.filters);
|
||||
// 处理空值
|
||||
return handleNullValue(value, parts.variable);
|
||||
}
|
||||
|
||||
/** 解析表达式为变量名和过滤器数组 */
|
||||
private ExpressionParts parseExpression(String expression) {
|
||||
if (!expression.contains("|")) {
|
||||
return new ExpressionParts(expression.trim(), new String[0]);
|
||||
}
|
||||
String[] parts = expression.split("\\|");
|
||||
String variable = parts[0].trim();
|
||||
String[] filters = new String[parts.length - 1];
|
||||
for (int i = 1; i < parts.length; i++) {
|
||||
filters[i - 1] = parts[i].trim();
|
||||
}
|
||||
return new ExpressionParts(variable, filters);
|
||||
}
|
||||
|
||||
/** 获取变量值,支持嵌套属性 */
|
||||
private Object getVariableValue(String variable, Map<String, Object> argsMap) {
|
||||
Object value = argsMap.get(variable);
|
||||
if (variable.contains(".")) {
|
||||
value = getNestedPropertyValue(variable, argsMap);
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
/** 获取嵌套属性值 */
|
||||
private Object getNestedPropertyValue(String deepKey, Map<String, Object> argsMap) {
|
||||
String[] keyPath = deepKeyCache.computeIfAbsent(deepKey, k -> k.split("\\."));
|
||||
try {
|
||||
Object value = argsMap.get(keyPath[0]);
|
||||
for (int i = 1; i < keyPath.length && value != null; i++) {
|
||||
value = Ref.getFieldValue(value, keyPath[i], Object.class);
|
||||
}
|
||||
Object value = argsMap.get(key);
|
||||
if (key.contains(".")) {
|
||||
// 反射获取
|
||||
try {
|
||||
String[] deepKey = key.split("\\.");
|
||||
value = argsMap.get(deepKey[0]);
|
||||
for (int i = 1; i < deepKey.length; i++) {
|
||||
value = Ref.getFieldValue(value, deepKey[i], Object.class);
|
||||
}
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException("ref field file: " + e.getMessage(), e);
|
||||
}
|
||||
return value;
|
||||
} catch (IllegalAccessException e) {
|
||||
throw new RuntimeException("ref field error: " + e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
/** 应用过滤器链 */
|
||||
private Object applyFilters(Object value, String[] filters) {
|
||||
if (TimiJava.isEmpty(filters) || TimiJava.isEmpty(filterMap) || value == null) {
|
||||
return value;
|
||||
}
|
||||
String currentValue = String.valueOf(value);
|
||||
for (String filterName : filters) {
|
||||
CallbackArgReturn<String, String> filter = filterMap.get(filterName);
|
||||
if (filter == null) {
|
||||
throw new IllegalArgumentException("not found %s filter for".formatted(filterName));
|
||||
}
|
||||
if (TimiJava.isNotEmpty(filterMap) && TimiJava.isNotEmpty(filters)) {
|
||||
// 过滤
|
||||
for (int i = 0; i < filters.length; i++) {
|
||||
CallbackArgReturn<String, String> filter = filterMap.get(filters[i].trim());
|
||||
if (filter == null) {
|
||||
throw new NullPointerException("not found %s filter for %s".formatted(filters[i], key));
|
||||
}
|
||||
value = filter.handler((String) value);
|
||||
}
|
||||
currentValue = filter.handler(currentValue);
|
||||
}
|
||||
return currentValue;
|
||||
}
|
||||
|
||||
/** 处理空值情况 */
|
||||
private String handleNullValue(Object value, String variable) {
|
||||
if (value == null) {
|
||||
if (!nullable) {
|
||||
throw new NullPointerException("not found %s value".formatted(variable));
|
||||
}
|
||||
if (value == null) {
|
||||
if (!nullable) {
|
||||
throw new NullPointerException("null pointer exception for arg: " + key);
|
||||
}
|
||||
value = "";
|
||||
}
|
||||
return String.valueOf(value);
|
||||
});
|
||||
return "";
|
||||
}
|
||||
return String.valueOf(value);
|
||||
}
|
||||
|
||||
public void putFilter(String name, CallbackArgReturn<String, String> callback) {
|
||||
if (filterMap == null) {
|
||||
filterMap = new HashMap<>();
|
||||
}
|
||||
filterMap.put(name, callback);
|
||||
}
|
||||
|
||||
public void putAllFilter(Map<String, CallbackArgReturn<String, String>> filterMap) {
|
||||
if (this.filterMap == null) {
|
||||
this.filterMap = new HashMap<>();
|
||||
}
|
||||
this.filterMap.putAll(filterMap);
|
||||
public void putAllFilter(Map<String, CallbackArgReturn<String, String>> filters) {
|
||||
filterMap.putAll(filters);
|
||||
}
|
||||
|
||||
public void removeFilter(String name) {
|
||||
if (TimiJava.isNotEmpty(filterMap)) {
|
||||
filterMap.remove(name);
|
||||
}
|
||||
filterMap.remove(name);
|
||||
}
|
||||
|
||||
public void clearFilter() {
|
||||
if (TimiJava.isNotEmpty(filterMap)) {
|
||||
filterMap.clear();
|
||||
}
|
||||
filterMap.clear();
|
||||
}
|
||||
|
||||
public void setNullable(boolean nullable) {
|
||||
@ -125,4 +157,16 @@ public class StringInterpolator {
|
||||
public boolean isNullable() {
|
||||
return nullable;
|
||||
}
|
||||
|
||||
/** 表达式部分内部类 */
|
||||
private record ExpressionParts(String variable, String[] filters) {
|
||||
}
|
||||
|
||||
public static StringInterpolator createDollarInterpolator() {
|
||||
return new StringInterpolator(DOLLAR_OBJ);
|
||||
}
|
||||
|
||||
public static StringInterpolator createSimpleInterpolator() {
|
||||
return new StringInterpolator(SIMPLE_OBJ);
|
||||
}
|
||||
}
|
||||
|
||||
@ -2,14 +2,12 @@ package com.imyeyu.utils;
|
||||
|
||||
import com.imyeyu.java.TimiJava;
|
||||
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.security.SecureRandom;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.Map;
|
||||
import java.util.UUID;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
/**
|
||||
* @author 夜雨
|
||||
@ -20,63 +18,6 @@ public class Text {
|
||||
/** 十六进制小写 */
|
||||
public static char[] HEX_DIGITS_LOWER = "0123456789abcdef".toCharArray();
|
||||
|
||||
/** 十六进制大写 */
|
||||
public static char[] HEX_DIGITS_UPPER = "0123456789ABCDEF".toCharArray();
|
||||
|
||||
/**
|
||||
* 字节数据转 16 进制字符串
|
||||
*
|
||||
* @param bytes 字节数据
|
||||
* @return 16 进制字符串
|
||||
*/
|
||||
public static String byteToHex(byte[] bytes) {
|
||||
final int l = bytes.length;
|
||||
final char[] c = new char[l << 1];
|
||||
for (int i = 0, j = 0; i < l; i++) {
|
||||
c[j++] = Text.HEX_DIGITS_LOWER[(0xF0 & bytes[i]) >>> 4];
|
||||
c[j++] = Text.HEX_DIGITS_LOWER[0x0F & bytes[i]];
|
||||
}
|
||||
return new String(c);
|
||||
}
|
||||
|
||||
/**
|
||||
* 16 进制字符串转字节数据
|
||||
*
|
||||
* @param hex 16 进制字符串
|
||||
* @return 字节数据
|
||||
* @throws UnsupportedEncodingException 不支持的编码
|
||||
*/
|
||||
public static byte[] hexToByte(String hex) throws UnsupportedEncodingException {
|
||||
final char[] c = hex.toCharArray();
|
||||
final byte[] b = new byte[c.length >> 1];
|
||||
|
||||
final int len = c.length;
|
||||
if ((len & 0x01) != 0) {
|
||||
throw new UnsupportedEncodingException("Odd number of characters.");
|
||||
}
|
||||
|
||||
final int outLen = len >> 1;
|
||||
if (c.length < outLen) {
|
||||
throw new UnsupportedEncodingException("Output array is not large enough to accommodate decoded data.");
|
||||
}
|
||||
for (int i = 0, j = 0; j < len; i++) {
|
||||
int f = toDigit(c[j], j) << 4;
|
||||
j++;
|
||||
f = f | toDigit(c[j], j);
|
||||
j++;
|
||||
b[i] = (byte) (f & 0xFF);
|
||||
}
|
||||
return b;
|
||||
}
|
||||
|
||||
private static int toDigit(final char ch, final int index) throws UnsupportedEncodingException {
|
||||
final int digit = Character.digit(ch, 16);
|
||||
if (digit == -1) {
|
||||
throw new UnsupportedEncodingException("Illegal hexadecimal character " + ch + " at index " + index);
|
||||
}
|
||||
return digit;
|
||||
}
|
||||
|
||||
/**
|
||||
* 是否为半角字符
|
||||
*
|
||||
@ -126,17 +67,6 @@ public class Text {
|
||||
return String.format("%0" + l + "d", number);
|
||||
}
|
||||
|
||||
/**
|
||||
* 正则表达式测试
|
||||
*
|
||||
* @param reg 正则
|
||||
* @param value 文本
|
||||
* @return true 为匹配
|
||||
*/
|
||||
public static boolean testReg(String reg, String value) {
|
||||
return Pattern.compile(reg).matcher(value).matches();
|
||||
}
|
||||
|
||||
/**
|
||||
* 驼峰转下划线
|
||||
*
|
||||
@ -460,4 +390,14 @@ public class Text {
|
||||
}
|
||||
return sb.toString();
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取驼峰类名
|
||||
*
|
||||
* @param clazz 类
|
||||
* @return 驼峰类名
|
||||
*/
|
||||
public static String camelCaseClassName(Class<?> clazz) {
|
||||
return Character.toLowerCase(clazz.getSimpleName().charAt(0)) + clazz.getSimpleName().substring(1);
|
||||
}
|
||||
}
|
||||
|
||||
@ -255,6 +255,51 @@ public class Time {
|
||||
return Instant.ofEpochMilli(unixTime).atZone(ZoneId.systemDefault()).toLocalDateTime();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* 将时间字符串解析为毫秒值
|
||||
*
|
||||
* <p>支持的格式示例:
|
||||
* <pre>
|
||||
* 10 = 10
|
||||
* 10ms = 10
|
||||
* 10s = 10,000
|
||||
* 10m = 600,000
|
||||
* 10h = 36,000,000
|
||||
* 10d = 10D = 10 d = 864,000,000
|
||||
* 10.5d = 907,200,000
|
||||
* </pre>
|
||||
*
|
||||
* @param timeStr 时间字符串
|
||||
* @return 毫秒
|
||||
* @throws IllegalArgumentException 输入格式无效
|
||||
*/
|
||||
public static long parseToMS(String timeStr) {
|
||||
if (TimiJava.isEmpty(timeStr)) {
|
||||
throw new IllegalArgumentException("not found timeStr");
|
||||
}
|
||||
String normalized = timeStr.replaceAll("\\s+", "").toLowerCase();
|
||||
|
||||
Pattern pattern = Pattern.compile("^(\\d+(?:\\.\\d+)?)(ms|[dhms])?$");
|
||||
Matcher matcher = pattern.matcher(normalized);
|
||||
if (!matcher.matches()) {
|
||||
throw new IllegalArgumentException("invalid format: " + timeStr);
|
||||
}
|
||||
double value = Double.parseDouble(matcher.group(1));
|
||||
String unit = matcher.group(2);
|
||||
if (TimiJava.isEmpty(unit) || unit.equals("ms")) {
|
||||
return Math.round(value);
|
||||
}
|
||||
double multiplier = switch (unit) {
|
||||
case "s" -> Time.S;
|
||||
case "m" -> Time.M;
|
||||
case "h" -> Time.H;
|
||||
case "d" -> Time.D;
|
||||
default -> throw new IllegalArgumentException("invalid format unit: " + unit);
|
||||
};
|
||||
return Math.round(value * multiplier);
|
||||
}
|
||||
|
||||
/**
|
||||
* 媒体时间操作
|
||||
*
|
||||
|
||||
18
src/test/java/test/TestAES.java
Normal file
18
src/test/java/test/TestAES.java
Normal file
@ -0,0 +1,18 @@
|
||||
package test;
|
||||
|
||||
import com.imyeyu.utils.Encoder;
|
||||
import com.imyeyu.utils.Encryptor;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author 夜雨
|
||||
* @since 2025-12-28 11:44
|
||||
*/
|
||||
public class TestAES {
|
||||
|
||||
@Test
|
||||
public void testGenerateAESKey() {
|
||||
byte[] bytes = Encryptor.aesKey(256);
|
||||
System.out.println(Encoder.hex(bytes));
|
||||
}
|
||||
}
|
||||
22
src/test/java/test/TestDigest.java
Normal file
22
src/test/java/test/TestDigest.java
Normal file
@ -0,0 +1,22 @@
|
||||
package test;
|
||||
|
||||
import com.imyeyu.utils.Digest;
|
||||
import com.imyeyu.utils.Time;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author 夜雨
|
||||
* @since 2025-10-30 16:55
|
||||
*/
|
||||
public class TestDigest {
|
||||
|
||||
@Test
|
||||
public void testArgon2id() {
|
||||
String pwd = "hello argon";
|
||||
long start = Time.now();
|
||||
String hash = Digest.argon2id(pwd);
|
||||
System.out.println("digest time: " + (Time.now() - start) + "ms");
|
||||
System.out.println(hash);
|
||||
assert Digest.argon2idVerify(hash, pwd);
|
||||
}
|
||||
}
|
||||
@ -1,8 +1,24 @@
|
||||
package test;
|
||||
|
||||
import com.imyeyu.utils.Time;
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
/**
|
||||
* @author 夜雨
|
||||
* @since 2024-12-19 22:10
|
||||
*/
|
||||
public class TestTime {
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testParseToMS() {
|
||||
assert Time.parseToMS("10") == 10;
|
||||
assert Time.parseToMS("10ms") == 10;
|
||||
assert Time.parseToMS("10MS") == 10;
|
||||
assert Time.parseToMS("10 ms") == 10;
|
||||
assert Time.parseToMS("10s") == Time.S * 10;
|
||||
assert Time.parseToMS("10m") == Time.M * 10;
|
||||
assert Time.parseToMS("10h") == Time.H * 10;
|
||||
assert Time.parseToMS("10d") == Time.D * 10;
|
||||
assert Time.parseToMS("10.5d") == Time.D * 10 + Time.D * .5;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user