2

我想知道这是否是一种安全的身份验证方法:

theInput = raw_input("Enter password: ")
theHashed = hashlib.sha512(theInput).hexdigest()
if theHashed == "35211b890b19afebfabc3451f04d150f1423bcb54ff7d62095677d7af7560fcvb56c112e879288836cb506853516c5dbd1d779cfaedf4a2f6f6a303600c0c589":
    print "Correct!"

如果没有,我该怎么做才能使它更安全?

4

4 回答 4

4

也许,只要有人无法阅读或修改您的代码。

如果这是在一台计算机上本地运行的程序,并且文件以普通用户无法更改的方式安装,并且您知道没有安装键盘记录程序,那么也许没关系。

即使用户可以读取此文件,他们也可以制作副本并修改其副本以删除身份验证步骤。

程序安全性是一个复杂而深刻的话题,不仅仅是选择散列算法。

于 2012-01-06T03:53:21.777 回答
2

Greg Hewgill 的第一点值得强调。我刚刚发现——有点令我惊讶——在我的笔记本上,系统 hashlib.py 对世界开放。因此,击败上述身份验证是微不足道的:

localhost-2:coding $ cat hashcrack.py 
class always_equal(object):
    def __eq__(self, other):
        return True

class sha512(object):
    def __init__(self, password):
        pass
    def hexdigest(self):
        return always_equal()
localhost-2:coding $ cat hashcrack.py >> /Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/hashlib.py
localhost-2:coding $ cat notsosecure.py 
import hashlib
theInput = raw_input("Enter password: ")
theHashed = hashlib.sha512(theInput).hexdigest()
if theHashed == "35211b890b19afebfabc3451f04d150f1423bcb54ff7d62095677d7af7560fcvb56c112e879288836cb506853516c5dbd1d779cfaedf4a2f6f6a303600c0c589":
    print "Correct!"
localhost-2:coding $ python notsosecure.py 
Enter password: pwned
Correct!

想想看,我什至不需要创建一个新的 sha512 类,我可以简单地在旧的类中添加 monkeypatched hexdigest。

无论如何,+1 表示主要的安全隐患不是哈希中的位数。

于 2012-01-06T04:58:37.077 回答
2

使用import getpass然后theInput = getpass.getpass("Enter password: ")代替 raw_input()。

于 2012-01-06T05:43:49.510 回答
1

For password authentication in general, you should be thinking more about KDFs like PBKDF2 and scrypt. You should also check out the new cryptography library:

https://cryptography.io/en/latest/

于 2014-01-09T20:33:28.507 回答