Django密码系统实现过程详解

yipeiwu_com6年前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 中几种字符串格式化方法及其比较

起步 在 Python 中,提供了很多种字符串格式化的方式,分别是 %-formatting、str.format 和 f-string 。本文将比较这几种格式化方法。 %- 格式化 这...

python处理两种分隔符的数据集方法

python处理两种分隔符的数据集方法

在做机器学习的时候,遇到这样一个数据集... 一共399行10列, 1-9列是用不定长度的空格分割, 第9-10列之间用'\t'分割, 前九列都是数值类型,其中第三列有若干个'?...

python生成日历实例解析

本文实例展示了Python生成日历的实现方法。该实例可实现一个月的日历生成5x7的列表,列表里的没个日期为datetime类型,采用python自带的 calendar 模块实现。 程序...

python实现文件路径和url相互转换的方法

本文实例讲述了python实现文件路径和url相互转换的方法。分享给大家供大家参考。具体实现方法如下: import urllib pathname = 'path/to/file...

浅谈python下含中文字符串正则表达式的编码问题

浅谈python下含中文字符串正则表达式的编码问题

前言 Python文件默认的编码格式是ascii ,无法识别汉字,因为ascii码中没有中文。 所以py文件中要写中文字符时,一般在开头加 # -*- coding: utf-8 -*-...