增加 appid appkey appsecrect 生成工具类
This commit is contained in:
parent
66374e858d
commit
bb65b24e15
@ -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();
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成 appid(16位,基于时间戳+随机数,确保唯一性和可读性)
|
||||||
|
*/
|
||||||
|
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();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成 appkey(32位,基于SHA-256哈希,高安全性)
|
||||||
|
*/
|
||||||
|
public static String generateAppKey() {
|
||||||
|
return generateSecureString(32);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 生成 appsecret(64位,比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位
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Reference in New Issue
Block a user