签名规则说明
1. 将参数先按键值排序(只做一级排序),进行key和value的拼接
2. 拼接完后,在最后面再拼接上分配的 appSecret 然后用sha1对拼接串加密
签名示例
假如传入参数:
$param = array(
'access_token' => '',
'app_key' => '152968d9af768bf084dad750f78d6866',
'once' => '911091697599',
'version' => '1.0',
'sdk_from' => 'java',
'phone' => '13800138000',
'type' => 0,
'client' => '{"channel":"mcontact_md_ahlrj_api_android","imei":"1","version":"v3.9.8"}',
);
1.将参数先按键值排序(只做一级排序),即变成
$param = array(
'access_token' => '',
'app_key' => '152968d9af768bf084dad750f78d6866',
'client' => '{"channel":"mcontact_md_ahlrj_api_android","imei":"1","version":"v3.9.8"}',
'once' => '911091697599',
'phone' => '13800138000',
'sdk_from' => 'java',
'type' => 0,
'version' => '1.0',
);
2.然后拼接起来:
access_token=app_key=152968d9af768bf084dad750f78d6866client={"channel":"mcontact_md_ahlrj_api_android","imei":"1","version":"v3.9.8"}once=911091697599……
3. 拼接完后,在最后面再拼接上分配的 appSecret 然后用sha1对拼接串加密
//////////////////////////////////////////////////////////////
示例代码,仅供参考:
签名生成方式
将需要提交的参数(signature除外),按key做字典排序。 然后 key=value 形式拼接, 然后在后面 拼上 appscret。 再sha1加密
//具体php实现
function generateSignature($params_array, $appsecret) {
if(!$appsecret){
return '';
}
$str = '';
ksort($params_array, SORT_STRING);
foreach ($params_array as $k=>$v) {
$str .= $k."=".$v;
}
$str .= $appsecret;
return sha1($str);
} $signature = generateSignature($params_array, '分配的appscret'); //$params_array 就是需要提交的参数 //具体java实现
public String getNornmalSignature(HashMap<String, String> params, String appscret) {
String signature = null;
Map<String, String> sortedParams = new TreeMap<String, String>(params);
Set<Entry<String, String>> entrys = sortedParams.entrySet();
StringBuilder basestring = new StringBuilder();
for (Entry<String, String> param : entrys) {
basestring.append(param.getKey())
.append("=")
.append(param.getValue() == null ? "" : param.getValue());
}
basestring.append(appscret);
try {
signature = DigestUtils.shaHex(basestring.toString());
} catch (Exception e) {
e.printStackTrace();
}
return signature;
}
//java调用例子
String once1 = new SimpleDateFormat("ssSSSS").format(new Date()) + String.valueOf(Math.random()).substring(2, 8);
HashMap<String, String> hm1 = new HashMap<String, String>(); hm1.put("app_key", app_key);
hm1.put("once", once1);
hm1.put("version", version);
hm1.put("sdk_from", sdk_from);
hm1.put("channel", channel);
hm1.put("mobile", mobile);
……
String signature1 = getNornmalSignature(hm1,appscret);