mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-05-02 20:02:37 +08:00
🎨 避免对曾变化的guava方法的依赖,以免对使用不同版本guava的用户造成困惑
This commit is contained in:
parent
7cd213da65
commit
0cfcc8d4a0
@ -5,6 +5,7 @@ import lombok.AllArgsConstructor;
|
||||
import lombok.Data;
|
||||
import me.chanjar.weixin.common.error.WxRuntimeException;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.xml.sax.InputSource;
|
||||
@ -65,7 +66,7 @@ public class WxCryptUtil {
|
||||
public WxCryptUtil(String token, String encodingAesKey, String appidOrCorpid) {
|
||||
this.token = token;
|
||||
this.appidOrCorpid = appidOrCorpid;
|
||||
this.aesKey = Base64.decodeBase64(CharMatcher.whitespace().removeFrom(encodingAesKey));
|
||||
this.aesKey = Base64.decodeBase64(StringUtils.remove(encodingAesKey, " "));
|
||||
}
|
||||
|
||||
private static String extractEncryptPart(String xml) {
|
||||
|
@ -1,10 +1,10 @@
|
||||
package me.chanjar.weixin.cp.util.crypto;
|
||||
|
||||
import com.google.common.base.CharMatcher;
|
||||
import com.google.common.io.BaseEncoding;
|
||||
import me.chanjar.weixin.common.error.WxErrorException;
|
||||
import me.chanjar.weixin.common.util.crypto.WxCryptUtil;
|
||||
import me.chanjar.weixin.cp.config.WxCpConfigStorage;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import sun.security.util.DerInputStream;
|
||||
import sun.security.util.DerValue;
|
||||
|
||||
@ -28,7 +28,7 @@ public class WxCpCryptUtil extends WxCryptUtil {
|
||||
|
||||
this.token = token;
|
||||
this.appidOrCorpid = corpId;
|
||||
this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey));
|
||||
this.aesKey = Base64.getDecoder().decode(StringUtils.remove(encodingAesKey, " "));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -4,6 +4,9 @@ import com.google.common.base.CharMatcher;
|
||||
import com.google.common.io.BaseEncoding;
|
||||
import me.chanjar.weixin.common.util.crypto.WxCryptUtil;
|
||||
import me.chanjar.weixin.cp.config.WxCpTpConfigStorage;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* @author someone
|
||||
@ -24,7 +27,7 @@ public class WxCpTpCryptUtil extends WxCryptUtil {
|
||||
|
||||
this.token = token;
|
||||
this.appidOrCorpid = corpId;
|
||||
this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey));
|
||||
this.aesKey = Base64.getDecoder().decode(StringUtils.remove(encodingAesKey, " "));
|
||||
}
|
||||
|
||||
|
||||
|
@ -1,8 +1,7 @@
|
||||
package me.chanjar.weixin.cp.util.crypto;
|
||||
|
||||
import com.google.common.base.CharMatcher;
|
||||
import com.google.common.io.BaseEncoding;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.testng.annotations.Test;
|
||||
|
||||
import static org.testng.Assert.assertEquals;
|
||||
@ -16,8 +15,8 @@ public class WxCpCryptUtilTest {
|
||||
public void test() {
|
||||
String encodingAesKey = "jWmYm7qr5nMoAUwZRjGtBxmz3KA1tkAj3ykkR6q2B2C";
|
||||
final byte[] commonsCodec = Base64.decodeBase64(encodingAesKey + "=");
|
||||
final byte[] guava = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey));
|
||||
final byte[] guava1 = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey + "="));
|
||||
final byte[] guava = java.util.Base64.getDecoder().decode(StringUtils.remove(encodingAesKey, " "));
|
||||
final byte[] guava1 = java.util.Base64.getDecoder().decode(StringUtils.remove(encodingAesKey + "=", " "));
|
||||
assertEquals(commonsCodec, guava);
|
||||
assertEquals(guava1, guava);
|
||||
}
|
||||
|
@ -14,6 +14,7 @@ import com.google.common.base.CharMatcher;
|
||||
import com.google.common.io.BaseEncoding;
|
||||
import me.chanjar.weixin.common.error.WxRuntimeException;
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
import org.bouncycastle.jce.provider.BouncyCastleProvider;
|
||||
|
||||
import cn.binarywang.wx.miniapp.config.WxMaConfig;
|
||||
@ -28,7 +29,7 @@ public class WxMaCryptUtils extends me.chanjar.weixin.common.util.crypto.WxCrypt
|
||||
public WxMaCryptUtils(WxMaConfig config) {
|
||||
this.appidOrCorpid = config.getAppid();
|
||||
this.token = config.getToken();
|
||||
this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(config.getAesKey()));
|
||||
this.aesKey = java.util.Base64.getDecoder().decode(StringUtils.remove(config.getAesKey(), " "));
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -20,6 +20,9 @@ package me.chanjar.weixin.mp.util.crypto;
|
||||
import com.google.common.base.CharMatcher;
|
||||
import com.google.common.io.BaseEncoding;
|
||||
import me.chanjar.weixin.mp.config.WxMpConfigStorage;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Base64;
|
||||
|
||||
public class WxMpCryptUtil extends me.chanjar.weixin.common.util.crypto.WxCryptUtil {
|
||||
|
||||
@ -40,7 +43,7 @@ public class WxMpCryptUtil extends me.chanjar.weixin.common.util.crypto.WxCryptU
|
||||
|
||||
this.token = token;
|
||||
this.appidOrCorpid = appId;
|
||||
this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey));
|
||||
this.aesKey = Base64.getDecoder().decode(StringUtils.remove(encodingAesKey, " "));
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -3,6 +3,9 @@ package me.chanjar.weixin.open.util;
|
||||
import com.google.common.base.CharMatcher;
|
||||
import com.google.common.io.BaseEncoding;
|
||||
import me.chanjar.weixin.open.api.WxOpenConfigStorage;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import java.util.Base64;
|
||||
|
||||
/**
|
||||
* @author <a href="https://github.com/007gzs">007</a>
|
||||
@ -25,6 +28,6 @@ public class WxOpenCryptUtil extends me.chanjar.weixin.common.util.crypto.WxCryp
|
||||
|
||||
this.token = token;
|
||||
this.appidOrCorpid = appId;
|
||||
this.aesKey = BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(encodingAesKey));
|
||||
this.aesKey = Base64.getDecoder().decode(StringUtils.remove(encodingAesKey, " "));
|
||||
}
|
||||
}
|
||||
|
@ -1,7 +1,5 @@
|
||||
package com.github.binarywang.wxpay.v3.util;
|
||||
|
||||
import com.google.common.base.CharMatcher;
|
||||
import com.google.common.io.BaseEncoding;
|
||||
import org.apache.commons.lang3.StringUtils;
|
||||
|
||||
import javax.crypto.Cipher;
|
||||
@ -10,6 +8,7 @@ import javax.crypto.NoSuchPaddingException;
|
||||
import javax.crypto.spec.GCMParameterSpec;
|
||||
import javax.crypto.spec.SecretKeySpec;
|
||||
import java.io.IOException;
|
||||
import java.nio.charset.StandardCharsets;
|
||||
import java.security.GeneralSecurityException;
|
||||
import java.security.InvalidAlgorithmParameterException;
|
||||
import java.security.InvalidKeyException;
|
||||
@ -58,7 +57,7 @@ public class AesUtils {
|
||||
}
|
||||
|
||||
public String decryptToString(byte[] associatedData, byte[] nonce, String ciphertext)
|
||||
throws GeneralSecurityException, IOException {
|
||||
throws GeneralSecurityException, IOException {
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
|
||||
|
||||
@ -68,7 +67,7 @@ public class AesUtils {
|
||||
cipher.init(Cipher.DECRYPT_MODE, key, spec);
|
||||
cipher.updateAAD(associatedData);
|
||||
|
||||
return new String(cipher.doFinal(BaseEncoding.base64().decode(CharMatcher.whitespace().removeFrom(ciphertext))), "utf-8");
|
||||
return new String(cipher.doFinal(Base64.getDecoder().decode(StringUtils.remove(ciphertext, " "))), StandardCharsets.UTF_8);
|
||||
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
|
||||
throw new IllegalStateException(e);
|
||||
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
|
||||
@ -76,7 +75,7 @@ public class AesUtils {
|
||||
}
|
||||
}
|
||||
|
||||
public static String decryptToString(String associatedData, String nonce, String ciphertext,String apiV3Key)
|
||||
public static String decryptToString(String associatedData, String nonce, String ciphertext, String apiV3Key)
|
||||
throws GeneralSecurityException, IOException {
|
||||
try {
|
||||
Cipher cipher = Cipher.getInstance("AES/GCM/NoPadding");
|
||||
@ -87,7 +86,7 @@ public class AesUtils {
|
||||
cipher.init(Cipher.DECRYPT_MODE, key, spec);
|
||||
cipher.updateAAD(associatedData.getBytes());
|
||||
|
||||
return new String(cipher.doFinal(Base64.getDecoder().decode(ciphertext)), "utf-8");
|
||||
return new String(cipher.doFinal(Base64.getDecoder().decode(ciphertext)), StandardCharsets.UTF_8);
|
||||
} catch (NoSuchAlgorithmException | NoSuchPaddingException e) {
|
||||
throw new IllegalStateException(e);
|
||||
} catch (InvalidKeyException | InvalidAlgorithmParameterException e) {
|
||||
@ -116,9 +115,9 @@ public class AesUtils {
|
||||
public static String HMACSHA256(String data, String key) {
|
||||
try {
|
||||
Mac sha256_HMAC = Mac.getInstance("HmacSHA256");
|
||||
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256");
|
||||
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(StandardCharsets.UTF_8), "HmacSHA256");
|
||||
sha256_HMAC.init(secret_key);
|
||||
byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8"));
|
||||
byte[] array = sha256_HMAC.doFinal(data.getBytes(StandardCharsets.UTF_8));
|
||||
StringBuilder sb = new StringBuilder();
|
||||
for (byte item : array) {
|
||||
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3));
|
||||
|
Loading…
Reference in New Issue
Block a user