gpt4 book ai didi

php - 用 PHP 和 MySQL 给我的哈希加盐

转载 作者:IT王子 更新时间:2023-10-29 00:34:50 26 4
gpt4 key购买 nike

像大多数用户一样,我只是想找出一种安全的方式来存储密码。我在这里没有找到(或者可能是我缺乏理解)是如何在我的数据库中检索加盐散列并将盐与散列密码分开,尤其是每个密码都有唯一的盐,而将盐+密码保存在单个列中。

我正在寻找所有这些很酷的方法来加密密码(SHA-256,但 MySQL 是否只支持 SHA/1 和 MD5?)以及 PHP 手册中的其他内容,但不确定如何存储和检索密码。

所以,到目前为止,我所了解的就这些了:

SHA('$salt'.'$password') // My query sends the password and salt 
// (Should the $salt be a hash itself?)

在那之后我迷失了盐分。

没有盐的情况下检索密码很容易,但是盐让我很困惑。我在哪里可以再次从 $salt 获得值(value),特别是如果它是唯一且安全的?我是否将它们隐藏在另一个数据库中?常量(似乎不安全)?

编辑: 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;

现在,如果您想验证密码,您可以:

// 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';
}

您可能想看看 phpass ,它也使用了这种技术。它是一种 PHP 哈希解决方案,在其他一些东西中使用了加盐。

您一定要看看 WolfOdrade 链接到的问题的答案。

关于php - 用 PHP 和 MySQL 给我的哈希加盐,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3273293/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com