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;
}
Being a little smarter:
check it here.
I'm going to post an answer because some of the existing answers are close but have one of:
This answer will circumvent the
count/strlen
issue as the security of the generated password, at least IMHO, transcends how you're getting there. I'm also going to assume PHP > 5.3.0.Let's break the problem down into the constituent parts which are:
For the first part, PHP > 5.3.0 provides the function
openssl_random_pseudo_bytes
. Note that whilst most systems use a cryptographically strong algorithm, you have to check so we'll use a wrapper:For the second part, we'll use
base64_encode
since it takes a byte string and will produce a series of characters that have an alphabet very close to the one specified in the original question. If we didn't mind having+
,/
and=
characters appear in the final string and we want a result at least$n
characters long, we could simply use:The
3/4
factor is due to the fact that base64 encoding results in a string that has a length at least a third bigger than the byte string. The result will be exact for$n
being a multiple of 4 and up to 3 characters longer otherwise. Since the extra characters are predominantly the padding character=
, if we for some reason had a constraint that the password be an exact length, then we can truncate it to the length we want. This is especially because for a given$n
, all passwords would end with the same number of these, so that an attacker who had access to a result password, would have up to 2 less characters to guess.For extra credit, if we wanted to meet the exact spec as in the OP's question then we would have to do a little bit more work. I'm going to forgo the base conversion approach here and go with a quick and dirty one. Both need to generate more randomness than will be used in the result anyway because of the 62 entry long alphabet.
For the extra characters in the result, we can simply discard them from the resulting string. If we start off with 8 bytes in our byte-string, then up to about 25% of the base64 characters would be these "undesirable" characters, so that simply discarding these characters results in a string no shorter than the OP wanted. Then we can simply truncate it to get down to the exact length:
If you generate longer passwords, the padding character
=
forms a smaller and smaller proportion of the intermediate result so that you can implement a leaner approach, if draining the entropy pool used for the PRNG is a concern.Quick One. Simple, clean and consistent format if that is what you want
I've been meaning to try implementing my own "secure" and "random" password generator.
I thought it would be interesting and more flexible to provide a
length
andtype
parameter to the function.Here is a function that performs a few checks to make sure the
length
is not too short or long (also allowing variable length). It also allows the ability to choose your password charset in thetype
parameter.It returns
null
if thelength
ortype
are invalid and uses PHP'smt_rand()
function which is more "random" thanrand()
.This is basically the same as the much simpler
substr(md5(uniqid()), 0, 8);
If you want 8 characters with 1 uppercase and 1 number.