Create a shorter public identifier

I am looking for a way to create public identifiers for my objects (e.g. facebook uid). The identifier must be unique and the user must not guess it (or the next one).

Now I use this:

sha1('a fixed random string' . $this->getId()) 

The problem is that sha1 generates very long lines (40 characters) and I would like it to be shorter. I thought about using MD5, but as you know, it will not save, because you can create conflicts. Is this really a problem in my case?

What alternatives do I have?

+1
source share
5 answers

I did some tests to find out how long id would use different hash functions. I also used convBase to convert from 0-9a-z to 0-9a-zA-Z as suggested :

 $i = mt_rand(1, PHP_INT_MAX); sha1($i): 40 322c73c44958e4219fd6679aead094192cb672fe convBase(sha1($i)): 34 1JHrlXObHSVMcbn2bHRBCBIRD3RVKQHMQzg md5($i): 32 7b09f8cd76be44403b90e971a5a61e6c convBase(md5($i)): 28 5bgdGpBZekbb3PQlILrSKMtHC24A $i: 9 107300785 convBase($i): 5 7gdPP 

So, sha1 is very long, even if it is converted to another base. Also, I'm still not sure if MD5 is safe. Anyway, with 28 characters, it's still pretty long if you want to post it to twitter url: http://mydomainsux.com/invite/5bgdGpBZekbb3PQlILrSKMtHC24A

The shortest solution with convBase($i) has a non-uniqueness problem. You will need to check this manually. However, I think this is the way to go, because if you use a hash, there can also be a natural conspiracy, so you still need to check.

I read that you can pre-create an identifier in a table and just select once when you need it. Thus, you do not need to check uniqueness every time.

0
source

If you use strong salt (random string), this is not a big MD5 collision problem.

+1
source

Why do you have unique meanings? I think it's much better to just use a unique integer like this.

While you really want to hash it, take a look at this: http://www.php.net/manual/en/function.hash-algos.php , that is, a complete list of hashing functions available. Check what is installed on your server.

+1
source

If you are going to store identifiers in a database, you can go uniqid . If you are going to use a regular identifier (those auto_increments) in the database and just want to use a representative identifier, create a function with some math calculations inside :)

+1
source

Use a random integer rather than perform a basic conversion. The idea is that you use the target encoding of many characters, such as AZ, az, and 0-9. Thus, your number can be encoded in a very short line.

For a simple version that users 0-9 and az you can use the PHP base_convert function: (Http://codepad.org/9craDgbt)

 echo base_convert ( 123456789132465798132 , 10 , 36 ); 

gives you

 c5m8nqw9ps 

If you want it to be even shorter, look at the "convBase" function in the comments http://php.net/manual/de/function.base-convert.php . By increasing the number of characters in the target encoding, the result becomes shorter.

 convBase($randomInt,'0123456789','0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'); 
+1
source

Source: https://habr.com/ru/post/1385318/


All Articles