CryptoSys Home > PKI > Generate a UUID compliant with RFC 4122

Generate a UUID compliant with RFC 4122


A Universally Unique IDentifier (UUID) - also called a Global Unique IDentifier (GUID) - is a 128-bit value formatted into blocks of hexadecimal digits separated by a hyphen ("-", U+002D). A typical UUID is AA97B177-9383-4934-8543-0F91A7A02836. It doesn't matter whether the letters A-F are upper or lower case.

A version 4 UUID is defined in RFC 4122: 128 randomly-generated bits with six bits at certain positions set to particular values. For example,

AA97B177-9383-4934-8543-0F91A7A02836
              ^    ^
              1    2

The digit at position 1 above is always "4" and the digit at position 2 is always one of "8", "9", "A" or "B".

Procedure

The procedure to generate a version 4 UUID is as follows:

  1. Generate 16 random bytes (=128 bits)
  2. Adjust certain bits according to RFC 4122 section 4.4 as follows:
    1. set the four most significant bits of the 7th byte to 0100'B, so the high nibble is "4"
    2. set the two most significant bits of the 9th byte to 10'B, so the high nibble will be one of "8", "9", "A", or "B".
  3. Encode the adjusted bytes as 32 hexadecimal digits
  4. Add four hyphen "-" characters to obtain blocks of 8, 4, 4, 4 and 12 hex digits
  5. Output the resulting 36-character string "XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"

Source code

The following code using the CryptoSys PKI Toolkit shows how to create a version 4 UUID according to RFC 4122.

Programming languageSource code
VB6/VBAUUID.bas
VB.NET/VB2005+Uuid.vb
C#Uuid.cs
C/C++Uuid.c

Typical output looks like

6948DF80-14BD-4E04-8842-7668D9C001F5
4B8302DA-21AD-401F-AF45-1DFD956B80B5
8628FE7C-A4E9-4056-91BD-FD6AA7817E39
10929DF8-15C5-472B-9398-7158AB89A0A6
ED280816-E404-444A-A2D9-FFD2D171F928
D952EB9F-7AD2-4B1B-B3CE-386735205990
3F897E85-62CE-4B2C-A957-FCF0CCE649FD
8E7C2F0A-6BB8-485C-917E-6B605A0DDF29
1AD2F3EF-87C8-46B4-BD1D-94C174C278EE
AA97B177-9383-4934-8543-0F91A7A02836

Reference

Contact

For more information or to comment on this page, please send us a message.

This page last updated 18 December 2017