Skip to content

Instantly share code, notes, and snippets.

@zyphlar
Created December 20, 2014 22:36
Show Gist options
  • Star 17 You must be signed in to star a gist
  • Fork 9 You must be signed in to fork a gist
  • Save zyphlar/7217f566fc83a9633959 to your computer and use it in GitHub Desktop.
Save zyphlar/7217f566fc83a9633959 to your computer and use it in GitHub Desktop.
Generating secure passwords in PHP
<?php
// usage: $newpassword = generatePassword(12); // for a 12-char password, upper/lower/numbers.
// functions that use rand() or mt_rand() are not secure according to the PHP manual.
function getRandomBytes($nbBytes = 32)
{
$bytes = openssl_random_pseudo_bytes($nbBytes, $strong);
if (false !== $bytes && true === $strong) {
return $bytes;
}
else {
throw new \Exception("Unable to generate secure token from OpenSSL.");
}
}
function generatePassword($length){
return substr(preg_replace("/[^a-zA-Z0-9]/", "", base64_encode(getRandomBytes($length+1))),0,$length);
}
@Cahl-Dee
Copy link

Close your php tag 😃

@sclearion
Copy link

no need to close tags actually :) From PHP Manual:
The closing tag of a PHP block at the end of a file is optional, and in some cases omitting it is helpful when using include() or require(), so unwanted whitespace will not occur at the end of files, and you will still be able to add headers to the response later. It is also handy if you use output buffering, and would not like to see added unwanted whitespace at the end of the parts generated by the included files.

@Lewiscowles1986
Copy link

In fact @Cahl-Dee, closing tags in non-effect files (view code), is a really _Terrible_ practice, because it can cause effects

@lacek
Copy link

lacek commented Jan 28, 2016

Be careful of the length!
String from base64_encode may contain + and / characters. If you simply remove them by preg_replace, there is chance the string length is shorter than $length

@pixelbart
Copy link

pixelbart commented Feb 18, 2020

The variable $strong in line 7 is not defined anywhere. Otherwise: Thank you very much for this!

@zyphlar
Copy link
Author

zyphlar commented Mar 19, 2020 via email

@pixelbart
Copy link

Actually $strong is a passed-by-reference argument that is defined as a result of the function. Openssl uses it to tell you if the bytes are sufficiently random or not.

You're right, sorry! Thanks for your answer.

@RobinRadic
Copy link

Thanks!
A minor improvement, for clearer intention and lower complexity:

    if (false !== $bytes && true === $strong) {
        return $bytes;
    }
    else {
        throw new \Exception("Unable to generate secure token from OpenSSL.");
    }

into

    if (false !== $bytes && true === $strong) {
        return $bytes;
    }
    throw new \Exception("Unable to generate secure token from OpenSSL.");

@zyphlar
Copy link
Author

zyphlar commented Aug 13, 2020 via email

@jhonnatan1806
Copy link

Thanks, excellent code, I will definitely use it in my projects :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment