我的身份验证加密有用吗?
所以我读了很多关于 PHP 加密的文章。 以至于我不确定安全存储登录信息的真正好方法是什么。
然而,以下功能是我想出的:
function loginHash($username, $password){
$salt = str_split($password,(strlen($password)/2)+1);
$hash = hash('whirlpool', $username.$salt[0].'centerSalt'.$salt[1]);
return $hash;
}
我这样做的方法正确吗? 它用于验证密码与用户名的组合,以及将生成的哈希值与数据库中存储的哈希值进行比较以验证登录的能力。
So I've been reading a lot about encryption in PHP. So much that I am not sure exactly what's a really good method to securely store login information.
However, the following function is what I came up with:
function loginHash($username, $password){
$salt = str_split($password,(strlen($password)/2)+1);
$hash = hash('whirlpool', $username.$salt[0].'centerSalt'.$salt[1]);
return $hash;
}
Am I doing it the right way? It's used for authenticating a password combined with a username, and the ability to compare the generated hash with the one stored in a database to verify a login.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
加密!=散列。 它们都被普遍认为属于密码学的范畴,但是当某些东西可以加密时,它就可以被解密,而散列则不是这样。 哈希只是哈希,仅此而已。
盐确实没有正确构建。 它应该是通过 fopen() 调用从 /dev/urandom 读取的 x 字节。 比如我个人用的就是16字节的salt。 这可以有效防止彩虹表攻击。
为了使事情更安全,也可以使用密钥。 例如:
$key 只是随机数据。 例如,您可以在文档根目录上方的隐藏文件夹中生成一个名为“key.bin”的 64 kB 文件,并在哈希处理之前使用 file_get_contents()。
为什么要使用密钥? 如果您将哈希值和盐存储在数据库中,并将密钥存储在文件系统中,那么这可以防止任何人在获得您存储的哈希值和盐时破解您的哈希值。 因此,攻击者需要破解数据库和文件系统才能破解您的哈希值,但请注意,如果任何人已经破解了您的整个应用程序,那么任何人再破解您的哈希值都是毫无意义的,这意味着您的哈希方案是好的。
Encrypting != Hashing. They both are generally accepted to be in the category of Cryptography, but when something can be encrypted, it can be decrypted, which is not the case in Hashing. Hashing is just hashing, and that's it.
The salt is indeed not properly constructed. It should be x-bytes read from /dev/urandom with a fopen() call. For example, 16 bytes of salt is what I personally use. This prevents rainbow table attacks effectively.
To make things more secure, use a secret key, too. For example:
The $key is simply random data. You could generate a 64 kB file, for instance, that is called "key.bin" in a hidden folder above the document root and use file_get_contents() before the hash process.
Why to use secret keys? If you store the hashes and salts in a database and the key in the filesystem, then this prevents anyone from cracking your hash if they get their hands on your stored hashes and salts. So, an attacker would need to crack into both the database and the filesystem to crack your hashes, but notice that it's pointless for anyone to crack your hashes anymore if they have already cracked your whole application, which implies that your hashing scheme is good.
我的建议是永远、永远、永远不要编写自己的加密和哈希函数。 即使是专家也经常会做错,所以不要自己尝试。
我听说 phpass (Openwall) 是一个很好的哈希框架,我建议您使用它。
他们在哈希值中使用盐,并有相当多的参数来调整哈希值。
My advise is to never, never, never write your own encryption and hash functions. Even experts do it wrong all the time, so dont try it yourself.
Ive heared that phpass (Openwall) is a nice hashing framework, i'd suggest you use that.
They use salts in their hashes and have quite some parameters to tweak the hash.
你实际上并没有使用盐。
Salt 是随机生成的字符串,包含在哈希函数的输入中。 因此,每次都会有所不同。
这个想法是,当用户存储密码时,您会生成一个盐,并且该盐包含在您的数据存储中。 进行身份验证时,您检索盐和存储的哈希值,用存储的盐作为给定密码的前缀,然后将两者哈希在一起。 然后将结果与存储的哈希值进行比较。
You're not actually using a salt.
Salt is a randomly generated string that is included in the input for your hash function. As such, it will be different every time.
The idea is that you generate a salt when a user stores a password, and that this salt is included in your data storage. When authenticating, you retrieve the salt and the stored hash, you prefix the given password with the stored salt, and hash the two together. Then compare the result with the stored hash.
我认为上面的代码检查了两个框。
I think the above code checks the two boxes.
使用盐解决了两个问题:
彩虹表:彩虹表只是预先计算的哈希值,与源值一起存储。 通过比较哈希值,您可以获得未哈希值(密码)。 通过添加盐,您会获得另一层复杂性 - 攻击者必须知道生成自定义哈希表的盐。
哈希值的差异:没有盐,相同的 2 个密码会生成相同的 2 个哈希值。 现在很容易看出两个用户是否使用相同的密码(这里的弱点与彩虹表大致相同,但仍然如此)。 这可能不算什么,但仍然是一个值得关注的问题。
此外,您不应该使用快速算法进行密码散列。 md5快,sha也快。 越慢越好。
matsano chargen 博客是一个很好的(而且有趣的)资源,提供有关安全的提示和指示。
using salt solves two problems:
rainbow tables: rainbow tables are just precalculated hashes, stored with the source value. by comparing the hashes, you get the unhashed value (password). by adding salt you got another layer of complexity - the attacker must know the salt for generating a custom hashing table.
difference of hashed values: without salt, the same 2 passwords generate the same 2 hashes. now it's easy to see if two users use the same password (the weak point here is about the same as with the rainbow tables, but still). that may not amount to much, but is still a point of concern.
additionally, you shouldn't use fast algorithms for password hashing. md5 is fast, sha is fast. the slower, the better.
the matsano chargen blog is a good (and funny) resource for hints and pointers regarding security.