本程式實現了對’&’符號串連的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=gbkimport structimport hashlibdef 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 md5valueif __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】