python3 與python2 使用map的坑


對字符串hashlib加密

注意兩點巨坑

1.在py2中 不用對字符encode()編碼,py3中必須encode()編碼否則sha1.hexdigest()答案將不是你想要的。

2.在py3中 必須對map使用list 或tuple或循環輸出才會得到正確答案。

 在python2中正確方法

import hashlib

token = "sw7v82sf9hvw"
lis = [token,'1544002201','129793960']
lis.sort()
sha1 = hashlib.sha1()
map(sha1.update,lis)
hashcode = sha1.hexdigest()
print hashcode

 在python3中正確方法

import hashlib

token = "sw7v82sf9hvw"
lis = [token,'1544002201','129793960']
lis.sort()
sha1 = hashlib.sha1()
list(map(sha1.update,[x.encode() for x in lis ]))
hashcode = sha1.hexdigest()
print(hashcode)



免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM