Python MD5 Hashing
By:Roy.LiuLast updated:2020-06-28
在用python做开发,或者用Django 的时候,经常接触到MD5检验,或者用MD5做签名,或者做密码加密。在python中很容易用:
import hashlib result = hashlib.md5(b"Hello MD5").hexdigest() print(result) result = hashlib.md5("Hello MD5".encode("utf-8")).hexdigest() print(result) m = hashlib.md5(b"Hello MD5") print(m.name) print(m.digest_size) # 16 bytes (128 bits) print(m.digest()) # bytes print(m.hexdigest()) # bytes in hex representation
用Python MD5做文件校验
import hashlib def md5checksum(fname): md5 = hashlib.md5() # handle content in binary form f = open(fname, "rb") while chunk := f.read(4096): md5.update(chunk) return md5.hexdigest() result = md5checksum("c:\\test\\readme.txt") print(result)
From:一号门
COMMENTS