user1297406
user1297406

Reputation: 1331

Compare password with Hashed + Salt generated by Symfony 2

I'm trying to authenticate users in Mysql database generated by Symfony 2. In Security.yml I have this :

security:
    encoders:
        "FOS\UserBundle\Model\UserInterface": sha512

In User Table there is 2 fields : Salt and Password.

All passwords are like that :

YqkYUe0pV/TAw12aG2UcBax0hnJNeHez/S0uBGbnDDBxWD2Yeetqm4DfMn/8WKILIeRpM7ncTJ9coYOiNPGeOA==

I'm working on a webservice to authenticate users using PHP. I don't which functions do I have to use to compare plain password with the encrypted ones?

Upvotes: 0

Views: 1802

Answers (2)

S. A. Kıyak
S. A. Kıyak

Reputation: 127

This is the class that handles the sha512 encryption in Symfony2

https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Security/Core/Encoder/MessageDigestPasswordEncoder.php

Upvotes: 0

Yoann Chambonnet
Yoann Chambonnet

Reputation: 1333

You have to get the password encoder factory from the container.

You can do so like this :

$factory = $container->get('security.encoder_factory'); //$container refers to your container, it can be also $this->container
$user = new Your\Bundle\Entity\User();

$encoder = $factory->getEncoder($user);
$encodedPassword = $encoder->encodePassword($nonEncodedPassword, $user->getSalt());

This should be enough. Of course you can set "by hand" the second encodePassword parameter as its the salt used to encode all paswords. It's usually defined in your user implementation class, that's why we give here an entity instancied object.

Upvotes: 1

Related Questions