【问题标题】:Laravel Hash::check() always return falseLaravel Hash::check() 总是返回 false
【发布时间】:2014-02-25 01:24:28
【问题描述】:

我有个人资料表格供用户编辑自己的个人资料。在这种形式中,我有当前密码。必须从 seved 匹配到数据库中。

表格:

{{ Form::password('currPassword', array('id'=>'currPassword')) }}

我想在 Controller 中有这个功能来检查数据库。

$data = User::find($id);
if( ! Hash::check( $data->password , Input::get('currPassword') ) )
{
    return Redirect::to('/admin/profile')
        ->with('message', 'Current Password Error !')
        ->withInput();
}

123456 散列到数据库中的密码可以,并且在将123456 放入currPassword 之后,必须返回TRUE,但总是返回FALSE

【问题讨论】:

  • 更多详情请参考this

标签: php laravel laravel-4


【解决方案1】:

您使用了错误的参数顺序。是Hash::check($input, $hash),而不是相反。

简短的修补程序示例:

[1] > $pw = 123456;
// 123456
[2] > $hashed = Hash::make($pw);
// '$2y$10$xSugoyKv765TY8DsERJ2/.mPIOwLNdM5Iw1n3x1XNVymBlHNG4cX6'
[3] > Hash::check($hashed, $pw);
// false
[4] > Hash::check($pw, $hashed);
// true

【讨论】:

    【解决方案2】:

    Hash::check() 有两个参数第一个是平面密码和 另一个是散列密码。如果密码与哈希匹配,它将 返回真。

    Hash::check(normal_password,hashed_password);
    

    示例:

    Hash::check('123456a','$2y$10$.XB30GO4jn7bx7EauLrWkugIaCNGxiQCgrFTeFDeSSrGdQYd6Rneq');
    

    【讨论】:

      【解决方案3】:

      虽然上述答案对所提供的问题有效,但我正在添加更多解释以提供详细见解

      根据哈希验证密码

      check 方法允许您验证给定的纯文本字符串是否对应于给定的哈希值。然而,如果你使用 Laravel 自带的 LoginController,你可能不需要直接使用它,因为这个控制器会自动调用这个方法:

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

      check() 方法在 HasherInterface 中声明

      此方法是根据哈希检查给定的普通值。

       bool check(string $value, string $hashedValue, array $options = array())
      

      根据散列检查给定的普通值。

      参数

      字符串 $value
      字符串 $hashedValue
      数组 $options

      返回值

      布尔

      以您为例:

      $data = User::find($id);
      if( ! Hash::check(Input::get('currPassword') , $data->password  ) )
      {
          return Redirect::to('/admin/profile')
              ->with('message', 'Current Password Error !')
              ->withInput();
      }
      

      【讨论】:

        【解决方案4】:

        我遇到了同样的问题并这样解决了:

        我发现我在我的 RegistrationService 类中使用了 Hash::make 函数,更重要的是我已经在我的 User 中使用了 setPasswordAttribute 函数模型很快就被遗忘了:

        class User extends Model implements AuthenticatableContract, AuthorizableContract
        {
           ...
        
            /**
             * @param $value
             */
            public function setPasswordAttribute($value)
            {
                $this->attributes['password'] = Hash::make($value);
            }
        }
        

        所以密码是双重哈希的,当然每个 Hash::check 调用都不正确并返回 false。

        【讨论】:

        • 非常感谢!我有一个播种机,它生成超级管理员,称为哈希,然后在用户管理模块上工作时,我做了和你一样的事情
        【解决方案5】:

        我遇到了同样的问题,在花了 2 个小时解决之后,我发现我在更新密码之前对密码进行了两次哈希处理。
        1. 从 PasswordResetController,
        2.在用户模型中,我有这个功能:

        public function setPasswordAttribute($password)
        {   
            $this->attributes['password'] = bcrypt($password);
        }
        

        【讨论】:

          猜你喜欢
          • 2021-06-05
          • 2019-08-15
          • 2020-02-14
          • 1970-01-01
          • 1970-01-01
          • 2014-10-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多