增加 appid appkey appsecrect 生成工具类

This commit is contained in:
Jack 2025-07-26 09:54:55 +08:00
parent 66374e858d
commit bb65b24e15

View File

@ -0,0 +1,115 @@
/*
* Copyright (c) 2025. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
* Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
* Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
* Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
* Vestibulum commodo. Ut rhoncus gravida arcu.
*/
package com.suisung.mall.common.utils;
import javax.crypto.SecretKeyFactory;
import javax.crypto.spec.PBEKeySpec;
import java.security.NoSuchAlgorithmException;
import java.security.SecureRandom;
import java.security.spec.InvalidKeySpecException;
import java.time.Instant;
import java.util.Base64;
public class CredentialGenerator {
// 静态 SecureRandom 实例线程安全避免频繁创建
private static final SecureRandom SECURE_RANDOM = new SecureRandom();
// 编码方式URL安全的Base64避免特殊字符
private static final Base64.Encoder BASE64_ENCODER = Base64.getUrlEncoder().withoutPadding();
/**
* 生成 appid16位基于时间戳+随机数确保唯一性和可读性
*/
public static String generateAppId() {
// 1. 获取当前时间戳秒级4字节
long timestamp = Instant.now().getEpochSecond();
// 2. 生成随机数4字节
byte[] randomBytes = new byte[4];
SECURE_RANDOM.nextBytes(randomBytes);
// 3. 拼接时间戳和随机数转为16进制字符串8+8=16位
StringBuilder appId = new StringBuilder();
appId.append(String.format("%08x", timestamp)); // 时间戳8位16进制
for (byte b : randomBytes) {
appId.append(String.format("%02x", b)); // 随机数8位16进制
}
return appId.toString();
}
/**
* 生成 appkey32位基于SHA-256哈希高安全性
*/
public static String generateAppKey() {
return generateSecureString(32);
}
/**
* 生成 appsecret64位比appkey更长用于核心加密
*/
public static String generateAppSecret() {
return generateSecureString(64);
}
/**
* 生成指定长度的安全随机字符串基于Base64编码
*
* @param length 目标字符串长度
*/
private static String generateSecureString(int length) {
// 计算需要的随机字节数Base64编码中3字节4字符向上取整
int byteLength = (int) Math.ceil(length * 3.0 / 4);
byte[] randomBytes = new byte[byteLength];
SECURE_RANDOM.nextBytes(randomBytes);
// 编码并截取指定长度
String encoded = BASE64_ENCODER.encodeToString(randomBytes);
return encoded.substring(0, length);
}
/**
* 可选生成带签名的appkey结合appid和密钥增强安全性
*
* @param appId 已生成的appid
*/
public static String generateSignedAppKey(String appId) {
try {
// 1. 生成随机盐值16字节
byte[] salt = new byte[16];
SECURE_RANDOM.nextBytes(salt);
// 2. 使用PBKDF2算法增强随机性迭代1000次
PBEKeySpec spec = new PBEKeySpec(
appId.toCharArray(),
salt,
1000, // 迭代次数
256 // 输出长度256位=32字节
);
SecretKeyFactory factory = SecretKeyFactory.getInstance("PBKDF2WithHmacSHA256");
byte[] keyBytes = factory.generateSecret(spec).getEncoded();
// 3. 编码为32位字符串
return BASE64_ENCODER.encodeToString(keyBytes).substring(0, 32);
} catch (NoSuchAlgorithmException | InvalidKeySpecException e) {
throw new RuntimeException("Failed to generate signed appkey", e);
}
}
public static void main(String[] args) {
// 测试生成
String appId = generateAppId();
String appKey = generateAppKey();
String appSecret = generateAppSecret();
String signedAppKey = generateSignedAppKey(appId);
System.out.println("appid: " + appId); // 16位
System.out.println("appkey: " + appKey); // 32位
System.out.println("signedAppKey: " + signedAppKey); // 带签名的32位
System.out.println("appsecret: " + appSecret); // 64位
}
}