简体   繁体   English

Python登录验证系统

[英]Python login verification system

This is a login system where the user types in their details and then this code verifies if the user exists 这是一个登录系统,用户输入其详细信息,然后此代码验证用户是否存在

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")

Right now the code looks in the text file for this format: 现在,代码在文本文件中查找以下格式:

username: (username) password: (password)

But now I want to add a hash code for every user so the format in the file looks like this: 但是现在我想为每个用户添加一个哈希码,因此文件中的格式如下所示:

49ad2322f9a401e9e3c4ae7694b6b1f1 username: (username) password: (password)

How would I change the verfiy_login to make it look for a random 32 characters at the beginning of every user? 如何更改verfiy_login,使其在每个用户的开头都随机查找32个字符?

Instead of reconstructing the line using "username: " + username + " password: " + password then comparing it to the line from the text file, you can parse it by first split ting the string then unpacking the tokens. 与其使用"username: " + username + " password: " + password重建行,然后将其与文本文件中的行进行比较,您可以通过首先split字符串然后解压缩标记来对其进行解析。 By splitting the line, you get access to the hash, username, and password individually as variables. 通过拆分行,您可以分别访问哈希,用户名和密码作为变量。

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

Unpacking a token into _ is a convention saying that "we will dump this value". 按照惯例,将令牌解压缩为_表示“我们将转储该值”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM