我正在尝试在将密码发送到数据库之前对其进行哈希处理。 这是我的问题:
i=0
while ( i < len(userList)):
query = "INSERT INTO login (created, username, password, email) VALUES (NOW(), '" + userList[i][0] + "','" + hashmodel.PasswordHash.new_password_hash(userList[i][1]) + "','" + userList[i][2] + "')"
i += 1
settings.database_connection.execute_sql(query)
,密码哈希类如下所示:
class PasswordHash():
def new_password_hash(password):
hashed = bcrypt.hashpw(password.encode('utf-8'), bcrypt.gensalt())
return hashed
首先我在尝试散列未编码的密码时收到错误消息:“Unicode对象必须在散列之前编码”然后当我添加.encode('utf-8')时我得到“不能将'bytes'对象隐式转换为str“。
任何人都有解决方案吗?
错误:
Traceback (most recent call last):
File "/Users/xxx/Documents/xxx/setup.py", line 1, in <module>
from lib.models import loginmodel, appmodel, imagemodel, versionmodel, packagemodel
File "/Users/xxx/Documents/xxxx/lib/models/loginmodel.py", line 7, in <module>
class Login(basemodel.BaseExtModel):
File "/Users/xxxx/Documents/xxxx/lib/models/loginmodel.py", line 21, in Login
query = "INSERT INTO login (created, username, password, email) VALUES (NOW(), '" + userList[i][0] + "','" + hashmodel.PasswordHash.new_password_hash(userList[i][1]) + "','" + userList[i][2] + "')"
TypeError: Can't convert 'bytes' object to str implicitly