Page 1 of 1

Generating a random key

Posted: Sat Mar 07, 2009 4:01 pm
by JellyFish
I was wondering if there was already of php function included with php that can accomplish generating a random string of 32 numbers, upper-case letters, and lower-case letters? For example, "1a2b3d4f5s6D7f8s9S0s1G3j48DgaIb8".

If there isn't a function included in php main library, then how would I go about creating such a function. What would be the best way to generate a string like this? What I mean by best, the most clean code-wise, uses the least amount of server resources, etc.

I have an idea of how I could do this. I don't want to stop you from coming up with your own and potentially better solution so don't mind to much with my idea if you think you have a better solution.

Sense the string is 32 characters long, and sense each character can hold 0-9-a-z-A-Z then that means each character is 62 bits (0 to 9 + a to z + A to Z = 10 + 26 + 26 = 62). Sense each character is 62 bits that means that the whole string is 62^32 bits (32 because that's the length of the string). This is obviously a large number so large that you need to use the e+ notation to express it:

2.2726578844967515e+57

So, my idea is generate a random number from 0 to 62^32 and then convert that to base 62! :D Sense my string is virtually just a base 62 number with 32 digits, this would work, right?

The only reason I haven't done this yet is because I don't know how to generate a random number in the range of 0 to 62^32 and then convert that to base 62?

Thanks for reading. All help is very, very so much appreciated. :lol:

Re: Generating a random key

Posted: Sat Mar 07, 2009 4:02 pm
by Benjamin
You are free to use this:

viewtopic.php?f=50&t=96344

Re: Generating a random key

Posted: Sat Mar 07, 2009 5:23 pm
by JellyFish
I think I got it. Writing the OP helped me to think it though a bit.

Code: Select all

   function makeKey()
    {
        $chars = str_split("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
        
        for ($i = 0; $i < 32; $i++)
        {
            $randNum = rand(0, 61);
            $key .= $chars[$randNum];
        }
        return $key;
    } 

Re: Generating a random key

Posted: Sat Mar 07, 2009 5:45 pm
by requinix
You don't even need the str_split: you can use [] with strings too.

Re: Generating a random key

Posted: Sat Mar 07, 2009 6:32 pm
by JellyFish
tasairis wrote:You don't even need the str_split: you can use [] with strings too.
What do you mean?

Re: Generating a random key

Posted: Sat Mar 07, 2009 6:43 pm
by requinix
JellyFish wrote:
tasairis wrote:You don't even need the str_split: you can use [] with strings too.
What do you mean?

Code: Select all

$chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$chars[0] is "0", $chars[10] is "a", and $chars[61] is "Z".

Re: Generating a random key

Posted: Sat Mar 07, 2009 9:08 pm
by JellyFish
tasairis wrote:
JellyFish wrote:
tasairis wrote:You don't even need the str_split: you can use [] with strings too.
What do you mean?

Code: Select all

$chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
$chars[0] is "0", $chars[10] is "a", and $chars[61] is "Z".
Oh sweet! I never knew that until now. Thanks. :D