python實現對以‘&’串連的參數串排序後計算md5

來源:互聯網
上載者:User

本程式實現了對’&’符號串連的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】

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.