python实现注册登录,密码用md5加密,但是两次加密得出的值不一样,这是为什么啊?

时间:2022-10-28 16:52:42
import hashlib
md5=hashlib.md5()
sha1=hashlib.sha1()
name=[]
password=[]
user={}
dict(user)
#用户注册
while True:
choice = input('1:用户注册\t2:验证登录\t')
choice = int(choice)
while choice==1:
print('----用户注册----'.center(40))
name_=input('(用户名由数字、字母、符号组成)\n请输入要注册的用户名:')
name.append(name_)
password_=input('(密码由数字、字母、符号组成)\n请设置用户密码')
print(password_)
print('通过md5加密中...'.center(40))
md5.update(password_.encode('utf-8'))
print(md5.hexdigest())
password.append(md5.hexdigest())
#将用户名和密码保存到字典
choice1=input('注册成功!\b\n输入1返回\t输入2继续注册\t')
choice1=int(choice1)
if choice1==1:
break
else:
continue
user = dict(zip(name, password))
print(user)
#验证登录
while choice==2:
print('----用户登入----'.center(40))
_name=input('用户名:')
if _name in user.keys():
_password = input('密码:')
print(_password)
print('md5加密中...'.center(40))
md5.update(_password.encode('utf-8'))
print(md5.hexdigest())
if md5.hexdigest()==user[_name]:
print('登录成功!\n欢迎使用!'.center(40))
break
else:
print('密码错误!'.center(40))
continue
else:
print('用户名不存在!'.center(40))
continue