Sign签名
-
-
将除Sign参数外的所有系统级参数和应用级级参数的值进行字典排序生成串(versions=1.1时按ASCII码排序)
例:
appkey=75845872,accesstoken=adf345346dfgd34534vg,format=xml,method=wdgj.member.recharge.list,timestamp=1469608715,versions=1.0,page_no=1,page_size=100,begin_time=2016-07-25 00:00:00,end_time=2016-07-26 00:00:00,content=<?xml version="1.0" encoding="utf-8" ?><trade_order_info><trade_order></trade_order></trade_order_info>
排序之后:
<?xml version="1.0" encoding="utf-8" ?><trade_order_info><trade_order></trade_order></trade_order_info>|1|1.0|100|1469608715|2016-07-25 00:00:00|2016-07-26 00:00:00|75845872|adf345346dfgd34534vg|wdgj.member.recharge.list|xml
每个参数值这里用“|”隔开方便大家理解,实际是没有“|”线
-
然后将AppSecret加到该串的首和尾并进行MD5加密生成32位字符串,此时的生成的串即为参数Sign
若:
Appsecret=aa2345678bb
则:
sign=MD5(aa2345678bb<?xml version="1.0" encoding="utf-8" ?><trade_order_info><trade_order></trade_order></trade_order_info>11.010014696087152016-07-25 00:00:002016-07-26 00:00:0075845872adf345346dfgd34534vgwdgj.member.recharge.listxmlaa2345678bb)
-
-
Appkey=75845872 Accesstoken=adf345346dfgd34534vg Format=xml Method=wdgj.member.recharge.list Timestamp=1469608715 versions=1.0 page_no=1 page_size=100 begin_time=2016-07-25 00:00:00 end_time=2016-07-26 00:00:00 content=<?xml version="1.0" encoding="utf-8" ?><trade_order_info><trade_order></trade_order></trade_order_info> Sign=eb7e5300db6d8148d247a6fb496ae4a6
-
timestamp=UrlEnCode(1469608715)&sign=UrlEnCode(eb7e5300db6d8148d247a6fb496ae4a6)&appkey=UrlEnCode(75845872)&method=UrlEnCode(wdgj.member.recharge.list)&accesstoken=UrlEnCode(adf345346dfgd34dfytyjh6534vg)&format=UrlEnCode(xml)&versions=UrlEnCode(1.0)&page_no=UrlEnCode(1)&page_size=UrlEnCode(100)&begin_time=UrlEnCode(2016-07-25 00:00:00)&end_time=UrlEnCode(2016-07-26 00:00:00)&content=UrlEnCode(<?xml version="1.0" encoding="utf-8" ?><trade_order_info><trade_order></trade_order></trade_order_info>)
-
private void Sign() { string versions = "1.1"; //收集所有参数值除了appsecret List<string> lt = new List<string>(); lt.Add("1488961025");//timestamp lt.Add("07645036");//appkey lt.Add(versions);//versions lt.Add("wdgj.wms.trade.confirm");//method lt.Add("b6e5f1bd8cf34fa1a60ba696f30a4ee6");//accesstoken lt.Add("xml");//format lt.Add("1");//page_no lt.Add("50");//page_size lt.Add("2016-07-25 00:00:00");//begin_time lt.Add("2016-07-26 11:00:33");//end_time lt.Add("<trade_order_info><trade_order></trade_order></trade_order_info>");//content //....其他用到的参数值,根据每个方法需要传的参数 if (versions == "1.1") { //ASCII码排序 lt.Sort(StringComparer.Ordinal); } else { //字典排序 lt.Sort(); } string appsecret = "407283c4177841e2b827928cbdf989eb";//密钥 string strSgin = appsecret + string.Join("", lt) + appsecret;//签名之前的字符串 string sign = System.Web.Security.FormsAuthentication.HashPasswordForStoringInConfigFile(strSgin, "MD5").ToLower(); }
-
//收集签名需要的参数 public static Map<string, object> getMap() { String timestamp = System.currentTimeMillis() / 1000; HashMap<string, object> map = new HashMap<>(); map.put("accesstoken", "b6e5f1bd8cf34fa1a60ba696f30a4ee6"); map.put("appkey", "07645036"); map.put("timestamp", timestamp); map.put("versions", "1.1"); map.put("format","xml"); map.put("page_no","1"); map.put("page_size","50"); map.put("begin_time","2016-07-25 00:00:00"); map.put("end_time",2016-07-26 11:00:33"); map.put("content","<trade_order_info><trade_order></trade_order></trade_order_info>"); //map.put(除appsecret参数外的所有系统级参数和应用级级参数值,根据每个方法需要传的参数) return map; } //生成签名 private static String getSign(Map paraMap) { Iterator iter = paraMap.entrySet().iterator(); List<string> list = new ArrayList<>(); String AppSecret = context.getString(R.string.dfAppSecret); while (iter.hasNext()) { Map.Entry entry = (Map.Entry) iter.next(); String val = entry.getValue().toString(); list.add(val); } //ASCII码排序 Collections.sort(list, new Comparator<string>() { public int compare(String o1, String o2) { return o1.compareTo(o2); } }); StringBuilder builder = new StringBuilder(); //参数拼接 builder.append(AppSecret); for (String value : list) { builder.append(value); } builder.append(AppSecret); //md5加密后得到最终的签名 return getMd5Value(builder.toString()); } //自己处理一下md5加密,原生的md5加密方法有问题 public static String getMd5Value(String sSecret) { MessageDigest bmd5 = MessageDigest.getInstance("MD5"); bmd5.update(sSecret.getBytes("UTF-8")); int i; StringBuffer buf = new StringBuffer(); byte[] b = bmd5.digest(); for (int offset = 0; offset < b.length; offset++) { i = b[offset]; if (i < 0) i += 256; if (i < 16) buf.append("0"); buf.append(Integer.toHexString(i)); } return buf.toString(); }
-
import requests import json import time import hashlib def openwdgj(content): # 固定常量 AppSecret = 'n800m9iaei27l1n3b5eh7ko87gfl2k0g' # 公共固定参数 data = { 'Appkey': '71005530', 'Accesstoken': '191167eb31f242bfa4a089359b77f5e4', 'Format': 'json', 'Versions': '1.1' } data['Timestamp'] = int(time.time()) # 遍历参数,放入data for ckey in content: data[ckey] = content.get(ckey) #取出字典里所有的值,放到sorted_key_list作排序。 sorted_key_list = [] for item in data: sorted_key_list.append(data[item]) #对数组进行排序,默认是按数值排序,KEY=STR强转成字符串排序。 sorted_key_list = sorted(sorted_key_list, key=str) #print(sorted_key_list) sign = '{}'.format(AppSecret) #按开放平台的要求进行拼接参数值 for key in sorted_key_list: sign = '{}{}'.format(sign, key) sign = '{}{}'.format(sign, AppSecret) #把拼接的结果计算MD5 hl = hashlib.md5() hl.update(sign.encode(encoding='utf-8')) sign = hl.hexdigest()