Browse Source

//tt pay v2

release_toaliyun_real
xhxu 4 years ago
parent
commit
5d6c220867
2 changed files with 90 additions and 1 deletions
  1. +1
    -1
      mallinkService/src/main/java/com/iformall/douyin/payv2/config/TtPayConfig.java
  2. +89
    -0
      mallinkService/src/main/java/com/iformall/douyin/payv2/util/PemUtils.java

+ 1
- 1
mallinkService/src/main/java/com/iformall/douyin/payv2/config/TtPayConfig.java View File

@@ -1,10 +1,10 @@
package com.iformall.douyin.payv2.config;

import com.github.binarywang.wxpay.v3.util.PemUtils;
import com.iformall.douyin.pay.exception.TtPayException;
import com.iformall.douyin.payv2.auth.CertificatesVerifier;
import com.iformall.douyin.payv2.auth.TtPayValidator;
import com.iformall.douyin.payv2.auth.Verifier;
import com.iformall.douyin.payv2.util.PemUtils;
import com.iformall.douyin.payv2.util.TtPayV2HttpClientBuilder;
import jodd.util.ResourcesUtil;
import lombok.Data;


+ 89
- 0
mallinkService/src/main/java/com/iformall/douyin/payv2/util/PemUtils.java View File

@@ -0,0 +1,89 @@
package com.iformall.douyin.payv2.util;

import me.chanjar.weixin.common.error.WxRuntimeException;
import sun.misc.BASE64Decoder;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyFactory;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.PublicKey;
import java.security.cert.*;
import java.security.spec.InvalidKeySpecException;
import java.security.spec.PKCS8EncodedKeySpec;
import java.security.spec.X509EncodedKeySpec;
import java.util.Base64;

public class PemUtils {

public static PrivateKey loadPrivateKey(InputStream inputStream) {
try {
ByteArrayOutputStream array = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
array.write(buffer, 0, length);
}

String privateKey = array.toString("utf-8")
.replace("-----BEGIN RSA PRIVATE KEY-----", "")
.replace("-----END RSA PRIVATE KEY-----", "")
.replaceAll("\\s+", "");

KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePrivate(
new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey)));
} catch (NoSuchAlgorithmException e) {
throw new WxRuntimeException("当前Java环境不支持RSA", e);
} catch (InvalidKeySpecException e) {
throw new WxRuntimeException("无效的密钥格式");
} catch (IOException e) {
throw new WxRuntimeException("无效的密钥");
}
}

public static X509Certificate loadCertificate(InputStream inputStream) {
try {
CertificateFactory cf = CertificateFactory.getInstance("X509");
X509Certificate cert = (X509Certificate) cf.generateCertificate(inputStream);
cert.checkValidity();
return cert;
} catch (CertificateExpiredException e) {
throw new WxRuntimeException("证书已过期", e);
} catch (CertificateNotYetValidException e) {
throw new WxRuntimeException("证书尚未生效", e);
} catch (CertificateException e) {
throw new WxRuntimeException("无效的证书", e);
}
}

public static PublicKey loadPublicKey(InputStream inputStream) {

try {
ByteArrayOutputStream array = new ByteArrayOutputStream();
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) != -1) {
array.write(buffer, 0, length);
}

String publicKey = array.toString("utf-8")
.replace("-----BEGIN PUBLIC KEY-----", "")
.replace("-----END PUBLIC KEY-----", "")
.replaceAll("\\s+", "");

KeyFactory kf = KeyFactory.getInstance("RSA");
return kf.generatePublic(
new X509EncodedKeySpec(new BASE64Decoder().decodeBuffer(publicKey)));

} catch (NoSuchAlgorithmException e) {
throw new WxRuntimeException("当前Java环境不支持RSA", e);
} catch (InvalidKeySpecException e) {
throw new WxRuntimeException("无效的密钥格式");
} catch (IOException e) {
throw new WxRuntimeException("无效的密钥");
}
}
}

Loading…
Cancel
Save