密码散列每次在 Laravel 4.2 中产生不同的结果

jan*_*kii 1 php passwords hash laravel-4

我有密码散列问题。这是我的控制器

 public function registerUser() {
    $valid = Validator::make(Input::all(), array(
        'pass' => 'required|min:5',
        'pass2' => 'required|same:pass'
    ));

    if($valid->fails()) {
        return Redirect::route('register')->withErrors($valid)->withInput();
    }
    // $password = Input::get('pass');
    if(Input::hasFile('photo')) {
        $img = Input::file('photo');
        if($img->isValid()) {
            echo Hash::make(Input::get('pass'));
        }else{
            return Redirect::route('register')->withInput()->with('errorimg','image-error');
        }
    }else{
        echo Hash::make(Input::get('pass'));
    }

    //return Redirect::route('register')->with('success','register-success');
}
Run Code Online (Sandbox Code Playgroud)

每次我刷新浏览器时,散列传递总是改变。

例如:如果我把“qwerty”作为通行证,它应该显示

$2y$10$PPgHGUmdHFl.fgF39.thDe7qbLxct5sZkJCH9mHNx1yivMTq8P/zi

mar*_*kli 5

每次生成不同的散列是有意的,因为Hash::make()方法会生成一个随机盐。随机盐是安全保护用户密码所必需的。

要根据存储的散列检查输入的密码,您可以使用方法Hash::check(),它将从散列值中提取使用的盐并使用它来生成可比较的散列。

// Hash a new password for storing in the database.
// The function automatically generates a cryptographically safe salt.
$hashToStoreInDb = Hash::make($password);

// Check if the hash of the entered login password, matches the stored hash.
// The salt and the cost factor will be extracted from $existingHashFromDb.
$isPasswordCorrect = Hash::check($password, $existingHashFromDb);
Run Code Online (Sandbox Code Playgroud)