Skip to content

Commit eaa5911

Browse files
committed
增加webhooks 签名验证示例
1 parent f61c264 commit eaa5911

10 files changed

Lines changed: 90 additions & 1 deletion

File tree

example/SimpleExample/.classpath

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@
44
<classpathentry kind="lib" path="lib/gson-2.2.4.jar"/>
55
<classpathentry kind="lib" path="lib/pingpp-java-2.0.5.jar"/>
66
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
7+
<classpathentry kind="lib" path="lib/commons-codec-1.10.jar"/>
78
<classpathentry kind="output" path="bin"/>
89
</classpath>

example/SimpleExample/bin/charge

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"id":"evt_04qN8cXQvIhssduhS4hpqd9p","created":1427555016,"livemode":false,"type":"account.summary.available","data":{"object":{"acct_id":"acct_0eHSiDyzv9G09ejT","object":"account_daily_summary","acct_display_name":"xx公司","created":1425139260,"summary_from":1425052800,"summary_to":1425139199,"charges_amount":1000,"charges_count":100}},"object":"event","pending_webhooks":2,"request":null,"scope":"acct_1234567890123456","acct_id":"acct_1234567890123456"}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzs8SiPoFQT9K0lWa6WSx
3+
0d6UnA391KM2aFwijY0AK7r+MiAe07ivenopzFL3dqIRhQjuP7d30V85kWydN5UZ
4+
cm/tZgm4K+8wttb988hOrzSjtPOMghHK+bnDwE8FIB+ZbHAZCEVhNfE6i9kLGbHH
5+
Q617+mxUTJ3yEZG9CIgke475o2Blxy4UMsRYjo2gl5aanzmOmoZcbiC/R5hXSQUH
6+
XV9/VzA7U//DIm8Xn7rerd1n8+KWCg4hrIIu/A0FKm8zyS4QwAwQO2wdzGB0h15t
7+
uFLhjVz1W5ZPXjmCRLzTUoAvH12C6YFStvS5kjPcA66P1nSKk5o3koSxOumOs0iC
8+
EQIDAQAB
9+
-----END PUBLIC KEY-----

example/SimpleExample/bin/sign

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PcU0SMJhbPObiIVinNnalZOjI02koWozxLrxa3WQW3rK/n7I+EuVGuXvhsq2MIfUaNiHZDgRFYybGtKr1uuFzEXjA4PwmnDHfWgwRPdjgseoU0eke6ZqGpklBRVTbF6PUy6/vAqur4xb7h1wpdrteUpCPafzDmVPsQLicdojJ/TF9ACjQW8gTNiS6tE9gL5hxy0RJ3/okRJo6dz2pvJBWkjCrgp/r98z/LQijA1o//atZrH63+DcL/GwEOgaymqbodzusXF+g6WMJ/GTJgjdPRHvpO9UAAUKkOQqvwthJvsXIH/L1xqvy+tFpo2J0Ptwg85bowKoyy1qC5ak3sqWqw==
278 KB
Binary file not shown.

example/SimpleExample/src/charge

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
{"id":"evt_04qN8cXQvIhssduhS4hpqd9p","created":1427555016,"livemode":false,"type":"account.summary.available","data":{"object":{"acct_id":"acct_0eHSiDyzv9G09ejT","object":"account_daily_summary","acct_display_name":"xx公司","created":1425139260,"summary_from":1425052800,"summary_to":1425139199,"charges_amount":1000,"charges_count":100}},"object":"event","pending_webhooks":2,"request":null,"scope":"acct_1234567890123456","acct_id":"acct_1234567890123456"}

