Skip to content

Commit

Permalink
添加注释 & 文件格式化
Browse files Browse the repository at this point in the history
  • Loading branch information
Eric-Lee-Handyman committed Nov 3, 2021
1 parent dc9c846 commit 71fb295
Show file tree
Hide file tree
Showing 17 changed files with 230 additions and 193 deletions.
4 changes: 3 additions & 1 deletion .editorconfig
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# EditorConfig helps maintain consistent coding styles for multiple developers working on the same project across various editors and IDEs
# https://editorconfig.org
root = true

[*]
Expand Down Expand Up @@ -25,4 +27,4 @@ continuation_indent_size = 8
[{*.markdown,*.md}]
indent_size = 4
tab_width = 4
continuation_indent_size = 8
continuation_indent_size = 8
5 changes: 0 additions & 5 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -91,8 +91,3 @@ publishing {
signing {
sign publishing.publications.maven
}





1 change: 0 additions & 1 deletion settings.gradle
Original file line number Diff line number Diff line change
@@ -1,2 +1 @@
rootProject.name = 'wechatpay-apache-httpclient'

Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package com.wechat.pay.contrib.apache.httpclient;

import java.io.IOException;
import org.apache.http.client.methods.HttpRequestWrapper;

public interface Credentials {

String getSchema();

String getToken(HttpRequestWrapper request) throws IOException;

}
package com.wechat.pay.contrib.apache.httpclient;

import java.io.IOException;
import org.apache.http.client.methods.HttpRequestWrapper;

/**
* @author xy-peng
*/
public interface Credentials {

String getSchema();

String getToken(HttpRequestWrapper request) throws IOException;

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@
import java.io.IOException;
import org.apache.http.client.methods.CloseableHttpResponse;

/**
* @author xy-peng
*/
public interface Validator {

boolean validate(CloseableHttpResponse response) throws IOException;

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.execchain.ClientExecChain;

/**
* @author xy-peng
*/
public class WechatPayHttpClientBuilder extends HttpClientBuilder {

private static final String OS = System.getProperty("os.name") + "/" + System.getProperty("os.version");
Expand Down Expand Up @@ -78,4 +81,4 @@ protected ClientExecChain decorateProtocolExec(final ClientExecChain requestExec
return new SignatureExec(this.credentials, this.validator, requestExecutor);
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
import org.apache.http.entity.mime.HttpMultipartMode;
import org.apache.http.entity.mime.MultipartEntityBuilder;

/**
* @author xy-peng
*/
public class WechatPayUploadHttpPost extends HttpPost {

private final String meta;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@

/**
* 在原有CertificatesVerifier基础上,增加自动更新证书功能
*
* @author xy-peng
*/
public class AutoUpdateCertificatesVerifier implements Verifier {

Expand Down Expand Up @@ -155,4 +157,4 @@ protected List<X509Certificate> deserializeToCerts(byte[] apiV3Key, String body)
return newCertList;
}

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@
import java.util.List;
import java.util.NoSuchElementException;

/**
* @author xy-peng
*/
public class CertificatesVerifier implements Verifier {

protected final HashMap<BigInteger, X509Certificate> certificates = new HashMap<>();
Expand Down Expand Up @@ -58,4 +61,4 @@ public X509Certificate getValidCertificate() {
throw new NoSuchElementException("没有有效的微信支付平台证书");
}

}
}
Original file line number Diff line number Diff line change
@@ -1,37 +1,40 @@
package com.wechat.pay.contrib.apache.httpclient.auth;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.SignatureException;
import java.util.Base64;

public class PrivateKeySigner implements Signer {

protected final String certificateSerialNumber;
protected final PrivateKey privateKey;

public PrivateKeySigner(String serialNumber, PrivateKey privateKey) {
this.certificateSerialNumber = serialNumber;
this.privateKey = privateKey;
}

@Override
public SignatureResult sign(byte[] message) {
try {
Signature sign = Signature.getInstance("SHA256withRSA");
sign.initSign(privateKey);
sign.update(message);
return new SignatureResult(Base64.getEncoder().encodeToString(sign.sign()), certificateSerialNumber);

} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("当前Java环境不支持SHA256withRSA", e);
} catch (SignatureException e) {
throw new RuntimeException("签名计算失败", e);
} catch (InvalidKeyException e) {
throw new RuntimeException("无效的私钥", e);
}
}

}
package com.wechat.pay.contrib.apache.httpclient.auth;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.security.PrivateKey;
import java.security.Signature;
import java.security.SignatureException;
import java.util.Base64;

/**
* @author xy-peng
*/
public class PrivateKeySigner implements Signer {

protected final String certificateSerialNumber;
protected final PrivateKey privateKey;

public PrivateKeySigner(String serialNumber, PrivateKey privateKey) {
this.certificateSerialNumber = serialNumber;
this.privateKey = privateKey;
}

@Override
public SignatureResult sign(byte[] message) {
try {
Signature sign = Signature.getInstance("SHA256withRSA");
sign.initSign(privateKey);
sign.update(message);
return new SignatureResult(Base64.getEncoder().encodeToString(sign.sign()), certificateSerialNumber);

} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("当前Java环境不支持SHA256withRSA", e);
} catch (SignatureException e) {
throw new RuntimeException("签名计算失败", e);
} catch (InvalidKeyException e) {
throw new RuntimeException("无效的私钥", e);
}
}

}
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
package com.wechat.pay.contrib.apache.httpclient.auth;

public interface Signer {

SignatureResult sign(byte[] message);

class SignatureResult {

protected final String sign;
protected final String certificateSerialNumber;

public SignatureResult(String sign, String serialNumber) {
this.sign = sign;
this.certificateSerialNumber = serialNumber;
}
}

}
package com.wechat.pay.contrib.apache.httpclient.auth;

/**
* @author xy-peng
*/
public interface Signer {

SignatureResult sign(byte[] message);

class SignatureResult {

protected final String sign;
protected final String certificateSerialNumber;

public SignatureResult(String sign, String serialNumber) {
this.sign = sign;
this.certificateSerialNumber = serialNumber;
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,13 @@

import java.security.cert.X509Certificate;

/**
* @author xy-peng
*/
public interface Verifier {

boolean verify(String serialNumber, byte[] message, String signature);

X509Certificate getValidCertificate();

}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author xy-peng
*/
public class WechatPay2Credentials implements Credentials {

protected static final Logger log = LoggerFactory.getLogger(WechatPay2Credentials.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,9 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* @author xy-peng
*/
public class WechatPay2Validator implements Validator {

protected static final Logger log = LoggerFactory.getLogger(WechatPay2Validator.class);
Expand Down Expand Up @@ -108,4 +111,4 @@ protected final String getResponseBody(CloseableHttpResponse response) throws IO
return (entity != null && entity.isRepeatable()) ? EntityUtils.toString(entity) : "";
}

}
}
Original file line number Diff line number Diff line change
@@ -1,47 +1,50 @@
package com.wechat.pay.contrib.apache.httpclient.util;

import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;

public class AesUtil {

private static final String TRANSFORMATION = "AES/GCM/NoPadding";

private static final int KEY_LENGTH_BYTE = 32;
private static final int TAG_LENGTH_BIT = 128;

private final byte[] aesKey;

public AesUtil(byte[] key) {
if (key.length != KEY_LENGTH_BYTE) {
throw new IllegalArgumentException("无效的ApiV3Key,长度必须为32个字节");
}
this.aesKey = key;
}

public String decryptToString(byte[] associatedData, byte[] nonce, String ciphertext)
throws GeneralSecurityException {
try {
SecretKeySpec key = new SecretKeySpec(aesKey, "AES");
GCMParameterSpec spec = new GCMParameterSpec(TAG_LENGTH_BIT, nonce);

Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, key, spec);
cipher.updateAAD(associatedData);
return new String(cipher.doFinal(Base64.getDecoder().decode(ciphertext)), StandardCharsets.UTF_8);

} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new IllegalStateException(e);
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
throw new IllegalArgumentException(e);
}
}
}
package com.wechat.pay.contrib.apache.httpclient.util;

import java.nio.charset.StandardCharsets;
import java.security.GeneralSecurityException;
import java.security.InvalidAlgorithmParameterException;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;
import javax.crypto.Cipher;
import javax.crypto.NoSuchPaddingException;
import javax.crypto.spec.GCMParameterSpec;
import javax.crypto.spec.SecretKeySpec;

/**
* @author xy-peng
*/
public class AesUtil {

private static final String TRANSFORMATION = "AES/GCM/NoPadding";

private static final int KEY_LENGTH_BYTE = 32;
private static final int TAG_LENGTH_BIT = 128;

private final byte[] aesKey;

public AesUtil(byte[] key) {
if (key.length != KEY_LENGTH_BYTE) {
throw new IllegalArgumentException("无效的ApiV3Key,长度必须为32个字节");
}
this.aesKey = key;
}

public String decryptToString(byte[] associatedData, byte[] nonce, String ciphertext)
throws GeneralSecurityException {
try {
SecretKeySpec key = new SecretKeySpec(aesKey, "AES");
GCMParameterSpec spec = new GCMParameterSpec(TAG_LENGTH_BIT, nonce);

Cipher cipher = Cipher.getInstance(TRANSFORMATION);
cipher.init(Cipher.DECRYPT_MODE, key, spec);
cipher.updateAAD(associatedData);
return new String(cipher.doFinal(Base64.getDecoder().decode(ciphertext)), StandardCharsets.UTF_8);

} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
throw new IllegalStateException(e);
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
throw new IllegalArgumentException(e);
}
}
}
Loading

0 comments on commit 71fb295

Please sign in to comment.