import hashlib
user = 'jointwisdom'
pwd = 'zhonghui123'
m2 = hashlib.md5()
m2.update(pwd.encode("utf-8"))#參數必須是byte類型,否則報Unicode-objects must be encoded before hashing錯誤
print(m2.hexdigest())
# 但是對中文字符串md5怎么辦?
# 中文字符在Python中是以unicode存在的,同一個字符串在不同的編碼體系下有不同的值,所以在hash前要進行編碼,個人建議轉為gb2312,因為對比發現,我下載的一個工具算出的md5值是與gb2312編碼后算出的md5值一樣。(!網上md5的工具很多,是不是所有的md5工具都是這樣的,未去考證,有興趣的可以研究一下)
# [python] view plain copy print?
# import hashlib
# data='我是'
# m = hashlib.md5(data.encode(encoding='gb2312'))
# print(m.hexdigest())