<p>dbr说:</p>
<blockquote>
<pre><code>def hash_password(password):
"""Returns the hashed version of a string
"""
return hasher.new( str(password) ).hexdigest()
</code></pre>
</blockquote>
<p>这是一种非常不安全的散列密码方法。你不想这么做。如果你想知道为什么要读那些为OpenBSD做密码散列系统的人写的<a href="http://www.openbsd.org/papers/bcrypt-paper.pdf" rel="noreferrer" title=""B-Crypt Paper">Bycrypt Paper</a>。此外,如果想好好讨论密码是如何破解的,请与开膛手杰克(流行的unix密码破解器)的作者一起查看<a href="http://www.securityfocus.com/columnists/388" rel="noreferrer">this interview</a>。</p>
<p>现在B-Crypt很好,但是我不得不承认我没有使用这个系统,因为我没有EKS Blowfish算法,我不想自己实现它。我使用的是FreeBSD系统的一个稍微更新的版本,我将在下面发布。关键是这个。不要只是散列密码。将密码加盐,然后散列密码并重复10000次左右。</p>
<p>如果没有意义,这里是代码:</p>
<pre><code>#note I am using the Python Cryptography Toolkit
from Crypto.Hash import SHA256
HASH_REPS = 50000
def __saltedhash(string, salt):
sha256 = SHA256.new()
sha256.update(string)
sha256.update(salt)
for x in xrange(HASH_REPS):
sha256.update(sha256.digest())
if x % 10: sha256.update(salt)
return sha256
def saltedhash_bin(string, salt):
"""returns the hash in binary format"""
return __saltedhash(string, salt).digest()
def saltedhash_hex(string, salt):
"""returns the hash in hex format"""
return __saltedhash(string, salt).hexdigest()
</code></pre>
<p>对于部署这样的系统,需要考虑的关键问题是HASH-REPS常量。这是这个系统中可扩展的成本因素。您将需要进行测试,以确定您希望等待计算每个散列的可容许时间量与基于脱机字典的密码文件攻击的风险。</p>
<p>安全性是很难的,我提出的方法并不是实现这一点的最佳方法,但它比简单的散列要好得多。此外,实现起来非常简单。所以即使你没有选择更复杂的解决方案,这也不是最糟糕的。</p>
<p>希望能帮上忙,
提姆</p>