一:软件介绍:
一款基于pyqt5开发的MD5和哈希文件校验工具
核心代码
# 小文件的处理函数
def get_cipher_small(file_path, fun):
md5 = None
if os.path.exists(file_path) and os.path.isfile(file_path):
with open(file_path, 'rb') as f:
if fun == 'md5':
md5_obj = hashlib.md5()
elif fun == 'sha1':
md5_obj = hashlib.sha1()
elif fun == 'sha256':
md5_obj = hashlib.sha256()
elif fun == 'sha512':
md5_obj = hashlib.sha512()
elif fun == 'crc32':
md5 = hex(crc32(file_path)).upper()
return md5
md5_obj.update(f.read())
hash_code = md5_obj.hexdigest()
md5 = str(hash_code).lower().upper()
return md5
# 大文件的处理函数
def get_cipher_big(file_path, fun):
md5 = None
if os.path.exists(file_path) and os.path.isfile(file_path):
with open(file_path, 'rb') as f:
if fun == 'md5':
md5_obj = hashlib.md5()
elif fun == 'sha1':
md5_obj = hashlib.sha1()
elif fun == 'sha256':
md5_obj = hashlib.sha256()
elif fun == 'sha512':
md5_obj = hashlib.sha512()
elif fun == 'crc32':
md5 = hex(crc32(file_path))
return md5
while True:
d = f.read(8096)
if not d:
break
md5_obj.update(d)
hash_code = md5_obj.hexdigest()
md5 = str(hash_code).lower().upper()
return md5
def get_md5(file, fuc):
"""
根据文件大小计算文件MD5值
:param fuc:算法
:param file:文件
:return:加密值
"""
path = file
if os.path.exists(path=path) and os.path.isfile(path=path):
# 200M以上的当作大文件
if os.path.getsize(path) >= 200 * 1024 * 1024:
md5 = get_cipher_big(path, fuc)
else:
md5 = get_cipher_small(path, fuc)
return md5
def crc32(filepath):
block_size = 1024 * 1024
num = 0
try:
fd = open(filepath, 'rb')
while True:
buffer = fd.read(block_size)
if len(buffer) == 0: # EOF or file empty. return hashes
fd.close()
if sys.version_info[0] < 3 and num < 0:
num += 2 ** 32
return num # 返回的是十进制的值
num = zlib.crc32(buffer, num)
except Exception as e:
if sys.version_info[0] < 3:
error = e
else:
error = str(e)
return 0, error
源码地址
https://gitee.com/Ataoges/md5-check
编译好的版本下载
蓝凑云: 下载地址
密码: gmv0
存在问题
- 拖拽不能同时拖拽多个,每次只能拖一个
- 没有实现进度条功能