简体   繁体   English

在 Bcrypt (Python) 中使用 SQLite 作为密码字段返回

[英]Using an SQLite return as a password field within Bcrypt (Python)

I want to check if the input password is the same as that stored in the database but when I use bcrypt.checkpw() it returns an error saying that it expects a string or byte because the SQL query returns a tuple.我想检查输入的密码是否与数据库中存储的密码相同,但是当我使用bcrypt.checkpw()时,它返回一个错误,指出它需要一个字符串或字节,因为 SQL 查询返回一个元组。 I can't find a way to convert the database response to a byte from a tuple to make it compatible.我找不到将数据库响应转换为元组中的字节以使其兼容的方法。

sql = ''' SELECT password FROM user_data WHERE username=? '''

username = input('Input username: ')
password = bytes(input('Input Password: '), encoding='utf-8')

cur = conn.cursor()
cur.execute(sql, (username,))
rows = cur.fetchall()

for row in rows:

    if bcrypt.checkpw(password, row):
        details = (user_id, username, password)
        print('logged in')

        return details

        break

Simply adding row[0] within the function solves the problem as it returns the first (and only) value inside the tuple.只需在 function 中添加 row[0] 即可解决问题,因为它会返回元组中的第一个(也是唯一一个)值。

Treat it like a list in other words换句话说,把它当作一个列表

Extracting the a value from a tuple when the other values are unused 当其他值未使用时从元组中提取一个值

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

相关问题 在登录 function 中使用 Python、ZF754876303939E78E6A5 - Getting a password to matches with its hash in a login function using Python, SQlite and bcrypt 如何使用bcrypt使用Python Flask-Security加密密码? - How to encrypt password using Python Flask-Security using bcrypt? 在Python中使用Bcrypt哈希密码时出错 - Getting error while hashing password using Bcrypt in Python Python是否可以识别Java中使用BCrypt的哈希密码? - Can a hashed password using BCrypt in Java be recognised by Python? 使用python / bcrypt将密码保存为用户集合中mongodb中的salted哈希 - save password as salted hash in mongodb in users collection using python/bcrypt Python,使用 SQL 连接器进行 Bcrypt 密码检查 - Python, Bcrypt password checking with SQL-Conncector 使用 bcrypt 密码散列进行用户身份验证 - using bcrypt password hashing for user authentication 如何使用 Chromedriver 和 Selenium Python 在 Instagram 登录页面中找到用户名和密码字段 - How to locate the username and password field within Instagram login page using Chromedriver and Selenium Python 如何使用 bcrypt 将纯文本密码与散列密码进行比较? - How to compare plain text password to hashed password using bcrypt? 在sqlite3语句中使用Python变量 - Using Python variables within sqlite3 statement
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM