Tar*_*rik 10 php mysql security salt
像大多数用户一样,我只是想找出一种存储密码的安全方法.我在这里找不到的(或者可能是我缺乏理解)是如何在我的数据库中检索盐渍哈希并将盐与哈希密码分开,特别是对每个密码使用唯一的盐,同时保持盐+密码在单列.
我发现所有这些很酷的方法来加密密码(SHA-256,但MySQL只支持SHA/1和MD5吗?)以及PHP手册中的其他内容,但不确定如何存储和检索密码.
所以,这就是我理解的全部内容:
SHA('$salt'.'$password') // My query sends the password and salt
// (Should the $salt be a hash itself?)
Run Code Online (Sandbox Code Playgroud)
在那之后,我迷失了盐.
在没有盐的情况下检索密码很容易,但盐让我感到困惑.我从哪里再次获得$ salt的价值,特别是如果它独特且安全的话?我将它们隐藏在另一个数据库中吗?常数(似乎不安全)?
编辑: HMAC中的关键变量应该是盐还是其他的东西?
首先,您的DBMS(MySQL)不需要对加密哈希值有任何支持.你可以在PHP方面做到这一切,这也是你应该做的.
如果要在同一列中存储salt和hash,则需要将它们连接起来.
// the plaintext password
$password = (string) $_GET['password'];
// you'll want better RNG in reality
// make sure number is 4 chars long
$salt = str_pad((string) rand(1, 1000), 4, '0', STR_PAD_LEFT);
// you may want to use more measures here too
// concatenate hash with salt
$user_password = sha512($password . $salt) . $salt;
Run Code Online (Sandbox Code Playgroud)
现在,如果要验证密码,请执行以下操作:
// the plaintext password
$password = (string) $_GET['password'];
// the hash from the db
$user_password = $row['user_password'];
// extract the salt
// just cut off the last 4 chars
$salt = substr($user_password, -4);
$hash = substr($user_password, 0, -4);
// verify
if (sha512($password . $salt) == $hash) {
echo 'match';
}
Run Code Online (Sandbox Code Playgroud)
您可能想看看phpass,它也使用了这种技术.它是一种PHP散列解决方案,在其他一些东西中使用salting.
你一定要看看WolfOdrade链接的问题的答案.
| 归档时间: |
|
| 查看次数: |
3660 次 |
| 最近记录: |