【问题标题】:how do i compare the contents of a text file with that of a set of inputs如何将文本文件的内容与一组输入的内容进行比较
【发布时间】:2018-03-15 11:57:38
【问题描述】:

所以我正在尝试创建一个程序,允许用户使用用户名和密码登录学校项目。但是,我的老师(他允许我问 FYI)希望我们想办法确保它的安全。

所以我的想法是允许用户创建登录名并将用户名和密码存储在记事本文件中。为了确保这些安全,我决定使用hash() 函数,这样即使访问文本文件也无法看到用户名和密码。我遇到的问题是我无法弄清楚如何让程序在文本文件中查看保存的用户名和密码的哈希版本,然后将其与输入进行比较以渴望在不打印哈希和或将它们保存为变量。

但是我不能这样做,因为如果我在登录文件中保存了多个登录名,我就无法将所有散列登录名保存为一个变量。

如果有人能提供帮助,将不胜感激

import sys
import time

print ("welcome to this quiz.")


account = input ("first off do you have a account already. please enter yes or no only").lower


if account == no:
    account_create = input ("to continue with this quiz you need a password would you like to create a account. please enter yes or no only").lower
    if account_create == no:
        print (" the program will close in 30 seconds. if you change your mind come back and make an account")
        time.sleep(30)
        sys.exit
    else:
        print ("thank you for creating an account")

        username = input ("first off you need a username. please enter a username. be carefull once it is chasen it cant be changed")
        # need to add a function that searches the login file and compares no username is repeated
        password = input ("secondly you need a password. please choose a password. be carefull you can change it later but you will need the current one to do this.")

        username = hash(username)
         password = hash(password)

         file = open("Login.txt","w")
         file.write (username)
         file.write (",")
         file.write (password)
         file.write("\n")
         file.close()

print ("Your login details have been saved. ")
        print ("now please login")

else:

    login? = input ("would you like to login to the program").lower
    if login? == no:
        print ("please come back another time")
        time.sleep(20)
        sys.exit
    else:
        username_check = input ("please enter your username")
        password_check = input ("please enter your username")

        username_check = hash(username_check)
        password_check = hash(password_check)

        file = open("Login.txt","r")
        if username_check == 

【问题讨论】:

  • 我最近也确实认为我可以为每个人分配一个与他们在文本文件中的行相关的登录代码。这将允许代码使用它来识别文件中与帐户对应的特定行。但这并不完全理想,就像用户忘记了他们无法登录的代码一样。
  • 如果您提供一个您尝试过但对您不起作用的代码示例,您的问题会更受关注。尝试使该代码示例也立即可运行。

标签: python text-files login-script


【解决方案1】:

将文件作为字典加载,将用户名(或其哈希)映射到密码的哈希。

您可以遍历文件并将每一行加载到您的字典中。

但是这会有点脆弱。如果某些用户想在他们的用户名中有空格或逗号怎么办。更好更简单的方法是使用库来序列化和反序列化字典。 python 中常用的一个是pickle,或者如果你希望数据在某种程度上仍然是人类可读的json

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-08
    • 2013-03-29
    • 2013-03-18
    • 1970-01-01
    相关资源
    最近更新 更多