The GUIDe: How to make a GUID

The spec (RFC 4122) isn't exactly light reading so here is a step by step guide created from the specification on how to generate various versions of GUIDs.


The structure of a GUID

The format is 16 octets (128 bits) that conform to a specific structure. To make matters confusing there's a few different structures but the most common one is the structure defined in RFC 4122 - and that's the structure we'll be using for our GUID guide today. The structure is determined by the "variant" which is specified in the most significant bits of the 8th octet. We'll cover off setting this later on in the guide.

The variant of GUID we're creating defines the following fields


How to generate a version 1 GUID (date-time & MAC address)

You need 3 basic ingredients to create a Version 1 GUID

  1. A timestamp (provide temporal unqiueness)
  2. Clock Sequence (initially a random number)
  3. A node ID (provide spatial uniqueness, e.g. MAC address)

The timestamp is the number of 100 nanosecond intervals since the adoption of the Gregorian calendar in the West (15 October 1582 00:00:000000000). If you can't get a UTC time, local will do but remember to change the clock sequence if the local clock is ever changed.

The timestamp has a resolution of 100 nanoseconds, if you need to generate GUIDs more frequently then multiple Node IDs are required. If your machine generates a datetime based on a different starting point e.g. 1970-1-1 then you'll need to offset it to match the start of the Gregorian calendar.

The clock sequence is used to help avoid duplicates, it's a 14 bit value that's initially set to a random number. If the clock is ever set backwards, or if the node ID changes then this number is incremented.

The node ID is used to uniquely identify the device generating the GUIDs, so that two seperate devices will generate two different GUIDs if created at the exact same time. Ideally this is the IEEE 802 MAC address of the machine.

Mapping the components to a GUID


For this example we'll generate a GUID for 2000-01-01 on a machine with a random MAC address of 29-06-76-EC-E2-D7 and a clock sequence of 30802

  1. Convert time stamp to bytes and insert into the GUID: 01d3bfde63b00000
    GUID so far: 63b00000-bfde-01d3-xxxx-xxxxxxxxxxxx
  2. Convert clock sequence to bytes and insert into the GUID: 7852
    GUID so far: 63b00000-bfde-01d3-7852-xxxxxxxxxxxx
  3. Insert the node ID into the GUID: 290676ece2d7
    GUID so far: 63b00000-bfde-01d3-7852-290676ece2d7
  4. Next we set the version. Take the 7th byte perform an and operation with 0x0f followed by an or operation of 0x10.
    GUID so far: 63b00000-bfde-11d3-7852-290676ece2d7
  5. Finally we set the variant. Take the 9th byte perform an and operation with 0x3f followed by an or operation of 0x80.
    GUID so far: 63b00000-bfde-11d3-b852-290676ece2d7

The GUID for this example is: 63b00000-bfde-11d3-b852-290676ece2d7


How to generate a version 3 or 5 GUID (MD5 or SHA-1 hash & namespace)

The only difference between version 3 and 5 is the hashing method used, this example will use SHA-1 (version 5)

You need two basic ingredients to make a name based GUID

  1. A name to generate the GUID from
  2. A namespace to generate the GUID from
There are a few standard namespaces mentioned in the specification
  1. DNS: 6ba7b810-9dad-11d1-80b4-00c04fd430c8
  2. URL: 6ba7b811-9dad-11d1-80b4-00c04fd430c8
  3. ISO OID: 6ba7b812-9dad-11d1-80b4-00c04fd430c8
  4. X.500 DN: 6ba7b814-9dad-11d1-80b4-00c04fd430c8

Mapping the components to a GUID


For this example we'll generate a GUID for the name "www.example.com" using the namespace "DNS"

  1. Convert the name into bytes
    77-77-77-2E-77-69-64-67-65-74-73-2E-63-6F-6D
  2. Convert the namespace into bytes
    10-B8-A7-6B-AD-9D-D1-11-80-B4-00-C0-4F-D4-30-C8
  3. Concatenate them and hash using the correct hashing method
    2E-D6-65-7D-E9-27-46-8B-55-E1-26-65-A8-AE-A6-A2-2D-EE-3E-35
  4. Break up the hash into the main components of a GUID, timestamp, clock sequence, and node ID
    2E-D6-65-7D-E9-27-46-8B-55-E1-26-65-A8-AE-A6-A2
  5. Insert the timestamp component into the GUID: 2ed6657de927468b
    GUID so far: 2ed6657d-e927-468b-xxxx-xxxxxxxxxxxx
  6. Insert the clock sequence component into the GUID: 55e1
    GUID so far: 2ed6657d-e927-468b-55e1-xxxxxxxxxxxx
  7. Insert the node ID component into the GUID: 2665a8aea6a2
    GUID so far: 2ed6657d-e927-468b-55e1-2665a8aea6a2
  8. Next we set the version. Take the 7th byte perform an and operation with 0x0f followed by an or operation of 0x50.
    GUID so far: 2ed6657d-e927-568b-55e1-2665a8aea6a2
  9. Finally we set the variant. Take the 9th byte perform an and operation with 0x3f followed by an or operation of 0x80.
    GUID so far: 2ed6657d-e927-568b-95e1-2665a8aea6a2

The GUID for this example is: 2ed6657d-e927-568b-95e1-2665a8aea6a2


How to generate a version 4 GUID (random)

This is the easiest one to generate - just fill it up with random bytes then set the version and variant

  1. Generate some random data: 20-0A-DB-3D-B3-F4-AB-DE-29-C8-2C-68-88-D6-BE-30 GUID so far: 200adb3d-b3f4-abde-29c8-2c6888d6be30
  2. Next we set the version. Take the 7th byte perform an and operation with 0x0f followed by an or operation of 0x40.
    GUID so far: 200adb3d-b3f4-4bde-29c8-2c6888d6be30
  3. Finally we set the variant. Take the 9th byte perform an and operation with 0x3f followed by an or operation of 0x80.
    GUID so far: 200adb3d-b3f4-4bde-a9c8-2c6888d6be30

The GUID for this example is: 200adb3d-b3f4-4bde-a9c8-2c6888d6be30


The storage of a GUID

All GUIDs are displayed the same regardless of system, however not all systems store GUIDs in the same manner. E.g. If you are using a Microsoft system then their storage format for GUIDs differs slightly from the spec. Microsoft defines the format as follows which may differ in byte order depending on the system.

bits name endianness (MS GUID) endianness (RFC 4122)
32 Data1 Native Big
16 Data2 Native Big
16 Data3 Native Big
64 Data4 Big Big

Generate a GUID

Quickly generate a compliant version 4 GUID using our online tool

Learn more »

What is a GUID

A GUID or UUID is a 128-bit unique reference number used in computing, a GUID follows a specific structure defined in RFC 4122 and come in a few different versions and variants.

Learn more »

GUID tools

Sites like this are great for quickly generating a GUID or two - but most development environments have inbuilt tools or commands which are quicker to use on a regular basis.

Learn more »