使用passlib验证带有$或$字符的密码失败

2024-09-29 17:24:16 发布

您现在位置:Python中文网/ 问答频道 /正文

我将passlib==1.7.1用于以下导入:

from passlib.apps import custom_app_context as pwd_context

然后使用以下内容散列密码:

pwd_context.encrypt(password)

然后,我验证:

pwd_context.verify(password, self.password_hash)

这是可以的,但验证失败的某些字符。e、 g.“英镑”或“$”。你知道吗

有人知道为什么会这样吗?你知道吗

谢谢你!你知道吗


更新:

非常感谢大家。有了这些信息,我进行了更多的调查,似乎问题不是passlib,而是位于angular4之间的某个地方,我将base64授权头发送到flask应用程序。你知道吗

我目前正在使用以下方法来执行此操作:

let headers: Headers = new Headers({
        'Content-Type': 'application/json',
        'Authorization': 'Basic ' + btoa(userLoginRequest.username + ':' + userLoginRequest.password)
    });

我今天读了很多关于unescape的文章(它支持decodeURI())。我也读了很多关于在base64编码中支持unicode的文章。我尝试了这些东西的组合,但没有什么不同。我现在真的很困惑!你知道吗

为了测试发生了什么,我做了下面的步骤。在angular4中,我执行以下操作:

let encodedString = btoa('doug:Tree£9')
console.log(encodedString)
console.log(atob(encodedString))

正如预期的那样,这会将以下内容打印到控制台。你知道吗

ZG91ZzpUcmVlozk=
doug:Tree£9

所以编码和解码都很正常。你知道吗

在Python中执行相同的过程。。。你知道吗

import base64
encoded = base64.b64encode('doug:Tree£9')
print encoded
print base64.b64decode(encoded)

我在终点站得到了以下信息。你知道吗

ZG91ZzpUcmVlwqM5
doug:Tree£9

我注意到“zg91zzpucmvlowzk=”和“ZG91ZzpUcmVlwqM5”不一样。然而,这两种方法都在各自的语言中工作。你知道吗

如果我将“ZG91ZzpUcmVlozk=”javascript中的编码字符串放入python中,并按如下方式对其进行解码。。。你知道吗

import base64
print base64.b64decode("ZG91ZzpUcmVlozk=")

我得到:

doug:Tree�9

请注意,字符现在已被捣碎。你知道吗

其他Unicode字符也会失败。你知道吗

因此,我认为问题是如何对授权头进行编码,以便python正确识别字符,以及用户为其密码选择的任何其他字符?你知道吗

非常感谢!你知道吗


编辑:已解决!你知道吗

我找到了这个Using Javascript's atob to decode base64 doesn't properly decode utf-8 strings,其中有一些细节。我使用@brandonscript推荐的以下方法解决了这个问题。你知道吗

b64EncodeUnicode(str) : string{
    return btoa(encodeURIComponent(str).replace(/%([0-9A-F]{2})/g, function(match, p1) {
        return String.fromCharCode(parseInt(p1, 16))
    }))
}

工作完美!呸!你知道吗


Tags: 方法importtree编码pwdcontextpassword字符
2条回答
# -*- coding: utf-8 -*-
from passlib.apps import custom_app_context as pwd_contex

password='£$'

encrypted=pwd_contex.encrypt(password)

print(pwd_contex.verify('£$', encrypted))
print(pwd_contex.verify('john', encrypted))

True
False

在我的系统上运行得很好。也许您需要在脚本的顶部设置默认编码# -*- coding: utf-8 -*-

我猜你在文档中遇到了编码问题

http://passlib.readthedocs.io/en/stable/narr/hash-tutorial.html#hashing

Use PasswordHash.hash() to hash a password. This call takes care of unicode encoding....

from passlib.hash import pbkdf2_sha256

hash = pbkdf2_sha256.hash("$password")
pbkdf2_sha256.verify("$password", hash)
# True

相关问题 更多 >

    热门问题