我正在使用 Symfony 4.0。我想在注册时用 sha256 加密我的用户密码。我不想使用 Symfony 安全性,但如果没有其他方法,我将使用它。
我尝试使用 md5 并工作,但使用 sha256 会引发异常。
Warning: Use of undefined constant sha256 - assumed 'sha256' (this will
throw an Error in a future version of PHP)
那是我的控制器。
public function register(Request $request)
{
$user = new User();
$form = $this->createForm(UserType::class, $user);
$form->handleRequest($request);
if ($form->isSubmitted() && $form->isValid()) {
$user = $form->getData();
$entityManager = $this->getDoctrine()->getManager();
//$user->setPassword(hash(sha256,$user->getPassword()));
$user->setPassword(md5($user->getPassword()));
$entityManager->persist($user);
$entityManager->flush();
$flashbag = $this->get('session')->getFlashBag();
$flashbag->add("SuccessfullRegister", "You successfully registered in our site!");
return $this->redirectToRoute('EventsAndPeople');
}
return $this->render('home/RegisterForm.html.twig', array(
'form' => $form->createView(),
));
}
出于某种原因, md5 有效但 sha256 无效。
sha256不是常量,您必须将其作为字符串提供。改变这一行:
$user->setPassword(hash(sha256, $user->getPassword()));
至
$user->setPassword(hash("sha256", $user->getPassword()));
请注意,我在 roud 中添加了引号sha256
。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句