微信支付簽名工具類---Signature
package com.jeeplus.mobile.utils;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
/**
* @ClassName: Signature
* @Description: 微信支付簽名工具類
* @author
* @date 2017年3月8日 下午1:53:24
*/
public class Signature {
/**
* 簽名算法
*
* @param o 要參與簽名的數據對象
* @return 簽名
* @throws IllegalAccessException
*/
public static String getSign(Object o) throws IllegalAccessException{
ArrayList<String> list = new ArrayList<String>();
Class cls = o.getClass();
Field[] fields = cls.getDeclaredFields();
for (Field f : fields){
f.setAccessible(true);
if (f.get(o) != null && f.get(o) != ""){
list.add(f.getName() + "=" + f.get(o) + "&");
}
}
int size = list.size();
String[] arrayToSort = list.toArray(new String[size]);
Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < size; i++){
sb.append(arrayToSort[i]);
}
String result = sb.toString();
result += "key=" + PaymentConst.WECHAT_KEY;
result = MD5.GetMD5Code(result).toUpperCase();
return result;
}
/**
* 簽名算法
*
* @param map 要參與簽名的數據對象
* @return 簽名
* @throws IllegalAccessException
*/
public static String getSign(Map<String, Object> map){
ArrayList<String> list = new ArrayList<String>();
for (Map.Entry<String, Object> entry : map.entrySet()){
if ( !"".equals(entry.getValue())){
list.add(entry.getKey() + "=" + entry.getValue() + "&");
}
}
int size = list.size();
String[] arrayToSort = list.toArray(new String[size]);
Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < size; i++){
sb.append(arrayToSort[i]);
}
String result = sb.toString();
result += "key=" + PaymentConst.WECHAT_KEY;
System.out.println("簽名:"+result);
result = MD5.GetMD5Code(result).toUpperCase();
return result;
}
public static String createSign(String characterEncoding,SortedMap<String,Object> parameters){
StringBuffer sb = new StringBuffer();
Set es = parameters.entrySet();
Iterator it = es.iterator();
while(it.hasNext()) {
Map.Entry entry = (Map.Entry)it.next();
String k = (String)entry.getKey();
Object v = entry.getValue();
if(null != v && !"".equals(v)
&& !"sign".equals(k) && !"key".equals(k)) {
sb.append(k + "=" + v + "&");
}
}
sb.append("key=" + PaymentConst.WECHAT_KEY);
System.out.println("簽名:"+sb.toString());
String sign =MD5.GetMD5Code(sb.toString()).toUpperCase();
return sign;
}
/**
* 從API返回的XML數據裏面重新計算一次簽名
*
* @param responseString API返回的XML數據
* @return 新鮮出爐的簽名
* @throws ParserConfigurationException
* @throws IOException
* @throws SAXException
*/
public static String getSignFromResponseString(String responseString) throws Exception
{
Map<String, Object> map = XMLUtils.getMapFromXML(responseString);
// 清掉返回數據對象裏面的Sign數據(不能把這個數據也加進去進行簽名),然後用簽名算法進行簽名
map.put("sign", "");
// 將API返回的數據根據用簽名算法進行計算新的簽名,用來跟API返回的簽名進行比較
return Signature.getSign(map);
}
/**
* 檢驗API返回的數據裏面的簽名是否合法,避免數據在傳輸的過程中被第三方篡改
*
* @param responseString API返回的XML數據字符串
* @return API簽名是否合法
* @throws ParserConfigurationException
* @throws IOException
* @throws SAXException
*/
public static boolean checkIsSignValidFromResponseString(String responseString) throws Exception,
SAXException
{
Map<String, Object> map = XMLUtils.getMapFromXML(responseString);
String signFromAPIResponse = map.get("sign").toString();
if (signFromAPIResponse == "" || signFromAPIResponse == null)
{
return false;
}
// 清掉返回數據對象裏面的Sign數據(不能把這個數據也加進去進行簽名),然後用簽名算法進行簽名
map.put("sign", "");
// 將API返回的數據根據用簽名算法進行計算新的簽名,用來跟API返回的簽名進行比較
String signForAPIResponse = Signature.createSign("UTF-8",new TreeMap<String, Object>(map));
if (!signForAPIResponse.equals(signFromAPIResponse)){
// 簽名驗不過,表示這個API返回的數據有可能已經被篡改了
return false;
}
return true;
}
}
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;
/**
* @ClassName: Signature
* @Description: 微信支付簽名工具類
* @author
* @date 2017年3月8日 下午1:53:24
*/
public class Signature {
/**
* 簽名算法
*
* @param o 要參與簽名的數據對象
* @return 簽名
* @throws IllegalAccessException
*/
public static String getSign(Object o) throws IllegalAccessException{
ArrayList<String> list = new ArrayList<String>();
Class cls = o.getClass();
Field[] fields = cls.getDeclaredFields();
for (Field f : fields){
f.setAccessible(true);
if (f.get(o) != null && f.get(o) != ""){
list.add(f.getName() + "=" + f.get(o) + "&");
}
}
int size = list.size();
String[] arrayToSort = list.toArray(new String[size]);
Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < size; i++){
sb.append(arrayToSort[i]);
}
String result = sb.toString();
result += "key=" + PaymentConst.WECHAT_KEY;
result = MD5.GetMD5Code(result).toUpperCase();
return result;
}
/**
* 簽名算法
*
* @param map 要參與簽名的數據對象
* @return 簽名
* @throws IllegalAccessException
*/
public static String getSign(Map<String, Object> map){
ArrayList<String> list = new ArrayList<String>();
for (Map.Entry<String, Object> entry : map.entrySet()){
if ( !"".equals(entry.getValue())){
list.add(entry.getKey() + "=" + entry.getValue() + "&");
}
}
int size = list.size();
String[] arrayToSort = list.toArray(new String[size]);
Arrays.sort(arrayToSort, String.CASE_INSENSITIVE_ORDER);
StringBuilder sb = new StringBuilder();
for (int i = 0; i < size; i++){
sb.append(arrayToSort[i]);
}
String result = sb.toString();
result += "key=" + PaymentConst.WECHAT_KEY;
System.out.println("簽名:"+result);
result = MD5.GetMD5Code(result).toUpperCase();
return result;
}
public static String createSign(String characterEncoding,SortedMap<String,Object> parameters){
StringBuffer sb = new StringBuffer();
Set es = parameters.entrySet();
Iterator it = es.iterator();
while(it.hasNext()) {
Map.Entry entry = (Map.Entry)it.next();
String k = (String)entry.getKey();
Object v = entry.getValue();
if(null != v && !"".equals(v)
&& !"sign".equals(k) && !"key".equals(k)) {
sb.append(k + "=" + v + "&");
}
}
sb.append("key=" + PaymentConst.WECHAT_KEY);
System.out.println("簽名:"+sb.toString());
String sign =MD5.GetMD5Code(sb.toString()).toUpperCase();
return sign;
}
/**
* 從API返回的XML數據裏面重新計算一次簽名
*
* @param responseString API返回的XML數據
* @return 新鮮出爐的簽名
* @throws ParserConfigurationException
* @throws IOException
* @throws SAXException
*/
public static String getSignFromResponseString(String responseString) throws Exception
{
Map<String, Object> map = XMLUtils.getMapFromXML(responseString);
// 清掉返回數據對象裏面的Sign數據(不能把這個數據也加進去進行簽名),然後用簽名算法進行簽名
map.put("sign", "");
// 將API返回的數據根據用簽名算法進行計算新的簽名,用來跟API返回的簽名進行比較
return Signature.getSign(map);
}
/**
* 檢驗API返回的數據裏面的簽名是否合法,避免數據在傳輸的過程中被第三方篡改
*
* @param responseString API返回的XML數據字符串
* @return API簽名是否合法
* @throws ParserConfigurationException
* @throws IOException
* @throws SAXException
*/
public static boolean checkIsSignValidFromResponseString(String responseString) throws Exception,
SAXException
{
Map<String, Object> map = XMLUtils.getMapFromXML(responseString);
String signFromAPIResponse = map.get("sign").toString();
if (signFromAPIResponse == "" || signFromAPIResponse == null)
{
return false;
}
// 清掉返回數據對象裏面的Sign數據(不能把這個數據也加進去進行簽名),然後用簽名算法進行簽名
map.put("sign", "");
// 將API返回的數據根據用簽名算法進行計算新的簽名,用來跟API返回的簽名進行比較
String signForAPIResponse = Signature.createSign("UTF-8",new TreeMap<String, Object>(map));
if (!signForAPIResponse.equals(signFromAPIResponse)){
// 簽名驗不過,表示這個API返回的數據有可能已經被篡改了
return false;
}
return true;
}
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.