所以我对passlib
的使用感到有些困惑。我有以下代码用N量算法散列X密码数量。
#!/usr/bin/python
from passlib.hash import *
from passlib.context import CryptContext
from passlib.utils import Base64Engine, h64
from base64 import b64decode
from binascii import hexlify
def hash_password(passwd):
schemes = ["pbkdf2_sha1",
"md5_crypt",
"sha1_crypt",
"sha256_crypt",
"nthash",
"lmhash"]
print "Hashing \"%s\"" % passwd
print "-----------------------------------------------"
# Create the hashing context
pwd_context = CryptContext(schemes)
for i in range(len(schemes)):
pw_hash = pwd_context.encrypt(passwd, schemes[i])
print "%s - %s" % (schemes[i], pw_hash)
print "-----------------------------------------------"
def main():
passwords = [line.rstrip('\n') for line in open("passwords.txt")]
for i in range(len(passwords)):
hash_password(passwords[i])
if __name__ == "__main__":
main()
我得到以下输出:
Hashing "Hello"
-----------------------------------------------
pbkdf2_sha1 - $pbkdf2$60000$ojQGACCEcM7ZW0vpnVMqRQ$M53EY/FaE4bn.5Peaagi51cza54
pbkdf2_sha256 - $pbkdf2- sha256$20000$4JxzLsWYk1KKUSoFwPifsw$6LCJ.389zCs4gaQuYTMTKUis7t/4yv.c9/mH/XFKEAo
md5_crypt - $1$uDGM/Vuu$jiygPs0AgWJQjdVkc5BbE0
sha1_crypt - $sha1$64000$ge8006iW$Ccn.2CfTJFHUNHKINw5uOaMP1U3v
sha256_crypt - $5$rounds=110000$BaeA4z3k.JSjarb5$veeo24NDYkEzMqhQqqUkjEst0FTqh9fWxxGPp/pO4xA
nthash - 916a8e7b1540ec179f196f8ddb603d85
lmhash - fda95fbeca288d44aad3b435b51404ee
-----------------------------------------------
Hashing "Password"
-----------------------------------------------
pbkdf2_sha1 - $pbkdf2$60000$fO.91/o/x/g/p3Qu5XxvLQ$HPcZm9FqiN59c7VurmE3gKPUlFk
pbkdf2_sha256 - $pbkdf2-sha256$20000$mrNWKkVISSll7D3nPGds7Q$AoEg6PiE7.YFQDxtmvKVlRk/i6niC1RVflFFZiZ1g.Y
md5_crypt - $1$0qgwC1qt$bpoGT56iTN2cHHXN3v0Hk/
sha1_crypt - $sha1$64000$D3aHiFZ3$8StKt6Kt0MVl.flOvuoulBJNPVG/
sha256_crypt - $5$rounds=110000$Zxm1u65zMawD1.2F$pqywgWCi556iuBTxemWIccy68IBQxqAKZgHYc0MAFY4
nthash - a4f49c406510bdcab6824ee7c30fd852
lmhash - e52cac67419a9a224a3b108f3fa6cb6d
-----------------------------------------------
Hashing "NewPassword"
-----------------------------------------------
pbkdf2_sha1 - $pbkdf2$60000$Uao1RgjhPEcI4dzbG8OYkw$TWkaN5uJ5HMh3tV75wEnMkYjZVI
pbkdf2_sha256 - $pbkdf2-sha256$20000$55zT2tub8/6f0zqHUOrdOw$voDY5VObZQieFDCqG2of1NO0NVMc4AVsznwqv9GzAhA
md5_crypt - $1$CkQuGd5z$1CcBnpwL4cFPOYiw958pZ1
sha1_crypt - $sha1$64000$hIbbDQIi$J.1kLykb3tzFUPrHZZ23TcSj.zAR
sha256_crypt - $5$rounds=110000$g7J4/8teBFGyKmI2$OakX25d3mePIAj9V1UYDWWBayaMfngWZ6fZOdgqg9l0
nthash - 91f172926b123808d76f4d40b1db18e5
lmhash - 09eeab5aa415d6e4d408e6b105741864
我有两个问题:
提前致谢。
答案 0 :(得分:2)
encrypt
时, passlib
会自动加密密码:
classmethod PasswordHash.encrypt(secret,** kwds)
使用格式特定算法摘要密码,返回结果哈希字符串。
对于Passlib支持的大多数哈希,返回的字符串将包含:算法标识符,成本参数,盐字符串,最后是密码摘要本身。
这解释了您在大多数哈希中看到的输出。 nthash
和lmhash
都记录为不支持盐,这解释了为什么它们的输出更简单。
对于你的第二个问题,我会说,一般来说你不应该拉出实际的哈希值,因为passlib
被设计为使用{返回的整个哈希字符串{1}}验证给定的密码。但是如果你愿意,你可以在encrypt
上拆分返回的pw_hash
,然后选择最后一项:
$
这适用于不像digest = pw_hash.split('$')[-1]
和nthash
那样不适合的哈希。