Question

I want to do something similar to what YouTube does. For example, this is a valid YouTube video ID didzxUkrtS0

Right now I am storing user's IDs with an integer number, and I want to translate those numbers to a 8 character alphanumerical identification. For example: FZ3EY1IC (not hexadecimal)

I already know that I can implement it with MD5 and then take the first 8 hex numbers, but it doesn't cover the entire alphabet.

What should I do to create a unique pattern using integers, that should never repeat?

Was it helpful?

Solution

Make your integer 5 8-bit bytes long (by adding a byte with a random value if your integer is 32-bit), that's 40 bits of data.

Cryptographically encrypt the 5 bytes of your integer using some key, which you probably want to keep private.

Slice the 40 encrypted bits into 8 5-bit parts. Encode each part using 32 alphanumeric characters. You may choose to use different 32 (out of the total 36) characters for each part.

The reverse operation is trivial.

OTHER TIPS

I'm pretty sure what sites like youtube and bitly do is store a big table in the database that translates the alphanumerical identifiers for each link to the internal ID of what they're for (either that or it's stored in the row). And when it needs a new identifier, compute a random one and store it. The reason why you need to do this is so that an attacker cannot predict the ID of the next piece of content to be added.

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