Skip to content

Commit 0cfcc8d

Browse files
committed
🎨 避免对曾变化的guava方法的依赖,以免对使用不同版本guava的用户造成困惑
1 parent 7cd213d commit 0cfcc8d

File tree

8 files changed

+28
-19
lines changed

8 files changed

+28
-19
lines changed

weixin-java-common/src/main/java/me/chanjar/weixin/common/util/crypto/WxCryptUtil.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import lombok.Data;
66
import me.chanjar.weixin.common.error.WxRuntimeException;
77
import org.apache.commons.codec.binary.Base64;
8+
import org.apache.commons.lang3.StringUtils;
89
import org.w3c.dom.Document;
910
import org.w3c.dom.Element;
1011
import org.xml.sax.InputSource;
@@ -65,7 +66,7 @@ public WxCryptUtil() {
6566
public WxCryptUtil(String token, String encodingAesKey, String appidOrCorpid) {
6667
this.token = token;
6768
this.appidOrCorpid = appidOrCorpid;
68-
this.aesKey = Base64.decodeBase64(CharMatcher.whitespace().removeFrom(encodingAesKey));
69+
this.aesKey = Base64.decodeBase64(StringUtils.remove(encodingAesKey, " "));
6970
}
7071

7172
private static String extractEncryptPart(String xml) {

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/crypto/WxCpCryptUtil.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
package me.chanjar.weixin.cp.util.crypto;
22

3-
import com.google.common.base.CharMatcher;
43
import com.google.common.io.BaseEncoding;
54
import me.chanjar.weixin.common.error.WxErrorException;
65
import me.chanjar.weixin.common.util.crypto.WxCryptUtil;
76
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
7+
import org.apache.commons.lang3.StringUtils;
88
import sun.security.util.DerInputStream;
99
import sun.security.util.DerValue;
1010

@@ -28,7 +28,7 @@ public WxCpCryptUtil(WxCpConfigStorage wxCpConfigStorage) {
2828

2929
this.token = token;
3030
this.appidOrCorpid = corpId;
31-
this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey));
31+
this.aesKey = Base64.getDecoder().decode(StringUtils.remove(encodingAesKey, " "));
3232
}
3333

