PHP 密码存储与 HMAC+nonce - 随机数随机性重要吗?
几年来,我在 stackoverflow 上询问如何确保 PHP 密码存储安全。 主要答案建议使用以下哈希算法:
function hash_password($password, $nonce) {
global $site_key;
return hash_hmac('sha512', $password . $nonce, $site_key);
}
答案建议使用随机随机数。与简单的唯一随机数相比,使用随机随机数有什么优势吗?
例如,每个用户可以有自己的 ID,该 ID 不会改变。然而,我们假设用户 ID 是连续的(使用 MySQL 的自动增量功能构建),因此不是随机的。用户 ID 是一个好的随机数还是随机性很重要?
现在,每个用户都可以选择一个用户名。每个用户都有自己的用户名,并且不会改变,两个不同的用户不能有相同的用户名。 用户名仍然不是随机的,但也不是连续的。用户名作为随机数是否足够好?这会比使用用户 ID 更好吗?
A few years I asked here on stackoverflow about how to make PHP password storage safe.
The main answer suggests using the following hashing algorithm:
function hash_password($password, $nonce) {
global $site_key;
return hash_hmac('sha512', $password . $nonce, $site_key);
}
The answer suggests using a random nonce. Is there any advantage in having a random nonce over simple unique nonces?
For instance, each user can have its own ID which does not change. However, let's assume user IDs are sequential(built with MySQL's auto increment feature) and therefore not random. Would the user ID be a good nonce or is randomness important?
Now, each user can pick an username. Each user has its own username which does not change, and two different users can't have the same username.
Usernames are still not random, but they aren't sequential either. Would usernames be good enough as a nonce? Would it be better than using the user ID?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
这都是基于随机数是盐的假设......
如果随机数是指盐,那么是的,这需要制作更多的彩虹表。通常,一次加盐超过 20 个字符就足够了,但对于极端的安全条件,您可能需要为每个密码使用一个新的随机加盐。
在慢速哈希中也是不错的选择 http://www.php.net/ Manual/en/function.hash.php#89574,没有讽刺。但我喜欢成熟的。
没看到你回复的下半部分。详细说明:随机数用于防止使用彩虹表。 ID 是否有效仅取决于 ID 的长度。随机性在技术上并不重要,但只是需要更多的彩虹表。一个例子是,假设您使用字符“a”作为随机数,并且密码有 2 个字符长,则必须创建 a-aa、a-ab a-ac 等彩虹表。如果您每次都使用随机字符,则可能必须完成“a”的所有排列+其他随机字符的所有排列。
但一般来说制作彩虹桌需要相当长的时间。因此,如果你想出一种很长的盐,那么它很可能是彩虹表,因为它不存在。
THIS IS ALL ON THE ASSUMPTION THAT A NONCE IS A SALT...
If by nonce you mean a salt then yes that requires more rainbow tables to be made. Usually once salt over 20 characters suffices, but for extreme security conditions you would want a new random salt for each password.
Also good choice in a slow hash http://www.php.net/manual/en/function.hash.php#89574, no sarcasm. But I like ripemd.
Didnt see the bottom half of your response. To elaborate: Nonces are used to prevent the use of rainbow tables. Whether the ID's would work depends merely on the length of the IDs. Randomness is not technically important, but just makes more rainbow tables required. An example would be, lets say you used a character "a" as a nonce and the password were 2 characters long, a rainbow table of a-aa, a-ab a-ac and so on would have to be created. If you use a random one each time maybe all the permutations of 'a' would have to be done + all the permuatations of the other random characters.
But in general making rainbow tables take quite a long time. So if you come up with a salt thats long its likely the rainbow table for it doesnt exists.
我发现网上有一个关于这个主题的相当不错的教程。我不太记得在谷歌上我在哪里找到它,但让我看看我是否可以自己分解这个函数,因为它就在我面前......
首先是函数,它可以创建任何大小的密钥长度。我冒昧地对它进行了相当多的评论...
我觉得这可能是最好的方法。也许我太偏执了?
I found that there was a fairly nice tutorial written online about this topic. I don't quite remember where on google I found it but let me see if I can break the function down well enough myself as it is right in front of me...
First the function, it can create a key length of any size. I took the liberty of commenting it fairly heavily...
I feel this is probably the best way to do this. Maybe I am too paranoid?
为了存储足以使用的密码:
salt
对于每个用户来说应该是随机且唯一的。随机盐将使预先计算的哈希表攻击变得不可能:每个用户都需要自己计算的哈希表。如果您不使用随机盐,那么预先计算的表存在的机会会更高。将盐放在密码之前将有助于隐藏哈希模式,以防某些用户具有相同的密码。
不需要Nonce,因为它是为了防止回复攻击。这种保护在您的架构中是不可能的。
使用HMAC来防止冲突是没有用的,因为a)我们使用散列而不是MAC,b)使SHA-512的冲突概率为50%您需要计算大约 2^256 个值。而2^256确实是一个天文数字。
For storing password enough to use:
salt
should be random and unique for each user. Random salt will make precalculated hash tables attack impossible: each user will require his own calculated hash tables. If you'll use not random salt, then chance that precalculated table exists will be higher.Position salt before password will help to hide hash patterns in case some users have same password.
Nonce is not needed, because it is for prevention a reply attack. This protection is not possible in your architecture.
Using HMAC to prevent collisions is useless, because a) we use hash not for MAC, b) to make probability of collision 50% for SHA-512 you need to calculate about 2^256 values. And 2^256 is truly astronomical number.