From 5498701eaeff89a97252fd519661dc952b2ccb92 Mon Sep 17 00:00:00 2001 From: wanggeng <450292408@qq.com> Date: Tue, 8 Apr 2025 16:01:23 +0800 Subject: [PATCH] feat: sha256 --- .../com/tenlion/operator/util/Sha256Util.java | 130 ++++++++++++++++++ 1 file changed, 130 insertions(+) create mode 100644 src/main/java/cn/com/tenlion/operator/util/Sha256Util.java diff --git a/src/main/java/cn/com/tenlion/operator/util/Sha256Util.java b/src/main/java/cn/com/tenlion/operator/util/Sha256Util.java new file mode 100644 index 0000000..7059e88 --- /dev/null +++ b/src/main/java/cn/com/tenlion/operator/util/Sha256Util.java @@ -0,0 +1,130 @@ +package cn.com.tenlion.operator.util; + +import org.apache.commons.codec.binary.Base64; +import org.bouncycastle.util.io.pem.PemReader; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.FileInputStream; +import java.io.InputStreamReader; +import java.io.StringReader; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.security.KeyFactory; +import java.security.PrivateKey; +import java.security.PublicKey; +import java.security.Signature; +import java.security.cert.CertificateFactory; +import java.security.cert.X509Certificate; +import java.security.spec.PKCS8EncodedKeySpec; + +/** + * ClassName: Sha256Util + * Description: + * Author: wanggeng + * Date: 2025/4/8 14:47 + * Version: 1.0 + */ +public class Sha256Util { + + private static final Logger LOG = LoggerFactory.getLogger(Sha256Util.class); + + private static final String SIGN_SHA256RSA_ALGORITHMS = "SHA256WithRSA"; + + /** + * 签名 + * + * @param content 待签名的内容 + * @param privateKey 私钥 + * @return 签名后的Base64编码字符串 + * @throws Exception 签名过程中可能出现的异常 + */ + public static String sign(String content, PrivateKey privateKey) throws Exception { + Signature signature = Signature.getInstance(SIGN_SHA256RSA_ALGORITHMS); + signature.initSign(privateKey); + signature.update(content.getBytes(StandardCharsets.UTF_8)); + // 签名使用Base64编码后得到的值即为请求数据中signature字段的值 + return Base64.encodeBase64String(signature.sign()); + } + + /** + * 验签 + * + * @param content 待验证的内容 + * @param signature 签名的Base64编码字符串 + * @param publicKey 公钥 + * @return 验签结果,true表示验签通过,false表示验签失败 + */ + public static Boolean checkSign(String content, String signature, PublicKey publicKey) { + try { + LOG.info("content = " + content); + LOG.info("signature = " + signature); + Signature signatureTool = Signature.getInstance(SIGN_SHA256RSA_ALGORITHMS); + signatureTool.initVerify(publicKey); + signatureTool.update(content.getBytes(StandardCharsets.UTF_8)); + byte[] signbyte = Base64.decodeBase64(signature.getBytes()); + return signatureTool.verify(signbyte); + } catch (Exception e) { + LOG.error("signature error", e); + } + return false; + } + + /** + * 从文件路径加载 pkcs8 格式私钥 + * + * @param path 私钥文件的路径 + * @return 加载后的私钥 + * @throws Exception 加载私钥过程中可能出现的异常,如文件不存在、格式错误等 + */ + public static PrivateKey loadPrivateKeyByPath(String path) throws Exception { + PemReader pemReader = new PemReader(new InputStreamReader(Files.newInputStream(Paths.get(path)))); + PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(pemReader.readPemObject().getContent()); + pemReader.close(); + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + return keyFactory.generatePrivate(pkcs8EncodedKeySpec); + } + + /** + * 从字符串加载私钥 + * + * @param privateKey 私钥的字符串表示 + * @return 加载后的私钥 + * @throws Exception 加载私钥过程中可能出现的异常,如格式错误等 + */ + public static PrivateKey loadPrivateKey(String privateKey) throws Exception { + PemReader pemReader = new PemReader(new StringReader(privateKey)); + PKCS8EncodedKeySpec pkcs8EncodedKeySpec = new PKCS8EncodedKeySpec(pemReader.readPemObject().getContent()); + pemReader.close(); + KeyFactory keyFactory = KeyFactory.getInstance("RSA"); + return keyFactory.generatePrivate(pkcs8EncodedKeySpec); + } + + /** + * 从证书加载公钥 + * + */ + public static PublicKey loadPublicKey(String path) throws Exception { + try { + CertificateFactory fact = CertificateFactory.getInstance("X.509"); + try (FileInputStream is = new FileInputStream(path)) { + X509Certificate cer = (X509Certificate) fact.generateCertificate(is); + return cer.getPublicKey(); + } + } catch (Exception e) { + // 打印详细的异常信息,帮助定位问题 + System.err.println("Failed to load public key from file: " + path); + e.printStackTrace(); + throw e; + } + } + + public static void main(String[] args) throws Exception { + PrivateKey privateKey = loadPrivateKeyByPath("/Users/wanggeng/Desktop/application_test.pem"); + String content = "wx2421b1c4370ec43b\\n1554208460\\n593BEC0C930BF1AFEB40B4A08C8FB242\\nprepay_id=wx201410272009395522657a690389285100\\n"; + String signature = sign(content, privateKey); + System.out.println(signature); + } + +}