2023-05-16 16:24:26 +08:00
|
|
|
|
package com.jsowell.common.util;
|
|
|
|
|
|
|
2023-07-05 14:10:05 +08:00
|
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
|
|
|
|
2023-05-16 16:24:26 +08:00
|
|
|
|
import java.math.BigDecimal;
|
2023-07-05 14:13:50 +08:00
|
|
|
|
import java.nio.charset.StandardCharsets;
|
2023-07-05 14:10:05 +08:00
|
|
|
|
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;
|
2023-05-16 16:24:26 +08:00
|
|
|
|
import java.text.DecimalFormat;
|
2023-07-05 14:10:05 +08:00
|
|
|
|
import java.util.Base64;
|
2023-05-16 16:24:26 +08:00
|
|
|
|
|
2023-05-16 16:31:23 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 汇付支付的工具类
|
|
|
|
|
|
*/
|
2023-07-05 14:10:05 +08:00
|
|
|
|
@Slf4j
|
2023-05-16 16:24:26 +08:00
|
|
|
|
public class AdapayUtil {
|
2023-05-16 16:31:23 +08:00
|
|
|
|
|
2023-05-16 16:24:26 +08:00
|
|
|
|
public static void main(String[] args) {
|
|
|
|
|
|
String amount = "1110.5309";
|
|
|
|
|
|
String s = formatAmount(amount);
|
|
|
|
|
|
System.out.println(s);
|
|
|
|
|
|
|
|
|
|
|
|
BigDecimal bigDecimal = new BigDecimal(amount);
|
|
|
|
|
|
String s2 = formatAmount(bigDecimal);
|
|
|
|
|
|
System.out.println(s2);
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-16 16:31:23 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 格式化数字 保留两位小数,不足补0
|
|
|
|
|
|
* @param amount
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
2023-05-16 16:24:26 +08:00
|
|
|
|
public static String formatAmount(String amount) {
|
|
|
|
|
|
//保留2位小数
|
2023-05-16 16:31:23 +08:00
|
|
|
|
double d = new BigDecimal(amount).setScale(2, BigDecimal.ROUND_HALF_UP).doubleValue();
|
2023-05-16 16:24:26 +08:00
|
|
|
|
//不足两位则补0
|
|
|
|
|
|
DecimalFormat decimalFormat = new DecimalFormat("0.00#");
|
2023-05-16 16:31:23 +08:00
|
|
|
|
return decimalFormat.format(d);
|
2023-05-16 16:24:26 +08:00
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-16 16:31:23 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* 格式化数字 保留两位小数,不足补0
|
|
|
|
|
|
* @param amount
|
|
|
|
|
|
* @return
|
|
|
|
|
|
*/
|
2023-05-16 16:24:26 +08:00
|
|
|
|
public static String formatAmount(BigDecimal amount) {
|
|
|
|
|
|
return formatAmount(amount.toString());
|
|
|
|
|
|
}
|
2023-07-05 14:10:05 +08:00
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
|
* 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);
|
2023-07-05 14:13:50 +08:00
|
|
|
|
signature.update(data.getBytes(StandardCharsets.UTF_8));
|
2023-07-05 14:10:05 +08:00
|
|
|
|
return Base64.getEncoder().encodeToString(signature.sign());
|
|
|
|
|
|
} catch (Exception e) {
|
2023-07-05 14:14:44 +08:00
|
|
|
|
log.error("sign Exception", e);
|
2023-07-05 14:10:05 +08:00
|
|
|
|
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);
|
2023-07-05 14:13:50 +08:00
|
|
|
|
signature.update(data.getBytes(StandardCharsets.UTF_8));
|
2023-07-05 14:10:05 +08:00
|
|
|
|
return signature.verify(Base64.getDecoder().decode(sign));
|
|
|
|
|
|
} catch (Exception e) {
|
2023-07-05 14:14:44 +08:00
|
|
|
|
log.error("verify Exception", e);
|
2023-07-05 14:10:05 +08:00
|
|
|
|
return false;
|
|
|
|
|
|
}
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2023-05-16 16:24:26 +08:00
|
|
|
|
}
|