Mats
Mats

Reputation: 14817

Is the hash of a GUID unique?

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

Upvotes: 26

Views: 32433

Answers (7)

Simon Buchan
Simon Buchan

Reputation: 13245

Nope.

See here, if you want a mini GUID: https://devblogs.microsoft.com/oldnewthing/20080627-00/?p=21823

Upvotes: 11

mattlant
mattlant

Reputation: 15451

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.

Upvotes: 22

Patrick
Patrick

Reputation: 92520

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.

Upvotes: 8

zvrba
zvrba

Reputation: 24546

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?

Upvotes: 0

Robert Paulson
Robert Paulson

Reputation: 18061

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?

Upvotes: 2

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391406

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.

Upvotes: 4

Paweł Hajdan
Paweł Hajdan

Reputation: 18542

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?

Upvotes: 2

Related Questions