V
V
Vitaly Zhuk2013-06-02 15:31:00
PHP
Vitaly Zhuk, 2013-06-02 15:31:00

Unique Key (Algorithm)?

Hello to all habra community.
Target:
Periodically generate unique keys in very large numbers (Approximately: 500,000 - 2,000,000). All of these keys must be completely unique for subsequent application functions. The key length may vary depending on the quantity. Start starts with 8 characters. The key must be subject to the pattern: /^[a-zA-Z0-9]+$/
The total number of keys is unknown (maybe even more than 1 billion).
The essence of the problem lies in the calculation of this key. Personally, I would prefer to use:
1. uniqid , but alas, it is not very suitable, since the number of characters can be different (and here at least 13).
2. Random calculation (rand, mt_rand) - but they do not guarantee 100% uniqueness.
3. md5(time() + $randHash) - but alas, the length is already 32 characters
Question: Perhaps someone knows some good algorithm for generating a random key by the number of characters? If the algorithm has additional parameters for generation that will need to be obtained during generation (module, hour mark, etc.), which affect the creation of a unique key, then no problem, the keys are stored in the database.
A primitive example in life is scratch codes for topping up phones. There is a set of keys that simply do not match :)
Thank you!

Answer the question

In order to leave comments, you need to log in

6 answer(s)
A
Anastasia_K, 2013-06-02
@Anastasia_K

option to trim md5 to the desired number of characters for some reason is not suitable? uniqueness can be checked at the stage of inserting into the database if the corresponding field is "unique".

F
FilimoniC, 2013-06-02
@FilimoniC

If your keys are not secret, then just take a timestamp and some ID (for example, serial) and encode in Base62 (this is Base64 without +- characters)
. The timestamp is 4 bytes (8 bytes if you plan to live to 2038).

F
FilimoniC, 2013-06-02
@FilimoniC

And why not just use sequential keys?

V
Vitaliy Zhuk, 2013-06-02
@ZhukV

So, regarding the answers, everything is clear that nothing is understood. Everyone writes what they want, while not reading the question itself: “are there any mathematical algorithms for generating random numbers?”
The reason is not that we cannot use md5 from random or timestamp, but that at the moment we do not know the final amount. The key length should not be difficult for a simple user to enter. And if the length is 32 characters? Well, let's enter! It will probably be convenient ... And at the same time, if we take only 8 characters at the moment, and as a result we need a huge number of keys, then what?
If you know which algorithm to create, then then there will be no problems at all with increasing the key.

R
rakot, 2013-06-02
@rakot

1. Write a function to represent a decimal number in the key you need. Only necessarily two-way, not a hash (so that there are no collisions) (although you can not write, but take the same Base62).
2. Take a starting point for generating sequences, for example the number 123231314.
3. Add rand(50000,100000) to the number and generate a key from the new number.
4. After generating the required quantity, save the new starting point.

M
MikhailEdoshin, 2013-06-04
@MikhailEdoshin

There was a similar question recently, here is my answer , see if it fits. The idea is that you take consecutive numbers and sort of encrypt them with some simple function (for example, invert and rearrange the bits in a known pattern). The result is numbers that are outwardly mixed, but one hundred percent unique. In your case, you are also serializing the results to base32.
Eight characters each of five bits gives 40 bits of information, that is, 1,099,511,627,776 numbers. For a billion numbers, 30 bits is sufficient (2^30 = 1,073,741,824). The remaining ten bits (which, of course, can go out of order) can be filled with random information and / or used for a checksum, additional marks (series number), etc. Of course, if you have longer numbers, then there is still room more.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question