Question

I need to generate unique URL like tinyurl's: domain.com/pgDzs7, domain.com/ab4dh3 BUT (!) the problem is that I don't want users to have a possibility to view previous and next URLs by just changing last letters in the URL.

For example, if somebody creates a content which gets URL domain.com/pgDzs7, I want next visitor to get an absolutely different unique URL (for example, "ab4dh3") so nobody can't find out how these URLs have been generated and see content of other users unless they know its URL.

All I found here on Stackoverflow is to convert table's primary integer key into a base64 form. But I need different solution which is also will not generate collisions and doesn't have for/while cycles (if it's possible) since my MySQL table has dozens of GBytes.

Was it helpful?

Solution 3

I found a better solution. All I need is to shuffle the alphabet [a-zA-Z0-9] so visitors aren't able to iterate URLs. At least it won't be so easy as before.

OTHER TIPS

You can make a formula to get the next index. Like: LastID*2+5

You won't have colisions or loops to check if the id was used before.

You could use modular exponentiation to map your key to a different key, which you can easily map back to the original, exploiting some special properties of modular exponentiation where the modulus has the form p*q with p and q prime.

Concrete example:

p=31
q=17
a=343
b=7

such that (a*b) = 1 mod (p-1)*(q-1)

given original secret index "id", you have public index "pid"

pid = id ^ a mod p*q

and given the public id "pid" you can find the secret id

id = pid ^ b mod p*q

This generates the following table:

id      pid     recovered_id
0       0       0
1       1       1
2       349     2
3       334     3
4       64      4
5       129     5
6       99      6
7       267     7
8       202     8
9       359     9
10      226     10
11      207     11
12      296     12
13      259     13
etc

up to a maximum id of p*q-1 (526) after which the cycle repeats.

Of cource you will need much bigger p and q, and an offset to the id, but the principe will certainly work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top