salt + sha512 + md5加密

use*_*088 -1 php security passwords hash password-protection

我有以下代码:(只是用于加密/散列的测试文件)

<!doctype html>
<html>
<head></head>
<body>

  <?php

    error_reporting('off');

    if (isset($_POST['submit'])) {
        $salt = "2bZ@<^$";
        $hash = hash("sha512", $_POST['hash']);
        $hash = $salt . $hash;
        $hash  = md5($hash);
        echo $hash;
        $hashLen = strlen($hash);
        echo "<br>The length of the hashed word is " . $hashLen . " characters long!";
    }

    ?>

    <form action="hashed.php" method="post">
      <input type="text" name="hash">
      <input type="submit" value="Hash" name="submit">
    </form>

</body>
</html>
Run Code Online (Sandbox Code Playgroud)

这有多安全?我知道这很可能是被黑客入侵,但需要多长时间?我目前正在制作一个php/mysqli注册表,并希望尽可能安全地使用户的密码,这样黑客就需要很长时间才能破解用户的密码.为了加密它,我可以使用它,例如:

用sha512,md5(md5),加入不同的盐,两个sha512,另一个md5和另一种不同的盐来混合它!

听起来有多安全?黑客破解密码需要多长时间?请您使用非常非常安全的加密方法告诉我.此外,我想让用户使用cookie登录:需要一种安全的方式将他们的信息存储在cookie中!

提前致谢 :)

小智 9

这是不安全的.它与Dave的家庭酿造哈希/有点愚蠢的算法类似,答案解释了它的错误.在你的情况下,我只会说你只使用非常快的哈希进行2次计算,而这远远不足以击败基于GPU的破解.

此外,您永远不应该使用自己的加密技术,当然这也适用于散列函数.而是使用PHP附带的标准且经过良好测试的密码散列函数:

$password = "HelloStackOverflow"; // example password

$hash = password_hash($password, PASSWORD_BCRYPT); // here's the hash of the previous password

// you can set the "complexity" of the hashing algorithm, it uses more CPU power
// but it'll be harder to crack, even though the default is already good enough

$hash = password_hash($password, PASSWORD_BCRYPT, ["cost" => 10]); 

if (password_verify($password, $hash)) { // checking if a password is valid
    echo "Welcome back !"; // valid password
} else {
    echo "You're not the one you're pretending to be..."; // invalid password
}
Run Code Online (Sandbox Code Playgroud)

如果您的PHP安装太旧(<5.5)并且没有这些password_*功能,您可以使用此库提供与这些功能的向前兼容性; 用法与上面的例子保持一致.