首页 > 解决方案 > 拉拉维尔 7/8。自定义哈希驱动程序

问题描述

我有旧数据库,其中密码由 sha3-256 散列。我正在创建一个新站点,我应该使用旧数据库。但是 Auth::attempt() 使用 bcrypt(默认)。如何设置散列驱动程序 sha3-256?

标签: phplaravellaravel-7laravel-8

解决方案


它更喜欢使用 Laravel 自带的哈希算法,因为 Laravel 只支持 Bcrypt 和 Argon2。

但是,为了帮助您迁移到新算法,您可以在 Users 表中创建一个名为sha3_password的列,在其中放置旧密码,并在登录时创建一个 if 语句来检查 sha3_password 是否不为 null 并比较 typed-使用 sha3-256 输入密码,如果匹配,则使用 bcrypt 更新用户密码字段,并将 sha3_password 设置为 null。这样,当用户登录时,如果是他第一次登录,他的密码将被更新,否则他会正常登录。

我用这种方法来迁移一个项目,它就像一个魅力,我的登录功能如下,你基本上可以使用相同的逻辑:

    public function login(Request $request)
    {
        #Update old password after migration to new platform
        $user= User::where('email',request('email'))
            ->where('password_sha1', sha1(request('password')))
            ->where('password', null)
            ->first();
        if($user)
        {
            $user->password = bcrypt(request('password'));
            $user->password_sha1 = null;
            $user->update();
        }

        if (Auth::attempt(['email' => request('email'), 'password' => request('password'), 'status' => 1])) {
            // Authentication passed...\
            return redirect($this->redirectPath());
        }
    }

推荐阅读