首页 > 解决方案 > GetPassWarning:无法控制终端上的回显

问题描述

我正在创建一个骰子游戏,并想引入密码哈希,用户需要输入他们的用户名和密码。我是散列新手,我试过这个:

from getpass import getpass
from hashlib import pbkdf2_hmac as pwhash

def login(users):
    username = input('please enter username: ')
    if username not in users:
        print("access denied")
        exit()

    password_hash = None
    while password_hash != users[username]:
        password_hash = pwhash("sha256", getpass().encode("ascii"), b"t4h20p8g24j", 100000)

    print("access granted")
    return username
login(users)

然后我在控制台上收到以下消息:

GetPassWarning: Can not control echo on the terminal.

所以我尝试了一个不同的ide(从idle到intellij pycharm)但同样的问题仍然出现。

我看到了其他问题,但 an environment where stdin, stdout and stderr are connected to /dev/tty, or another PTY-compliant device. 对我来说真的没有意义我试图发表评论,但我需要更多代表。我也在 PyCharm 上运行而不是空闲

标签: pythonhash

解决方案


回显输入是因为 while 循环从未退出,因为条件不满足:字典中的密码存储为纯文本,只需要散列字典中的值。

def check_password(hashed_password, user_password):
    password, salt = hashed_password.split(':')
    return password == hashlib.sha256(salt.encode() + user_password.encode()).hexdigest()

推荐阅读