Hello!
I am trying to generate two keys for user accounts, a public key (Which the client will see), and a private key (For the system).
Currently, I am just using substr(str_shuffle()) to generate the keys for testing, but for production, I have to be 100% certain that no two keys are alike. Also, the private key must be near impossible to guess.
Generating Private Key
My idea was like this: RandomString + Number. Example: “jHudSbE12” with the number being unique.
The code I have so far:
$pKeyPartOne = substr(str_shuffle('qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM'),0,10);
$pKeyPartTwo = 0 //I got stuck here
$finalpKey = $pKeyPartOne.$pKeyPartTwo
The final private key is then inserted into the database.
I want the number to be unique (Like Auto_Increment), but could not figure out how to get the Auto_Increment number into the code before it is inserted into the database (I know I could insert it into the database, then fetch the Increment ID and update it, but I don’t want to do that).
Generating the Public Key
The public key needs to be 8 numbers that are unique. My original idea was to just generate an 8-digit string with substr(str_shuffle() and check if it already exists in the database, but that could take a long time, especially if the code keeps generating a string that already exists.
I thought about using the PHP date() or time() function, but either it would not be unique, or not the right length. I also considered using Auto_Increment (I know you can set a starting value), but I would prefer random generation. Is there a better way to do this?
My Questions
- How can I get the Increment ID that the database entry that will be assigned before the information is entered into the database?
- How can I generate an 8-digit numerical string that is 100% unique?
Sorry for any confusion, and thanks in advance!