控制器中的密码解码

时间:2012-10-27 14:00:03

标签: symfony

我用它来编码我的密码:

 $entity->setSalt(md5(time()));
 $encoder = new MessageDigestPasswordEncoder('sha1');
 $password = $encoder->encodePassword($editForm->get('password')->getData(), $entity->getSalt());
 $entity->setPassword($password);

但是如何才能重新开始呢?也就是说,我怎么能得到未加密的密码?如果我使用这个

$entity->getPassword()

告诉我这个:

xOGjEeMdi4nwanOustbbJlDkug8=

非常感谢您的回复。我正在尝试创建一个表单,用户输入旧密码并验证它是否为真。以我的形式:

            ->add('antigua', 'password', array('property_path' => false))
        ->add('password', 'repeated', array('first_name' => 'Nueva contraseña','second_name' => 'Repite contraseña','type' => 'password'));

当我去编辑crud中的用户时,我有这个: 在更新操作中:

public function updateAction($id)
    {
        $em = $this->getDoctrine()->getEntityManager();

        $entity = $em->getRepository('miomioBundle:Empleado')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Empleado entity.');
        }

        $editForm   = $this->createForm(new EmpleadoType(), $entity);
        $deleteForm = $this->createDeleteForm($id);

        $request = $this->getRequest();
        **$entity->getPassword() is blank why?**
        $editForm->bindRequest($request);

        if ($editForm->isValid()){
            $em->persist($entity);
            $em->flush();
        }
            return $this->redirect($this->generateUrl('empleado_edit', array('id' => $id)));

        return array(
            'entity'      => $entity,
            'edit_form'   => $editForm->createView(),
            'delete_form' => $deleteForm->createView(),
        );
    }
问题是我无法获得编码密码为空。 (在db中是正确的) 感谢

2 个答案:

答案 0 :(得分:4)

您应该以与旧密码相同的方式加密,即用户输入的密码。结果加密密码应该相同。

$encoder = new MessageDigestPasswordEncoder('sha1');
$password = $encoder->encodePassword($editForm->get('antigua')->getData(), $entity->getSalt());

现在您可以将旧的加密密码与输入的新用户进行比较...

答案 1 :(得分:2)

无法解密sha1或md5中编码的密码,这些隐藏方法被创建为无法解密!

自定义编码器:

唯一的方法是使用自制方法创建自己的自定义编码器来加密(以便解密)您的密码,例如:http://blogsh.de/2011/09/29/create-a-custom-password-encoder-for-symfony/

你没有被迫在encodePassword()中使用$ salt,你可以用特定的数字替换每个字母,这样你可以通过相反的方式检索密码,你也可以切割盐并添加部分在密码内等...

明文,不推荐:

或者不太建议,不加密密码并让它们明文:

# app/config/security.yml
security:
    encoders:
        Symfony\Component\Security\Core\User\User: plaintext