Django自带加密模块的使用

本文介绍了如何使用Django内置模块进行密码的加密与验证操作。详细讲述了make_password()用于创建哈希密码,check_password()用于验证密码的方法,并提供了PASSWORD_HASHERS设置示例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

django官网介绍:https://docs.djangoproject.com/en/2.1/topics/auth/passwords/#module-django.contrib.auth.hashers

Manually managing a user’s password¶
The django.contrib.auth.hashers module provides a set of functions to create and validate hashed passwords. You can use them independently from the User model.

check_password(password, encoded)[source]¶
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.

make_password(password, salt=None, hasher='default')[source]¶
Creates a hashed password in the format used by this application. It takes one mandatory argument: the password in plain-text. Optionally, you can provide a salt and a hashing algorithm to use, if you don’t want to use the defaults (first entry of PASSWORD_HASHERS setting). See Included hashers for the algorithm name of each hasher. If the password argument is None, an unusable password is returned (one that will never be accepted by check_password()).

is_password_usable(encoded_password)[source]¶
Returns False if the password is a result of User.set_unusable_password().

Changed in Django 2.1:
In older versions, this also returns False if the password is None or an empty string, or if the password uses a hasher that’s not in the PASSWORD_HASHERS setting. That behavior is considered a bug as it prevents users with such passwords from requesting a password reset.
View Code

通过django自带的类库,来加密解密很方便,下面来简单介绍下;

在settings.py里添加:

PASSWORD_HASHERS = [
    'django.contrib.auth.hashers.PBKDF2PasswordHasher',
    'accounts.hashers.PBKDF2WrappedSHA1PasswordHasher',
]

 

导入包:

from django.contrib.auth.hashers import make_password, check_password

加密:

make_password(password, salt=None, hasher='default')

Creates a hashed password in the format used by this application.
It takes one mandatory argument: the password in plain-text.
Optionally, you can provide a salt and a hashing algorithm to use, if you don’t want to use the defaults (first entry of PASSWORD_HASHERS setting).
See Included hashers for the algorithm name of each hasher.
If the password argument is None, an unusable password is returned (one that will never be accepted by check_password()).

验证:

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.

注:

  

hasher可取以下值:

    pbkdf2_sha256
    pbkdf2_sha1
    argon2
    bcrypt_sha256
    bcrypt
    sha1
    md5
    unsalted_sha1
    unsalted_md5
    crypt

 

转载于:https://www.cnblogs.com/navysummer/p/9426507.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值