UUIDs: a Universally Unique IDentifiers (UUIDs) library written in Ada

I had a need for UUIDs compliant with RFC 9562, notably UUIDv7, so I figured I might as well publish it in Alire.

It’s able to generate UUIDv1 through UUIDv8 (excluding UUIDv2, which is not covered in the spec). It can identify the version and variant of the UUIDs, as well as some other things like printing (using 'Image thanks to Ada2022) and converting the raw values to an element array.

It seeds the random number generator thanks to System_Random with the options to source all randomness via system_random if needed (this will be blocking though).

You can add it to your project via alr with uuids or test it via

alr get uuids && cd uuids* && cd tests && alr run

Creating a UUID – either versioned or from a string etc – is simple; here’s an example from the readme:

pragma Ada_2022;
with UUIDs; use UUIDs;
with UUIDs.V4;
with UUIDs.V5;
with Ada.Text_IO; use Ada.Text_IO;
procedure Uuid_Test is
   --  This will hold our version number
   V_Num : Natural;
   --  Create a UUID from an existing UUID string
   Uid : UUID := From_String ("6ba7b810-9dad-11d1-80b4-00c04fd430c8");
   --  Create a UUID from defined octets
   Uid2 : UUID := From_Field ([16#6b#, 16#a7#, 16#b8#, 16#10#,
                              16#9d#, 16#ad#, 16#11#, 16#d1#,
                              16#80#, 16#b4#, 16#00#, 16#c0#,
                              16#4f#, 16#d4#, 16#30#, 16#c8#]);
   --  Create a random UUID
   U4 : UUID := V4.UUID4;
   --  Create a hashed UUID
   U5 : UUID := V5.UUID5 (Namespace_DNS, "example.org");

begin
   if Uid = Uid2 then
      --  This will print: "They're 6ba7b810-9dad-11d1-80b4-00c04fd430c8"
      Put_Line ("They're " & Uid'Image);
   end if;
   if U4.Version = Random then
      V_Num := U4.Version_Number;
      Put_Line (U4'Image & " is a random UUID which is number " & V_Num'Image);
   end if;

   --  This prints "aad03681-8b63-5304-89e0-8ca8f49461b5"
   Put_Line (U5'Image);
end Uuid_Test;

Full details can be found in the github project:

Full API documentation can be viewed here: Table of Contents

8 Likes

Interesting. I missed the standard update. Simple Components implement RFC412. Where the new UUIDs are used?

1 Like

Of course Simple Components would have it! :sweat_smile:

Technically 9562 is on the standards track (it’s been in draft since 2020, and published last March).

The biggest change is UUIDv7 which is a unix time stamp and random data (as opposed to Gregorian-based and a once-generated node). It gives better database locality compared to UUIDv4’s purely random, at the expense of exposing when the UUID was created (which v1 and v6 exposed anyway).

OK, so it is a combination of random and time stamp. Maybe I should update it after I am ready with arbitrary precision arithmetic and prime numbers…