微信支付签名工具类---Signature
来源:互联网 发布:skype business mac 编辑:程序博客网 时间:2024/05/07 10:52
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;
}
}
阅读全文
0 0
- 微信支付签名工具类---Signature
- 微信支付--工具类--创建签名
- 微信支付--工具类--产生二维码验证签名
- F2.微信支付签名工具
- 微信支付---签名
- 微信支付签名
- 生成微信支付接口的签名工具
- 微信支付工具类
- 微信支付工具类
- 微信支付签名错误
- 微信支付签名方法
- 微信支付生成签名
- 微信支付签名错误
- 微信支付签名错误
- 微信支付签名算法
- 微信支付签名算法
- 微信支付 支付验证签名失败
- 微信js sdk invalid signature签名错误 问题解决。
- 第二章 java语言中的数据与运算符
- 最简单的方式上传到git
- 互联网时代的区域电商思维
- 3小时搞定数据可视化大屏,升职加薪有望了
- 浏览器缓存
- 微信支付签名工具类---Signature
- ######02.【根据表关系 写实体】,【直接凭业务逻辑 和页面显示信息,写不出:体现实体之间的依赖关系 的属性。】
- Python——map(),reduce(),filter()函数
- 结构体数组
- 01背包问题——经典DP问题,优化与未优化
- 实现视频+图片轮播
- Linux进程与线程的区别
- 关于oracle的锁表解决 ORA-00031: session marked for kill
- Java 常用的正则匹配