这是一个登录系统,用户输入其详细信息,然后此代码验证用户是否存在
counter = 0
def validation():
global counter
print("Sorry, this username or password does not exist please try again")
counter += 1
if counter == 3:
print("----------------------------------------------------")
print("You have been locked out please restart to try again")
sys.exit()
def verify_login(username, password, login_data):
for line in login_data:
if ("username: " + username + " password: " + password) == line.strip():
return True
return False
check_failed = True
while check_failed:
with open("accountfile.txt","r") as username_finder:
print("Could player 1 enter their username and password")
username1=input("Please enter your username ")
password1=input("Please enter your password ")
if verify_login(username1, password1, username_finder):
print("you are logged in")
现在,代码在文本文件中查找以下格式:
username: (username) password: (password)
但是现在我想为每个用户添加一个哈希码,以便文件中的格式如下:
49ad2322f9a401e9e3c4ae7694b6b1f1 username: (username) password: (password)
我如何更改verfiy_login以使其随机查找32 每个用户开头的字符?
答案 0 :(得分:0)
与其使用"username: " + username + " password: " + password
来重建行,然后将其与文本文件中的行进行比较,还可以通过首先split
设置字符串然后解压缩标记来对其进行解析。通过拆分行,您可以分别访问哈希,用户名和密码作为变量。
def verify_login(username, password, login_data):
for line in login_data:
hsh, _, username_from_file, _, password_from_file = line.strip().split()
if len(hsh) == 32 and username == username_from_file and password == password_from_file:
return True
return False
将令牌解包到_
中是一种惯例,它表示“我们将转储该值”。