example/SimpleExample/src/example/ChargeExample.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ public void retrieve(String id) {
8383
//Expand app
8484
Charge charge = Charge.retrieve(id, param);
8585
if (charge.getApp() instanceof App) {
86-
App app = (App) charge.getApp();
86+
//App app = (App) charge.getApp();
8787
// System.out.println("App Object ,appId = " + app.getId());
8888
} else {
8989
// System.out.println("String ,appId = " + charge.getApp());
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package example;
2+
3+
import java.io.FileInputStream;
4+
import java.security.InvalidKeyException;
5+
import java.security.KeyFactory;
6+
import java.security.NoSuchAlgorithmException;
7+
import java.security.PublicKey;
8+
import java.security.Signature;
9+
import java.security.SignatureException;
10+
import java.security.spec.X509EncodedKeySpec;
11+
12+
import org.apache.commons.codec.binary.Base64;
13+
14+
/**
15+
* Created by sunkai on 15/5/19. webhooks 验证签名示例
16+
*/
17+
public class WebHooksVerifyExample {
18+
private static String filePath = "src/my-server.pub";
19+
private static String eventPath = "src/charge";
20+
private static String signPath = "src/sign";
21+
22+
public static void main(String[] args) throws Exception {
23+
24+
boolean result = verifyData(getByteFromFile(eventPath, false), getByteFromFile(signPath, true), getPubKey());
25+
System.out.println("验签结果:"+result);
26+
}
27+
28+
public static byte[] getByteFromFile(String file, boolean base64) throws Exception {
29+
FileInputStream in = new FileInputStream(file);
30+
byte[] fileBytes = new byte[in.available()];
31+
in.read(fileBytes);
32+
in.close();
33+
String pubKey = new String(fileBytes, "UTF-8");
34+
if (base64) {
35+
fileBytes = Base64.decodeBase64(pubKey);
36+
}
37+
return fileBytes;
38+
}
39+
40+
public static PublicKey getPubKey() throws Exception {
41+
// read key bytes
42+
FileInputStream in = new FileInputStream(filePath);
43+
byte[] keyBytes = new byte[in.available()];
44+
in.read(keyBytes);
45+
in.close();
46+
47+
String pubKey = new String(keyBytes, "UTF-8");
48+
pubKey = pubKey.replaceAll("(-+BEGIN PUBLIC KEY-+\\r?\\n|-+END PUBLIC KEY-+\\r?\\n?)", "");
49+
50+
keyBytes = Base64.decodeBase64(pubKey);
51+
52+
// generate public key
53+
X509EncodedKeySpec spec = new X509EncodedKeySpec(keyBytes);
54+
KeyFactory keyFactory = KeyFactory.getInstance("RSA");
55+
PublicKey publicKey = keyFactory.generatePublic(spec);
56+
return publicKey;
57+
}
58+
59+
public static boolean verifyData(byte[] data, byte[] sigBytes, PublicKey publicKey) throws NoSuchAlgorithmException, InvalidKeyException, SignatureException {
60+
Signature signature = Signature.getInstance("SHA256withRSA");
61+
signature.initVerify(publicKey);
62+
signature.update(data);
63+
return signature.verify(sigBytes);
64+
}
65+
66+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
-----BEGIN PUBLIC KEY-----
2+
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAzs8SiPoFQT9K0lWa6WSx
3+
0d6UnA391KM2aFwijY0AK7r+MiAe07ivenopzFL3dqIRhQjuP7d30V85kWydN5UZ
4+
cm/tZgm4K+8wttb988hOrzSjtPOMghHK+bnDwE8FIB+ZbHAZCEVhNfE6i9kLGbHH
5+
Q617+mxUTJ3yEZG9CIgke475o2Blxy4UMsRYjo2gl5aanzmOmoZcbiC/R5hXSQUH
6+
XV9/VzA7U//DIm8Xn7rerd1n8+KWCg4hrIIu/A0FKm8zyS4QwAwQO2wdzGB0h15t
7+
uFLhjVz1W5ZPXjmCRLzTUoAvH12C6YFStvS5kjPcA66P1nSKk5o3koSxOumOs0iC
8+
EQIDAQAB
9+
-----END PUBLIC KEY-----

example/SimpleExample/src/sign

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
PcU0SMJhbPObiIVinNnalZOjI02koWozxLrxa3WQW3rK/n7I+EuVGuXvhsq2MIfUaNiHZDgRFYybGtKr1uuFzEXjA4PwmnDHfWgwRPdjgseoU0eke6ZqGpklBRVTbF6PUy6/vAqur4xb7h1wpdrteUpCPafzDmVPsQLicdojJ/TF9ACjQW8gTNiS6tE9gL5hxy0RJ3/okRJo6dz2pvJBWkjCrgp/r98z/LQijA1o//atZrH63+DcL/GwEOgaymqbodzusXF+g6WMJ/GTJgjdPRHvpO9UAAUKkOQqvwthJvsXIH/L1xqvy+tFpo2J0Ptwg85bowKoyy1qC5ak3sqWqw==

0 commit comments

Comments
 (0)