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

Password hashing produces different results every time in Laravel 4.2

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

 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');
}

每次我刷新浏览器时,散列通行证总是在变化。

ex :如果我把 "qwerty" 作为 pass,它应该显示

y$PPgHGUmdHFl.fgF39.thDe7qbLxct5sZkJCH9mHNx1yivMTq8P/zi

那是因为如果你不给一个 salt bcrypt 每次哈希一些东西时都会创建一个。

Source

每次生成不同的散列是有目的的,因为 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);