From 8809ca2343d34fe48fafd87be8e49158c0206fb9 Mon Sep 17 00:00:00 2001 From: AprilWind <2100166581@qq.com> Date: Tue, 24 Mar 2026 14:06:53 +0800 Subject: [PATCH] =?UTF-8?q?update=20=E4=BC=98=E5=8C=96=E5=AE=89=E5=85=A8?= =?UTF-8?q?=E7=9B=B8=E5=85=B3=E5=B7=A5=E5=85=B7=E7=B1=BB,=E5=A2=9E?= =?UTF-8?q?=E5=8A=A0sm2=E9=AA=8C=E7=AD=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../common/encrypt/utils/EncryptUtils.java | 44 +++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/utils/EncryptUtils.java b/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/utils/EncryptUtils.java index ff0fbc812..fb15a3b04 100644 --- a/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/utils/EncryptUtils.java +++ b/ruoyi-common/ruoyi-common-encrypt/src/main/java/org/dromara/common/encrypt/utils/EncryptUtils.java @@ -222,6 +222,50 @@ public class EncryptUtils { return sm2.decryptStr(data, KeyType.PrivateKey, StandardCharsets.UTF_8); } + /** + * SM2公钥验签(Base64编码) + * + * @param data 原文数据 + * @param sign 签名值 + * @param publicKey 公钥 + * @return true-验签成功,false-验签失败 + */ + public static boolean verifySm2Sign(String data, String sign, String publicKey) { + if (StrUtil.isBlank(data)) { + throw new IllegalArgumentException("SM2验签需要传入原文数据"); + } + if (StrUtil.isBlank(sign)) { + throw new IllegalArgumentException("SM2验签需要传入签名值"); + } + if (StrUtil.isBlank(publicKey)) { + throw new IllegalArgumentException("SM2验签需要传入公钥"); + } + SM2 sm2 = SmUtil.sm2(null, publicKey); + return sm2.verify(data.getBytes(StandardCharsets.UTF_8), sign.getBytes(StandardCharsets.UTF_8)); + } + + /** + * SM2公钥验签(Hex编码) + * + * @param dataHex 原文数据(Hex编码) + * @param signHex 签名值(Hex编码) + * @param publicKey 公钥 + * @return true-验签成功,false-验签失败 + */ + public static boolean verifySm2SignHex(String dataHex, String signHex, String publicKey) { + if (StrUtil.isBlank(dataHex)) { + throw new IllegalArgumentException("SM2验签需要传入Hex格式的原文数据"); + } + if (StrUtil.isBlank(signHex)) { + throw new IllegalArgumentException("SM2验签需要传入Hex格式的签名值"); + } + if (StrUtil.isBlank(publicKey)) { + throw new IllegalArgumentException("SM2验签需要传入公钥"); + } + SM2 sm2 = SmUtil.sm2(null, publicKey); + return sm2.verifyHex(dataHex, signHex); + } + /** * 产生RSA加解密需要的公钥和私钥 *