【问题标题】:Why do I need to add the original salt to each hash iteration of a password?为什么我需要将原始盐添加到密码的每个哈希迭代中?
【发布时间】:2014-01-27 00:00:09
【问题描述】:

我了解在多次迭代中散列密码以使攻击者更难操作非常重要。我已经读过很多次了,在处理这些迭代时,不仅要散列前一次散列的结果,而且还要每次都附加原始盐,这一点至关重要。换句话说:

我需要这样做:

var hash = sha512(salt + password); 
for (i = 0; i < 1000; i++) {
    hash = sha512(hash); 
}

相反,需要这样做:

var hash = sha512(salt + password); 
for (i = 0; i < 1000; i++) {
    hash = sha512(salt + hash); 
}

我的问题是关于这里的数学。为什么我上面的坏例子会让攻击者的事情变得更容易?我听说它会增加碰撞的可能性,但我不明白为什么。

【问题讨论】:

  • 谁说它更好?我不是说它不是,但也许上下文包含一些论点?
  • 你能提供一个参考,说明你应该更喜欢第二个而不是第一个吗?
  • 我个人认为这并不重要(尽管我可能会使用第二个,因为为什么不这样做)但可能会改为在 crypto.stackexchange 中发布。
  • 这个问题是边缘问题,因为它更多的是关于加密算法的安全性而不是它的实现。无论如何,它一直是reposted on Cryptography

标签: hash passwords salt


【解决方案1】:

这并不是说您只需要执行“hash = sha512(salt + hash)” - 它比这更复杂。 HMAC 是添加盐的更好方法(PBKDF2 基于 HMAC - 有关 PBKDF2 的更多详细信息,请参见下文) - 有关这些详细信息,请在 When is it safe to use a broken hash function? 进行很好的讨论。

您是正确的,因为您需要对哈希函数进行多次迭代以确保安全。

但是,不要自己动手。请参阅How to securely hash passwords?,并注意 PBKDF2、BCrypt 和 Scrypt 都是这样做的方法。

PBKDF2,也称为 PKCS#5v2 和 RFC2898 实际上与您正在做的事情相当接近(正常哈希函数的多次迭代),特别是 PBKDF2-HMAC-SHA-512 的形式,在特定的第 5.2 节列出:

     For each block of the derived key apply the function F defined
     below to the password P, the salt S, the iteration count c, and
     the block index to compute the block:

               T_1 = F (P, S, c, 1) ,
               T_2 = F (P, S, c, 2) ,
               ...
               T_l = F (P, S, c, l) ,

     where the function F is defined as the exclusive-or sum of the
     first c iterates of the underlying pseudorandom function PRF
     applied to the password P and the concatenation of the salt S
     and the block index i:

             F (P, S, c, i) = U_1 \xor U_2 \xor ... \xor U_c

     where

               U_1 = PRF (P, S || INT (i)) ,
               U_2 = PRF (P, U_1) ,
               ...
               U_c = PRF (P, U_{c-1}) .

     Here, INT (i) is a four-octet encoding of the integer i, most
     significant octet first.

附: SHA-512 是哈希原语的不错选择 - SHA-512(和 SHA-384)也​​优于 MD5、SHA-1,甚至 SHA-224 和 SHA-256,因为 SHA-384 及更高版本使用 64 位操作当前的 GPU(2014 年初)在执行 32 位操作时与当前 CPU 相比没有那么多优势,因此降低了攻击者对离线攻击的优势余地。

【讨论】:

  • 确定你写的是真的,但问题是关于在每次迭代中添加盐的问题非常具体。该问题已在crypto.stackexchange.com 转发,请查看那里的答案。
猜你喜欢
  • 2016-09-17
  • 2011-06-16
  • 2012-01-08
  • 2011-12-06
  • 2010-11-15
  • 2015-05-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多