您的位置:首页 > 编程语言 > Python开发

python passlib生成Hash密文并进行验证--Hash a password with passlib

2016-06-03 08:14 921 查看
encrypt()
 -
generate new salt, return hash of password.
verify()
 -
verify password against existing hash.

from passlib.hash import sha256_crypt

password = sha256_crypt.encrypt("password")
password1 = sha256_crypt.encrypt("password")

print(password)
print(password1)

print(sha256_crypt.verify("password", password))

identify()
 -
check if hash belongs to this algorithm.

>>> # attempting to call verify() with another algorithm's hash will result in a ValueError:
>>> from passlib.hash import sha256_crypt, md5_crypt
>>> other_hash = md5_crypt.encrypt("password")
>>> sha256_crypt.verify("password", other_hash)
Traceback (most recent call last):
<traceback omitted>
ValueError: not a valid sha256_crypt hash

>>> # this can be prevented by using the identify method,
>>> # determines whether a hash belongs to a given algorithm:
>>> hash = sha256_crypt.encrypt("password")
>>> sha256_crypt.identify(hash)
True
>>> sha256_crypt.identify(other_hash)
False

官方文档:https://pythonhosted.org/passlib/password_hash_api.html
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: