views:

183

answers:

5

Say, I have a customers with a integer PK. In my web apps I want to have a profile page for them. I do not want a url like /profile/10375/ (For example, I do not want others to know how many customers I have). I also do not want a slug base url like, /profile/acme_corp/.

What is a good way to convert unique integers to unique random short strings? (For example, earlier Reddit used to have this type of urls, but it was a conversion from decimal to base 36, skipping some ids). But that too is not useful for me as it si easy to guess number of entities in DB with this scheme.

I cant use UUID etc, as they would make the url too large.

A: 

You could always base64 then url encode the id's.

in php:

urlencode (base64_encode("1234"))
> MTIzNA%3D%3D

base64_decode(urldecode("MTIzNA%3D%3D"))
> 1234
Byron Whitlock
+6  A: 

If the primary concern is that one customer can look at another customer's page I wouldn't depend on the method you are proposing. Ultimately it is "Security Through Obscurity". I would, instead, tie the display of the page to the customer's authentication credentials (providing you require your users to log in). If the customer tries to access a profile page other than their own they redirected to the login page or given an access authorization error message.

In fact you don't even need to have the customerID in the URL. Just use /profile/

William Edmondson
+1 You are right. that's the correct way to do this, just /profile/
Byron Whitlock
Yep, this is the answer. Maybe it's just me, but this seems so obvious!
Josh Stodola
The OP doesn't actually state that the issue is whether someone can view someone else's profile. The only issue cited is not wanting people to know how many users there are. If others profiles can't be viewed, this is a great solution; if not...
bdukes
Yes, one person can view other's profiles(for example by browsing through the site). They are public. My primary consideration is that I d not want to give away the pk used in url, essentially for two main reasons. 1. So someone cant do a next-next to see all profile, or make it too easy for bots to guess all urls etc.2. To keep the number of Customers from becoming public information.
uswaretech
A: 

maybe you could asociate a random pair of letters of the alphabet to each digit...

Jonathan
+1  A: 

The easiest and safest way to do this would be to create lookup table with a mapping from the customer id to a unique random string.

Otto Allmendinger
+2  A: 

There are some hash algorithms which produce short (8 characters, 0-9a-f) output strings, for example adler32 or crc32. You can generate them using PHP function hash() (see hash_algos() for a list of available algorithms), but I’m not sure your DB engine itself can handle it. If that’s the case, generating random slugs would be a better solution.

And add salt when hashing, so it’s more secure.

Maciej Łebkowski
He can also use a hash function with a much longer output and only take the first N bytes.
Otto Allmendinger
the first N bytes doesn’t have to be unique
Maciej Łebkowski