🎨 优化代码

This commit is contained in:
Binary Wang
2020-09-26 16:15:56 +08:00
parent 5ecfaf7cd0
commit 1d7344309a
67 changed files with 277 additions and 234 deletions

View File

@@ -10,6 +10,7 @@ import com.github.binarywang.wxpay.v3.util.PemUtils;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxRuntimeException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.CloseableHttpClient;
@@ -94,7 +95,7 @@ public class AutoUpdateCertificatesVerifier implements Verifier {
autoUpdateCert();
instant = Instant.now();
} catch (IOException | GeneralSecurityException e) {
throw new RuntimeException(e);
throw new WxRuntimeException(e);
}
}

View File

@@ -1,5 +1,7 @@
package com.github.binarywang.wxpay.v3.auth;
import me.chanjar.weixin.common.error.WxRuntimeException;
import java.math.BigInteger;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
@@ -30,11 +32,11 @@ public class CertificatesVerifier implements Verifier {
sign.update(message);
return sign.verify(Base64.getDecoder().decode(signature));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("当前Java环境不支持SHA256withRSA", e);
throw new WxRuntimeException("当前Java环境不支持SHA256withRSA", e);
} catch (SignatureException e) {
throw new RuntimeException("签名验证过程发生了错误", e);
throw new WxRuntimeException("签名验证过程发生了错误", e);
} catch (InvalidKeyException e) {
throw new RuntimeException("无效的证书", e);
throw new WxRuntimeException("无效的证书", e);
}
}

View File

@@ -1,5 +1,7 @@
package com.github.binarywang.wxpay.v3.auth;
import me.chanjar.weixin.common.error.WxRuntimeException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
@@ -27,11 +29,11 @@ public class PrivateKeySigner implements Signer {
return new SignatureResult(
Base64.getEncoder().encodeToString(sign.sign()), certificateSerialNumber);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("当前Java环境不支持SHA256withRSA", e);
throw new WxRuntimeException("当前Java环境不支持SHA256withRSA", e);
} catch (SignatureException e) {
throw new RuntimeException("签名计算失败", e);
throw new WxRuntimeException("签名计算失败", e);
} catch (InvalidKeyException e) {
throw new RuntimeException("无效的私钥", e);
throw new WxRuntimeException("无效的私钥", e);
}
}
}

View File

@@ -1,5 +1,7 @@
package com.github.binarywang.wxpay.v3.util;
import me.chanjar.weixin.common.error.WxRuntimeException;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
@@ -35,11 +37,11 @@ public class PemUtils {
return kf.generatePrivate(
new PKCS8EncodedKeySpec(Base64.getDecoder().decode(privateKey)));
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("当前Java环境不支持RSA", e);
throw new WxRuntimeException("当前Java环境不支持RSA", e);
} catch (InvalidKeySpecException e) {
throw new RuntimeException("无效的密钥格式");
throw new WxRuntimeException("无效的密钥格式");
} catch (IOException e) {
throw new RuntimeException("无效的密钥");
throw new WxRuntimeException("无效的密钥");
}
}
@@ -50,11 +52,11 @@ public class PemUtils {
cert.checkValidity();
return cert;
} catch (CertificateExpiredException e) {
throw new RuntimeException("证书已过期", e);
throw new WxRuntimeException("证书已过期", e);
} catch (CertificateNotYetValidException e) {
throw new RuntimeException("证书尚未生效", e);
throw new WxRuntimeException("证书尚未生效", e);
} catch (CertificateException e) {
throw new RuntimeException("无效的证书", e);
throw new WxRuntimeException("无效的证书", e);
}
}
}

View File

@@ -2,6 +2,7 @@ package com.github.binarywang.wxpay.v3.util;
import com.github.binarywang.wxpay.exception.WxPayException;
import com.github.binarywang.wxpay.v3.SpecEncrypt;
import me.chanjar.weixin.common.error.WxRuntimeException;
import javax.crypto.BadPaddingException;
import javax.crypto.Cipher;
@@ -70,7 +71,7 @@ public class RsaCryptoUtil {
byte[] ciphertext = cipher.doFinal(data);
return Base64.getEncoder().encodeToString(ciphertext);
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new RuntimeException("当前Java环境不支持RSA v1.5/OAEP", e);
throw new WxRuntimeException("当前Java环境不支持RSA v1.5/OAEP", e);
} catch (InvalidKeyException e) {
throw new IllegalArgumentException("无效的证书", e);
} catch (IllegalBlockSizeException | BadPaddingException e) {
@@ -87,7 +88,7 @@ public class RsaCryptoUtil {
byte[] data = Base64.getDecoder().decode(ciphertext);
return new String(cipher.doFinal(data), StandardCharsets.UTF_8);
} catch (NoSuchPaddingException | NoSuchAlgorithmException e) {
throw new RuntimeException("当前Java环境不支持RSA v1.5/OAEP", e);
throw new WxRuntimeException("当前Java环境不支持RSA v1.5/OAEP", e);
} catch (InvalidKeyException e) {
throw new IllegalArgumentException("无效的私钥", e);
} catch (BadPaddingException | IllegalBlockSizeException e) {

View File

@@ -1,12 +1,14 @@
package com.github.binarywang.wxpay.v3.util;
import me.chanjar.weixin.common.error.WxRuntimeException;
import java.security.*;
import java.util.Base64;
import java.util.Random;
public class SignUtils {
public static String sign(String string, PrivateKey privateKey){
public static String sign(String string, PrivateKey privateKey) {
try {
Signature sign = Signature.getInstance("SHA256withRSA");
sign.initSign(privateKey);
@@ -14,23 +16,24 @@ public class SignUtils {
return Base64.getEncoder().encodeToString(sign.sign());
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("当前Java环境不支持SHA256withRSA", e);
throw new WxRuntimeException("当前Java环境不支持SHA256withRSA", e);
} catch (SignatureException e) {
throw new RuntimeException("签名计算失败", e);
throw new WxRuntimeException("签名计算失败", e);
} catch (InvalidKeyException e) {
throw new RuntimeException("无效的私钥", e);
throw new WxRuntimeException("无效的私钥", e);
}
}
/**
* 随机生成32位字符串.
*/
public static String genRandomStr(){
public static String genRandomStr() {
return genRandomStr(32);
}
/**
* 生成随机字符串
*
* @param length 字符串长度
* @return
*/