签名规则
为了数据的安全,所有请求都需要进行签名。
签名步骤
- 将请求参数按参数名ASCII码从小到大排序(字典序)
- 按
key1=value1&key2=value2&key3=value3格式拼接得到字符串sign_tmp - 给
sign_tmp拼接上API密匙,sign_tmp = sign_tmp + '&key=' + api_key - 使用 MD5 函数加密
sign_tmp,得到签名sign
注意事项
- 如果参数的值为空不参与签名
- 参数名区分大小写
- MD5 后得到 sign 需要转为小写
签名示例
待签名参数
{ "user_id": 1, "time_stamp": 1623389833, "nonce_str": "bcf9cc8302839c368cd8fc584e13ec74", "name": "热血火龙03区测试区", "auto_update": 1, "update_name": "热血火龙03区正式区", "update_time": "2021-1-1 10:30", "group_id": "11,22,33", "ip": "172.20.64.1", "port": 8888, "path": "D:\MirServer", "tpl_id": 219256, "egg": 0, "oepn_time": "2021-1-1 10:00", "create_script": 1 }
1、将请求参数按参数名ASCII码从小到大排序(字典序)
{ "auto_update": 1, "create_script": 1, "egg": 0, "group_id": "11,22,33", "ip": "172.20.64.1", "name": "热血火龙03区测试区", "nonce_str": "bcf9cc8302839c368cd8fc584e13ec74", "oepn_time": "2021-1-1 10:00", "path": "D:\MirServer", "port": 8888, "time_stamp": 1623389833, "tpl_id": 219256, "update_name": "热血火龙03区正式区", "update_time": "2021-1-1 10:30", "user_id": 1 }
2、按特定格式拼接得到字符串 sign_tmp
sign_tmp = auto_update=1&create_script=1&egg=0&group_id=11,22,33&ip=172.20.64.1&name=热血火龙03区测试区&nonce_str=bcf9cc8302839c368cd8fc584e13ec74&oepn_time=2021-1-1 10:00&path=D:\MirServer&port=8888&time_stamp=1623389833&tpl_id=219256&update_name=热血火龙03区正式区&update_time=2021-1-1 10:30&user_id=1
3、给 sign_tmp 拼接上 API密匙,假设 api_key = AFB2680C37490A5D730E07612A8D9C9A
sign_tmp = auto_update=1&create_script=1&egg=0&group_id=11,22,33&ip=172.20.64.1&name=热血火龙03区测试区&nonce_str=bcf9cc8302839c368cd8fc584e13ec74&oepn_time=2021-1-1 10:00&path=D:\MirServer&port=8888&time_stamp=1623389833&tpl_id=219256&update_name=热血火龙03区正式区&update_time=2021-1-1 10:30&user_id=1&key=AFB2680C37490A5D730E07612A8D9C9A
4、 使用 MD5 函数加密 sign_tmp,得到签名 sign
sign = MD5(sign_tmp) sign = 'ee90e7583b39024c45b6e2d98f0c3b45'
5、将 sign 加入请求参数
{ "user_id": 1, "time_stamp": 1623389833, "nonce_str": "bcf9cc8302839c368cd8fc584e13ec74", "name": "热血火龙03区测试区", "auto_update": 1, "update_name": "热血火龙03区正式区", "update_time": "2021-1-1 10:30", "group_id": "11,22,33", "ip": "172.20.64.1", "port": 8888, "path": "D:\MirServer", "tpl_id": 219256, "egg": 0, "oepn_time": "2021-1-1 10:00", "create_script": 1, "sign": "ee90e7583b39024c45b6e2d98f0c3b45" }
各语言代码示例
PHP
<?php function sign(array $data, string $key) { ksort($data); $arr = []; foreach ($data as $k => $v) { if ($v === '' || $v === null || $k === 'sign') continue;$arr[] = $k . '=' . $v; } $arr[] = 'key=' . $key; return md5(implode('&', $arr)); }