codecamp

服务 ―― 哈希

服务 —— 哈希

1、简介

Laravel `Hash使用该Bcrypt。

Bcrypt是散列密码的绝佳选择,因为其”工作因子“是可调整的,这意味着随着硬件功能的提升,生成哈希所花费的时间也会增加。

2、基本使用

可以调用Hash门面上的make方法散列存储密码:

<?php

namespace App\Http\Controllers;

use Hash;
use App\User;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class UserController extends Controller{
    /**
     * 更新用户密码
     *
     * @param  Request  $request
     * @param  int  $id
     * @return Response
     */
    public function updatePassword(Request $request, $id)
    {
        $user = User::findOrFail($id);

        // 验证新密码长度...

        $user->fill([
            'password' => Hash::make($request->newPassword)
        ])->save();
    }
}

此外,还可以使用全局的帮助函数bcrypt

bcrypt('plain-text');

2.1 验证哈希密码

check方法允许你验证给定原生字符串和给定哈希是否相等,然而,如果你在使用Laravel自带的AuthController(详见用户认证一节),就不需要再直接使用该方法,因为自带的认证控制器自动调用了该方法:

if (Hash::check('plain-text', $hashedPassword)) {
    // 密码匹配...
}

2.2 检查密码是否需要被重新哈希

needsRehash方法允许你判断哈希计算器使用的工作因子在上次密码被哈希后是否发生改变:

if (Hash::needsRehash($hashed)) {
    $hashed = Hash::make('plain-text');
}
服务 ―― 文件系统/云存储
服务 ―― 帮助函数
温馨提示
下载编程狮App,免费阅读超1000+编程语言教程
取消
确定
目录

关闭

MIP.setData({ 'pageTheme' : getCookie('pageTheme') || {'day':true, 'night':false}, 'pageFontSize' : getCookie('pageFontSize') || 20 }); MIP.watch('pageTheme', function(newValue){ setCookie('pageTheme', JSON.stringify(newValue)) }); MIP.watch('pageFontSize', function(newValue){ setCookie('pageFontSize', newValue) }); function setCookie(name, value){ var days = 1; var exp = new Date(); exp.setTime(exp.getTime() + days*24*60*60*1000); document.cookie = name + '=' + value + ';expires=' + exp.toUTCString(); } function getCookie(name){ var reg = new RegExp('(^| )' + name + '=([^;]*)(;|$)'); return document.cookie.match(reg) ? JSON.parse(document.cookie.match(reg)[2]) : null; }