windows 文件hash校验,字符串加密,base64转换工具

对下载的文件使用hash校验验证文件完整性

import hashlib
def md5():
    md5_value = hashlib.md5()
    with open('C:\Intel\S6720HI-V200R019C10SPC500.cc','rb') as f:
        while True:
            data = f.read(2048)
            if not data:
                break
            md5_value.update(data)
    print (md5_value.hexdigest())
md5()

也可用于前端页面注册用户信息,加密发送给后端

import hashlib
#创建对象,加盐
has = hashlib.md5('奔波的驱魔人'.encode('utf-8'))
pwd = input('请输入密码>>>')
#传入需要加密的字段
has.update(pwd.encode('utf-8'))
print(has.hexdigest())

#以上代码md5值等同于以下
has = hashlib.md5()
pwd = input('请输入密码>>>')
#传入需要加密的字段
has.update('奔波的驱魔人'.encode('utf-8'))
has.update(pwd.encode('utf-8'))
print(has.hexdigest())

base64编码与解码

import base64
a = 'vx:cs19911124'
a_encode = base64.b64encode(a.encode('utf-8'))
print(a_encode.decode('utf-8'))
b_decode = base64.b64decode(a_encode).decode('utf-8')
print(b_decode)

封装进 tkinter

import tkinter
from tkinter import filedialog
import hashlib,base64
class dark:
    def __init__(self):
        self.init_window = tkinter.Tk()
        self.init_window.title('MD5转换工具')
        self.init_window.geometry('1068x681+10+10')
    def run(self):
        self.left_lable = tkinter.Label(self.init_window,text='原始数据')
        self.left_lable.grid(row=0,column=0)
        self.right_lable = tkinter.Label(self.init_window,text='转换后数据')
        self.right_lable.grid(row=0,column=12)
        self.source_text = tkinter.Text(self.init_window,height=48,width=65)
        self.source_text.grid(row=1,column=0,rowspan=11,columnspan=10)
        self.change_text = tkinter.Text(self.init_window,height=48,width=65)
        self.change_text.grid(row=1,rowspan=11,column=12,columnspan=10)
        self.button = tkinter.Button(self.init_window,text='文件md5校验',command=self.filetomd5)
        self.button.grid(row=4,column=11)
        self.button = tkinter.Button(self.init_window,text='字符串转MD5',command=self.changetomd5)
        self.button.grid(row=5,column=11)
        self.button2 = tkinter.Button(self.init_window,text='字符串转BASE64',command=self.strtobase64)
        self.button2.grid(row=7,column=11)
        self.button3 = tkinter.Button(self.init_window,text='BASE64转字符串',command=self.base64tostr)
        self.button3.grid(row=8,column=11)
        self.init_window.mainloop()
    def changetomd5(self):
        source = self.source_text.get(1.0,tkinter.END).strip().replace("\n","").encode()
        changemd5 = hashlib.md5()
        changemd5.update(source)
        ret = changemd5.hexdigest()
        self.insert(ret)
    def strtobase64(self):
        source = self.source_text.get(1.0,tkinter.END).strip().replace("\n","").encode()
        change_base64 = base64.b64encode(source).decode()
        self.insert(change_base64)
    def base64tostr(self):
        source = self.source_text.get(1.0,tkinter.END).strip().replace("\n","").encode()
        change_base64 = base64.b64decode(source).decode()
        self.insert(change_base64)
    def filetomd5(self):
        selectFile = filedialog.askopenfilename()
        md5_value = hashlib.md5()
        try:
            with open(selectFile,'rb') as f:
                while True:
                    data = f.read(2048)
                    if not data:
                        break
                    md5_value.update(data)
            self.insert(md5_value.hexdigest())
        except:
            pass
    def insert(self,data):
        self.change_text.delete(1.0,tkinter.END)
        self.change_text.insert(1.0,data)
obj = dark()
obj.run()

打包成exe程序,运行效果如下

链接: https://pan.baidu.com/s/1YGz-jP77wcHAk-qiz5PuOw 提取码: 3rqq

Leave a Reply

Your email address will not be published. Required fields are marked *

X