php Laravel Hash::check()always return false

5fjcxozz  于 6个月前  发布在  PHP
关注(0)|答案(6)|浏览(73)

**已关闭。**此问题为not reproducible or was caused by typos。目前不接受回答。

这个问题是由一个错字或一个无法再重现的问题引起的。虽然类似的问题可能在这里是on-topic,但这个问题的解决方式不太可能帮助未来的读者。
2天前关闭。
社区正在审查是否重新开放这个问题,截至2天前。
Improve this question
我有配置文件形式的用户可以编辑自己的配置文件。在这种形式下,我有当前的密码。必须从seved到数据库匹配。
形式:

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

字符串
我想在控制器中有这个功能来检查数据库。

$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

bqucvtff

bqucvtff1#

你用错了参数顺序。应该是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

字符串

nfzehxib

nfzehxib2#

我遇到了同样的问题,并这样解决了它:
我发现我在我的RegistrationService类中使用了Hash::make函数,更重要的是,我已经在我的User model中使用了setPasswordAttribute函数,但很快就忘记了:

class User extends Model implements AuthenticatableContract, AuthorizableContract
{
   ...

    /**
     * @param $value
     */
    public function setPasswordAttribute($value)
    {
        $this->attributes['password'] = Hash::make($value);
    }
}

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

9gm1akwq

9gm1akwq3#

Hash::check()有两个参数,一个是平面密码,另一个是哈希密码。如果密码与哈希匹配,则返回true。

Hash::check(normal_password,hashed_password);

字符串

示例:

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

wqnecbli

wqnecbli4#

虽然以上的答案是有效的问题提供,我添加更多的解释,给予细节的见解

通过哈希算法验证密码

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

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

字符串

check()方法在HasherInterface中声明

这个方法是检查给定的普通值对哈希。

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


根据哈希值检查给定的普通值。

参数

string $value
string $hashedValue
array $选项

返回值

bool

您的示例:

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

tpxzln5u

tpxzln5u5#

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

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

字符串

hjzp0vay

hjzp0vay6#

我不认为使用这种方法是一个好主意。这可能会失败,因为它包含特殊字符,如$ / .等。这些字符将在此哈希检查期间进行转换,并且您可能无法始终获得您想要的结果。请尝试使用其他方法。

相关问题