使用hashlib进行登录校验
注册登录和密码验证
用户注册时,文件中保存用户名,和密码的密文
登录时,密码与文件中的密文进行比较,如果相同就同意登录
import hashlib # 导入模块 def md5(username,password): # 加密函数 md5 = hashlib.md5(username[::-1].encode('utf-8')) # 加盐加密,加密方式md5 md5.update(password.encode('utf-8')) return md5.hexdigest() def get_line(): with open('userinfo', encoding='utf-8') as f: # 登录验证 for line in f: user, pwd = line.strip().split(',') yield user,pwd def register(): flag = True # 账号注册函数 while flag: username = input('user :') password = input('passwd :') for user,pwd in get_line(): if user == username: print('您输入的用户名已经存在') break else: flag = False password = md5(username,password) with open('userinfo',encoding='utf-8',mode='a') as f: # 文件写入 f.write('%s,%s\n'%(username,password)) def login(): username = input('user :') password = input('passwd :') for user,pwd in get_line(): # 登录验证,调用get_line函数 if username == user and pwd == md5(username,password): return True ret = login() if ret: print('登陆成功')