使用标识符进行相应更改

使用标识符进行相应更改

本文介绍了类似YouTube的ID,使用标识符进行相应更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在考虑如何制作一个small function来生成类似YouTubeID并且不会引起碰撞.字符集应该为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:

aaaaaaaaaaaaaaaaaaabaaaaaaaaac

我知道理论上您可以制作一个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,但是我没有100charset,而是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 :上述草图可能实际上在以下(或更多)情况下发生碰撞:
  • 如果ID99,这并不意味着下一个字符不能由chance代替,尽管不太可能; 可以,因此collide990:-(

  • 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 :我认为如果我们在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,使用标识符进行相应更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-22 20:57