首页 > 解决方案 > Bcrypt Hash Returns TypeError("Unicode-objects must be encrypted before hashing") and Invalid Salt

问题描述

我已经查看了与此相关的所有 StackOverflow 问题,但我似乎无法弄清楚这一点。当我对密码进行哈希处理并对其自身进行检查时,它会使用当前代码返回 TypeError "Unicode-objects must be encrypted before hashing":

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

标签: pythonflaskbcryptsalt

解决方案


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

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

处理这个问题的最简洁的方法是使用第 1Binary——或者更好的是2列——来存储你的哈希值,而不是列。任何支持的 DB-API都只会按原样存储,并将其作为 a 返回,这正是您想要的。Binary(60)StringBinarybytesbytes


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

2. 在默认设置下,bcrypt可打印的摘要总是正好是 60 字节。BINARY(60)数据库通常可以更紧凑地存储固定宽度字段,并且比可变宽度字段(如VARBINARY. 只使用 plainBINARY可能很好,但它也可能像 一样工作VARBINARY,或者它可能会浪费空间并像 一样工作BINARY(255),等等。


推荐阅读