一、Django密码存储和加密方式
#算法+迭代+盐+加密 <algorithm>$<iterations>$<salt>$<hash>
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
#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]为正在使用的加密存储方式,其他为检验密码时,可以使用的方式
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
[ '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', ]
二、手动校验密码
#和数据库的密码进行校验 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算法升级
- 自定义密码格式验证