我正在尝试使用Bcrypt加密用户在注册时提供的密码,然后使用Bcrypt验证用户在登录时存储在数据库中的散列版本时提供的密码.

有关如何通过Django docs安装Bcrypt的一些非常好的文档,但它们实际上并没有向您展示如何使用Bcrypt来散列密码或使用其他命令.

你需要从某个地方导入Brcrypt吗?如果是这样,它的正确语法是什么?散列密码和将散列密码与非散列密码进行比较的语法是什么?

我在settings.py文件中安装了Bcrypted库,并通过pip安装了Bcrypt.使用Bcrypt还需要做什么?

解决方法

在你的链接:

The password attribute of a User object is a string in this format:

<algorithm>$<iterations>$<salt>$<hash> Those are the components used
for storing a User’s password
,separated by the dollar-sign character
and consist of: the hashing algorithm,the number of algorithm
iterations (work factor),the random salt,and the resulting password
hash. The algorithm is one of a number of one-way hashing or password
storage algorithms Django can use; see below. Iterations describe the
number of times the algorithm is run over the hash. Salt is the random
seed used and the hash is the result of the one-way function.

I installed the Bcrypted library in the settings.py file…
What else do I need to do to use Bcrypt?

我不确定第一句话是什么意思.您需要在settings.py中添加以下内容:

PASSWORD_HASHERS = (
    'django.contrib.auth.hashers.BCryptSHA256PasswordHasher','django.contrib.auth.hashers.BCryptPasswordHasher','django.contrib.auth.hashers.PBKDF2PasswordHasher','django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher','django.contrib.auth.hashers.SHA1PasswordHasher','django.contrib.auth.hashers.MD5PasswordHasher','django.contrib.auth.hashers.CryptPasswordHasher',)

use Bcrypt to validate a password a user provides upon login against
the hashed version stored in the database.

您可以手动执行此操作:

The django.contrib.auth.hashers module provides a set of functions to
create and validate hashed password. You can use them independently
from the User model.

check_password(password,encoded)
If you’d like to manually authenticate a user by comparing a plain-text password to the hashed
password in the database,use the convenience function
check_password(). It takes two arguments: the plain-text password to
check,and the full value of a user’s password field in the database
to check against,and returns True if they match,False otherwise.

https://docs.djangoproject.com/en/1.9/topics/auth/passwords/#module-django.contrib.auth.hashers

或者,您可以使用authenticate():

authenticate(**credentials)
To authenticate a given username and password,use authenticate(). It takes credentials in the form of
keyword arguments,for the default configuration this is username and
password,and it returns a User object if the password is valid for
the given username. If the password is invalid,authenticate() returns
None. Example:

06001

https://docs.djangoproject.com/en/1.9/topics/auth/default/#authenticating-users

这里有些例子:

(django186p34)~/django_projects/dj1$python manage.py shell

Python 3.4.3 (v3.4.3:9b73f1c3e601,Feb 23 2015,02:52:03) 
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help","copyright","credits" or "license" for more information.
(InteractiveConsole)

>>> from django.conf import settings
>>> print(settings.PASSWORD_HASHERS)

('django.contrib.auth.hashers.PBKDF2PasswordHasher','django.contrib.auth.hashers.BCryptSHA256PasswordHasher','django.contrib.auth.hashers.UnsaltedSHA1PasswordHasher','django.contrib.auth.hashers.UnsaltedMD5PasswordHasher','django.contrib.auth.hashers.CryptPasswordHasher')

这些是默认值:我的settings.py中没有PASSWORD_HASHERS条目.

>>> from django.contrib.auth.models import User

>>> my_user = User.objects.create_user('ea87','ea@gmail.com','666monkeysAndDogs777')

>>> my_user.save()
>>> my_user.password
'pbkdf2_sha256$20000$L7uq6goI1HIl$RYqywMgPywhhku/YqIxWKbpxODBeczfLm5zthHjNSSk='
>>> my_user.username
'ea87'

>>> from django.contrib.auth import authenticate

>>> authenticate(username='ea87',password='666monkeysAndDogs777')
<User: ea87>

>>> print(authenticate(username='ea87',password='wrong password'))
None

>>> from django.contrib.auth.hashers import check_password

>>> check_password('666monkeysAndDogs777',my_user.password)
True

>>> exit()

接下来,我将以下内容添加到settings.py中:

PASSWORD_HASHERS = (
    'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',)
(django186p34)~/django_projects/dj1$python manage.py shell

