1. 程式人生 > >用戶註冊和登陸系統,采用hmac加密密碼

用戶註冊和登陸系統,采用hmac加密密碼

md5 lease format you pri pick exist utf wrong

import hashlib
import pickle, os
import hmac, random


def hmac_sha1(key, s):
return hmac.new(key.encode(‘utf-8‘), s.encode(‘utf-8‘), ‘MD5‘).hexdigest()


# 獲取用戶名和密碼信息
if os.path.exists(‘/Users/lewisliu/user_information.txt‘):
with open(‘/Users/lewisliu/user_information.txt‘, ‘rb‘) as f:
user_information = pickle.load(f)
else:
user_information = dict()

# 註冊或者登陸
while True:
# 選擇模式,註冊、登陸、退出
model = input("pleas input enroll/login/exit:")
if model == ‘enroll‘:
name = input("please input your name:")
# 用戶名重復識別
if name in user_information.keys():
print("name is occupied!")
continue
password = input("please input your password:")
# 數據庫添加用戶名和密碼
key = ‘liu‘ # ‘‘.join([chr(random.randint(48, 122)) for i in range(20)])
user_information[name] = hmac_sha1(key, password)
# sha1 = hashlib.sha1()
# sha1.update(password.encode(‘utf-8‘))
# user_information[name] = sha1.hexdigest()
elif model == ‘login‘:
name = input("please input your name:")
if name not in user_information.keys():
print("name is not exist!")
continue
password = input("please input your password:")
key = ‘liu‘ # ‘‘.join([chr(random.randint(48, 122)) for i in range(20)])
password = hmac_sha1(key, password)
# sha1 = hashlib.sha1()
# sha1.update(password.encode(‘utf-8‘))
# password = sha1.hexdigest()
if user_information[name] == password:
print("Wellcome!")
else:
print("wrong name or password! Exit!")
with open(‘/Users/lewisliu/user_information.txt‘, ‘wb‘) as f:
pickle.dump(user_information, f)
exit()
elif model == ‘exit‘:
with open(‘/Users/lewisliu/user_information.txt‘, ‘wb‘) as f:
pickle.dump(user_information, f)
exit()
else:
print("model wrong!")

用戶註冊和登陸系統,采用hmac加密密碼