支持MD5/SHA1/SHA224/SHA256/SHA384/SHA512/BLAKE2B/BLAKE2S算法的hash值计算,支持文件拖拽,支持两个hash值校验一致性
目录
准备工作
- Python 3.12.4(华为镜像下载,https://mirrors.huaweicloud.com/python/)
- Notepad++:网上搜的任意版本都可以
- 配置notepad++:编写python后Ctrl+S保存.py文件,F5运行选择python安装目录下的python.exe或输入“python路径\python.exe "$(FULL_CURRENT_PATH)" & ECHO. & PAUSE & EXIT”,双引号中间为自己的python路径,保存设置快捷键F10,后期编辑按设置的快捷键F10即可运行
- 安装库:pip install hahslib tkinter tkinterdnd2
- pyinstaller:exe打包工具,cmd命令行输入 pip install pyinstaller 安装即可
Python代码
# Author: Johnny Zhang
# Date: 2024-08-30
# Description: This is a tool for hash calculation.
import hashlib
import os
import tkinter as tk
from tkinter import filedialog, messagebox
from tkinterdnd2 import TkinterDnD, DND_FILES
# 定义浏览文件方法
def browse_file(text_path):
file_path = filedialog.askopenfilename()
text_path.delete("1.0", tk.END)
text_path.insert(tk.END, file_path)
# 定义计算hash方法
def calculate_hash(file_path, algorithm):
if os.path.isfile(file_path):
with open(file_path, 'rb') as f:
data = f.read()
if algorithm == 'SHA512':
hash_value = hashlib.sha512(data).hexdigest()
elif algorithm == 'SHA384':
hash_value = hashlib.sha384(data).hexdigest()
elif algorithm == 'SHA256':
hash_value = hashlib.sha256(data).hexdigest()
elif algorithm == 'SHA224':
hash_value = hashlib.sha224(data).hexdigest()
elif algorithm == 'SHA1':
hash_value = hashlib.sha1(data).hexdigest()
elif algorithm == 'MD5':
hash_value = hashlib.md5(data).hexdigest()
elif algorithm == 'BLAKE2B':
hash_value = hashlib.blake2b(data).hexdigest()
elif algorithm == 'BLAKE2S':
hash_value = hashlib.blake2s(data).hexdigest()
return hash_value
else:
return "文件不存在"
# 定义更新hash方法
def update_hash(text_path, text_hash, algorithm_var):
file_path = text_path.get("1.0", tk.END).strip()
algorithm = algorithm_var.get()
hash_value = calculate_hash(file_path, algorithm)
text_hash.delete("1.0", tk.END)
text_hash.insert(tk.END, hash_value)
# 定义校验hash方法
def check_hash(text_path, text_hash, original_hash):
calculated_hash = text_hash.get("1.0", tk.END).strip()
original = original_hash.get("1.0", tk.END).strip()
if calculated_hash == original:
messagebox.showinfo("校验结果", "校验通过")
else:
messagebox.showinfo("校验结果", "校验不通过")
# 定义窗体名称
root = TkinterDnD.Tk()
root.title("Hash计算工具_V1.6 By Johnny")
# 第一行:选择文件按钮和文件路径输入框
browse_button = tk.Button(root, text="选择文件", width=15, command=lambda: browse_file(text_path))
browse_button.grid(row=0, column=0, padx=10, pady=10)
# 文本框长度 75 字符,默认高度 3 行
text_path = tk.Text(root, width=75, height=3, wrap=tk.WORD)
text_path.grid(row=0, column=1, padx=10, pady=10)
# 鼠标左键长按拖动文件到文本框,松开鼠标输入文件路径
text_path.drop_target_register(DND_FILES)
text_path.dnd_bind('<<Drop>>', lambda event: text_path.insert(tk.END, event.data))
# 第二行:三个算法选择按钮
algorithm_var = tk.StringVar()
algorithm_var.set('SHA256') # 默认选中 SHA256
# 创建一个框架来容纳按钮
frame = tk.Frame(root)
frame.grid(row=1, column=0, columnspan=2, padx=10, pady=10) # 框架占据两列
# 自行调整单选按钮前后顺序
sha512_button = tk.Radiobutton(frame, text="SHA512", variable=algorithm_var, value='SHA512')
sha512_button.pack(side=tk.LEFT, padx=10)
sha384_button = tk.Radiobutton(frame, text="SHA384", variable=algorithm_var, value='SHA384')
sha384_button.pack(side=tk.LEFT, padx=10)
sha256_button = tk.Radiobutton(frame, text="SHA256", variable=algorithm_var, value='SHA256')
sha256_button.pack(side=tk.LEFT, padx=10)
sha224_button = tk.Radiobutton(frame, text="SHA224", variable=algorithm_var, value='SHA224')
sha224_button.pack(side=tk.LEFT, padx=10)
sha1_button = tk.Radiobutton(frame, text="SHA1", variable=algorithm_var, value='SHA1')
sha1_button.pack(side=tk.LEFT, padx=10)
md5_button = tk.Radiobutton(frame, text="MD5", variable=algorithm_var, value='MD5')
md5_button.pack(side=tk.LEFT, padx=10)
blake2b_button = tk.Radiobutton(frame, text="BLAKE2B", variable=algorithm_var, value='BLAKE2B')
blake2b_button.pack(side=tk.LEFT, padx=10)
blake2s_button = tk.Radiobutton(frame, text="BLAKE2S", variable=algorithm_var, value='BLAKE2S')
blake2s_button.pack(side=tk.LEFT, padx=10)
# 第三行:计算哈希值按钮和哈希值输入框
calculate_button = tk.Button(root, text="计算Hash", width=15, command=lambda: update_hash(text_path, text_hash, algorithm_var))
calculate_button.grid(row=2, column=0, padx=10, pady=10)
# 文本框长度 75 字符,默认高度 3 行
text_hash = tk.Text(root, width=75, height=3, wrap=tk.WORD)
text_hash.grid(row=2, column=1, padx=10, pady=10)
# 第四行:原始 Hash 校验按钮和输入框
check_button = tk.Button(root, text="原始Hash校验", width=15, command=lambda: check_hash(text_path, text_hash, original_hash))
check_button.grid(row=3, column=0, padx=10, pady=10)
# 文本框长度 75 字符,默认高度 3 行
original_hash = tk.Text(root, width=75, height=3, wrap=tk.WORD)
original_hash.grid(row=3, column=1, padx=10, pady=10)
root.mainloop()
bat一键打包exe独立程序
常用打包方法:在py文件目录地址栏输入cmd,输入以下命令打包exe,如要改exe程序logo,在py同目录自备logo.ico文件,最终exe文件在dist文件夹下
-
pyinstaller -F test.py -ntest #打包不带logo的exe
-
pyinstaller -F -w -i logo.ico test.py -ntest #打包带logo的exe
bat脚本命令打包:新建空白txt,输入以下命令后重名为*.bat后双击运行即可
@echo off
pyinstaller -F -w -i logo.ico --add-data "C:\Users\izhan\AppData\Local\Programs\Python\Python312\Lib\site-packages\tkinterdnd2;tkinterdnd2" hash.py
pause
因拖拽需使用tkinterdnd2库,常规打包运行后报错找不到tkdnd,估打包时需引用本地库,方法为“
pyinstaller -F -w -i logo.ico --add-data "本地库地址;库名" 文件名.py”