# -*- coding: utf-8 -*-
import os
from hashlib import md5, sha1, sha256
from zlib import crc32strFilePath = os.path.join(os.getcwd() + "\\" + "“捷创源科技”公众号.jpg")def getMd5(strFilePath): # 计算md5mdfive = md5()with open(strFilePath, 'rb') as f:mdfive.update(f.read())return mdfive.hexdigest().upper()def getSha1(strFilePath): # 计算sha1sha1Obj = sha1()with open(strFilePath, 'rb') as f:sha1Obj.update(f.read())return sha1Obj.hexdigest().upper()def getSha256(strFilePath): # 计算sha256sha256Obj = sha256() # Get the hash algorithm.with open(strFilePath, 'rb') as f:sha256Obj.update(f.read()) # Hash the data.return sha256Obj.hexdigest().upper() # Get he hash value.def getCrc32(strFilePath): # 计算crc32with open(strFilePath, 'rb') as f:return hex(crc32(f.read()))[2:].upper()"""-----------------------------------------------
主函数
-----------------------------------------------"""
if __name__ == '__main__':print('{:8} {}'.format('MD5:', getMd5(strFilePath)))print('{:8} {}'.format('SHA1:', getSha1(strFilePath)))print('{:8} {}'.format('SHA256:', getSha256(strFilePath)))print('{:8} {}'.format('CRC32:', getCrc32(strFilePath)))
运行效果:
如果本文对您有所帮助,请关注微信公众号“捷创源科技”。
获取更多资料,请访问我的博客