赞
踩
基于
django
搭建的系统中,用到pbkdf2_sha256((Password-Based Key Derivation Function 2))
加密算法,这里做些代码测试、总结。
Password-Based Key Derivation Function 2
,即基于密码的密钥派生函数2。settings.py
文件配置PASSWORD_HASHERS = (
'django.contrib.auth.hashers.MD5PasswordHasher',
'django.contrib.auth.hashers.PBKDF2PasswordHasher',
'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',
'django.contrib.auth.hashers.BCryptPasswordHasher',
'django.contrib.auth.hashers.SHA1PasswordHasher',
'django.contrib.auth.hashers.CryptPasswordHasher',
)
SECRET_KEY = '9z%v-4&h$86qo@o8%c7ep^it*5$%sscl5hd$emb070pgo=1$6#'
SECRET KEY
from django.core.management.utils import get_random_secret_key
print( get_random_secret_key() )
# hgic$t55335b7(z9h(gs&1j2+ralahabczs-hq0h&49erm1^&k
from django.contrib.auth.hashers import make_password, check_password
import os
import django
os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'YouliTest.settings')
# django.setup()
# DJANGO_SETTINGS_MODULE=YouliTest.settings;
pwd = '123456789'
mkpwd = make_password(pwd, None, 'pbkdf2_sha256') # 创建django密码,第三个参数为加密算法
print( 'make_password 测试: %s' % (mkpwd) )
mkpwd_bool = check_password(pwd, mkpwd) # 返回的是一个bool类型的值,验证密码正确与否
print( 'check_password 测试: %r' % (mkpwd_bool) )
# make_password 测试: pbkdf2_sha256$100000$CsSTgYxLUkkr$jHH29Qq+QZ2JoTXBPKymXjYBQoXPWNO9V9ZAk+I9V3Q=
# check_password 测试: True
django.core.exceptions.ImproperlyConfigured: Requested setting PASSWORD_HASHERS, but settings are not configured. You must either define the environment variable DJANGO_SETTINGS_MODULE or call settings.configure() before accessing settings.
settings.py
文件未配置,配置好后需要在代码中引入或在运行参数中引入
Run/Debug Configurations
参数引入示例如下:Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。