为什么每次我 运行 脚本时这个 passlib 散列脚本都会创建一个新结果?

Why does this passlib hashing script create a new result every time I run the script?

我正在使用以下 script from the passlib docs 来散列密码:

# import the hash algorithm                                                         
from passlib.hash import sha256_crypt                                               

# generate new salt, and hash a password
hash = sha256_crypt.encrypt("toomanysecrets")
print hash  # <== WHY IS THIS ALWAYS A DIFFERENT STRING?
# verifying the password
print sha256_crypt.verify("toomanysecrets", hash)  # Outputs "True"
print sha256_crypt.verify("joshua", hash)  # Outputs "False"

sha256_crypt.verify 能够验证多个不同的哈希值似乎很奇怪 "toomanysecrets" - 为什么这个密码不只有一个哈希值?

哈希结果取决于输入和salt。其中盐 - 它是随机生成的值,与散列结果一起包含在输出字符串中。这就是为什么每次调用 sha256_crypt.encrypt 输出字符串看起来是随机的,但保留了密码验证能力。