bcrypt.checkpw返回TypeError:检查之前必须对Unicode对象进行编码 [英] bcrypt.checkpw returns TypeError: Unicode-objects must be encoded before checking

查看:167
本文介绍了bcrypt.checkpw返回TypeError:检查之前必须对Unicode对象进行编码的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在呼叫bcrypt.checkpw,以检查未加密的密码是否与存储在凭据数据库中的哈希密码匹配,但是会收到

I am calling bcrypt.checkpw to check unencrypted password matches with hashed password stored in the credential database, but receive

TypeError:必须先对Unicode对象进行编码

TypeError: Unicode-objects must be encoded before checking

我应如何解决此问题?有什么建议吗?
我安装了python 2.7.6bcrypt 3.1.1

How should I resolve this issue? Any suggestion?
I installed python 2.7.6, and bcrypt 3.1.1

我有以下代码:

def check_password(password, hashed_password)
    if not bcrypt.checkpw(password, hashed_password):
        raise InvalidCredentials("403 Forbidden")
    else:
        return true

并收到以下错误:

文件"/home/qt/virtualenv/lib/python2.7/site-packages/bcrypt/init.py",第100行,在checkpw中
引发TypeError(检查之前必须对Unicoed对象进行编码")
TypeError:检查之前必须先对Unicode对象进行编码

File "/home/qt/virtualenv/lib/python2.7/site-packages/bcrypt/init.py", line 100, in checkpw
raise TypeError("Unicoed-objects must be encoded before checking")
TypeError: Unicode-objects must be encoded before checking

我调查了bcrypt/__init__.py,但不确定为什么

I looked into bcrypt/__init__.py, but I'm not sure why

def checkpw(password, hashed_password):    
    if (isinstance(password, six.text_type) or            
        isinstance(hashed_password, six.text_type)):        
    raise TypeError("Unicode-objects must be encoded before checking")

推荐答案

我假设您使用的是Python3.对于Python 3,默认情况下,字符串是unicode字符串.

I make the assumption that you use Python 3. With Python 3, strings are, by default, unicode strings.

如果您使用unicode值调用bcrypt.checkpw()函数:

If you call the bcrypt.checkpw() function with unicode values:

import bcrypt

password = "seCr3t"  # unicode string
hashed_password = "hashed_seCr3t"  # unicode string

bcrypt.checkpw(password, hashed_password)

您将收到此异常

Traceback (most recent call last):
  ...
TypeError: Unicode-objects must be encoded before checking

原因很简单:加密函数仅适用于字节字符串(或实际上是数组).

The reason is simple: cryptographic functions only work on bytes strings (or arrays in fact).

您的密码 hashed_pa​​ssword 必须都是字节字符串.

You password and hashed_password must be both bytes strings.

如果使用bcrypt.hashpw()函数,则您的 hashed_pa​​ssword 必须是字节字符串,我认为问题出在 password 值上.此密码必须来自类似形式的HTML形式.要使用bcrypt.checkpw()函数,必须首先使用与使用bcrypt.hashpw()函数对密码进行加密的相同编码对字符串值进行编码.通常,我们选择"utf8"编码.

If you use the bcrypt.hashpw() function, your hashed_password must be a bytes string, and I think the problem is for the password value. This password must come from a HTML form of something similar. To use the bcrypt.checkpw() function, you must first encode the string value using the same encoding you use to encrypt the password with the bcrypt.hashpw() function. Usually, we choose 'utf8' encoding.

例如(Python 2和3):

For instance (Python 2 & 3):

import bcrypt

# at creation first:
password = u"seCr3t"
hashed_password = bcrypt.hashpw(password.encode('utf8'), bcrypt.gensalt())

# first attempt:
password = u"seCrEt"
bcrypt.checkpw(password.encode('utf8'), hashed_password)
# -> False

# second attempt:
password = u"seCr3t"
bcrypt.checkpw(password.encode('utf8'), hashed_password)
# -> True

Gihub页面上查看简单用法

这篇关于bcrypt.checkpw返回TypeError:检查之前必须对Unicode对象进行编码的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