1. 把登录与注册的密码都换成密文形式, 注册功能改用json实现.
import hashlib
import os
import json
from functools import wraps
login_user = None
func_dic = {}
db_dir_path = os.path.normpath(os.path.join(__file__, '..', 'user_data'))
if not os.path.isdir(db_dir_path):
os.mkdir(db_dir_path)
def encryption(password: str):
m = hashlib.md5()
info1 = '你愁啥?'
m.update(info1.encode('utf-8'))
m.update(password.encode('utf-8'))
info2 = '瞅你咋地?'
m.update(info2.encode('utf-8'))
return m.hexdigest()
def function_func(func_dic, description):
def deco(func):
if func_dic:
max_num = max(func_dic, key=lambda key: int(key))
max_num = int(max_num)
func_dic[str(max_num + 1)] = (func, description)
else:
func_dic['0'] = (func, description)
@wraps
def wrapper(*args, **kwargs):
res = func(*args, **kwargs)
return res
return wrapper
return deco
def select(username):
user_file_path = os.path.join(db_dir_path, f'{username}.json')
if os.path.isfile(user_file_path):
with open(user_file_path, 'rt', encoding='utf-8') as f:
return json.load(f)
def save(user_dic):
user_file_path = os.path.join(db_dir_path, f'{user_dic["username"]}.json')
with open(user_file_path, 'wt', encoding='utf-8') as f:
json.dump(user_dic, f, ensure_ascii=True)
@function_func(func_dic, '注册功能')
def register(balance=15000):
username = input("please input your username>>:").strip()
user_dic = select(username)
if user_dic:
print('对不起, 该用户名已经注册!')
return
password = input("please input your password>>:").strip()
if '' in [username, password]:
print('对不起, 输入内容不能为空')
return
confirm_password = input("confirm password>>:").strip()
if password == confirm_password:
user_dic = {
'username': username,
'password': encryption(password),
'balance': balance,
}
save(user_dic)
print("恭喜注册成功!".center(50, '-'))
else:
print("密码2次输入不一致")
@function_func(func_dic, '登录功能')
def login():
username = input("please input your username>>:").strip()
user_dic = select(username)
if not user_dic:
print("对不起, 该用户不存在")
return
password = input("please input your password>>:").strip()
if user_dic['password'] == encryption(password):
global login_user
login_user = username
print("登录成功")
else:
print("登录失败")
def run():
while True:
for key, value in func_dic.items():
print(f'({key}):{value[1]} ', end='')
print()
choice = input("输入对应编号执行相应功能>>:").strip()
if choice not in func_dic:
print("对不起, 输入范围有误")
continue
func_dic[choice][0]()
run()
2. 文件完整性校验(考虑大文件)
def verify_file(path):
if not os.path.isfile(path):
print("对不起, 文件不存在")
return
with open(path, 'rb') as f:
total_size = os.path.getsize(path)
# 解决思路: 存放文件中4个位置的值,每个位置获取10字节
# 开头10个字节 末尾10个字节 平均每1/3读取10个字节(共2次, 不包括末尾) offset: 偏移量
start_offset = 0
average_offset = total_size // 3
average_offset2 = average_offset * 2
end_offset = total_size - 10
offset_list = [start_offset, average_offset, average_offset2, end_offset]
md5_obj = hashlib.md5()
for offset in offset_list:
f.seek(offset)
md5_obj.update(f.read(10))
return md5_obj.hexdigest()
3. 使用configparser生成cfg或ini类型的文件, 并读取文件进行认证解析
import configparser
import os
path = os.path.normpath(os.path.join(__file__, '..', 'db.cfg'))
config_obj = configparser.ConfigParser()
config_obj['egon'] = {
'password': '123',
'balance': 15000,
'lock': False,
'is_admin': False,
}
config_obj['alex'] = {
'password': '123',
'balance': 15000,
'lock': False,
'is_admin': False,
}
if not os.path.isfile(path):
with open(path, 'wt', encoding='utf-8') as f:
config_obj.write(f)
# 用户登录认证
username = input("please input your username>>:").strip()
password = input("please input your password>>:").strip()
# 读取db.cfg配置文件进行效验
config_obj = configparser.ConfigParser()
config_obj.read(path)
for username in config_obj.sections():
if config_obj[username]['password'] == password:
print('登录成功!')
break
else:
print("登录失败!")