tl:dr version:
- Is the first code secure enough to use on a live site?
- How do I implement RandomLib? Clear instructions for beginners please.
I appreciate this question has been asked before but there does not seem to be a clear answer and guidance on how to use the recommended method.
I need to create random reset codes where a user has forgotten their password. This reset code is then added to the URL which is emailed to them. When they click on the URL the reset code and email is compared to the code and email in the database and if a match, a reset form is offered.
My first attempt at creating the code is simple, but is it secure enough? How easy would it be to hack? I will be asking people to register on the site so I need to make the system secure.
<php?
$bytes = openssl_random_pseudo-bytes(3);
$reset_code = bin2hex($bytes);
I've seen a lot of recommendations on this site and others for RandomLib https://github.com/ircmaxell/RandomLib but I cannot work out how to use this and I have seen others struggle with no guidance. I have downloaded the zip and added to my website documents. I either receive an error message "Fatal error: Class 'RandomLib\Factory' not found" or when I link to the factory.php file with "require_once("RandomLib\factory.php");" I receive an error "Fatal error: require_once(): Failed opening required 'RandomLibactory.php' (include_path='.;C:\php\pear')"
require_once("RandomLib\factory.php");
$factory = new RandomLib\Factory;
$generator = $factory->getMediumStrengthGenerator(8, alphabet);
In short, if the RandomLib is the best option, it would be helpful if someone could offer guidance on how to use it for beginners in a position similar to myself. Or if the first option is secure enough, I will leave it as it is.
UPDATE:
As pointed out by Narf below, the function to create the random string was missing the second parameter which ensures the generated string is secure.
openssl_random_pseudo-bytes(3);
It should read:
openssl_random_pseudo-bytes( 3 , $cstrong );
Where the second parameter $cstrong returns a boolean which is true if it is secure but false if not. http://php.net/manual/en/function.openssl-random-pseudo-bytes.php