gpt4 book ai didi

python - Bcrypt Hash 返回 TypeError ("Unicode-objects must be encoded before hashing") 和 Invalid Salt

转载 作者:行者123 更新时间:2023-12-01 14:02:49 26 4
gpt4 key购买 nike

我已经查看了与此相关的所有 StackOverflow 问题,但我似乎无法弄清楚这一点。当我对密码进行散列并对其自身进行检查时,它会使用当前代码返回 TypeError“Unicode 对象必须在散列之前进行编码”:

from scripts import tabledef
from flask import session
from sqlalchemy.orm import sessionmaker
from contextlib import contextmanager
import bcrypt

(Unrelated Python code...)

def hash_password(password):
return bcrypt.hashpw(password.encode('utf8'), bcrypt.gensalt())


def credentials_valid(username, password):
with session_scope() as s:
user = s.query(tabledef.User).filter(
tabledef.User.username.in_([username])).first()
if user:

return bcrypt.checkpw(password.encode('utf8'), user.password)
else:
return False

当我尝试通过设置 user.password= user.password.encode('utf8') 来修复此错误时,我得到“无效的盐”。

这段代码有什么问题?

更新:
我通过用户的 Flask 输入存储密码:
import json
import sys
import os
import plotly
import pandas as pd
import numpy as np
import plotly.graph_objs as go


from scripts import tabledef
from scripts import forms
from scripts import helpers
from flask import Flask, redirect, url_for, render_template, request, session, flash, Markup
from flask_socketio import SocketIO, emit

@app.route('/', methods=['GET', 'POST'])
def login():
if not session.get('logged_in'):
form = forms.LoginForm(request.form)
if request.method == 'POST':
username = request.form['username'].lower()
password = request.form['password']
if form.validate():
if helpers.credentials_valid(username, password):
session['logged_in'] = True
session['username'] = username
session['email'] = request.form['email']
session['password'] = request.form['password']
return json.dumps({'status': 'Login successful'})
return json.dumps({'status': 'Invalid user/pass'})
return json.dumps({'status': 'Both fields required'})
return render_template('login.html', form=form)
user = helpers.get_user()
return render_template('home.html', user=user)

@app.route('/signup', methods=['GET', 'POST'])
def signup():
if not session.get('logged_in'):
form = forms.LoginForm(request.form)
if request.method == 'POST':
username = request.form['username'].lower()
password = helpers.hash_password(request.form['password'])
email = request.form['email']
if form.validate():
if not helpers.username_taken(username):
helpers.add_user(username, password, email)
session['logged_in'] = True
session['username'] = username
session['email'] = request.form['email']
session['password'] = request.form['password']
return json.dumps({'status': 'Signup successful'})
return json.dumps({'status': 'Username taken'})
return json.dumps({'status': 'User/Pass required'})
return render_template('login.html', form=form)
return redirect(url_for('login'))

这是我得到的错误:
/lib/python3.5/site-packages/flask/app.py", line 1718, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/home/suraj/Documents/Programming/current-projects/GW_Dining_Tracker/env/lib/python3.5/site-packages/flask/_compat.py", line 35, in reraise
raise value
File "/home/suraj/Documents/Programming/current-projects/GW_Dining_Tracker/env/lib/python3.5/site-packages/flask/app.py", line 1813, in full_dispatch_request
rv = self.dispatch_request()
File "/home/suraj/Documents/Programming/current-projects/GW_Dining_Tracker/env/lib/python3.5/site-packages/flask/app.py", line 1799, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/home/suraj/Documents/Programming/current-projects/GW_Dining_Tracker/Flaskex-master/app.py", line 34, in login
if helpers.credentials_valid(username, password):
File "/home/suraj/Documents/Programming/current-projects/GW_Dining_Tracker/Flaskex-master/scripts/helpers.py", line 64, in credentials_valid
return bcrypt.checkpw(password.encode('utf8'), user.password)
File "/home/suraj/Documents/Programming/current-projects/GW_Dining_Tracker/env/lib/python3.5/site-packages/bcrypt/__init__.py", line 101, in checkpw
raise TypeError("Unicode-objects must be encoded before checking")
TypeError: Unicode-objects must be encoded before checking

最佳答案

问题是您从 SQLAlchemy String 获取值列并将其传递给 bcrypt.checkpw . String用于 Unicode 字符串,它提供的值是 str .但是bcrypt仅适用于字节字符串,因此它需要 bytes .这就是TypeError说“在散列之前必须对Unicode对象进行编码”是在告诉你。

当您保存 bytes 时,取决于您使用的数据库后端和 DB-API 库(对于某些后端,还取决于您的数据库的配置方式)。值(value) sString列,它可能会保存 s.decode() ,在这种情况下,您可以使用 user.password.encode()取回相同的字节——但它可能不会。例如,它也可以只保存,比如 str(s) .在这种情况下,如果散列是 bytes值(value) b'abcd' ,列值将是字符串 "b'abcd'" ,所以和调用 encode在这让你b"b'abcd'" ,不是 b'abcd' .

处理此问题的最简洁方法是使用 Binary column1——或者,也许更好,Binary(60) 2—存储您的哈希值,而不是 String柱子。任何支持 Binary 的 DB-API将只存储一个 bytes按原样返回,并将其作为 bytes 返回,这正是您想要的。

1. Binary是可选类型。如果您的 DB-ABI 不存在它,则可以使用与 BINARY 相同的类型。 .如果没有,请查看 the list of types并尝试从 _Binary 继承的其他类型.名称或首字母缩写词中没有“大”的那些可能会更有效率,但否则它们中的任何一个都应该工作。

2. 使用默认设置,bcrypt可打印的摘要总是正好是 60 个字节。数据库通常可以存储固定宽度的字段,如 BINARY(60)VARBINARY 等宽度可变的字段更紧凑,搜索速度更快.仅使用普通 BINARY可能没问题,但它也可能像 VARBINARY 一样工作,否则可能会浪费空间并像 BINARY(255) 一样工作, 等等。

关于python - Bcrypt Hash 返回 TypeError ("Unicode-objects must be encoded before hashing") 和 Invalid Salt,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51052632/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com