问题描述
我正在考虑如何制作一个small function
来生成类似YouTube
的ID
并且不会引起碰撞.字符集应该为A-Za-z0-9-_
,可以说我希望它的长度为10 characters
,这等于64 ^ 10 / Quadrillion
-1153 Quadrillion
的可能性.
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.
small
是非常重要的,因为它是网站的主要功能之一,并且应该足够清晰以便易于修改.万一发生紧急情况,几秒钟之内.
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.
不必一定是secure
,因为我们并不真的介意作为局外人是否容易转换回ID
.但是,不应使用以下模式:
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
我知道理论上您可以制作一个charset
并在其中循环遍历,直到您拥有10
个随机字符,但这可能会发生冲突(我意识到这种机会很小,您可以执行查询以查看是否key
之前已经生成过,但是我想使依赖性最小,并且不依赖SQL
.)
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
).
我还可以想象像0-99
一样将charset
设置为100 characters
,然后在不满足length of 10
条件的情况下添加和/或附加random
字符.例如:
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:
- 重要:下面概述的场景假定
A
是集合中的第一个字符,而_
是最后一个字符.
- IMPORTANT: the scenario sketched below assumes
A
is the first character in the set and_
is the last one.
我认为这会阻止collisions
,但是我没有100
的charset
,而是64
.
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 :上述草图可能实际上在以下(或更多)情况下发生碰撞:
-
如果
ID
是99
,这并不意味着下一个字符不能由chance
代替,尽管不太可能; 可以,因此collide
与990
:-(
- UPDATE1: The above sketch could actually collide in the following (and more) circumstance(s):
If the
ID
is99
it doesn't mean that the next character can't beA
bychance
although very unlikely; it could thereforecollide
with990
:-(
UPDATE2 :我认为如果我们在ID
之后和random component
之前使用static component
(不属于.
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逆转问题您要做的就是将以10为底的ID转换为以10为底的ID ...
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 ...
您可以简单地对其进行base64编码并完成...
you could simply base64 encode it and be done ...
如果您希望它看起来像是随机的,请在...之前对数字进行变换,例如,只需将其与静态的60位值进行异或...
if you want it to look like random, transform the number before ... for example, simply xor it with a static 60 bit value ...
这篇关于类似YouTube的ID,使用标识符进行相应更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!