简体   繁体   English

检查bcrypt密码总是失败Phalcon php

[英]Check bcrypt passwords always fails Phalcon php

I have a little issue with checking bcrypted passwords with Phalcon php. 我用Phalcon php检查加密密码有点问题。 What I have is : Login script where I check the password 我所拥有的是:登录脚本,我检查密码

$username = $this->request->getPost('username', 'string');
            $password = $this->request->getPost('password', 'string');
            $conditions = "Username = :username:";
            $parameters = array (
                "username" => $username
            );

            $user = Users::findFirst(array($conditions, 'bind' => $parameters));
            //check if user exists
            if (count($user) > 0 && $user !== false) {

                if ($this->security->checkHash($password, $user->Password))  //always fails {
                    //login 
                    $this->session->set('username', $user->Password);
                    $this->response->redirect('index');

                }

In my Registration I have : 在我的注册中我有:

$name = $this->request->getPost('name', 'string');
            $lastName = $this->request->getPost('lastName', 'string');
            $username = $this->request->getPost('username', 'string');
            $password = $this->request->getPost('password', 'string');
            $email = $this->request->getPost('email', 'email');

            $user = new Users(); //model
            $user->Name = $name;
            $user->LastName = $lastName;
            $user->Username = $username;
            $user->Password = $this->security->hash($password);
            $user->Email = $email;
            if ($user->save() == true) {
                //registered
            } else {
                //error
            }

It seems like I am doing everything accordind to the documentation but it doesn't seem to work. 看起来我正在做文件的所有事情,但它似乎不起作用。 Could anybody help me please. 请有人帮帮我。

In your database the stored password must be the encrypted value of jt26 , ie, the product of $this->security->hash('jt26') . 在您的数据库中,存储的密码必须是jt26的加密值,即$this->security->hash('jt26') Probably you stored the password first and then implemented the register / login function. 您可能先存储密码然后实现注册/登录功能。 Just replace jt26 in your database with the string generated by $this->security->hash('jt26') and everything should start working. 只需将数据库中的jt26替换为$this->security->hash('jt26')生成的字符串,一切都应该开始工作。

It produces different string each and every time.Should it be like that? 它每次都会产生不同的字符串。应该是这样吗?

Yes, that's exactly what it should do. 是的,这正是应该做的。 See this for details. 请参阅了解详情。 The salt is always randomly generated (unless provided), based on which the hash is generated. 始终随机生成salt(除非提供),基于该生成的哈希值。 When verifying the password, Bcrypt uses salt to regenerate the hash and then checks that it matches. 验证密码时,Bcrypt使用salt重新生成散列,然后检查它是否匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM