[英]python password generator for django
How can I manually generate password for django? 如何为django手动生成密码? For example, in other application, but using the same database as django .For username 'admin' password like this
例如,在其他应用程序中,但使用与django相同的数据库。对于这样的用户名'admin'密码
pbkdf2_sha256$10000$T0BzrDwfZSrI$pSgvDEam9V9jcdYpYDVkYMMwtSnRrFdf6Aqow82Tjr8=
I think this maybe what you are looking for : 我想这也许就是你要找的东西:
Manually managing a user's password 手动管理用户密码
make_password(password[, salt, hashers])
make_password(密码[,盐,哈希])
Creates a hashed password in the format used by this application.
以此应用程序使用的格式创建哈希密码。 It takes one mandatory argument: the password in plain-text.
它需要一个强制参数:明文密码。 Optionally, you can provide a salt and a hashing algorithm to use, if you don't want to use the defaults (first entry of PASSWORD_HASHERS setting).
(可选)如果您不想使用默认值(PASSWORD_HASHERS设置的第一个条目),则可以提供salt和哈希算法。 Currently supported algorithms are: 'pbkdf2_sha256', 'pbkdf2_sha1', 'bcrypt_sha256' (see Using bcrypt with Django), 'bcrypt', 'sha1', 'md5', 'unsalted_md5' (only for backward compatibility) and 'crypt' if you have the crypt library installed.
目前支持的算法是:'pbkdf2_sha256','pbkdf2_sha1','bcrypt_sha256'(参见使用带有Django的bcrypt),'bcrypt','sha1','md5','unsalted_md5'(仅用于向后兼容)和'crypt'如果你已经安装了crypt库。 If the password argument is None, an unusable password is returned (a one that will be never accepted by check_password()).
如果password参数为None,则返回一个不可用的密码(check_password()永远不会接受该密码)。
I want write function for using without django
我想要没有django使用写功能
Well luckily Django is open source, so you can go and take what you need. 幸运的是Django是开源的,所以你可以去拿你需要的东西。 The functions source is visible here .
功能源在这里可见 。
The most common ( not safest ) algorithm for hashing is md5
. 最常见( 非最安全 )的散列算法是
md5
。 Extracting a few ideas from Django's password system can be this code: 从Django的密码系统中提取一些想法可以是这样的代码:
import hashlib
def make_password(password):
assert password
hash = hashlib.md5(password).hexdigest()
return hash
def check_password(hash, password):
"""Generates the hash for a password and compares it."""
generated_hash = make_password(password)
return hash == generated_hash
>>> hash = make_password('hello123')
>>> hash
'f30aa7a662c728b7407c54ae6bfd27d1'
>>> check_password(hash, 'hello123')
True
>>> check_password(hash, 'Hello123')
False
Use make_password
to generate a hash and check_password
to check if the entered password is the same as the stored one. 使用
make_password
生成散列和check_password
以检查输入的密码是否与存储的密码相同。
As @Emil pointed out, Django supports multiple password hashers such as pbkdf2_sha256 and pbkdf2_sha1, storing the string as a 3-fold value separated by $
: algorithm$salt$hash
. 正如@Emil指出的那样,Django支持多个密码哈希,如pbkdf2_sha256和pbkdf2_sha1,将字符串存储为由
$
: algorithm$salt$hash
分隔的3倍值。 salt
is a randomly generated string to prevent same password from repeating in the database. salt
是随机生成的字符串,用于防止在数据库中重复使用相同的密码。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.