128 lines
4.6 KiB
Java
128 lines
4.6 KiB
Java
package com.ruoyi.system.rental;
|
|
|
|
import static com.ruoyi.system.rental.RentalValues.*;
|
|
|
|
import org.springframework.beans.factory.annotation.Value;
|
|
import org.springframework.stereotype.Component;
|
|
|
|
import java.nio.charset.StandardCharsets;
|
|
import java.security.MessageDigest;
|
|
import java.security.SecureRandom;
|
|
import java.util.Base64;
|
|
|
|
import javax.crypto.Cipher;
|
|
import javax.crypto.Mac;
|
|
import javax.crypto.spec.GCMParameterSpec;
|
|
import javax.crypto.spec.SecretKeySpec;
|
|
|
|
/** HMAC四域隔离和Secret信封加密。主密钥仅通过外部配置注入,缺失时拒绝签发。 */
|
|
@Component
|
|
public class RentalCrypto {
|
|
@Value("${rental.master-key:}")
|
|
private String masterKey;
|
|
|
|
private static final SecureRandom RANDOM = new SecureRandom();
|
|
|
|
public static String sha256(String body) {
|
|
try {
|
|
return hex(
|
|
MessageDigest.getInstance("SHA-256")
|
|
.digest(body.getBytes(StandardCharsets.UTF_8)));
|
|
} catch (Exception ex) {
|
|
throw new IllegalStateException("摘要计算失败");
|
|
}
|
|
}
|
|
|
|
public static String sign(
|
|
String secret,
|
|
String scope,
|
|
String key,
|
|
long timestamp,
|
|
String nonce,
|
|
String method,
|
|
String path,
|
|
String body) {
|
|
for (String value : new String[] {scope, key, nonce, method, path}) {
|
|
require(value != null && !value.contains("\n") && !value.contains("\r"), "签名字段非法");
|
|
}
|
|
String plain =
|
|
String.join(
|
|
"\n",
|
|
"RENT_HMAC_SHA256_V1",
|
|
scope,
|
|
key,
|
|
Long.toString(timestamp),
|
|
nonce,
|
|
method,
|
|
path,
|
|
sha256(body));
|
|
try {
|
|
Mac mac = Mac.getInstance("HmacSHA256");
|
|
mac.init(new SecretKeySpec(secret.getBytes(StandardCharsets.UTF_8), "HmacSHA256"));
|
|
return hex(mac.doFinal(plain.getBytes(StandardCharsets.UTF_8)));
|
|
} catch (Exception ex) {
|
|
throw new IllegalStateException("签名计算失败");
|
|
}
|
|
}
|
|
|
|
public static boolean equal(String expected, String actual) {
|
|
return actual != null
|
|
&& MessageDigest.isEqual(
|
|
expected.getBytes(StandardCharsets.US_ASCII),
|
|
actual.getBytes(StandardCharsets.US_ASCII));
|
|
}
|
|
|
|
public static String random() {
|
|
byte[] bytes = new byte[32];
|
|
RANDOM.nextBytes(bytes);
|
|
return Base64.getUrlEncoder().withoutPadding().encodeToString(bytes);
|
|
}
|
|
|
|
public String encrypt(String secret) {
|
|
return crypt(secret, true);
|
|
}
|
|
|
|
public String decrypt(String value) {
|
|
return crypt(value, false);
|
|
}
|
|
|
|
private String crypt(String value, boolean encrypt) {
|
|
try {
|
|
byte[] key = Base64.getDecoder().decode(masterKey);
|
|
require(key.length == 32, "租赁主密钥必须是Base64编码的32字节密钥");
|
|
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
|
|
if (encrypt) {
|
|
byte[] iv = new byte[12];
|
|
RANDOM.nextBytes(iv);
|
|
cipher.init(
|
|
Cipher.ENCRYPT_MODE,
|
|
new SecretKeySpec(key, "AES"),
|
|
new GCMParameterSpec(128, iv));
|
|
return Base64.getEncoder().encodeToString(iv)
|
|
+ "."
|
|
+ Base64.getEncoder()
|
|
.encodeToString(
|
|
cipher.doFinal(value.getBytes(StandardCharsets.UTF_8)));
|
|
}
|
|
String[] parts = value.split("\\.");
|
|
require(parts.length == 2, "凭证格式错误");
|
|
cipher.init(
|
|
Cipher.DECRYPT_MODE,
|
|
new SecretKeySpec(key, "AES"),
|
|
new GCMParameterSpec(128, Base64.getDecoder().decode(parts[0])));
|
|
return new String(
|
|
cipher.doFinal(Base64.getDecoder().decode(parts[1])), StandardCharsets.UTF_8);
|
|
} catch (Exception ex) {
|
|
throw new IllegalStateException("租赁凭证加解密失败,请检查外部主密钥配置");
|
|
}
|
|
}
|
|
|
|
private static String hex(byte[] bytes) {
|
|
StringBuilder result = new StringBuilder(bytes.length * 2);
|
|
for (byte value : bytes) {
|
|
result.append(String.format("%02x", value & 255));
|
|
}
|
|
return result.toString();
|
|
}
|
|
}
|