ARAVINDA VISHWANATHAPURA

Reproducible UUIDs - Crystal language

Mar 13, 2023
1 minute read.
uuid crystal uuid3 uuid5

Sometimes applications need reproducible UUIDs (Universally Unique identifier) within a namespace, but it should be predictable for a given string.

UUID v3 (MD5 based) and v5 (SHA1 based) support this use case (Ref).

UUID v3
$ uuidgen -n 3d71a27f-eb95-499d-9cac-9736b5bf9cb9 -N "Hello World!" --md5
aaf79067-8fec-3592-ab16-d4be35bf96a3
UUID v5
$ uuidgen -n 3d71a27f-eb95-499d-9cac-9736b5bf9cb9 -N "Hello World!" --sha1
56608ec1-ba39-5b0c-84dd-95baee998a7f

The UUID library in the Crystal standard library doesn’t support generating V3 and V5 UUIDs. I created a shard for the same.

Add uuid_utils to shard.yml file

dependencies:
  uuid_utils:
    github: aravindavk/uuid_utils
require "uuid_utils"

namespace = UUID.new("3d71a27f-eb95-499d-9cac-9736b5bf9cb9")
msg = "Hello World!"
puts UUID.uuid3(namespace, msg) # => aaf79067-8fec-3592-ab16-d4be35bf96a3
puts UUID.uuid5(namespace, msg) # => 56608ec1-ba39-5b0c-84dd-95baee998a7f

Also added UUID.uuid4 to this library that just calls UUID.random function.

About Aravinda Vishwanathapura

Co-Founder & CTO at Kadalu Technologies, Creator of Sanka, Creator of Chitra, GlusterFS core team member, Maintainer of Kadalu Storage
Contact: Linkedin | Twitter | Facebook | Github | mail@aravindavk.in