|
|
|
@ -1,6 +1,8 @@ |
|
|
|
|
package com.hai.common.utils; |
|
|
|
|
|
|
|
|
|
import com.google.common.collect.Maps; |
|
|
|
|
import com.hai.common.pay.util.sdk.WXPayConstants; |
|
|
|
|
import com.hai.common.pay.util.sdk.WXPayXmlUtil; |
|
|
|
|
import com.thoughtworks.xstream.XStream; |
|
|
|
|
import com.thoughtworks.xstream.io.naming.NoNameCoder; |
|
|
|
|
import com.thoughtworks.xstream.io.xml.XppDriver; |
|
|
|
@ -8,6 +10,12 @@ import net.sf.cglib.beans.BeanMap; |
|
|
|
|
|
|
|
|
|
import javax.crypto.Mac; |
|
|
|
|
import javax.crypto.spec.SecretKeySpec; |
|
|
|
|
import javax.xml.transform.OutputKeys; |
|
|
|
|
import javax.xml.transform.Transformer; |
|
|
|
|
import javax.xml.transform.TransformerFactory; |
|
|
|
|
import javax.xml.transform.dom.DOMSource; |
|
|
|
|
import javax.xml.transform.stream.StreamResult; |
|
|
|
|
import java.io.StringWriter; |
|
|
|
|
import java.security.MessageDigest; |
|
|
|
|
import java.security.SecureRandom; |
|
|
|
|
import java.util.*; |
|
|
|
@ -18,6 +26,7 @@ public class WxUtils { |
|
|
|
|
|
|
|
|
|
private static final String SYMBOLS = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 生成签名 |
|
|
|
|
* |
|
|
|
@ -62,7 +71,7 @@ public class WxUtils { |
|
|
|
|
|
|
|
|
|
// 拼接key
|
|
|
|
|
sb.append("key=").append(mchKey); |
|
|
|
|
// MD5加密
|
|
|
|
|
// HMACSHA256加密
|
|
|
|
|
String sign = HMACSHA256(sb.toString(), mchKey).toUpperCase(); |
|
|
|
|
return sign; |
|
|
|
|
} |
|
|
|
@ -133,17 +142,15 @@ public class WxUtils { |
|
|
|
|
* @throws Exception |
|
|
|
|
*/ |
|
|
|
|
public static String HMACSHA256(String data, String key) throws Exception { |
|
|
|
|
String hash = ""; |
|
|
|
|
try { |
|
|
|
|
Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); |
|
|
|
|
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes(), "HmacSHA256"); |
|
|
|
|
sha256_HMAC.init(secret_key); |
|
|
|
|
byte[] bytes = sha256_HMAC.doFinal(data.getBytes()); |
|
|
|
|
hash = byteArrayToHexString(bytes); |
|
|
|
|
} catch (Exception e) { |
|
|
|
|
System.out.println("Error HmacSHA256 ===========" + e.getMessage()); |
|
|
|
|
} |
|
|
|
|
return hash.toUpperCase(); |
|
|
|
|
Mac sha256_HMAC = Mac.getInstance("HmacSHA256"); |
|
|
|
|
SecretKeySpec secret_key = new SecretKeySpec(key.getBytes("UTF-8"), "HmacSHA256"); |
|
|
|
|
sha256_HMAC.init(secret_key); |
|
|
|
|
byte[] array = sha256_HMAC.doFinal(data.getBytes("UTF-8")); |
|
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
|
for (byte item : array) { |
|
|
|
|
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3)); |
|
|
|
|
} |
|
|
|
|
return sb.toString().toUpperCase(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
@ -199,5 +206,90 @@ public class WxUtils { |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 生成签名. 注意,若含有sign_type字段,必须和signType参数保持一致。 |
|
|
|
|
* |
|
|
|
|
* @param data 待签名数据 |
|
|
|
|
* @param key API密钥 |
|
|
|
|
* @param signType 签名方式 |
|
|
|
|
* @return 签名 |
|
|
|
|
*/ |
|
|
|
|
public static String generateSignature(final Map<String, String> data, String key, WXPayConstants.SignType signType) throws Exception { |
|
|
|
|
Set<String> keySet = data.keySet(); |
|
|
|
|
String[] keyArray = keySet.toArray(new String[keySet.size()]); |
|
|
|
|
Arrays.sort(keyArray); |
|
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
|
for (String k : keyArray) { |
|
|
|
|
if (k.equals(WXPayConstants.FIELD_SIGN)) { |
|
|
|
|
continue; |
|
|
|
|
} |
|
|
|
|
if (data.get(k).trim().length() > 0) // 参数值为空,则不参与签名
|
|
|
|
|
sb.append(k).append("=").append(data.get(k).trim()).append("&"); |
|
|
|
|
} |
|
|
|
|
sb.append("key=").append(key); |
|
|
|
|
if (WXPayConstants.SignType.MD5.equals(signType)) { |
|
|
|
|
return MD5(sb.toString()).toUpperCase(); |
|
|
|
|
} |
|
|
|
|
else if (WXPayConstants.SignType.HMACSHA256.equals(signType)) { |
|
|
|
|
return HMACSHA256(sb.toString(), key); |
|
|
|
|
} |
|
|
|
|
else { |
|
|
|
|
throw new Exception(String.format("Invalid sign_type: %s", signType)); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 生成 MD5 |
|
|
|
|
* |
|
|
|
|
* @param data 待处理数据 |
|
|
|
|
* @return MD5结果 |
|
|
|
|
*/ |
|
|
|
|
public static String MD5(String data) throws Exception { |
|
|
|
|
java.security.MessageDigest md = MessageDigest.getInstance("MD5"); |
|
|
|
|
byte[] array = md.digest(data.getBytes("UTF-8")); |
|
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
|
for (byte item : array) { |
|
|
|
|
sb.append(Integer.toHexString((item & 0xFF) | 0x100).substring(1, 3)); |
|
|
|
|
} |
|
|
|
|
return sb.toString().toUpperCase(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
* 将Map转换为XML格式的字符串 |
|
|
|
|
* |
|
|
|
|
* @param data Map类型数据 |
|
|
|
|
* @return XML格式的字符串 |
|
|
|
|
* @throws Exception |
|
|
|
|
*/ |
|
|
|
|
public static String mapToXml(Map<String, String> data) throws Exception { |
|
|
|
|
org.w3c.dom.Document document = WXPayXmlUtil.newDocument(); |
|
|
|
|
org.w3c.dom.Element root = document.createElement("xml"); |
|
|
|
|
document.appendChild(root); |
|
|
|
|
for (String key: data.keySet()) { |
|
|
|
|
String value = data.get(key); |
|
|
|
|
if (value == null) { |
|
|
|
|
value = ""; |
|
|
|
|
} |
|
|
|
|
value = value.trim(); |
|
|
|
|
org.w3c.dom.Element filed = document.createElement(key); |
|
|
|
|
filed.appendChild(document.createTextNode(value)); |
|
|
|
|
root.appendChild(filed); |
|
|
|
|
} |
|
|
|
|
TransformerFactory tf = TransformerFactory.newInstance(); |
|
|
|
|
Transformer transformer = tf.newTransformer(); |
|
|
|
|
DOMSource source = new DOMSource(document); |
|
|
|
|
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); |
|
|
|
|
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); |
|
|
|
|
StringWriter writer = new StringWriter(); |
|
|
|
|
StreamResult result = new StreamResult(writer); |
|
|
|
|
transformer.transform(source, result); |
|
|
|
|
String output = writer.getBuffer().toString(); //.replaceAll("\n|\r", "");
|
|
|
|
|
try { |
|
|
|
|
writer.close(); |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) { |
|
|
|
|
} |
|
|
|
|
return output; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|