python 计算文件的md5、sha1、sha256、sha512 作者:马育民 • 2024-04-04 15:25 • 阅读:10018 相关文章:[散列加密:MD5、SHA-1、SHA-2、SHA-3、SHA256、SHA512](https://www.malaoshi.top/show_1IX7RopU2L36.html "散列加密:MD5、SHA-1、SHA-2、SHA-3、SHA256、SHA512") # 适合读取小文件 以二进制的方式一次读取文件(只适合读取小文件),然后计算相应 md5、sha1、sha256、sha512 值 ### md5 ``` import hashlib def md5(path): # hashlib.sha512() or hashlib.md5() algorithm = hashlib.md5() with open(path, 'rb') as f: algorithm.update(f.read()) return algorithm.hexdigest() path = 'md5.py' print(md5(path)) ``` ### sha1 ``` import hashlib def sha1(path): # hashlib.sha512() or hashlib.md5() algorithm = hashlib.sha1() with open(path, 'rb') as f: algorithm.update(f.read()) return algorithm.hexdigest() path = 'sha1.py' print(sha1(path)) ``` ### sha256 ``` import hashlib def sha256(path): # hashlib.sha512() or hashlib.md5() algorithm = hashlib.sha256() with open(path, 'rb') as f: algorithm.update(f.read()) return algorithm.hexdigest() path = 'sha256.py' print(sha256(path)) ``` ### sha512 ``` import hashlib def sha512(path): # hashlib.sha512() or hashlib.md5() algorithm = hashlib.sha512() with open(path, 'rb') as f: algorithm.update(f.read()) return algorithm.hexdigest() path = 'sha512.py' print(sha512(path)) ``` 参考:https://blog.csdn.net/TomorrowAndTuture/article/details/121356204 原文出处:https://malaoshi.top/show_1IX7RtT3rdv2.html