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中使用next()方法操作文件的教程

 next()方法当一个文件被用作迭代器,典型例子是在一个循环中被使用,next()方法被反复调用。此方法返回下一个输入行,或引发StopIteration异常EOF时被命中。...

Python的subprocess模块总结

subprocess意在替代其他几个老的模块或者函数,比如:os.system os.spawn* os.popen* popen2.* commands.* subprocess最简单...

Python-Seaborn热图绘制的实现方法

Python-Seaborn热图绘制的实现方法

制图环境: pycharm python-3.6 Seaborn-0.8 热图 import numpy as np import seaborn as sns import...

Python使用getpass库读取密码的示例

Python使用getpass库读取密码的示例

有这样一个经历,服务器挂掉了,请工程师维护,为了安全,工程师进行核心操作时,直接关掉显示器进行操作,完成后,再打开显示器,进行收尾工作... 密码 这个经历告诉我们: 为了安全...

python图形用户接口实例详解

python图形用户接口实例详解

本文实例为大家分享了python图形用户接口实例的具体代码,供大家参考,具体内容如下 运用tkinter图形库,模拟聊天应用界面,实现信息发送. from tkinter impor...