Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is the hash of a GUID unique?

Tags:

guid

unique

hash

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

like image 830
Mats Avatar asked Sep 26 '08 08:09

Mats


People also ask

Are GUIDs truly unique?

A customer needed to generate an 8-byte unique value, and their initial idea was to generate a GUID and throw away the second half, keeping the first eight bytes.

Can a GUID ever be the same?

Theoretically, no, they are not unique. It's possible to generate an identical guid over and over. However, the chances of it happening are so low that you can assume they are unique.

Is a GUID a hash of a file?

You can see why programmers use GUIDs as unique identifiers. It would take billions upon billions of years to generate two GUIDs that are the same. A hash is a value that is calculated by running some data (like a GUID) through an algorithm, which produces a checksum (kind of like a digital fingerprint).


3 Answers

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.

like image 175
mattlant Avatar answered Sep 19 '22 17:09

mattlant


Nope.

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

like image 32
Simon Buchan Avatar answered Sep 21 '22 17:09

Simon Buchan


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.

like image 42
Patrick Avatar answered Sep 21 '22 17:09

Patrick