Hashing

简介

Laravel 自带的 Hash facade 提供了采用 Bcrypt 加密算法的加密方式,用于存储用户密码。如果你正在使用 Laravel app 中自带的 AuthController 控制器(controller),它将自动为注册和验证过程采用 Bcrypt 算法。

对于加密密码来说,Bcrypt 是一个非常好的选择,因为它的 “加密系数(work factor)” 是可调整的,这就意味着,即便计算机硬件能力在逐步提升,通过调整 “加密系数”,计算一个哈希所消耗的时间也会随着提升,就是说破解的难度不会因为计算能力的提升而降低。

关于加密方面的知识,推荐看一看这篇文章:http://www.williamlong.info/archives/3224.html

基本用法

通过调用 Hash facade 中的 make 方法即可对密码进行加密:

  1. <?php
  2. namespace App\Http\Controllers;
  3. use Hash;
  4. use App\User;
  5. use Illuminate\Http\Request;
  6. use App\Http\Controllers\Controller;
  7. class UserController extends Controller
  8. {
  9. /**
  10. * Update the password for the user.
  11. *
  12. * @param Request $request
  13. * @param int $id
  14. * @return Response
  15. */
  16. public function updatePassword(Request $request, $id)
  17. {
  18. $user = User::findOrFail($id);
  19. // Validate the new password length...
  20. $user->fill([
  21. 'password' => Hash::make($request->newPassword)
  22. ])->save();
  23. }
  24. }

另外,你还可以直接使用全局作用域中的 bcrypt 辅助函数:

  1. bcrypt('plain-text');

根据哈希值校验密码

check 方法帮你检查一个纯文本字符串是否是一个哈希值。然而,如果你正在使用 Laravel 中自带AuthController,你就不需要直接调用这个函数了,因为 AuthController 已经自动帮你调用这个方法了:

  1. if (Hash::check('plain-text', $hashedPassword)) {
  2. // The passwords match...
  3. }

检查密码是否需要重新加密

needsRehash 函数帮助你检查自从密码被加密之后,加密系数(work factor)是否被改变了:

  1. if (Hash::needsRehash($hashed)) {
  2. $hashed = Hash::make('plain-text');
  3. }