- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在查看一些我自己没有编写的代码。该代码尝试使用 SHA512 对密码进行哈希处理,并仅使用 time()
作为盐。是 time()
太简单的盐或者这个代码安全吗?
感谢您的回答和评论。我在这里为新读者总结一下:
random, evenly distributed, high entropy
盐? 最佳答案
简短回答:
不,time()
不是好盐。
长答案:
复制自我对 Salt Generation and open source software 的回答
What is a salt?
A salt is a random set of bytes of a fixed length that is added to the input of a hash algorithm.
Why is salting (or seeding) a hash useful?
Adding a random salt to a hash ensures that the same password will produce many different hashes. The salt is usually stored in the database, together with the result of the hash function. Salting a hash is good for a number of reasons:
- Salting greatly increases the difficulty/cost of precomputated attacks (including rainbow tables)
- Salting makes sure that the same password does not result in the same hash. This makes sure you cannot determine if two users have the same password. And, even more important, you cannot determine if the same person uses the same password across different systems.
Salting increases the complexity of passwords, thereby greatly decreasing the effectiveness of bothDictionary-andBirthday attacks.(This is only true if the salt is stored separate from the hash).- Proper salting greatly increases the storage need for precomputation attacks, up to the point where they are no longer practical. (8 character case-sensitive alpha-numeric passwords with 16 bit salt, hashed to a 128 bit value, would take up just under 200 exabytes without rainbow reduction).
There is no need for the salt to be secret.
A salt is not a secret key, instead a salt 'works' by making the hash function specific to each instance. With salted hash, there is not one hash function, but one for every possible salt value. This prevent the attacker from attacking N hashed passwords for less than N times the cost of attacking one password. This is the point of the salt.
A "secret salt" is not a salt, it is called a "key", and it means that you are no longer computing a hash, but a Message Authentication Code (MAC). Computing MAC is tricky business (much trickier than simply slapping together a key and a value into a hash function) and it is a very different subject altogether.The salt must be random for every instance in which it is used. This ensures that an attacker has to attack every salted hash separately.
If you rely on your salt (or salting algorithm) being secret, you enter the realms of Security Through Obscurity (won't work). Most probably, you do not get additional security from the salt secrecy; you just get the warm fuzzy feeling of security. So instead of making your system more secure, it just distracts you from reality.
So, why does the salt have to be random?
Technically, the salt should be unique. The point of the salt is to be distinct for each hashed password. This is meant worldwide. Since there is no central organization which distributes unique salts on demand, we have to rely on the next best thing, which is random selection with an unpredictable random generator, preferably within a salt space large enough to make collisions improbable (two instances using the same salt value).
It is tempting to try to derive a salt from some data which is "presumably unique", such as the user ID, but such schemes often fail due to some nasty details:
If you use for example the user ID, some bad guys, attacking distinct systems, may just pool their resources and create precomputed tables for user IDs 1 to 50. A user ID is unique system-wide but not worldwide.
The same applies to the username: there is one "root" per Unix system, but there are many roots in the world. A rainbow table for "root" would be worth the effort, since it could be applied to millions of systems. Worse yet, there are also many "bob" out there, and many do not have sysadmin training: their passwords could be quite weak.
Uniqueness is also temporal. Sometimes, users change their password. For each new password, a new salt must be selected. Otherwise, an attacker obtained the hash of the old password and the hash of the new could try to attack both simultaneously.
Using a random salt obtained from a cryptographically secure, unpredictable PRNG may be some kind of overkill, but at least it provably protects you against all those hazards. It's not about preventing the attacker from knowing what an individual salt is, it's about not giving them the big, fat target that will be used on a substantial number of potential targets. Random selection makes the targets as thin as is practical.
In conclusion:
Use a random, evenly distributed, high entropy salt. Use a new salt whenever you create a new password or change a password. Store the salt along with the hashed password. Favor big salts (at least 10 bytes, preferably 16 or more).
A salt does not turn a bad password into a good password. It just makes sure that the attacker will at least pay the dictionary attack price for each bad password he breaks.
Usefull sources:
stackoverflow.com: Non-random salt for password hashes
Bruce Schneier: Practical Cryptography (book)
Matasano Security: Enough with the Rainbow Tables
usenix.org: Unix crypt used salt since 1976
owasp.org: Why add salt
openwall.com: SaltsDisclaimer:
I'm not a security expert. (Although this answer was reviewed by Thomas Pornin)
If any of the security professionals out there find something wrong, please do comment or edit this wiki answer.
/dev/random
或
/dev/urandom
[两者各有优缺点,选择你的毒药] ; 在 Windows 上,调用
CryptGenRandom()
)
<?php
/**
* Generate pseudo random bits
* @copyright: public domain
* @link http://www.openwall.com/phpass/
* @param int $length number of bits to generate
* @return string A string with the hexadecimal number
* @note don't try to improve this, you will likely just ruin it
*/
function random_bits($entropy) {
$entropy /= 8;
$state = uniqid();
$str = '';
for ($i = 0; $i < $entropy; $i += 16) {
$state = md5(microtime().$state);
$str .= md5($state, true);
}
$str = unpack('H*', substr($str, 0, $entropy));
// for some weird reason, on some machines 32 bits binary data comes out as 65! hex characters!?
// so, added the substr
return substr(str_pad($str[1], $entropy*2, '0'), 0, $entropy*2);
}
?>
关于php - time() 是一种好盐吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4983915/
我正在使用一个以明文形式存储密码的旧应用程序。我已将应用程序移植到 spring 3 mvc + security。我还成功地让 spring security 使用 sha256 + 基于用户名的盐
我试图按照 here. 中的代码获取哈希密码从中,我目前只使用 salt 的代码。方法,hash方法和isExpectedPassword方法。 我从文本字段获取密码: char[] passChar
我已经阅读了有关此问题的许多问题,但许多答案相互矛盾或我不明白。 您应该始终将密码存储为哈希值,而不是纯文本。但是您是否应该将盐(对于每个用户来说都是唯一的)存储在数据库中的散列密码+盐旁边。这对我来
我有一些 php 脚本,使我的用户能够使用电子邮件创建帐户。然后,他设置了一个经过哈希处理的密码,生成了一个盐并将其存储在我的数据库中。现在我正在重置密码,工作正常,但新密码(更改密码,即使其正确的密
我在申请中使用 shiro 进行身份验证。我使用加盐的散列密码,并将它们存储在我的数据库中,如下所示: private User createUserWithHashedPassword(St
我正在尝试找出在桌面应用程序中应该将应用程序 secret 和 key 存储在何处或如何存储。 例如 facebook 应用程序 key 或 dropbox key 和 secret 。 所以我读到我
ASP.NET 成员资格如何生成他们的 salt key ,然后他们如何对其进行编码(即是 salt + 密码还是密码 + salt)? 我正在使用 SHA-1使用我的成员(member)资格,但我想
我正在设置一个cookie。像这样的东西: $_COOKIE['test'] = SHA1('124'.'mysalt'); 现在 124 是我想要的 ID。所以在我的 MySQL 表中,我尝试运行如
我的应用需要加密一些数据(用户 session token )。我看到的大多数示例都有一种使用密码和盐生成 key 的方法,例如: public static Key generateKey(char
我想在数据库中的密码旁边存储一个(随机)盐。现在,问题是: 我应该将其存储为哈希值还是纯文本?有什么区别吗(更安全,更快?)?我应该付出多少努力来创建随机字符串? 示例代码: //Creati
当我收到此错误时,我正要在 Windows Azure 上部署我的服务: Error: The provided configuration file contains XML that could
如何在 C# 中创建以下 PHP 代码? PHP代码: 我将不胜感激任何形式的帮助。一直在测试以下内容: https://stackoverflow.com/a/1300927/7312781 但它
谁能推荐一个使用 javascript 创建 sha1 salt 的好方法? 最佳答案 首先,阅读盐的用途以确保您理解它。 This和 this会让您入门,但您应该阅读更多内容。 基本上,任何适当大小
我知道这可能是一个常见问题,网上有很多关于为密码哈希生成安全盐的文章。到目前为止,我已经了解到mcrypt_create_iv通常用于加密的初始化向量,它可用于安全地创建盐,特别是在针对MCRYPT_
我知道这个话题已经被讨论了一百万次。但这对我来说是新的,我读得越多,就越不了解实际发生或应该发生的事情。 我在用户密码的散列存储中添加了每个用户盐,因此存储的密码散列如下所示:hash(passwor
我正在学习本教程 ( http://kowsercse.com/2011/09/04/kohana-tutorial-beginners/ ) 并遇到此错误消息: Kohana_Exception [
我是一名优秀的程序员,十分优秀!