forked from wechatpay-apiv3/wechatpay-apache-httpclient
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dc9c846
commit 71fb295
Showing
17 changed files
with
230 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -91,8 +91,3 @@ publishing { | |
signing { | ||
sign publishing.publications.maven | ||
} | ||
|
||
|
||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1 @@ | ||
rootProject.name = 'wechatpay-apache-httpclient' | ||
|
27 changes: 15 additions & 12 deletions
27
src/main/java/com/wechat/pay/contrib/apache/httpclient/Credentials.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
77 changes: 40 additions & 37 deletions
77
src/main/java/com/wechat/pay/contrib/apache/httpclient/auth/PrivateKeySigner.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
|
||
} |
39 changes: 21 additions & 18 deletions
39
src/main/java/com/wechat/pay/contrib/apache/httpclient/auth/Signer.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
97 changes: 50 additions & 47 deletions
97
src/main/java/com/wechat/pay/contrib/apache/httpclient/util/AesUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} | ||
} |
Oops, something went wrong.