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

个人博客地址

http://www.darkghost.life

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

 1 import hashlib
 2 def md5():
 3     md5_value = hashlib.md5()
 4     with open('C:\Intel\S6720HI-V200R019C10SPC500.cc','rb') as f:
 5         while True:
 6             data = f.read(2048)
 7             if not data:
 8                 break
 9             md5_value.update(data)
10     print (md5_value.hexdigest())
11 md5()

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

 1 import hashlib
 2 #创建对象,加盐
 3 has = hashlib.md5('奔波的驱魔人'.encode('utf-8'))
 4 pwd = input('请输入密码>>>')
 5 #传入需要加密的字段
 6 has.update(pwd.encode('utf-8'))
 7 print(has.hexdigest())
 8 
 9 #以上代码md5值等同于以下
10 has = hashlib.md5()
11 pwd = input('请输入密码>>>')
12 #传入需要加密的字段
13 has.update('奔波的驱魔人'.encode('utf-8'))
14 has.update(pwd.encode('utf-8'))
15 print(has.hexdigest())

base64编码与解码

1 import base64
2 a = 'vx:cs19911124'
3 a_encode = base64.b64encode(a.encode('utf-8'))
4 print(a_encode.decode('utf-8'))
5 b_decode = base64.b64decode(a_encode).decode('utf-8')
6 print(b_decode)

 封装进 tkinter

 1 import tkinter
 2 from tkinter import filedialog
 3 import hashlib,base64
 4 class dark:
 5     def __init__(self):
 6         self.init_window = tkinter.Tk()
 7         self.init_window.title('MD5转换工具')
 8         self.init_window.geometry('1068x681+10+10')
 9     def run(self):
10         self.left_lable = tkinter.Label(self.init_window,text='原始数据')
11         self.left_lable.grid(row=0,column=0)
12         self.right_lable = tkinter.Label(self.init_window,text='转换后数据')
13         self.right_lable.grid(row=0,column=12)
14         self.source_text = tkinter.Text(self.init_window,height=48,width=65)
15         self.source_text.grid(row=1,column=0,rowspan=11,columnspan=10)
16         self.change_text = tkinter.Text(self.init_window,height=48,width=65)
17         self.change_text.grid(row=1,rowspan=11,column=12,columnspan=10)
18         self.button = tkinter.Button(self.init_window,text='文件md5校验',command=self.filetomd5)
19         self.button.grid(row=4,column=11)
20         self.button = tkinter.Button(self.init_window,text='字符串转MD5',command=self.changetomd5)
21         self.button.grid(row=5,column=11)
22         self.button2 = tkinter.Button(self.init_window,text='字符串转BASE64',command=self.strtobase64)
23         self.button2.grid(row=7,column=11)
24         self.button3 = tkinter.Button(self.init_window,text='BASE64转字符串',command=self.base64tostr)
25         self.button3.grid(row=8,column=11)
26         self.init_window.mainloop()
27     def changetomd5(self):
28         source = self.source_text.get(1.0,tkinter.END).strip().replace("\n","").encode()
29         changemd5 = hashlib.md5()
30         changemd5.update(source)
31         ret = changemd5.hexdigest()
32         self.insert(ret)
33     def strtobase64(self):
34         source = self.source_text.get(1.0,tkinter.END).strip().replace("\n","").encode()
35         change_base64 = base64.b64encode(source).decode()
36         self.insert(change_base64)
37     def base64tostr(self):
38         source = self.source_text.get(1.0,tkinter.END).strip().replace("\n","").encode()
39         change_base64 = base64.b64decode(source).decode()
40         self.insert(change_base64)
41     def filetomd5(self):
42         selectFile = filedialog.askopenfilename()
43         md5_value = hashlib.md5()
44         try:
45             with open(selectFile,'rb') as f:
46                 while True:
47                     data = f.read(2048)
48                     if not data:
49                         break
50                     md5_value.update(data)
51             self.insert(md5_value.hexdigest())
52         except:
53             pass
54     def insert(self,data):
55         self.change_text.delete(1.0,tkinter.END)
56         self.change_text.insert(1.0,data)
57 obj = dark()
58 obj.run()

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

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

posted @ 2021-10-05 11:20  无限's-blog  阅读(443)  评论(0编辑  收藏  举报