GUID 的哈希值是否唯一?

发布于 2024-07-07 06:55:16 字数 51 浏览 5 评论 0原文

我创建一个 GUID(作为字符串)并获取它的哈希值。 我可以认为这个哈希值是唯一的吗?

I create a GUID (as a string) and get the hash of it. Can I consider this hash to be unique?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

转身泪倾城 2024-07-14 06:55:16

不像 GUID 本身那样可靠唯一,不是。

为了扩展,您将唯一性减少了 4 倍,将可能的组合从 16 字节减少到 4 字节。

正如评论中指出的,哈希大小会产生影响。 4 字节的东西是一个假设,据我所知,它可能会在 .NET 中使用,其中默认的哈希大小是 4 字节(int)。 因此,您可以用您的哈希值的任何字节大小替换我上面所说的内容。

Not as reliably unique as the GUID itself, no.

Just to expand, you are reducing your uniqueness by a factor of 4, going from 16 bytes to 4 bytes of possible combinations.

As pointed out in the comments the hash size will make a difference. The 4 byte thing was an assumption, horrible at best I know, that it may be used in .NET, where the default hash size is 4 bytes (int). So you can replace what I said above with whatever byte size your hash may be.

任谁 2024-07-14 06:55:16

没有。

如果您想要迷你 GUID,请参见此处: https://devblogs.microsoft .com/oldnewthing/20080627-00/?p=21823

海的爱人是光 2024-07-14 06:55:16

一句话,不。

假设您的哈希值的位数少于 GUID,根据鸽子洞原理,某个 GUID 必须存在多个映射 -> 哈希只是因为哈希比 GUIDS 少。

如果我们假设散列的位数比 GUID 的位数多,则假设您使用的是良好的散列函数,则发生冲突的可能性非常小(但有限)。

In a word, no.

Let's assume that your hash has fewer bits than the GUID, by the pigeon hole principle, there must exist more than one mapping of some GUID -> hash simply because there are fewer hashes than GUIDS.

If we assume that the hash has a larger number of bits than the GUID, there is a very small--but finite--chance of a collision, assuming you're using a good hash function.

固执像三岁 2024-07-14 06:55:16

任何将任意大小的数据块减少到固定大小的位数的哈希函数都不会在两者之间产生 1 对 1 的映射。 总是存在将两个不同的数据块缩减为散列中相同的位序列的机会。

好的散列算法可以最大限度地减少发生这种情况的可能性,并且通常散列中的位数越多,发生冲突的可能性就越小。

No hash function that reduces an arbitrary sized data block to a fixed size number of bits will produce a 1-to-1 mapping between the two. There will always exist a chance of having two different data blocks be reduced to the same sequence of bits in the hash.

Good hash algorithms minimizes the likelihood of this happening, and generally, the more bits in the hash, the less chance of a collision.

绝影如岚 2024-07-14 06:55:16

由于哈希冲突不能保证。 GUID 本身几乎可以保证是这样。

出于实际原因,您可能可以假设哈希值是唯一的,但为什么不使用 GUID 本身呢?

It's not guranteed to be, due to hash collisions. The GUID itself is almost-guaranteed to be.

For practical reasons you probably can assume that a hash is unique, but why not use the GUID itself?

尽揽少女心 2024-07-14 06:55:16

不,我不会假设任何哈希值的唯一性。 这应该不重要,因为哈希值不需要唯一,它们只需要在其范围内均匀分布。 分布越均匀,发生的冲突就越少(在哈希表中)。 更少的冲突意味着更好的哈希表性能。

仅供参考 有关哈希表如何工作的详细描述,请阅读 什么是哈希表和哈希图及其典型用例?

No, and I wouldn't assume uniqueness of any hash value. That shouldn't matter because hash values don't need to unique, they just need to evenly distributed across their range. The more even the distribution, the fewer collisions occur (in the hashtable). Fewer collisions mean better hashtable performance.

fyi For a good description of how hash tables work, read the accepted answer to What are hashtables and hashmaps and their typical use cases?

初心 2024-07-14 06:55:16

如果您使用加密哈希(MD5、SHA1、RIPEMD160),则哈希将是唯一的(模数冲突是非常不可能的——SHA1 用于例如数字签名,并且 MD5 在随机输入上也具有抗冲突性 em>)。 但是,为什么要对 GUID 进行哈希处理呢?

If you use cryptographic hash (MD5, SHA1, RIPEMD160), the hash will be unique (modulo collisions which are very improbable -- SHA1 is used e.g. for digital signatures, and MD5 is also collision-resistant on random inputs). Though, why do you want to hash a GUID?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文