Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用£验证密码;或$characters无法使用passlib_Python_Passlib - Fatal编程技术网

Python 使用£验证密码;或$characters无法使用passlib

Python 使用£验证密码;或$characters无法使用passlib,python,passlib,Python,Passlib,我使用passlib==1.7.1进行以下导入: from passlib.apps import custom_app_context as pwd_context 然后使用以下内容对密码进行哈希运算: let encodedString = btoa('doug:Tree£9') console.log(encodedString) console.log(atob(encodedString)) pwd\u上下文。加密(密码) 然后,我用以下方式进行验证: pwd_context.ve

我使用passlib==1.7.1进行以下导入:

from passlib.apps import custom_app_context as pwd_context
然后使用以下内容对密码进行哈希运算:

let encodedString = btoa('doug:Tree£9')
console.log(encodedString)
console.log(atob(encodedString))
pwd\u上下文。加密(密码)

然后,我用以下方式进行验证:

pwd_context.verify(password, self.password_hash)
这很好,但验证在某些字符中失败。e、 g.“英镑”或“美元”

有人知道为什么会这样吗

谢谢大家!


更新:

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

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

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
我注意到“ZG91ZzpUcmVlozk=”和“ZG91ZzpUcmVlwqM5”不一样。然而,这两种方法都在各自的语言中工作

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

import base64
print base64.b64decode("ZG91ZzpUcmVlozk=")
我得到:

doug:Tree�9
请注意,%字符现在已被捣碎

其他Unicode字符也会失败

因此,我认为问题在于如何对授权标头进行编码,以便python正确识别字符,以及用户选择的任何其他字符作为密码

非常感谢


编辑:已解决

我找到了这个,里面有一些细节。我使用@brandonscript推荐的以下方法解决了这个问题

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

很好用!呸

从文档来看,我猜您遇到了编码问题

使用PasswordHash.hash()散列密码。此调用负责unicode编码


在我的系统上运行得非常好。也许你需要在你的脚本顶部设置你的默认编码,顺便说一句,我不能重现同样的问题,你的代码可以正常工作。所以请提供完整的代码输入和输出。
# -*- 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