0

所以我回来做旧项目,我找不到什么问题。这是第一次创建密码的部分,来自主脚本:

def first():
    if os.path.isfile("secret.txt"):
        folder()
    else:
        os.system("echo > secret.txt")
        password = getpass.getpass("Set your password please --> ")
        while len(password) < 4:
            print("Password must have more then 4 characters!")
        else:
            password1 = getpass.getpass("repeat your password please --> ")
            while password1 != password:
                print("Password don't match")
                password1 = getpass.getpass("repeat your password please --> ")
            if password1 == password:
                a = open('secret.txt', 'w').close()
                f = open('secret.txt', 'w')
                hashed_password = pbkdf2_sha256.hash(password)
                f.write(hashed_password)
                os.system("attrib +h secret.txt")
                folder()

这是登录脚本,从这里检查密码:

def log_in():
    f = open("secret.txt", "r")
    Password = f.read()
    x = 0
    while x < 5:
        getPass = getpass.getpass("Password:")
        if not pbkdf2_sha256.verify("getPass", Password):
            print("Password is invalid")
            x = x + 1
        else:
            f.close()
            os.system('cls')
            print("Welcome back sir\n")
            x = 10
            time.sleep(2)
    if x == 5:
        print("acces denied")
        time.sleep(5)
        os.system("nothing.bat")

所以问题是当我尝试验证密码时它说它不正确但密码是相同的。在文档中它说:

请注意,由于每次调用都会生成一个新的盐,因此生成的哈希的内容在调用之间会有所不同(尽管使用相同的密码作为输入):

如果这是 .verify() 的问题,那我该怎么办?

我不确定这是否足够的信息,如果不是,我将发布整个源代码

我可能错过了一些愚蠢的事情,但我似乎无法找到它..

4

1 回答 1

0

我认为问题是:

if not pbkdf2_sha256.verify("getPass", Password):

将其更改为:

if not pbkdf2_sha256.verify(getPass, Password):

您调用了 str“getPass”而不是用户输入的密码。

于 2017-08-20T02:10:29.853 回答