把登录与注册的密码都换成密文形式
def login():
ipt_name = input('请输入帐号:').strip()
ipt_pwd = input('请输入密码:').strip()
pwd_hash = hashlib.md5(ipt_pwd.encode('utf-8'))
with open('db.txt','rt',encoding='utf-8') as f:
for i in f:
if [ipt_name,pwd_hash.hexdigest()] == i.strip('
').split(':'):
print('login succ')
break
else:
print('输入账号或者密码错误')
def register():
ipt_account = input('Enter your account:')
ipt_pwd = input('Enter your password:')
with open('db.txt','at',encoding='utf-8') as f:
pwd_hash = hashlib.md5(ipt_pwd.encode('utf-8'))
f.write(f'{ipt_account}:{pwd_hash.hexdigest()}
')
login()
register()
文件完整性校验(考虑大文件)
import hashlib import os import random def check_file(file1,file2): m1 = hashlib.md5() m2 = hashlib.md5() lis = [] file_size = os.path.getsize(file1) with open(file1,'rb') as f, open(file2,'rb') as g: for i in range(10): lis.append(random.randint(0,file_size-20)) for i in lis: f.seek(i) g.seek(i) dataf = f.read(20) m1.update(dataf) datag = g.read(20) m2.update(datag) file1_hash = m1.hexdigest() file2_hash = m2.hexdigest() print(file1_hash == file2_hash) check_file('3.31day笔记.md','3.31day笔记_download.md')
注册功能改用json实现
import hashlib,json def register(): ipt_name = input('请输入用户名:').strip() ipt_pwd = input('请输入密码:').strip() with open('db.txt','at',encoding='utf-8') as f: pwd_hash = hashlib.md5(ipt_pwd.encode('utf-8')) register_info = { 'name':ipt_name, 'password':pwd_hash.hexdigest(), 'account_type':'user' } json.dump(f"{ipt_name}:{register_info}",f) f.write(' ') register()
项目的配置文件采用configparser进行解析