对字符串加密

1
2
3
4
5
6
import hashlib, base64
s = "0123456789"
h = hashlib.md5()
h.update(s.encode('utf-8'))
md5_value = base64.b64encode(h.digest()).decode('utf-8')
print(md5_value) # 'eB5eJF1ptWaXm4bijSPyxw=='

对文件加密

1
2
3
4
5
6
path = os.path.join(file_path, file_name)
file = open(path, 'rb').read()
h = hashlib.md5()
h.update(file)
content_md5 = base64.b64encode(h.digest()).decode('utf-8')
print(content_md5)