Symfony2: How do i generate sha512 hash of a password?

VishwaKumar picture VishwaKumar · Mar 30, 2012 · Viewed 15.4k times · Source

I need to compare the password entered by the user if it is valid or not. How do i get the sha512 encryption. I am also using FOSUserBundle in my project.

Answer

Mun Mun Das picture Mun Mun Das · Mar 30, 2012

In your controller you can do (assuming that you set sha512 as encoding algorithm in app/config/security.yml)

    $userName = 'username';
    $password = "pass";
    $userManager = $this->get('fos_user.user_manager');
    $user = $userManager->loadUserByUsername($userName);
    $encoder = $this->get('security.encoder_factory')->getEncoder($user);
    $encodedPass = $encoder->encodePassword($password, $user->getSalt());
    echo $user->getPassword() === $encodedPass;