Algorithms – What Is the Shortest Generating One-Way Hash Algorithm?

algorithmshashing

I have a 24 character long id which is guaranteed to be unique. I would like to shorten that to 7-10 (or even shorter) characters long. If I generate an short id randomly then I would have to check every time if that shortid is available. Is there an one-way hash algorithm that could return back 7-10 character hash from passing in the 24 character id.

Are there any other ways to guarantee that the generated short id is unique without checking?

Best Answer

You cannot guarantee that a shortened ID is unique, because there are much fewer 10-character strings than 24-character strings. Sooner or later, values will be reused.

Depending on how your IDs are generated, there are different things you could do in practice. If you get consecutive numbers, taking their last 10 characters will work for a while, but then wrap around after X^10 values. If the ID is itself totally random, you can take any 10 of its digits to get another value that is probably unique, but sooner or later will lead to collisions unpredictably. But the problem as posed is not solvable.

Related Topic