|
|
@@ -0,0 +1,111 @@
|
|
|
+package com.ruoyi.common.utils;
|
|
|
+
|
|
|
+import java.security.KeyPair;
|
|
|
+import java.util.Base64;
|
|
|
+import java.util.StringJoiner;
|
|
|
+
|
|
|
+/**
|
|
|
+ * 数据签名工具类 - SM2版本
|
|
|
+ */
|
|
|
+public class SignUtil {
|
|
|
+
|
|
|
+ // 默认密钥对 - 使用正确的密钥格式
|
|
|
+ private static String defaultPrivateKey = "MIGTAgEAMBMGByqGSM49AgEGCCqBHM9VAYItBHkwdwIBAQQg+k8IZ/BT7Bje06wNbD80q6dSaH9XHRVX1exGyKPY0UagCgYIKoEcz1UBgi2hRANCAAREPTrH5b7+tIK95Zyxzqf4YO2EWpP+UnkC6T06S2EtcKPDwe+bQ4Ti2oMX4VRdU2l1PJOl++pxveAYpaY33COZ";
|
|
|
+ private static String defaultPublicKey = "MFkwEwYHKoZIzj0CAQYIKoEcz1UBgi0DQgAERD06x+W+/rSCveWcsc6n+GDthFqT/lJ5Auk9OkthLXCjw8Hvm0OE4tqDF+FUXVNpdTyTpfvqcb3gGKWmN9wjmQ==";
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成SM2签名(使用默认私钥)
|
|
|
+ */
|
|
|
+ public static String generateSign(String... fields) {
|
|
|
+ // 构建签名内容
|
|
|
+ StringJoiner joiner = new StringJoiner("|");
|
|
|
+ for (String field : fields) {
|
|
|
+ joiner.add(field != null ? field : "");
|
|
|
+ }
|
|
|
+ String signContent = joiner.toString();
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 使用SM2私钥签名
|
|
|
+ String base64Signature = Sm2Utils.sign(signContent, defaultPrivateKey);
|
|
|
+
|
|
|
+ // 将Base64签名转换为16进制
|
|
|
+ return org.apache.commons.codec.binary.Hex.encodeHexString(
|
|
|
+ Base64.getDecoder().decode(base64Signature)
|
|
|
+ );
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("生成SM2签名失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证签名(使用默认公钥)
|
|
|
+ */
|
|
|
+ public static boolean verifySign(String signature, String... fields) {
|
|
|
+ if (signature == null) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ // 构建签名内容
|
|
|
+ StringJoiner joiner = new StringJoiner("|");
|
|
|
+ for (String field : fields) {
|
|
|
+ joiner.add(field != null ? field : "");
|
|
|
+ }
|
|
|
+ String signContent = joiner.toString();
|
|
|
+
|
|
|
+ try {
|
|
|
+ // 将签名内容转换为16进制字符串
|
|
|
+ String hexSignContent = org.apache.commons.codec.binary.Hex.encodeHexString(
|
|
|
+ signContent.getBytes()
|
|
|
+ );
|
|
|
+
|
|
|
+ // 使用SM2公钥验证签名
|
|
|
+ return Sm2Utils.verify(hexSignContent, signature, defaultPublicKey);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 获取默认公钥
|
|
|
+ */
|
|
|
+ public static String getDefaultPublicKey() {
|
|
|
+ return defaultPublicKey;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 设置自定义密钥对
|
|
|
+ */
|
|
|
+ public static void setCustomKeyPair(String privateKey, String publicKey) {
|
|
|
+ defaultPrivateKey = privateKey;
|
|
|
+ defaultPublicKey = publicKey;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 生成新的密钥对并设置为默认
|
|
|
+ */
|
|
|
+ public static void generateNewKeyPair() {
|
|
|
+ try {
|
|
|
+ KeyPair newKeyPair = Sm2Utils.generateSm2KeyPair();
|
|
|
+ defaultPrivateKey = Base64.getEncoder().encodeToString(newKeyPair.getPrivate().getEncoded());
|
|
|
+ defaultPublicKey = Base64.getEncoder().encodeToString(newKeyPair.getPublic().getEncoded());
|
|
|
+
|
|
|
+ System.out.println("新生成的私钥: " + defaultPrivateKey);
|
|
|
+ System.out.println("新生成的公钥: " + defaultPublicKey);
|
|
|
+ } catch (Exception e) {
|
|
|
+ throw new RuntimeException("生成新密钥对失败", e);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 验证当前密钥对是否有效
|
|
|
+ */
|
|
|
+ public static boolean validateKeyPair() {
|
|
|
+ try {
|
|
|
+ String testData = "密钥验证测试";
|
|
|
+ String signature = generateSign(testData);
|
|
|
+ return verifySign(signature, testData);
|
|
|
+ } catch (Exception e) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|