在Node.js中,有没有一种方法可以检查UUID是否是使用特定命名空间生成的?

huangapple go评论71阅读模式
英文:

Is there a way to check if a UUID was generated using a specific namespace in node.js?

问题

如何为UUID创建命名空间,以便以后可以检查它是否是在该命名空间下生成的?

特别是,我正在考虑使用UUID v5。然而,从我所了解的情况来看,没有办法在不知道名称的情况下检查特定UUID是否是使用特定命名空间生成的。

我考虑过简单地生成UUID v4,并将前4个字符替换为常量值,例如00010002,然后检查UUID是否使用了该前缀。我意识到这将减少UUID的基数。然而,这对于任何命名空间解决方案都是如此。我更担心的是这将使其成为无效的v4 UUID。

英文:

How to namespace a UUID such that I could later check if it was generated using the namespace?

In particular, I was considering using UUID v5. However, from what I can tell, there is no way to check if a particular UUID was generated using a particular namespace, without also knowing the name.

I've considered simply generating UUID v4 and replacing the first 4 characters with constant values, e.g. 0001 or 0002 and later checking if UUID uses that prefix. I realize that this will reduce the cardinality of the UUID. However, that would be true for any namespace solution. My bigger concern is that this will make it invalid v4 UUID.

答案1

得分: 0

>然而,据我所知,没有办法检查特定UUID是否是使用特定命名空间生成的,而不知道该名称。

是的,v5 UUID中的命名空间与密钥一起哈希,无法逆转它。

>我考虑过只是生成UUID v4,并将前4个字符替换为常量值。

当然可以。这是一种相当常见的方法,尽管通常我看到的人会在前缀中使用更多字节而不只是2个。我看到的大多数人在开头使用几个字节的常量但随机的前缀,然后是他们的内部命名空间,然后是随机id。但这并不重要。只要你的UUID不与其他系统的UUID混合在一起,你就不需要担心前缀部分的碰撞。只要有足够的字节仍然是随机的,你就不必担心其他地方的碰撞。

v4 UUID中只有6-8位是定义好的,而且它们不在开头。它是十六进制编码中的第13位和第17位数字的可变部分。所以我可能会避免修改这两个数字,但我从未遇到过实际验证版本信息的系统。如果你只是生成了128位的随机数,它在我见过的每个系统中都会很好地工作。但是我是一个格式的坚持者,所以我个人总是正确地设置这些位,主要是因为这让我感到高兴,而不是因为消费系统实际上关心这些。你的方案是可以的,只要你不会与其他可能选择了相同命名空间标识符的系统混合在一起。

英文:

> However, from what I can tell, there is no way to check if a particular UUID was generated using a particular namespace, without also knowing the name.

Correct. The namespace in a v5 UUID is hashed together with the key, and there is no way to reverse it.

>I've considered simply generating UUID v4 and replacing the first 4 characters with constant values

Sure. This is a pretty common approach, though usually I see folks use more bytes for their prefix than 2. Most folks I see use a several-byte constant-but-random prefix at the start, followed by their internal namespace, followed by a random id. But it doesn't really matter. As long as your UUIDs don't intermix with other systems' UUIDs, you don't need to worry about collisions in the prefix section. And you definitely don't have to worry about collisions elsewhere as long as enough of the bytes are still random.

There are only 6-8 bits in a v4 UUID that are defined and they're not at the beginning. It's the 13th and varying parts of the 17th digit in the hex encoding. So I probably would avoid messing with those two digits, but I've never encountered a system that actually validated the version information. If you just generated a random 128 bits, it's going to work fine in every system I've seen. But I'm a stickler for formats, so I personally always set the bits correctly, mostly because it makes me happy, not because consuming systems actually care. Your scheme is fine, as long as you're not intermixing with other systems where other people may have picked the same namespace identifier.

huangapple
  • 本文由 发表于 2023年5月22日 07:28:33
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302320.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定