当我查找python模块时,我发现了一个叫做'crypt‘的东西。我什么都不了解。我试着读过这个,这个'salt‘是什么东西,这个加密模块有什么用,有没有什么方法可以把' crypt’应用到这段python代码中?
import crypt
max_attempts = 3
attempt = 0
try:
while attempt < max_attempts:
uname = input('Username: ')
password = input('pass: ')
if uname == 'admin' and password == 'Khs9':
print('Welcome Admin')
break
else:
attempt += 1
if attempt == max_attempts:
raise RuntimeError("\nYou've reached the maximum number of attempts allowed.")
else:
print('Wrong credentials.\n Try again or press <ctrl+c> to exit.\n')
continue
except KeyboardInterrupt:
print('Terminated by the user.\nGood-bye.')
except RuntimeError as e:
print("Goodbye")发布于 2016-02-11 08:30:29
现在我已经看到你的代码,我知道密码是'Khs9‘,我可以登录到你的邮箱。
你可以私下运行下面的代码。
>>> crypt.crypt('Khs9', 'aa')
'aa0GPiClW35DQ现在您可以这样更新您的代码:
import crypt
max_attempts = 3
attempt = 0
stored_pw_hash = 'aa0GPiClW35DQ'
try:
while attempt < max_attempts:
uname = input('Username: ')
entered_pw_hash = crypt.crypt(input('pass: '), stored_pw_hash)
if uname == 'admin' and entered_pw_hash == stored_pw_hash:
print('Welcome Admin')
break
else:
attempt += 1
if attempt == max_attempts:
raise RuntimeError("\nYou've reached the maximum number of attempts allowed.")
else:
print('Wrong credentials.\n Try again or press <ctrl+c> to exit.\n')
continue
except KeyboardInterrupt:
print('Terminated by the user.\nGood-bye.')
except RuntimeError as e:
print("Goodbye")现在,如果你的代码被泄露,他们不能立即访问。你应该有足够的时间意识到你被黑客入侵了,然后更改了你的密码。
这是背景信息。
crypt.crypt( password )将返回密码的哈希值。您可以存储散列,而不是明文密码。这样一来,你就不会因为没有密码而被黑客弄丢了。丢失散列并不是一个大问题,因为它不能保证访问(如果您遵循最佳实践,其中包括使用盐)。
下次有人提供密码时,你计算它的哈希值,将其与你之前存储的哈希值进行比较,如果它们匹配,你就知道他们给了你正确的密码。
为什么你需要使用盐?因为有人花了很长的时间来生成一个包含常用密码和散列的表。一旦完成,它就是破解散列的快速检查。通过使用salt,您可以确保应用不同的查询表,该查询表可能不可用,并且一般黑客没有时间生成它。
crypt.crypt()需要两个字符才能用作盐。您可以向它传递一个两个字符的字符串,也可以使用该函数之前的输出。(crypt.crypt()返回一个字符串,前两个字符是盐,其余字符是散列)
发布于 2016-02-11 14:26:37
首先,请阅读Thomas Pornin's canonical answer to How to securely hash passwords。这将回答你关于“盐”是什么的问题。
其次,crypt是一种古老的算法--不要使用它。即使是基于Python SHA-512的加密可能也不能使用,因为您无法控制迭代计数( BCrypt可能称之为工作因子),因此您无法通过增加迭代计数来使其更安全。
第三,Python as of 3.4 has fast PBKDF2 based on OpenSSL built in。
Python 2.7.8也是如此。
这两种方法都支持hashlib中内置的合理散列类型!用这些代替吧!如何使用它们的一个示例如下:
import hashlib
BinaryOutput = hashlib.pbkdf2_hmac('sha512',password, salt, args.iterations, args.outputBytes)哪里
使用PBKDF2-HMAC-SHA-512使用64位操作,这可能会降低基于GPU的攻击者对您的优势。
如果您担心计时攻击,您可以使用各种常量时间比较之一。与高迭代PBKDF2-HMAC-SHA-512相比,它不需要太多成本,如下所示:
if hashlib.sha256(args.expectedBinary).hexdigest() == hashlib.sha256(BinaryOutput).hexdigest():https://stackoverflow.com/questions/35326183
复制相似问题