mirror of
https://codeup.aliyun.com/67c68d4e484ca2f0a13ac3c1/ydc/jsowell-charger-web.git
synced 2026-05-13 14:30:08 +08:00
加签 验签方法
This commit is contained in:
@@ -1,11 +1,21 @@
|
|||||||
package com.jsowell.common.util;
|
package com.jsowell.common.util;
|
||||||
|
|
||||||
|
import lombok.extern.slf4j.Slf4j;
|
||||||
|
|
||||||
import java.math.BigDecimal;
|
import java.math.BigDecimal;
|
||||||
|
import java.security.KeyFactory;
|
||||||
|
import java.security.PrivateKey;
|
||||||
|
import java.security.PublicKey;
|
||||||
|
import java.security.Signature;
|
||||||
|
import java.security.spec.PKCS8EncodedKeySpec;
|
||||||
|
import java.security.spec.X509EncodedKeySpec;
|
||||||
import java.text.DecimalFormat;
|
import java.text.DecimalFormat;
|
||||||
|
import java.util.Base64;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 汇付支付的工具类
|
* 汇付支付的工具类
|
||||||
*/
|
*/
|
||||||
|
@Slf4j
|
||||||
public class AdapayUtil {
|
public class AdapayUtil {
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
@@ -39,4 +49,56 @@ public class AdapayUtil {
|
|||||||
public static String formatAmount(BigDecimal amount) {
|
public static String formatAmount(BigDecimal amount) {
|
||||||
return formatAmount(amount.toString());
|
return formatAmount(amount.toString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* RSA私钥签名:签名方式SHA256WithRSA
|
||||||
|
* @param data 待签名字符串
|
||||||
|
* @param privateKeyBase64 私钥(Base64编码)
|
||||||
|
* @return 签名byte[]
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static String sign(String data, String privateKeyBase64) {
|
||||||
|
// Base64 --> Key
|
||||||
|
try {
|
||||||
|
byte[] bytes = Base64.getDecoder().decode(privateKeyBase64);
|
||||||
|
PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(bytes);
|
||||||
|
KeyFactory keyFactory;
|
||||||
|
keyFactory = KeyFactory.getInstance("RSA");
|
||||||
|
PrivateKey privateKey = keyFactory.generatePrivate(keySpec);
|
||||||
|
// Sign
|
||||||
|
Signature signature = Signature.getInstance("SHA256WithRSA");
|
||||||
|
signature.initSign(privateKey);
|
||||||
|
signature.update(data.getBytes("UTF-8"));
|
||||||
|
return Base64.getEncoder().encodeToString(signature.sign());
|
||||||
|
} catch (Exception e) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* 使用汇付RSA公钥验签
|
||||||
|
* @param data 待签名字符串
|
||||||
|
* @param publicKeyBase64 公钥(Base64编码)
|
||||||
|
* @return 验签结果
|
||||||
|
* @throws Exception
|
||||||
|
*/
|
||||||
|
public static boolean verify(String data, String publicKeyBase64, String sign) {
|
||||||
|
// Base64 --> Key
|
||||||
|
try {
|
||||||
|
byte[] bytes = Base64.getDecoder().decode(publicKeyBase64);
|
||||||
|
X509EncodedKeySpec keySpec = new X509EncodedKeySpec(bytes);
|
||||||
|
KeyFactory keyFactory;
|
||||||
|
keyFactory = KeyFactory.getInstance("RSA");
|
||||||
|
PublicKey publicKey = keyFactory.generatePublic(keySpec);
|
||||||
|
// verify
|
||||||
|
Signature signature = Signature.getInstance("SHA256WithRSA");
|
||||||
|
signature.initVerify(publicKey);
|
||||||
|
signature.update(data.getBytes("UTF-8"));
|
||||||
|
return signature.verify(Base64.getDecoder().decode(sign));
|
||||||
|
} catch (Exception e) {
|
||||||
|
log.error("Exception", e);
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user