python hashlib

"""
md5 加密算法
数据加密三大步骤:
1.获取一个加密对象
2.使用加密对象的update进行加密,update方法可以调用多次
3.通常使用hexdigest获取加密结果
"""
import hashlib
# m =hashlib.md5()
# m.update('中文add'.encode('utf-8'))
# res =m.hexdigest()
#登录 注册程序

def register(username,pwd):
#加密
res = get_md5(username,pwd)
#写入文件
with open('login',encoding='utf-8',mode='at') as f:
f.write(res)
f.write(' ')
def login(username,pwd):
#获取当前加密结果
res = get_md5(username,pwd)
with open('login',encoding='utf-8',mode='rt') as f:
for i in f:
if res == i.strip():
return True
else:
return False
def get_md5(username,pwd):
m = hashlib.md5()
m.update(username.encode('utf-8'))
m.update(pwd.encode('utf-8'))
return m.hexdigest()
while 1:
op =int(input('1.注册 2. 登录 3. 退出'+' '))
if op == 3:break
if op == 1:
username = input('用户名:').strip()
pwd = input('密码:').strip()
register(username,pwd)
elif op == 2:
username = input('用户名:').strip()
pwd = input('密码:').strip()
res = login(username,pwd)
if res:
print('success!')
else:
print('loser!')
原文地址:https://www.cnblogs.com/shaohuagu/p/12261527.html