I am trying to generate a random password in php.
However I am getting all 'a's and the return type is of type array and I would like it to be a string. Any ideas on how to correct the code?
Thanks.
function randomPassword() {
$alphabet = "abcdefghijklmnopqrstuwxyzABCDEFGHIJKLMNOPQRSTUWXYZ0123456789";
for ($i = 0; $i < 8; $i++) {
$n = rand(0, count($alphabet)-1);
$pass[$i] = $alphabet[$n];
}
return $pass;
}
This is based off another answer on this page, https://stackoverflow.com/a/21498316/525649
This answer generates just hex characters,
0-9,a-f
. For something that doesn't look like hex, try this:base64_encode
returns a wider spread of alphanumeric charsrtrim
removes the=
sometimes at the endExamples:
32eFVfGDg891Be5e7293e54z1D23110M3ZU3FMjb30Z9a740Ej0jz4
b280R72b48eOm77a25YCj093DE5d9549Gc73Jg8TdD9Z0Nj4b98760
051b33654C0Eg201cfW0e6NA4b9614ze8D2FN49E12Y0zY557aUCb8
y67Q86ffd83G0z00M0Z152f7O2ADcY313gD7a774fc5FF069zdb5b7
This isn't very configurable for creating an interface for users, but for some purposes that's okay. Increase the number of chars to account for the lack of special characters.
base_convert(uniqid('pass', true), 10, 36);
eg.
e0m6ngefmj4
EDIT
As I've mentioned in comments, the length means that brute force attacks would work better against it then timing attacks so it's not really relevant to worry about "how secure the random generator was." Security, specifically for this use case, needs to complement usability so the above solution is actually good enough for the required problem.
However, just in case you stumbled upon this answer while searching for a secure random string generator (as I assume some people have based on the responses), for something such as generating tokens, here is how a generator of such codes would look like:
If you are on PHP7 you could use the
random_int()
function:TL;DR:
random_int()
and the givenrandom_str()
.random_int()
, use random_compat.Explanation:
Since you are generating a password, you need to ensure that the password you generate is unpredictable, and the only way to ensure this property is present in your implementation is to use a cryptographically secure pseudorandom number generator (CSPRNG).
The requirement for a CSPRNG can be relaxed for the general case of random strings, but not when security is involved.
The simple, secure, and correct answer to password generation in PHP is to use RandomLib and don't reinvent the wheel. This library has been audited by industry security experts, as well as myself.
For developers who prefer inventing your own solution, PHP 7.0.0 will provide
random_int()
for this purpose. If you're still on PHP 5.x, we wrote a PHP 5 polyfill forrandom_int()
so you can use the new API before PHP 7 is released. Using ourrandom_int()
polyfill is probably safer than writing your own implementation.With a secure random integer generator on hand, generating a secure random string is easier than pie:
You want
strlen($alphabet)
, notcount
of the constantalphabet
(equivalent to'alphabet'
).However,
rand
is not a suitable random function for this purpose. Its output can easily be predicted as it is implicitly seeded with the current time. Additionally,rand
is not cryptographically secure; it is therefore relatively easy to determine its internal state from output.Instead, read from
/dev/urandom
to get cryptographically random data.Another one (linux only)