An ID obfuscation function that fits into a Tweet

If you’re bothered about long IDs in your app or want to shorten existing large number keys, there are many examples on the web that take your numeric ID and perform some witchcraft with a bunch of numbers, letters and a database to turn something like http://www.mydirtysocks.com into 3Q8zk. What’s basically happening is that the URL is stored in a database and a unique key is generated for it (usually by using the ID field of the table). Some services check if the URL already exists and, if it does, returns the existing key.

When someone visits the shortening service with the key, the serivce looks it up and redirects you to the original URL.

The basic premise of shortening is that once you’ve exhausted numbers 0 – 9, you then move on to a – z and then A – Z. So instead of a character pool of just 36 with numbers + lower case letters, you now have a pool of 62 to represent a long ID. This is almost never why people use these functions and most don’t care that their ID is shorter.

What they’re really using it is for hiding that ID 10001 came before 10002 and for this, a lot of the shortening mechanisms out there are severely overkill. And then there’s the fact that because you’re using both upper and lower case letters; if some genius decided to turn all uppercase letters in a URL to lowercase in their forum/blog/email-service or some other kitchen sink application, your whole shortening scheme is hosed. This happens far more often than you think.

If you just wanted to generate a unique key (provided the original ID was unique) instead of showing the original ID, then there’s an alternative that’s exactly 140 characters. I checked :

All this does is take that initial ID in string format, split it into 9 character chunks, generate a CRC32 hash of each chunk, convert it to base 32 (the maximum value allowed for the base_covnert function in PHP) and append to the output.

Of course, if you want a more nice-ified version of that…

function shortenID($k){

	$out='';
	$p = str_split($k,9);
	
	for( $i = 0; $i < count( $p ); $i++ ) {
		$out. = base_convert( crc32( $p[$i] ), 16, 36 );
	}
	
	return $out;
}

Now, generating the ID is only half of the implementation. Since the hash cannot be reversed, you should generate it once for that particular ID and store it in a separate field in the same row of the DB table. This is what almost all URL shortening services do, but of course, they use that upper and lower case character malarkey.

Of course, if you do still want that malarkey, Lalit has created a class that does it too. Stop searching now and go download that class.

However…

Please try to be sane with your URL shortening shenanigans. It’s really quite stupid to shorten an already short URL, not to mention very irritating.

About these ads

4 thoughts on “An ID obfuscation function that fits into a Tweet

  1. My iPhone reaaaaally struggles with these URL shorteners. Mobile browsers (or, at least, mobile Safari) can’t really seem to handle more than 1 redirect.

    • Ditto on Android Jelly Bean. Sometimes I wish Twitter would keep its own shortening service open for copy > pasting longer URLs.

      Or at least snip it to the shortened length (if not the shortened URL) before posting and replace it with a placeholder instead that you can then tap to reveal the full URL for edits etc…

  2. Pingback: Page not found | This page intentionally left ugly

  3. Pingback: ID Obfuscation Part II | This page intentionally left ugly

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s