从 UUID 解码名称或命名空间
Decode the name or namespace from a UUID
是否可以从给定的 UUID 解码命名空间或名称?
我的想法是稍后生成具有特定名称空间或名称的uuid,然后检索它以检查2个UUID是否属于同一名称空间或名称。这可能吗?
如RFC4122, UUID3
and UUID5
namespaces and names are hashed (with either MD5 or SHA1), which means there is no other way to “decode” the namespace or name from a given UUID than bruteforce (that is the whole point of hash functions)所述。
Compute the hash of the name space ID concatenated with the name.
RFC422 - 4.3 - Algorithm for Creating a Name-Based UUID
但是你可以直接比较哈希命名空间和名称来检测属于同一个命名空间的两个UUID是否和确实具有相同的名称。这是 Python 中的示例(使用标准 uuid
模块):
import uuid
name = 'whosebug.com'
a = uuid.uuid5(namespace=uuid.NAMESPACE_DNS, name=name)
b = uuid.uuid5(namespace=uuid.NAMESPACE_DNS, name=name)
assert a == b
print(a)
print(b)
cd84c40a-6019-50c7-87f7-178668ab9c8b
cd84c40a-6019-50c7-87f7-178668ab9c8b
是否可以从给定的 UUID 解码命名空间或名称?
我的想法是稍后生成具有特定名称空间或名称的uuid,然后检索它以检查2个UUID是否属于同一名称空间或名称。这可能吗?
如RFC4122, UUID3
and UUID5
namespaces and names are hashed (with either MD5 or SHA1), which means there is no other way to “decode” the namespace or name from a given UUID than bruteforce (that is the whole point of hash functions)所述。
Compute the hash of the name space ID concatenated with the name.
RFC422 - 4.3 - Algorithm for Creating a Name-Based UUID
但是你可以直接比较哈希命名空间和名称来检测属于同一个命名空间的两个UUID是否和确实具有相同的名称。这是 Python 中的示例(使用标准 uuid
模块):
import uuid
name = 'whosebug.com'
a = uuid.uuid5(namespace=uuid.NAMESPACE_DNS, name=name)
b = uuid.uuid5(namespace=uuid.NAMESPACE_DNS, name=name)
assert a == b
print(a)
print(b)
cd84c40a-6019-50c7-87f7-178668ab9c8b
cd84c40a-6019-50c7-87f7-178668ab9c8b