【8.3.4】【rule】更新一个code生成器的工具类

master
stylefeng 2025-05-04 22:28:01 +08:00
parent 808f7ad458
commit 3956efe691
3 changed files with 224 additions and 0 deletions

View File

@ -0,0 +1,44 @@
package cn.stylefeng.roses.kernel.rule.code;
/**
*
*
* @author fengshuonan
* @since 2025/4/20 18:49
*/
public class CodeGenerateUtil {
/**
*
*
* @author fengshuonan
* @since 2025/4/20 18:50
*/
public static String generateQrCode(CodeStrategy codeStrategy, Integer codeLength) {
// 初始化生成器需确保workerId和datacenterId集群唯一
UniqueTraceCodeGenerator generator = UniqueTraceCodeGenerator.getInstance();
// 生成固定15位混合编码
return generator.generateCode(codeStrategy, codeLength);
}
/**
* 使
*
* @author fengshuonan
* @since 2025/4/20 18:49
*/
public static void main(String[] args) {
for (int i = 0; i < 10000; i++) {
System.out.println("混合编码: " + CodeGenerateUtil.generateQrCode(CodeStrategy.MIXED, 15));
System.out.println("大写编码: " + CodeGenerateUtil.generateQrCode(CodeStrategy.UPPERCASE, 15));
System.out.println("小写编码: " + CodeGenerateUtil.generateQrCode(CodeStrategy.LOWERCASE, 15));
System.out.println("数字编码: " + CodeGenerateUtil.generateQrCode(CodeStrategy.DIGITS, 15));
}
}
}

View File

@ -0,0 +1,59 @@
package cn.stylefeng.roses.kernel.rule.code;
/**
*
*
* @author fengshuonan
* @since 2025/4/20 18:40
*/
public enum CodeStrategy {
/**
*
*/
MIXED(62, "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"),
/**
*
*/
UPPERCASE(26, "ABCDEFGHIJKLMNOPQRSTUVWXYZ"),
/**
*
*/
LOWERCASE(26, "abcdefghijklmnopqrstuvwxyz"),
/**
*
*/
DIGITS(10, "0123456789");
final int base;
final String charset;
CodeStrategy(int base, String charset) {
this.base = base;
this.charset = charset;
}
/**
*
*
* @author fengshuonan
* @since 2025/4/20 19:42
*/
public static CodeStrategy getByCode(Integer codeStrategyType) {
if (codeStrategyType.equals(1)) {
return DIGITS;
} else if (codeStrategyType.equals(2)) {
return UPPERCASE;
} else if (codeStrategyType.equals(3)) {
return LOWERCASE;
} else if (codeStrategyType.equals(4)) {
return MIXED;
}
return null;
}
}

View File

@ -0,0 +1,121 @@
package cn.stylefeng.roses.kernel.rule.code;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;
/**
*
* UUID
*
* @author fengshuonan
* @since 2025/4/20 18:42
*/
public class UniqueTraceCodeGenerator {
/**
*
*/
private static volatile UniqueTraceCodeGenerator instance;
private UniqueTraceCodeGenerator() {
}
/**
*
*
* @author fengshuonan
* @since 2025/4/20 18:41
*/
public static UniqueTraceCodeGenerator getInstance() {
if (instance == null) {
synchronized (UniqueTraceCodeGenerator.class) {
if (instance == null) {
instance = new UniqueTraceCodeGenerator();
}
}
}
return instance;
}
/**
*
*
* @param strategy
* @param length
* @return
* @author fengshuonan
* @since 2025/4/20 18:42
*/
public String generateCode(CodeStrategy strategy, int length) {
// 生成 UUID
String uuid = UUID.randomUUID().toString().replace("-", "");
// 对 UUID 进行哈希处理
String hashedUuid = hash(uuid);
// 转换为指定编码策略
return convert(hashedUuid, strategy, length);
}
/**
* SHA-256
*
* @param input
* @return
* @author fengshuonan
* @since 2025/4/20 18:42
*/
private String hash(String input) {
try {
MessageDigest digest = MessageDigest.getInstance("SHA-256");
byte[] hashBytes = digest.digest(input.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : hashBytes) {
String hex = Integer.toHexString(0xff & b);
if (hex.length() == 1) {
hexString.append('0');
}
hexString.append(hex);
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("哈希算法异常", e);
}
}
/**
*
*
* @author fengshuonan
* @since 2025/4/20 18:42
*/
private String convert(String input, CodeStrategy strategy, int fixLength) {
StringBuilder sb = new StringBuilder();
int base = strategy.base;
String charset = strategy.charset;
// 将输入的哈希值16进制字符串转换为指定编码策略
for (int i = 0; i < input.length(); i += 2) {
// 每次取两个字符(一个字节)
String hex = input.substring(i, i + 2);
int byteValue = Integer.parseInt(hex, 16);
// 将字节值转换为指定编码字符
sb.append(charset.charAt(byteValue % base));
}
// 如果长度不足,补足到固定长度
while (sb.length() < fixLength) {
sb.append(charset.charAt(0));
}
// 如果长度超过固定长度,截取前 fixLength 个字符
if (sb.length() > fixLength) {
sb.setLength(fixLength);
}
return sb.toString();
}
}