使用UUID唯一标识某些内容(我正在使用它来标识上传到服务器的文件)有多安全?据我所知,它是基于随机数。然而,在我看来,只要有足够的时间,它最终会完全偶然地重复它自己。是否有更好的系统或某种类型的模式来缓解这个问题?


当前回答

我同意其他的答案。uuid对于几乎所有的实际用途都是足够安全的,当然对你来说也是如此。

但假设(假设)它们不是。

是否有更好的系统或某种类型的模式来缓解这个问题?

这里有一些方法:

Use a bigger UUID. For instance, instead of a 128 random bits, use 256 or 512 or ... Each bit you add to a type-4 style UUID will reduce the probability of a collision by a half, assuming that you have a reliable source of entropy2. Build a centralized or distributed service that generates UUIDs and records each and every one it has ever issued. Each time it generates a new one, it checks that the UUID has never been issued before. Such a service would be technically straight-forward to implement (I think) if we assumed that the people running the service were absolutely trustworthy, incorruptible, etcetera. Unfortunately, they aren't ... especially when there is the possibility of governments' security organizations interfering. So, this approach is probably impractical, and may be3 impossible in the real world.


1 - If uniqueness of UUIDs determined whether nuclear missiles got launched at your country's capital city, a lot of your fellow citizens would not be convinced by "the probability is extremely low". Hence my "nearly all" qualification. 2 - And here's a philosophical question for you. Is anything ever truly random? How would we know if it wasn't? Is the universe as we know it a simulation? Is there a God who might conceivably "tweak" the laws of physics to alter an outcome? 3 - If anyone knows of any research papers on this problem, please comment.

其他回答

如果你所说的“有足够的时间”是指100年,你以每秒10亿的速度创造它们,那么是的,100年后你有50%的几率发生碰撞。

摘自维基百科:

因此,任何人都可以创建UUID并使用 用它来表示某物 合理相信 标识符永远不会是 无意中被某人用于 其他东西

它还非常详细地解释了它的安全性。所以回答你的问题:是的,它足够安全。

对于UUID4,我认为在一个边长360000公里的立方体盒子中,id的数量大约与沙粒的数量相同。这是一个边长约为木星直径2.5倍的盒子。

如果我搞砸了单位,就会有人告诉我:

沙粒体积0.00947mm^3 (Guardian) UUID4有122个随机位-> 5.3e36可能的值(维基百科) 那么多沙粒的体积= 5.0191e34 mm^3或5.0191e+25m^3 体积= 3.69E8m或369,000km的立方箱的边长 木星直径:139,820公里(谷歌)

我不知道这对您是否重要,但请记住,guid是全局惟一的,但guid的子字符串不是。

这个问题的答案很大程度上取决于UUID版本。

许多UUID生成器使用版本4的随机数。然而,其中许多使用伪随机数生成器来生成它们。

如果使用一个短周期的低种子PRNG来生成UUID,我认为这一点都不安全。一些随机数生成器的方差也很差。也就是说,更倾向于某些数字。这不会有好结果的。

因此,它的安全性取决于生成它的算法。

另一方面,如果您知道这些问题的答案,那么我认为使用版本4的uuid应该是非常安全的。事实上,我正在使用它来识别网络块文件系统上的块,到目前为止还没有发生冲突。

在我的情况下,我使用的PRNG是一个梅森龙卷风,我很小心,它的播种方式是来自多个来源,包括/dev/ urrandom。梅森龙卷风的周期为2^19937−1。在我看到一个重复的uuid之前,会有很长很长的时间。

因此,选择一个好的库或自己生成它,并确保使用合适的PRNG算法。