本程序实现了对’&’符号连接的http参数串排序后再计算md5.
主要步骤为:1.参数串以&为分割符拆成list,
2. 对list进行排序
3. 把排序完的list再用&连接为string
4. 对string计算md5
参数串例如
str1=”languageId=0&accessToken=0E4730695CDD9B4F8D56B8107171F525-001407107122545&outTradeNo=148879135312969764307b4a86&payPlatform=1&packageName=com.baidu.vodx&payMode=2&price=0.01&payInfoId=15543373&version=6.2&productname=book&sourceType=1&thirdAppCallBack=&timeStamp=1488793412&thirdappName=com.qq.vod”
代码在Python3.4.3调试通过。
#!/usr/bin/env python
#coding=gbk
import struct
import hashlib
def md5(input):
input =input.encode('utf-8');
md5sign = hashlib.md5()
md5sign.update(input)
str = md5sign.digest()
data = struct.unpack("IIII", str)
md5value = data[0] << 96 | data[1] << 64 | data[2] << 32 | data[3]
return md5value
if __name__ == "__main__":
str1="languageId=0&accessToken=0E4730695CDD9B4F8D56B8107171F525-001407107122545&outTradeNo=148879135312969764307b4a86&payPlatform=1&packageName=com.baidu.vodx&payMode=2&price=0.01&payInfoId=15543373&version=6.2&productname=book&sourceType=1&thirdAppCallBack=&timeStamp=1488793412&thirdappName=com.qq.vod"
#string 拆分为List
strSplit=str1.split('&') #str.split(str="", num=string.count(str)).
print (strSplit)
print ('\n')
#List排序
strSorted=sorted(strSplit)
print (strSorted)
print ('\n')
#List转为string,以&连接
strConvert = '&'.join(strSorted)
print(strConvert+'\n')
print (md5(strConvert))
处理结果:
[‘languageId=0’, ‘accessToken=0E4730695CDD9B4F8D56B8107171F525-001407107122545’, ‘outTradeNo=148879135312969764307b4a86’, ‘payPlatform=1’, ‘packageName=com.baidu.vodx’, ‘payMode=2’, ‘price=0.01’, ‘payInfoId=15543373’, ‘version=6.2’, ‘productname=book’, ‘sourceType=1’, ‘thirdAppCallBack=’, ‘timeStamp=1488793412’, ‘thirdappName=com.qq.vod’]
[‘accessToken=0E4730695CDD9B4F8D56B8107171F525-001407107122545’, ‘languageId=0’, ‘outTradeNo=148879135312969764307b4a86’, ‘packageName=com.baidu.vodx’, ‘payInfoId=15543373’, ‘payMode=2’, ‘payPlatform=1’, ‘price=0.01’, ‘productname=book’, ‘sourceType=1’, ‘thirdAppCallBack=’, ‘thirdappName=com.qq.vod’, ‘timeStamp=1488793412’, ‘version=6.2’]
accessToken=0E4730695CDD9B4F8D56B8107171F525-001407107122545&languageId=0&outTradeNo=148879135312969764307b4a86&packageName=com.baidu.vodx&payInfoId=15543373&payMode=2&payPlatform=1&price=0.01&productname=book&sourceType=1&thirdAppCallBack=&thirdappName=com.qq.vod&timeStamp=1488793412&version=6.2
250954159143332278860033366342707854695
可以看出,得益于Python强大的库函数,写这个程序基本不用知道算法。
欢迎关注今日头条号【gloryman】