-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Loading status checks…
new: #3402 【微信支付】支持配置微信支付公钥
1 parent
c6a38ae
commit 46dab3a
Showing
5 changed files
with
267 additions
and
19 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
39 changes: 39 additions & 0 deletions
39
...java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/PublicCertificateVerifier.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 |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.github.binarywang.wxpay.v3.auth; | ||
|
||
import java.security.*; | ||
import java.security.cert.X509Certificate; | ||
import java.util.Base64; | ||
import me.chanjar.weixin.common.error.WxRuntimeException; | ||
|
||
public class PublicCertificateVerifier implements Verifier{ | ||
|
||
private final PublicKey publicKey; | ||
|
||
private final X509PublicCertificate publicCertificate; | ||
|
||
public PublicCertificateVerifier(PublicKey publicKey, String publicId) { | ||
this.publicKey = publicKey; | ||
this.publicCertificate = new X509PublicCertificate(publicKey, publicId); | ||
} | ||
|
||
@Override | ||
public boolean verify(String serialNumber, byte[] message, String signature) { | ||
try { | ||
Signature sign = Signature.getInstance("SHA256withRSA"); | ||
sign.initVerify(publicKey); | ||
sign.update(message); | ||
return sign.verify(Base64.getDecoder().decode(signature)); | ||
} catch (NoSuchAlgorithmException e) { | ||
throw new WxRuntimeException("当前Java环境不支持SHA256withRSA", e); | ||
} catch (SignatureException e) { | ||
throw new WxRuntimeException("签名验证过程发生了错误", e); | ||
} catch (InvalidKeyException e) { | ||
throw new WxRuntimeException("无效的证书", e); | ||
} | ||
} | ||
|
||
@Override | ||
public X509Certificate getValidCertificate() { | ||
return this.publicCertificate; | ||
} | ||
} |
150 changes: 150 additions & 0 deletions
150
weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/auth/X509PublicCertificate.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 |
---|---|---|
@@ -0,0 +1,150 @@ | ||
package com.github.binarywang.wxpay.v3.auth; | ||
|
||
import java.math.BigInteger; | ||
import java.security.*; | ||
import java.security.cert.*; | ||
import java.util.Collections; | ||
import java.util.Date; | ||
import java.util.Set; | ||
|
||
public class X509PublicCertificate extends X509Certificate { | ||
|
||
private final PublicKey publicKey; | ||
|
||
private final String publicId; | ||
|
||
public X509PublicCertificate(PublicKey publicKey, String publicId) { | ||
this.publicKey = publicKey; | ||
this.publicId = publicId; | ||
} | ||
|
||
@Override | ||
public PublicKey getPublicKey() { | ||
return this.publicKey; | ||
} | ||
|
||
@Override | ||
public BigInteger getSerialNumber() { | ||
return new BigInteger(publicId.replace("PUB_KEY_ID_", ""), 16); | ||
} | ||
|
||
@Override | ||
public void checkValidity() throws CertificateExpiredException, CertificateNotYetValidException { | ||
} | ||
|
||
@Override | ||
public void checkValidity(Date date) throws CertificateExpiredException, CertificateNotYetValidException { | ||
|
||
} | ||
|
||
@Override | ||
public int getVersion() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public Principal getIssuerDN() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Principal getSubjectDN() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Date getNotBefore() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Date getNotAfter() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public byte[] getTBSCertificate() throws CertificateEncodingException { | ||
return new byte[0]; | ||
} | ||
|
||
@Override | ||
public byte[] getSignature() { | ||
return new byte[0]; | ||
} | ||
|
||
@Override | ||
public String getSigAlgName() { | ||
return ""; | ||
} | ||
|
||
@Override | ||
public String getSigAlgOID() { | ||
return ""; | ||
} | ||
|
||
@Override | ||
public byte[] getSigAlgParams() { | ||
return new byte[0]; | ||
} | ||
|
||
@Override | ||
public boolean[] getIssuerUniqueID() { | ||
return new boolean[0]; | ||
} | ||
|
||
@Override | ||
public boolean[] getSubjectUniqueID() { | ||
return new boolean[0]; | ||
} | ||
|
||
@Override | ||
public boolean[] getKeyUsage() { | ||
return new boolean[0]; | ||
} | ||
|
||
@Override | ||
public int getBasicConstraints() { | ||
return 0; | ||
} | ||
|
||
@Override | ||
public byte[] getEncoded() throws CertificateEncodingException { | ||
return new byte[0]; | ||
} | ||
|
||
@Override | ||
public void verify(PublicKey key) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException { | ||
|
||
} | ||
|
||
@Override | ||
public void verify(PublicKey key, String sigProvider) throws CertificateException, NoSuchAlgorithmException, InvalidKeyException, NoSuchProviderException, SignatureException { | ||
|
||
} | ||
|
||
@Override | ||
public String toString() { | ||
return ""; | ||
} | ||
|
||
|
||
@Override | ||
public boolean hasUnsupportedCriticalExtension() { | ||
return false; | ||
} | ||
|
||
@Override | ||
public Set<String> getCriticalExtensionOIDs() { | ||
return Collections.emptySet(); | ||
} | ||
|
||
@Override | ||
public Set<String> getNonCriticalExtensionOIDs() { | ||
return Collections.emptySet(); | ||
} | ||
|
||
@Override | ||
public byte[] getExtensionValue(String oid) { | ||
return new byte[0]; | ||
} | ||
} |
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