首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用Python加密密码时出错

使用Python加密密码时出错
EN

Stack Overflow用户
提问于 2017-07-10 20:21:18
回答 1查看 100关注 0票数 1

当我使用Python的bz2模块加密密码时,我得到了以下错误。在这里,我将加密值保存在DB中。

错误:

代码语言:javascript
复制
ProgrammingError at /signsave/
You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.
Request Method: POST
Request URL:    http://127.0.0.1:8000/signsave/
Django Version: 1.11.2
Exception Type: ProgrammingError
Exception Value:    
You must not use 8-bit bytestrings unless you use a text_factory that can interpret 8-bit bytestrings (like text_factory = str). It is highly recommended that you instead just switch your application to Unicode strings.

下面是我的代码:

代码语言:javascript
复制
def signsave(request):
    """This function helps to save signup data"""

    if request.method == 'POST':
        name = request.POST.get('uname')
        password = request.POST.get('pass')
        con_pass = request.POST.get('conpass')
        new_pass = bz2.compress(password) 
        if password == con_pass:
            passw = User(
                uname=name,
                password=new_pass,
                raw_password=password,
            )
            passw.save()
            message = "Registered successfully"
            return render(request, 'bookingservice/login.html', {'msg': message})
        else:
            message = "The password did not match "
            return render(request, 'bookingservice/signup.html', {'msg': message})

在这里,当我试图保存加密值时,这些错误就来了。

EN

回答 1

Stack Overflow用户

发布于 2017-07-10 20:34:23

除了压缩,你不应该使用bz2做任何事情。改用内置的hashlib模块。

hashlib.sha256(str.encode(password)).digest()替换bz2.compress(password。您将获得密码字符串的SHA256散列,您可以将其与其他字符串的散列进行比较,以证明其有效性。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/45011897

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档