Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate a Token in Symfony 3.4

I have my symfony 2.7 updatet to symfony 3.4. I used in Symfony 2.7 the function generateToken() to create a token for a file upload task. I found just information about Securely Generating Random Values for symfony 3.4. But how I can integrate it?

can I use the following Statement?

 return bin2hex(random_bytes(32));

generateToken Symfony 2.8

like image 718
helmi Avatar asked Oct 20 '25 05:10

helmi


1 Answers

I know this might be late but hopefuly it will help somone else because there is no function out-of-the-box from symfony that can be used to generate tokens.

so what i did when i run into this problem is that i used the same token generator that is used by FOSUserBundle wich is this:

public function generateToken()
{
    return rtrim(strtr(base64_encode(random_bytes(32)), '+/', '-_'), '=');
}

https://github.com/FriendsOfSymfony/FOSUserBundle/blob/master/Util/TokenGenerator.php

and as you can see it uses the random_bytes() function that is recommended in the official documentation of symfony combined with the php function base64_encode() wich is designed to make binary data survive transport through transport layers that are not 8-bit clean, such as mail bodies as explained in the official php documentation and they are using exactiy this example.

like image 57
younes elmorabit Avatar answered Oct 21 '25 19:10

younes elmorabit



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!