3434
/**

weixin-java-cp/src/main/java/me/chanjar/weixin/cp/util/crypto/WxCpTpCryptUtil.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
import com.google.common.io.BaseEncoding;
55
import me.chanjar.weixin.common.util.crypto.WxCryptUtil;
66
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
7+
import org.apache.commons.lang3.StringUtils;
8+
9+
import java.util.Base64;
710

811
/**
912
* @author someone
@@ -24,7 +27,7 @@ public WxCpTpCryptUtil(WxCpTpConfigStorage wxCpTpConfigStorage) {
2427

2528
this.token = token;
2629
this.appidOrCorpid = corpId;
27-
this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey));
30+
this.aesKey = Base64.getDecoder().decode(StringUtils.remove(encodingAesKey, " "));
2831
}
2932

3033

weixin-java-cp/src/test/java/me/chanjar/weixin/cp/util/crypto/WxCpCryptUtilTest.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
package me.chanjar.weixin.cp.util.crypto;
22

3-
import com.google.common.base.CharMatcher;
4-
import com.google.common.io.BaseEncoding;
53
import org.apache.commons.codec.binary.Base64;
4+
import org.apache.commons.lang3.StringUtils;
65
import org.testng.annotations.Test;
76

87
import static org.testng.Assert.assertEquals;
@@ -16,8 +15,8 @@ public class WxCpCryptUtilTest {
1615
public void test() {
1716
String encodingAesKey = "jWmYm7qr5nMoAUwZRjGtBxmz3KA1tkAj3ykkR6q2B2C";
1817
final byte[] commonsCodec = Base64.decodeBase64(encodingAesKey + "=");
19-
final byte[] guava = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey));
20-
final byte[] guava1 = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey + "="));
18+
final byte[] guava = java.util.Base64.getDecoder().decode(StringUtils.remove(encodingAesKey, " "));
19+
final byte[] guava1 = java.util.Base64.getDecoder().decode(StringUtils.remove(encodingAesKey + "=", " "));
2120
assertEquals(commonsCodec, guava);
2221
assertEquals(guava1, guava);
2322
}

weixin-java-miniapp/src/main/java/cn/binarywang/wx/miniapp/util/crypt/WxMaCryptUtils.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import com.google.common.io.BaseEncoding;
1515
import me.chanjar.weixin.common.error.WxRuntimeException;
1616
import org.apache.commons.codec.binary.Base64;
17+
import org.apache.commons.lang3.StringUtils;
1718
import org.bouncycastle.jce.provider.BouncyCastleProvider;
1819

1920
import cn.binarywang.wx.miniapp.config.WxMaConfig;
@@ -28,7 +29,7 @@ public class WxMaCryptUtils extends me.chanjar.weixin.common.util.crypto.WxCrypt
2829
public WxMaCryptUtils(WxMaConfig config) {
2930
this.appidOrCorpid = config.getAppid();
3031
this.token = config.getToken();
31-
this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(config.getAesKey()));
32+
this.aesKey = java.util.Base64.getDecoder().decode(StringUtils.remove(config.getAesKey(), " "));
3233
}
3334

3435
/**

weixin-java-mp/src/main/java/me/chanjar/weixin/mp/util/crypto/WxMpCryptUtil.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@
2020
import com.google.common.base.CharMatcher;
2121
import com.google.common.io.BaseEncoding;
2222
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
23+
import org.apache.commons.lang3.StringUtils;
24+
25+
import java.util.Base64;
2326

2427
public class WxMpCryptUtil extends me.chanjar.weixin.common.util.crypto.WxCryptUtil {
2528

@@ -40,7 +43,7 @@ public WxMpCryptUtil(WxMpConfigStorage wxMpConfigStorage) {
4043

4144
this.token = token;
4245
this.appidOrCorpid = appId;
43-
this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey));
46+
this.aesKey = Base64.getDecoder().decode(StringUtils.remove(encodingAesKey, " "));
4447
}
4548

4649
}

weixin-java-open/src/main/java/me/chanjar/weixin/open/util/WxOpenCryptUtil.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,9 @@
33
import com.google.common.base.CharMatcher;
44
import com.google.common.io.BaseEncoding;
55
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
6+
import org.apache.commons.lang3.StringUtils;
7+
8+
import java.util.Base64;
69

710
/**
811
* @author <a href="https://github.com/007gzs">007</a>
@@ -25,6 +28,6 @@ public WxOpenCryptUtil(WxOpenConfigStorage wxOpenConfigStorage) {
2528

2629
this.token = token;
2730
this.appidOrCorpid = appId;
28-
this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey));
31+
this.aesKey = Base64.getDecoder().decode(StringUtils.remove(encodingAesKey, " "));
2932
}
3033
}

weixin-java-pay/src/main/java/com/github/binarywang/wxpay/v3/util/AesUtils.java

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
package com.github.binarywang.wxpay.v3.util;
22

3-
import com.google.common.base.CharMatcher;
4-
import com.google.common.io.BaseEncoding;
53
import org.apache.commons.lang3.StringUtils;
64

75
import javax.crypto.Cipher;
@@ -10,6 +8,7 @@
108
import javax.crypto.spec.GCMParameterSpec;
119
import javax.crypto.spec.SecretKeySpec;
1210
import java.io.IOException;
11+
import java.nio.charset.StandardCharsets;
1312
import java.security.GeneralSecurityException;
1413
import java.security.InvalidAlgorithmParameterException;
1514
import java.security.InvalidKeyException;
@@ -58,7 +57,7 @@ public static byte[] decryptToByte(byte[] associatedData, byte[] nonce, byte[] c
5857
}
5958

6059
public String decryptToString(byte[] associatedData, byte[] nonce, String ciphertext)
61-
throws GeneralSecurityException, IOException {
60+
throws GeneralSecurityException, IOException {
6261
try {
6362
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
6463

@@ -68,15 +67,15 @@ public String decryptToString(byte[] associatedData, byte[] nonce, String cipher
6867
cipher.init(Cipher.DECRYPT_MODE, key, spec);
6968
cipher.updateAAD(associatedData);
7069

71-
return new String(cipher.doFinal(BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(ciphertext))), "utf-8");
70+
return new String(cipher.doFinal(Base64.getDecoder().decode(StringUtils.remove(ciphertext, " "))), StandardCharsets.UTF_8);
7271
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
7372
throw new IllegalStateException(e);
7473
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
7574
throw new IllegalArgumentException(e);
7675
}
7776
}
7877

79-
public static String decryptToString(String associatedData, String nonce, String ciphertext,String apiV3Key)
78+
public static String decryptToString(String associatedData, String nonce, String ciphertext, String apiV3Key)
8079
throws GeneralSecurityException, IOException {
8180
try {
8281
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
@@ -87,7 +86,7 @@ public static String decryptToString(String associatedData, String nonce, String
8786
cipher.init(Cipher.DECRYPT_MODE, key, spec);
8887
cipher.updateAAD(associatedData.getBytes());
8988

90-
return new String(cipher.doFinal(Base64.getDecoder().decode(ciphertext)), "utf-8");
89+
return new String(cipher.doFinal(Base64.getDecoder().decode(ciphertext)), StandardCharsets.UTF_8);
9190
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
9291
throw new IllegalStateException(e);
9392
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
@@ -116,9 +115,9 @@ public static String createSign(Map<String, String> map, String mchKey) {
116115
public static String HMACSHA256(String data, String key) {
117116
try {
118117
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
119-
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
118+
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
120119
sha256_HMAC.init(secret_key);
121-
byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
120+
byte[] array = sha256_HMAC.doFinal(data.getBytes(StandardCharsets.UTF_8));
122121
StringBuilder sb = new StringBuilder();
123122
for (byte item : array) {
124123
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));

0 commit comments

Comments
 (0)