用户类中的PHP密码哈希(MySQL)

问题描述 投票:1回答:1

因此,我正在处理合并功能,以进行哈希处理以将用户升级为具有bcrypt密码,并整合到我发现的现有类中,并且非常成功地建立了它,这非常好。

但是,此类缺少重新哈希检查,这对于现有用户数据库上的旧密码非常糟糕。我们需要处理SHA1密码!我们使用SHA1 + Salt,所以希望可以进行转换。

我正在使用此类,在这里找到:

https://alexwebdevelop.com/user-authentication/

因此,使用此类,我添加了以下公共函数:

public function authenticate($username, $password)
{
    /* Global $pdo object */
    global $pdo;

    // Database lookup
    $stmt = $pdo->prepare("SELECT id, password, legacy_password FROM users WHERE username = ?");
    $stmt->execute([$username]);
    $stored = $stmt->fetch(PDO::FETCH_ASSOC);
    if (!$stored) {

        // No such user, throw an exception
        throw new Exception('Invalid user.');
    }
    if ($stored['legacy_password']) {

        // This is the legacy password upgrade code
        if (password_verify(sha1($password), $stored['password'])) {
            $newHash = password_hash($password, PASSWORD_DEFAULT);
            $stmt    = $pdo->prepare("UPDATE users SET password = ?, legacy_password = FALSE WHERE id = ?");
            $stmt->execute([$newhash, $stored['id']]);

            // Return the user ID (integer)
            return $stored['id'];
        }
    } elseif (password_verify($password, $stored['password'])) {

        // This is the general purpose upgrade code e.g. if a future version of PHP upgrades to Argon2
        if (password_needs_rehash($stored['password'], PASSWORD_DEFAULT)) {
            $newhash = password_hash($password, PASSWORD_BCRYPT);
            $stmt    = $pdo->prepare("UPDATE users SET password = ? WHERE id = ?");
            $stmt->execute([$newhash, $stored['id']]);
        }

        // Return the user ID (integer)
        return $stored['id'];
    }

    // When all else fails, throw an exception
    throw new Exception('Rehashing failed.');
}

现在在类的login()函数内部,我已替换了

public function login($name, $passwd)
    {
...
    if (is_array($row)) {

        if (password_verify($passwd, $row['password'])) {
                        /* Authentication succeeded. Set the class properties (id and name) */
                        $this->id            = intval($row['id'], 10);
                        $this->name          = $name;
                        $this->authenticated = TRUE;

                        /* Register the current Sessions on the database */
                        $this->registerLoginSession();

                        /* Finally, Return TRUE */
                        return TRUE;
                    }
    }
}

使用此:

public function login($name, $passwd)
    {
...
        if (is_array($row)) {

            $userid = $this->authenticate($name, $row['password']);

            if (password_verify($passwd, $row['password'])) {
                /* Authentication succeeded. Set the class properties (id and name) */
                $this->id            = intval($userid);
                $this->name          = $name;
                $this->authenticated = TRUE;

                /* Register the current Sessions on the database */
                $this->registerLoginSession();

                /* Finally, Return TRUE */
                return TRUE;
            }

        }
}

因此,应该在检查/重新哈希后将手返回ID。因此,经过测试,它找到了我作为用户。好..所以现在所有authenticate()所做的都是抛出异常失败错误。我不知道如何从中得到错误信息。

这似乎与该ID有关,我在做什么错呢?

这一点:用户使用表单中的SHA1(加盐)密码登录,脚本重新启动了密码,并且用户登录时像什么都没有发生。

authenticate()我正在使用的转换函数:

https://paragonie.com/blog/2016/02/how-safely-store-password-in-2016#legacy-hashes

php mysql sha1 crypt
1个回答
0
投票

非常抱歉!我从这里的建议中学到了,感谢所有帮助!

所以我自己解决了这个问题。我所做的是删除了authenticate()函数,而是根据反馈注释直接解决了这个问题(我完全同意)。

我用以下代码替换了帖子中的最后一个代码块:

if (is_array($row)) {

    if (password_needs_rehash($row['password'], PASSWORD_DEFAULT)) {

        $newhash = password_hash($passwd, PASSWORD_BCRYPT);
        $stmt    = $pdo->prepare("UPDATE users SET password = ? WHERE id = ?");
        $stmt->execute([$newhash, $row['id']]);

    }
    if (password_verify($passwd, $row['password'])) {

        /* Authentication succeeded. Set the class properties (id and name) */
        $this->id            = intval($row['id'], 10);
        $this->name          = $name;
        $this->authenticated = TRUE;

        /* Register the current Sessions on the database */
        $this->registerLoginSession();

        /* Finally, Return TRUE */
        return TRUE;
    }

}

并且用户密码正在重新哈希,然后登录!

© www.soinside.com 2019 - 2024. All rights reserved.