Generating a random key
Posted: Sat Mar 07, 2009 4:01 pm
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!
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.
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!
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.