Django密码系统实现过程详解

yipeiwu_com5年前Python基础

一、Django密码存储和加密方式

#算法+迭代+盐+加密

<algorithm>$<iterations>$<salt>$<hash>

默认加密方式配置

#settings里的默认配置
PASSWORD_HASHERS = [
  'django.contrib.auth.hashers.PBKDF2PasswordHasher',
  'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
  'django.contrib.auth.hashers.Argon2PasswordHasher',
  'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',
  'django.contrib.auth.hashers.BCryptPasswordHasher',
]

#PASSWORD_HASHERS[0]为正在使用的加密存储方式,其他为检验密码时,可以使用的方式

默认加密方式配置

所有支持的hasher

[
  'django.contrib.auth.hashers.PBKDF2PasswordHasher',
  'django.contrib.auth.hashers.PBKDF2SHA1PasswordHasher',
  'django.contrib.auth.hashers.Argon2PasswordHasher',
  'django.contrib.auth.hashers.BCryptSHA256PasswordHasher',
  'django.contrib.auth.hashers.BCryptPasswordHasher',
  'django.contrib.auth.hashers.SHA1PasswordHasher',
  'django.contrib.auth.hashers.MD5PasswordHasher',
  'django.contrib.auth.hashers.UnsaltedSHA1PasswordHasher',
  'django.contrib.auth.hashers.UnsaltedMD5PasswordHasher',
  'django.contrib.auth.hashers.CryptPasswordHasher',
]

所有支持的hasher

二、手动校验密码

#和数据库的密码进行校验
check_password(password, encoded)

#手动生成加密的密码,如果password=None,则生成的密码永远无法被check_password()
make_password(password, salt=None, hasher='default')

#检查密码是否可被check_password()
is_password_usable(encoded_password)

三、密码格式验证

AUTH_PASSWORD_VALIDATORS = [

#检验和用户信息的相似度
  {
    'NAME': 'django.contrib.auth.password_validation.UserAttributeSimilarityValidator',
  },

#校验密码最小长度
  {
    'NAME': 'django.contrib.auth.password_validation.MinimumLengthValidator',
    'OPTIONS': {
      'min_length': 9,
    }
  },

#校验是否为过于简单(容易猜)密码
  {
    'NAME': 'django.contrib.auth.password_validation.CommonPasswordValidator',
  },

#校验是否为纯数字
  {
    'NAME': 'django.contrib.auth.password_validation.NumericPasswordValidator',
  },
]

四、自定义

  • 自定义hash算法
  • 对已有hash算法升级
  • 自定义密码格式验证

官方原文

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持【听图阁-专注于Python设计】。

相关文章

Python中的类与对象之描述符详解

描述符(Descriptors)是Python语言中一个深奥但却重要的一部分。它们广泛应用于Python语言的内核,熟练掌握描述符将会为Python程序员的工具箱添加一个额外的技巧。为了...

使用pandas实现csv/excel sheet互相转换的方法

1. xlsx to csv: import pandas as pd def xlsx_to_csv_pd(): data_xls = pd.read_excel('1.xl...

Python操作配置文件ini的三种方法讲解

python 操作配置文件ini的三种方法 方法一:crudini 命令 说明 crudini命令是Linux下的一个操作配置文件的命令工具 用法 crudini --set [--...

Python实现的简单发送邮件脚本分享

近来有些东西需要监控报警发邮件,然后在网上找了点材料,自己写了一个简单发送邮件的脚本,主要就是运用python的smtplib模块,分享给大家看一下: 复制代码 代码如下: #!/usr...

Python3数字求和的实例

以下实例为通过用户输入两个数字,并计算两个数字之和: # -*- coding: UTF-8 -*- # Filename : test.py # author by : www...