Youtube-like IDs that change accordingly with an i

2019-08-14 08:55发布

I was thinking about how to make a small function that generates a YouTube-like ID and does not cause collisions. The charset should be A-Za-z0-9-_ and lets say I want it to be 10 characters long which would equal 64 ^ 10 / Quadrillion - 1153 Quadrillion possibilities.

It's rather important to be small due to the fact that it's one of the major functionality of the website and should be clear enough to be modified easily. Within seconds in case an emergency occurs.

It doesn't have to be secure in the sense that we don't really mind whether it's easily to convert back to an ID as an outsider. It should however not be a pattern such as:

aaaaaaaaaa aaaaaaaaab aaaaaaaaac

I'm aware that you could theoretically make a charset and loop through it randomly until you have 10 random characters but this could collide ( I realize this chance is slim and you could perform a query to see whether the key has been generated before but I would like to keep dependencies minimal and it not relying on SQL ).

I can also imagine making a charset of 100 characters as in 0-99 and then prepend and/or append random characters if the length of 10 condition is not met. Eg:

  • IMPORTANT: the scenario sketched below assumes A is the first character in the set and _ is the last one.

id: 0 = A+9 random characters

id: 1 = B+9 random characters

id: 99 = _+9 random characters

id: 990 = _A+8 random characters

id: 99999999999999999999 = __________

I would think that would prevent collisions but I don't have a charset of 100 but 64.

Hope someone can toss some ideas.

  • UPDATE1: The above sketch could actually collide in the following (and more) circumstance(s):
  • If the ID is 99 it doesn't mean that the next character can't be A by chance although very unlikely; it could therefore collide with 990 :-(

  • UPDATE2: I don't think it would still collide if we use a static component after the ID and before the random component that is not part of the charset.

  • Updated Sketch (the static component will be $):

id: 0 = A$+8 random characters

id: 1 = B$+8 random characters

id: 99 = _$+8 random characters

id: 990 = _A$+7 random characters

id: 999999999999999999 = _________$

1条回答
▲ chillily
2楼-- · 2019-08-14 09:23

as in your other question here Encode/Decode ID Reversal issue all you want is to transform your ID which is in base 10 to a different base than 10 ...

you could simply base64 encode it and be done ...

if you want it to look like random, transform the number before ... for example, simply xor it with a static 60 bit value ...

查看更多
登录 后发表回答