DEV Community

Cover image for Tell me about GUIDs aka a game of chance?
Grant Hair
Grant Hair

Posted on

Tell me about GUIDs aka a game of chance?

https://images.unsplash.com/photo-1570303345338-e1f0eddf4946?ixid=MXwxMjA3fDB8MHxwaG90by1wYWdlfHx8fGVufDB8fHw%3D&ixlib=rb-1.2.1&auto=format&fit=crop&w=1265&q=80

This should be short and sweet.

We've probably all used GUIDs (globally unique identifiers) or UUIds (universally unique identifiers) but what do they actually mean.

A GUID looks like this: F024E833-8493-4A0D-8560-0DAA2A514251

I with my limited knowledge on this subject will try to explain some stuff, probably incorrectly, but yay me for trying.

                             +- UUID version
                             |
                            |-| 
 3 7 a 6 a d 6 3 - 7 0 0 c - 4 4 0 4 - b d d 4 - 8 2 8 5 7 5 8 4 5 0 4 5
|---------------| |-------| |---|---| |-------| |-----------------------|
        |             |       |   |       |                 |
        |             |       |   |       |                 +- node
        |             |       |   |       +- clock_seq_low
        |             |       |   +- clock_seq_hi_and_reserved
        |             |       +- time_hi_and_version
        |             +- time_mid
        +- time_low
Enter fullscreen mode Exit fullscreen mode

https://media.giphy.com/media/ERDOxjdyt7Zcc/giphy.gif

I'm not even gonna try explain the above but the tl;dr; about GUIDs is:

There are 3 (main) different types of GUIDs/UUIDs (that you will probs encounter)

V1

V4

&

V5

The differences between GUID versions

v1 uses your mac address, the current date and time and a random salt (meaning if you gen 2 GUIDS on the same machine at the exact same millisecond the chance of them being the same goes from practically impossible to very small)

v4 has no inherit logic thus giving a greater degree of random-ness

v5 uses a fixed namespace UUID and an input string to generate a non random unique identifier using the SHA1 hash. v5 UUIDs are always the same given the same namespace and input string so you must as the developer maintain their unique state

C# Dev Time

If you are a C# dev like me then you'll be wondering what does

Guid.NewGuid();

give me?

Well rest assured that some blog post I read confirmed that C# Guid.NewGuid(); will give you a v4 GUID every time.

Thanks, I hope some of the above is correct 👋

Links

https://stackoverflow.com/questions/20342058/which-uuid-version-to-use

https://www.sohamkamani.com/uuid-versions-explained/

https://stackoverflow.com/questions/55823448/how-to-generate-uuid-version-4-using-c-sharp

https://social.msdn.microsoft.com/Forums/vstudio/en-US/4956142a-0a5d-4f1e-b102-93a3eea1b5d5/does-guidnewguid-produce-uuid-version-4-according-to-rfc4122?forum=netfxbcl

https://johanvergeer.github.io/posts/net-guid-uuid-version

Top comments (0)