Laravel Hash :: make()和Hash :: check()不与Postgres一起使用

时间:2016-04-28 17:16:14

标签: postgresql laravel hash

我在Laravel 5,Postgres和Hash门面遇到了一个奇怪的问题。我有以下测试代码:

public static function addNewPartner($args){
    $user = new User;
    $user->username = $args['username'];
    $user->email = $args['email'];
    // Here starts the issue
    $password = Hash::make($args['password']);
    $user->password = $password;
    $user->remote_key = str_random(16);
    if(!$user->save()){
        return false;
    }
    $role = Role::where('name','=','partner')->first();
    $user->attachRole($role);
    Log::info("User::addNewPartner. User:" . json_encode($user));

    // At this point the password is hashed and saved into DB, I can
    // see the record saved. 

    // I compare the saved password with the input and it fails. 
    if(Hash::check($args['password'], $user->password)){
        Log::info('DB hash' . " " . $password);
    } else {
        Log::info('DB no hash' . " " . $password);
    }

    // I compare the password as it was generated with the input and 
    // it passes. 
    if(Hash::check($args['password'], $password)){
        Log::info('LOCAL hash' . " " . $password);
    } else {
        Log::info('LOCAL no hash' . " " . $password);
    }


    return $user;
}

日志输出为:

[2016-04-28 12:04:46] local.INFO:UserController :: postAddnewpartner。输入:{"令牌":" eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJzdWIiOjIsImlzcyI6Imh0dHA6XC9cL3Vici5sb2NhbDo4MDAwXC9hcGlcL3VzZXJcL2xvZ2luIiwiaWF0IjoxNDYxODYyNDYwLCJleHAiOjE0NjE4NjYwNjAsIm5iZiI6MTQ2MTg2MjQ2MCwianRpIjoiZmU0MTgzMTAwMzQyMjU3Mjg2MGJmOWJhN2FmOWIyMDMifQ.iUZzepZshdn7snUGOzTt3AEAyfRKNGPrCJpk5FxJtxw""全名":" usuario1""电子邮件&#34 ;:" usuario1@usuario.com","密码":" 1234""用户名":" usuario1" }

[2016-04-28 12:04:46] local.INFO:User :: addNewPartner。用户:{"用户名":" usuario1""电子邮件":" usuario1@usuario.com"," remote_key&#34 ;:" jhDpJgolw4bTdsZj"," updated_at":" 2016-04-28 12:04:46"," created_at":&#34 ; 2016-04-28 12:04:46"," id":6}

[2016-04-28 12:04:46] local.INFO:DB no hash $ 2y $ 10 $ PU24f.AkNguifnh1AkKSJuVu7I4idWGUz8SA2L / 37sRsI6JaVkQZC

[2016-04-28 12:04:46] local.INFO:LOCAL hash $ 2y $ 10 $ PU24f.AkNguifnh1AkKSJuVu7I4idWGUz8SA2L / 37sRsI6JaVkQZC

由于某种原因,如果从数据库中检索记录,则Hash :: check()不起作用。

奇怪的是,在播种过程中创建的测试用户工作:

class UserTableSeeder extends Seeder {

public function run() {
    $users = array(
        array(
            'username' => 'admin',
            'email' => 'admin@example.org',
            'password' => Hash::make('1234'),
            'parent_id'=>null,
            'created_at' => new DateTime,
            'updated_at' => new DateTime,
        ),

我的用户迁移是:

public function up() {
    Schema::create('users', function(Blueprint $table) {
        $table->increments('id');
        $table->integer('parent_id')->unsigned()->nullable();
        $table->string('username')->unique();
        $table->string('email')->unique();
        $table->string('password', 60);
        $table->string('remote_key')->default(str_random(16));
        $table->rememberToken();
        $table->timestamps();
    });
}

你知道Hash失败的原因吗?谢谢。

1 个答案:

答案 0 :(得分:0)

所以问题恰恰就在这些问题上:

$password = Hash::make($args['password']);
// more code
if(!$user->save()){
    return false;
}

在保存用户时,密码被散列,因此程序正在散列哈希。所以,行:

// I compare the saved password with the input and it fails. 
if(Hash::check($args['password'], $user->password)){
    Log::info('DB hash' . " " . $password);
} else {
    Log::info('DB no hash' . " " . $password);
}

实际上是将存储在数据库中的散列哈希与散列密码进行比较,结果比较返回false。