SM3加密修改

This commit is contained in:
cwchen 2025-09-16 17:17:10 +08:00
parent c5565a6327
commit 2efbef8e39
1 changed files with 34 additions and 5 deletions

View File

@ -1,5 +1,7 @@
package com.bonus.common.utils.encryption;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.crypto.SmUtil;
import cn.hutool.crypto.digest.SM3;
@ -14,18 +16,45 @@ import java.nio.charset.StandardCharsets;
*/
public class Sm3Util {
static SM3 sm3 = SmUtil.sm3WithSalt("2cc0c5f9f1749f1632efa9f63e902323".getBytes(StandardCharsets.UTF_8));
private static final String SALT = "2cc0c5f9f1749f1632efa9f63e902323";
public static String encrypt(String data) {
return Sm3Util.sm3.digestHex(data);
try {
// 与前端的处理方式完全一致字符串拼接 + SM3哈希
String dataWithSalt = SALT + data;
SM3 sm3 = SmUtil.sm3();
return sm3.digestHex(dataWithSalt);
} catch (Exception e) {
throw new RuntimeException("SM3 encryption failed", e);
}
}
// 其他方法也需要相应修改
public static String encrypt(InputStream data) {
return Sm3Util.sm3.digestHex(data);
// 对于流数据需要先读取内容再拼接
try {
String content = IoUtil.readUtf8(data);
String dataWithSalt = SALT + content;
SM3 sm3 = SmUtil.sm3();
return sm3.digestHex(dataWithSalt);
} catch (Exception e) {
throw new RuntimeException("SM3 encryption failed", e);
}
}
public static String encrypt(File dataFile) {
return Sm3Util.sm3.digestHex(dataFile);
try {
String content = FileUtil.readUtf8String(dataFile);
String dataWithSalt = SALT + content;
SM3 sm3 = SmUtil.sm3();
return sm3.digestHex(dataWithSalt);
} catch (Exception e) {
throw new RuntimeException("SM3 encryption failed", e);
}
}
public static void main(String[] args) {
System.out.println(Sm3Util.encrypt("pageNum=1&pageSize=10"));
}
}