Python 3.4.3 (v3.4.3:9b73f1c3e601,"credits" or "license" for more information.
(InteractiveConsole)

>>> from django.conf import settings
>>> print(settings.PASSWORD_HASHERS)
('django.contrib.auth.hashers.BCryptSHA256PasswordHasher','django.contrib.auth.hashers.CryptPasswordHasher')

注意元组前面的bcrypt哈希.

>>> from django.contrib.auth.models import User

>>> user = User.objects.get(username='ea87')
>>> user
<User: ea87>

>>> user.password
'pbkdf2_sha256$20000$DS20ZOCWTBFN$AFfzg3iC24Pkj5UtEu3O+J8KOVBQvaLVx43D0Wsr4PY='

>>> user.set_password('666monkeysAndDogs777')
>>> user.password
'bcrypt_sha256$$2b$12$QeWvpi7hQ8cPQBF0LzD4C.89R81AV4PxK0kjVXG73fkLoQxYBundW'

您可以看到密码已更改为bcrypt版本.

如何使用Bcrypt加密Django中的密码的更多相关文章

  1. 难以在Android和iPhone上部署Django应用程序..计划切换到PHP而不是Django

    .我是否需要学习PHP,C,Java或其他任何东西,或者只要知道django和python就可以做到这一点?

  2. Android发送发送请求到django服务器csrf失败

    我想我的Android应用程序能够发送一些信息到我的django服务器.所以我做的Android应用程序发送一个发布请求到mysite/上传页面,django的这个页面的视图将基于post数据工作.问题是服务器对post请求的响应,关于csrf验证失败.看看这个问题,似乎我可能必须先从服务器获取一个csrf令牌,然后用该令牌做帖子但是我不知道我该怎么做.编辑:我已经发现,我可以使用视图装饰器@cs

  3. pycharm社区版安装django并创建一个简单项目的全过程

    社区版的pycharm跟专业版的pycharm应用差别还是不太大,下面这篇文章主要给大家介绍了关于pycharm社区版安装django并创建一个简单项目的相关资料,文中通过实例代码介绍的非常详细,需要的朋友可以参考下

  4. Django 报错:Broken pipe from ('127.0.0.1', 58924)的解决

    这篇文章主要介绍了Django 报错:Broken pipe from ('127.0.0.1', 58924)的解决方案,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  5. 解决Django cors跨域问题

    这篇文章主要介绍了解决Django cors跨域问题,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  6. 虚拟环境下搭建一个Django项目

    这篇文章主要为大家介绍了虚拟环境下搭建一个Django项目的实现过程示例,有需要的朋友可以借鉴参考下,希望能够有所帮助,祝大家多多进步,早日升职加薪

  7. Django ORM F对象和Q对象查询

    Django提供了两个非常有用的工具:F对象和Q对象,方便了在一些特殊场景下的查询过程,这篇文章主要介绍了Django ORM F对象和Q对象查询,需要的朋友可以参考下

  8. Python Django教程之实现新闻应用程序

    Django是一个用Python编写的高级框架,它允许我们创建服务器端Web应用程序。在本文中,我们将了解如何使用Django创建新闻应用程序,感兴趣的可以尝试一下

  9. django安装xadmin及问题解决

    本文主要介绍了django安装xadmin及问题解决,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧

  10. 解决django 多个APP时 static文件的问题

    这篇文章主要介绍了解决django 多个APP时 static文件的问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

随机推荐

  1. 10 个Python中Pip的使用技巧分享

    众所周知,pip 可以安装、更新、卸载 Python 的第三方库,非常方便。本文小编为大家总结了Python中Pip的使用技巧,需要的可以参考一下

  2. python数学建模之三大模型与十大常用算法详情

    这篇文章主要介绍了python数学建模之三大模型与十大常用算法详情,文章围绕主题展开详细的内容介绍,具有一定的参考价值,感想取得小伙伴可以参考一下

  3. Python爬取奶茶店数据分析哪家最好喝以及性价比

    这篇文章主要介绍了用Python告诉你奶茶哪家最好喝性价比最高,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧

  4. 使用pyinstaller打包.exe文件的详细教程

    PyInstaller是一个跨平台的Python应用打包工具,能够把 Python 脚本及其所在的 Python 解释器打包成可执行文件,下面这篇文章主要给大家介绍了关于使用pyinstaller打包.exe文件的相关资料,需要的朋友可以参考下

  5. 基于Python实现射击小游戏的制作

    这篇文章主要介绍了如何利用Python制作一个自己专属的第一人称射击小游戏,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起动手试一试

  6. Python list append方法之给列表追加元素

    这篇文章主要介绍了Python list append方法如何给列表追加元素,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

  7. Pytest+Request+Allure+Jenkins实现接口自动化

    这篇文章介绍了Pytest+Request+Allure+Jenkins实现接口自动化的方法,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  8. 利用python实现简单的情感分析实例教程

    商品评论挖掘、电影推荐、股市预测……情感分析大有用武之地,下面这篇文章主要给大家介绍了关于利用python实现简单的情感分析的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下

  9. 利用Python上传日志并监控告警的方法详解

    这篇文章将详细为大家介绍如何通过阿里云日志服务搭建一套通过Python上传日志、配置日志告警的监控服务,感兴趣的小伙伴可以了解一下

  10. Pycharm中运行程序在Python console中执行,不是直接Run问题

    这篇文章主要介绍了Pycharm中运行程序在Python console中执行,不是直接Run问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

返回
顶部