views:

158

answers:

9

Hi there,

I'm interested in creating tiny url like links. My idea was to simply store an incrementing identifier for every long url posted and then convert this id to it's base 36 variant, like the following in PHP:

$tinyurl = base_convert($id, 10, 36)

The problem here is that the result is guessable, while it has to be hard to guess what the next url is going to be, while still being short (tiny). Eg. atm if my last tinyurl was a1, the next one will be a2. This is a bad thing for me.

So, how would I make sure that the resulting tiny url is not as guessable but still short?

A: 

Generate a random key every time, but remember to check that it's not already in use. Some sample code, generating a key consisting of 5 characters in the a-z range:

$key = "";
$key_length = 5;
$chars = range("a", "z");
$char_count = count($chars);
for ($i = 0; $i < $key_length; $i++) {
   $index = rand(0, $char_count - 1);
   $key .= $chars[$index];
}
mwittrock
This will work for a little while, but performance will get progressively worse every time a new link is created. I definitely wouldn't go this route.
Joe Enos
+2  A: 

I would simply crc32 url

$url = 'http://www.google.com';
$tinyurl = hash('crc32', $url ); // db85f073

cons: constant 8 character long identifier

dev-null-dweller
I like this idea, but the 8-character code is kind of a problem - with URL shorteners these days, every character counts, and 8 is a little high.
Joe Enos
+3  A: 

This is really cheap, but if the user doesn't know it's happening then it's not as guessable, but prefix and postfix the actual id with 2 or 3 random numbers/letters.

If I saw 9d2a1me3 I wouldn't guess that dm2a2dq2 was the next in the series.

BarrettJ
+1  A: 

Try Xor'ing the $id with some value, e.g. $id ^ 46418 - and to convert back to your original id you just perform the same Xor again i.e. $mungedId ^ 46418. Stack this together with your base_convert and perhaps some swapping of chars in the resultant string and it'll get quite tricky to guess a URL.

Will A
This is very easy to break.
Artefacto
For a slightly determined hacker sure - for Joe Public, not so much.
Will A
+4  A: 

What you are asking for is a balance between reduction of information (URLs to their indexes in your database), and artificial increase of information (to create holes in your sequence).

You have to decide how important both is for you. Another question is whether you just do not want sequential URLs to be guessable, or have them sufficiently random to make guessing any valid URL difficult.

Basically, you want to declare n out of N valid ids. Choose N smaller to make the URLs shorter, and make n smaller to generate URLs that are difficult to guess. Make n and N larger to generate more URLs when the shorter ones are taken.

To assign the ids, you can just take any kind of random generator or hash function and cap this to your target range N. If you detect a collision, choose the next random value. If you have reached a count of n unique ids, you must increase the range of your ID set (n and N).

relet
Regarding your last paragraph. I think he wants a value he can reverse, i.e., he wants an injective function.
Artefacto
No, he wants an unguessable function, really. ;) As he has to store the URLs in a database anyway, he can use the random number as an index. Reversal achieved.
relet
True, does not have to be injective.
Tom
A: 

You can pre-define the 4-character codes in advance (all possible combinations), then randomize that list and store it in this random order in a data table. When you want a new value, just grab the first one off the top and remove it from the list. It's fast, no on-the-fly calculation, and guarantees pseudo-randomness to the end-user.

Joe Enos
However, it does not scale.
relet
I should point out that this is exactly what I did for a URL shortener, and it's a bit of a pain to get started. There are an awful lot of possible combinations, which means you start out with a huge database file for such a simple concept.
Joe Enos
@relet What exactly are you referring to? The fact that there's a limited number that cannot increase? If that's it, then once you start running out of 4-character codes, then calculate all the 5-character codes and insert that into your queue table.
Joe Enos
+1  A: 

If you want an injective function, you can use any form of encryption. For instance:

<?php
$key = "my secret";
$enc = mcrypt_ecb (MCRYPT_3DES, $key, "42", MCRYPT_ENCRYPT);
$f = unpack("H*", $enc);
$value = reset($f);
var_dump($value); //string(16) "1399e6a37a6e9870"

To reverse:

$rf = pack("H*", $value);
$dec = rtrim(mcrypt_ecb (MCRYPT_3DES, $key, $rf, MCRYPT_DECRYPT), "\x00");
var_dump($dec); //string(2) "42"

This will not give you a number in base 32; it will give you the encrypted data with each byte converted to base 16 (i.e., the conversion is global). If you really need, you can trivially convert this to base 10 and then to base 32 with any library that supports big integers.

Artefacto
Keep in mind that the resulting url has to be short (1399e6a37a6e9870 is too long).
Tom
@Tom Well, he could convert it to base 64 or so and get (I think) 11 characters. Or use
Artefacto
+1  A: 

Another way would be to set the maximum number of characters for the URL (let's say it's n). You could then choose a random number between 1 and n!, which would be your permutation number.

On which new URL, you would increment the id and use the permutation number to associate the actual id that would be used. Finally, you would base 32 (or whatever) encode your URL. This would be perfectly random and perfectly reversible.

Artefacto
Duplicate IDs are possible though in this way, so you'd have to check for that and increment again if duplicate.
Tom
@Tom No, they wouldn't...
Artefacto
A: 

I ended up creating a md5 sum of the identifier, use the first 4 alphanumerics of it and if this is a duplicate simply increment the length until it is no longer a duplicate.

function idToTinyurl($id) {
    $md5 = md5($id);
    for ($i = 4; $i < strlen($md5); $i++) {
        $possibleTinyurl = substr($md5, 0, $i);
        $res = mysql_query("SELECT id FROM tabke WHERE tinyurl='".$possibleTinyurl."' LIMIT 1");
        if (mysql_num_rows($res) == 0) return $possibleTinyurl;
    }
    return $md5;
}

Accepted relet's answer as it's lead me to this strategy.

Tom