php - 拉拉维尔 7/8。自定义哈希驱动程序
问题描述
我有旧数据库,其中密码由 sha3-256 散列。我正在创建一个新站点,我应该使用旧数据库。但是 Auth::attempt() 使用 bcrypt(默认)。如何设置散列驱动程序 sha3-256?
解决方案
它更喜欢使用 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());
}
}
推荐阅读
- react-native - 尝试启动反应本机应用程序时出错
- c# - SendGrid 动态模板包含空数据 - .NET Core
- python - 这是在 Python 中使用 OOP 的正确方法吗?
- flutter - 为什么在颤振中传递列表作为小部件子级不会更新 UI,但 list.map() 在添加新子级时会更新
- node.js - 如何在 Node.js 中调用 2 个不同的 Api
- android - firebase 实时数据库分片的最大写入速度是多少?
- javascript - 如何在给定数组中仅添加具有相同_id的sumdigit列值,并且价格列在javascript中应保持不变
- html - HTML、CSS图像定位
- web-scraping - 从未调用 RULE 中的 CrawlSpider 回调函数
- java - 为什么try中的int编译成byte