如何破解密码哈希安全性

时间:2019-01-06 18:42:00

标签: php hash

我只是为密码设置了一个哈希值以保护我的登录脚本,因为我将用户名和密码存储在cookie中,并且我想知道此哈希值有多安全以及它是否不可破坏。

这是哈希函数:

    public static function hash_password($password) {
            /* hashing the password */
            $hash     = hash('sha512', $password);

            /* Iterating the hash a few times */
            for($i = 1;$i <= 1000;$i++) {
                $hash = hash('sha512', $hash);
            }

            return $hash;
   }

然后我这样做了$_POST['password'] = hash_password($_POST['password']);

password : onlytest 
password in hash form : edf566f3f02a6b083db5318d77d06c1fa61e51783f097a7fff84767303832f72489fcf08764f9d4ba6c8d6f4e53c577084d586b2b08c60fbdefdcde2248fbbec 

3 个答案:

答案 0 :(得分:3)

简短的答案是你不能 阅读有关此内容的简短信息

http://php.net/manual/en/function.password-hash.php

就我所知,目前password_hash()几乎是最好的,如果您可以将它从一个简单的“ Stackoverflow”问题中解脱出来,那么现在它实际上并不是一个很好的哈希它。

编辑

您无法破解哈希值,但始终会有BRUTE强制攻击,这意味着您的密码将针对所有类型的密码进行测试,并且迟早会对其进行猜测,除非您的用户密码正确或您拥有系统将锁定登录表单的时间设置为1-2分钟,然后再次重新输入

这个家伙身上有一个很好的视频,它的手表不错 https://www.youtube.com/watch?v=7U-RbOKanYs

答案 1 :(得分:2)

不要在Cookie中发送密码,也不要对其进行哈希或加密。改用令牌:

  1. 通过安全通道发送用户名和(纯文本)密码
  2. 服务器使用令牌进行响应,令牌基本上是一个很大的随机数
  3. 在随后的所有调用中使用令牌

这样,您可以删除令牌并禁用会话,然后用户将再次需要用户名和密码才能登录。此外,可以通过这种方式构建“记住我”功能。

话虽如此,回答你的问题:没有什么是坚不可摧的。

答案 2 :(得分:1)

您可能有兴趣查看this post on the pros and cons of "double hashing" a passwordthis FAQ on password hashing

使用某种算法al散列密码,然后使用al算法重新散列,散列结果更有可能减少密码熵。这意味着当由hash_password($password)处理时,不同的密码可能会给出相同的哈希值。

应该尽可能地使用native password API(从PHP 5.5开始),而不是实现自己的哈希函数。它的关键功能password_hash可以生成强盐以与哈希一起使用,并且可以抵御大多数蛮力攻击。

编辑:您可能希望通过计算密码的SHA-512值+随机盐,然后password_hash来赋予散列密码更多的熵(=随机性)这个哈希。最终的PHP代码如下所示:

function generateSalt() {
    // Cryptographically secure function to generate strings
    return random_bytes(64 /* use 64 bytes */);
}
/**
 * Generates a strong SHA-512 hash, for use by passwordSecureHash()
 * @param string $password the plain-text password
 * @param string $salt the salt to generate the hash, to be kept at ALL COSTS - this function will not do it for you!
 * @return string the computed hash.
 */
function sha512(string $password, string $salt) {
    return hash('sha512', $password . $salt);
}
/**
 * Returns the final, secure password hash for storage in database server.
 * @param string $hash the SHA-512 resulting from the sha512() call.
 * @return string the hashed password.
 */
function passwordSecureHash(string $hash) {
    if(strlen($hash) !== 128) {
        // Ensure the SHA-512 hash has 128 characters
        return FALSE;
    }
    // Always let PHP generate the final password salt for you
    return password_hash($hash);
}
相关问题