I create a GUID (as a string) and get the hash of it. Can I consider this hash to be 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.
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.
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).
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.
Nope.
See here, if you want a mini GUID: https://devblogs.microsoft.com/oldnewthing/20080627-00/?p=21823
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With