mirror of
https://gitee.com/binary/weixin-java-tools.git
synced 2025-11-02 04:29:48 +08:00
公众号支付
This commit is contained in:
@ -1,5 +1,7 @@
|
||||
package me.chanjar.weixin.common.util.crypto;
|
||||
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
|
||||
import java.security.MessageDigest;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Arrays;
|
||||
@ -21,7 +23,7 @@ public class SHA1 {
|
||||
for (String a : arr) {
|
||||
sb.append(a);
|
||||
}
|
||||
return genStr(sb.toString());
|
||||
return DigestUtils.sha1Hex(sb.toString());
|
||||
}
|
||||
|
||||
/**
|
||||
@ -40,25 +42,6 @@ public class SHA1 {
|
||||
sb.append('&');
|
||||
}
|
||||
}
|
||||
return genStr(sb.toString());
|
||||
return DigestUtils.sha1Hex(sb.toString());
|
||||
}
|
||||
|
||||
public static String genStr(String str) throws NoSuchAlgorithmException {
|
||||
MessageDigest sha1 = MessageDigest.getInstance("SHA1");
|
||||
sha1.update(str.getBytes());
|
||||
byte[] output = sha1.digest();
|
||||
return bytesToHex(output);
|
||||
}
|
||||
|
||||
protected static String bytesToHex(byte[] b) {
|
||||
char hexDigit[] = { '0', '1', '2', '3', '4', '5', '6', '7',
|
||||
'8', '9', 'a', 'b', 'c', 'd', 'e', 'f' };
|
||||
StringBuffer buf = new StringBuffer();
|
||||
for (int j = 0; j < b.length; j++) {
|
||||
buf.append(hexDigit[(b[j] >> 4) & 0x0f]);
|
||||
buf.append(hexDigit[b[j] & 0x0f]);
|
||||
}
|
||||
return buf.toString();
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@ -14,6 +14,7 @@
|
||||
package me.chanjar.weixin.common.util.crypto;
|
||||
|
||||
import org.apache.commons.codec.binary.Base64;
|
||||
import org.apache.commons.codec.digest.DigestUtils;
|
||||
import org.w3c.dom.Document;
|
||||
import org.w3c.dom.Element;
|
||||
import org.xml.sax.InputSource;
|
||||
@ -27,8 +28,7 @@ import javax.xml.parsers.ParserConfigurationException;
|
||||
import java.io.StringReader;
|
||||
import java.nio.charset.Charset;
|
||||
import java.security.NoSuchAlgorithmException;
|
||||
import java.util.Arrays;
|
||||
import java.util.Random;
|
||||
import java.util.*;
|
||||
|
||||
public class WxCryptUtil {
|
||||
|
||||
@ -224,6 +224,36 @@ public class WxCryptUtil {
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* 微信公众号支付签名算法(详见:http://pay.weixin.qq.com/wiki/doc/api/index.php?chapter=4_3)
|
||||
* @param packageParams 原始参数
|
||||
* @param signKey 加密Key(即 商户Key)
|
||||
* @param charset 编码
|
||||
* @return 签名字符串
|
||||
*/
|
||||
public static String createSign(Map<String, String> packageParams, String signKey) {
|
||||
SortedMap<String, String> sortedMap = new TreeMap<String, String>();
|
||||
sortedMap.putAll(packageParams);
|
||||
|
||||
List<String> keys = new ArrayList<String>(packageParams.keySet());
|
||||
Collections.sort(keys);
|
||||
|
||||
|
||||
StringBuffer toSign = new StringBuffer();
|
||||
for (String key : keys) {
|
||||
String value = packageParams.get(key);
|
||||
if (null != value && !"".equals(value) && !"sign".equals(key)
|
||||
&& !"key".equals(key)) {
|
||||
toSign.append(key + "=" + value + "&");
|
||||
}
|
||||
}
|
||||
toSign.append("key=" + signKey);
|
||||
System.out.println(toSign.toString());
|
||||
String sign = DigestUtils.md5Hex(toSign.toString())
|
||||
.toUpperCase();
|
||||
return sign;
|
||||
}
|
||||
|
||||
/**
|
||||
* 将一个数字转换成生成4个字节的网络字节序bytes数组
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user