"UUIDv4 generation libaries" -vs- "just rolling your own with random hex characters or bits" 之间的差异

Differences between "UUIDv4 generation libaries" -vs- "just rolling your own with random hex characters or bits"

关于一个UUID4的拼装话题,根据Wikipedia...

Version 4 UUIDs have the form xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx where x is any hexadecimal digit and y is one of 8, 9, A, or B

所以我可以想到程序员可以使用三种可能的方法来生成随机 UUID4:

方法A)使用已经存在的"proper" UUID4生成库

-或-

MethodB) 简单地通过在字符串上使用随机的十六进制字符来滚动你自己的:

上述步骤只是一个简单的示例,说明如何将其作为字符串完成。请考虑对字符串和随机选择的十六进制字符进行操作的任何其他方法仍然是 "MethodB",例如以空字符串开始并一次追加一个字符。

-或-

MethodC:使用按位运算自己动手:

我想大多数图书馆都是这样做的吧?主要使用随机位,同时确保“4”和“8/9/a/b”在最终生成的字符串中。

问题:

问题 1:根据 随机性或与数据库的一般兼容性 生成的 UUIDv4 在技术上是否存在任何差异等将存储 UUIDv4?

问题 2:使用方法 #2(随机十六进制字符)比使用方法 #1 或 #3(按位)有什么缺点吗?

问题 3:"proper" MethodA 中的 UUIDv4 生成库是否在 MethodB 和 MethodC 中的简单方法如何做的基础上做了什么特别的事情?

Q4:有没有什么方法比较容易运行陷入冲突?

问题 5:由 MethodB + MethodC 生成的结果 UUID 是否完全符合 UUIDv4 规范(即使它们不符合实现该规范的方法)。

备注:

Q1: Are there technically any differences in the resulting UUIDv4 that would be generated in terms of their randomness or general compatibility with databases etc that will store the UUIDv4?

从技术上讲,没有区别。

Q2: Are there any downsides to using method #2 (random hex characters) over #1 or #3 (bitwise)?

没有缺点;不是真的。

Q3: Are the "proper" UUIDv4 generation libraries in MethodA doing anything special on top of how the simple approaches in MethodB and MethodC would do it?

图书馆通常完全按照 MethodC 的要求行事

Q4: Is any method more like to run into conflicts?

不是真的。

Q5: Are the resulting UUIDs generated by MethodB + MethodC fully compliant with the UUIDv4 specification (even if they are not compliant in their methodology to get there).

是的。

成功生成(RFC 4122 变体)版本 4 UUID 并不复杂;但需要对随机数生成有一定的了解。例如,"pseudo-random" 数字生成与 "crypto" 质量随机数生成之间的差异。

例如,一个非常简单的 "pseudo-random" 数字生成器通常会生成完全相同的一系列 "random" 数字;这通常很烦人,可以引入 "seed" 来更改随机数序列。

当然,每次调用UUID生成器时生成相同的UUID也很烦人。因此,"pseudo-random" 数字生成器不适合生成 UUID。

"Crypto" 质量随机数可能随机性更高,并且被大多数 UUID 版本 4 生成器使用。

简而言之,最好的 UUID 版本 4 生成器是那些基于最好的随机数生成器的生成器。 RFC 4122 的第 4.4 节给出了有关如何实现更高程度的 UUID 版本 4 随机性的建议。

有一个 COMB UUID 派生自 RFC 4122 变体,您可能会感兴趣。

-- 奖励:您可能想查看 Mahonri Moriancumer's UUID and GUID Generator and Forensics 页面。它使用加密质量随机数生成器来生成版本 4 UUID。