Deno - Generate and Validate UUID Examples

This tutorial shows you how to generate UUID in Deno.

Deno has a module named uuid as a part of std modules. At the time this post was written, the module only supports v1, v4, and v5. In this tutorial, I am going to explain the differences between those versions and show the usage examples for each version.

Using Deno std UUID Module

First, we need to import the module. To make it easier to manage the dependency, it's better to import and re-export the module on deps.ts file.

deps.ts

  import {
    v1 as uuidV1,
    v4 as uuidV4,
    v5 as uuidV5,
  } from 'https://deno.land/std@0.82.0/uuid/mod.ts';

  export { uuidV1, uuidV4, uuidV5 };

Generate and Validate UUID v1

A version 1 UUID is generated by concatenating 48-bit MAC address of a node (the device) and a timestamp measured in units of 100 nanoseconds since October 15th, 1582. It uses a 13- or 14-bit "uniquifying" clock sequence that extends the timestamp which is used to handle if there are multiple processors to handle cases where the processor clock does not advance fast enough, or where there are multiple processors and UUID generators per node. Since version 1 depends on MAC addresses from network cards, it may cause a privacy issue as the computer who created it can be traced back. Therefore RFC 4122 allows the MAC address to be replaced by a random 48-bit number.

  function generate(
    options?: V1Options | null,
    buf?: number[],
    offset?: number,
  ): string | number[]

V1Options has the following fields.

  • node?: number[]: RFC node field as an Array[6] of byte values.
  • clockseq?: number: RFC "clock sequence" as a Number between 0 - 0x3fff.
  • msecs?: number: RFC timestamp field (Number of milliseconds, unix epoch)
  • nsecs?: number: RFC timestamp field (Number of nanseconds to add to msecs, should be 0-10,000)
  • random?: number[]: Array of 16 random bytes (0-255).
  • rng?: () => number[]: An alternative to random, a function that returns an array of 16 random bytes (0-255).

Below is the basic usage example where we don't set any option.

  const uuid = uuidV1.generate() as string;
  console.log(uuid);

And below is the example if you need to customize the value for node and timestamp.

  const uuid = uuidV1.generate({
    node: [0x01,0x02,0x03,0x04,0x05,0x06],
    clockseq: 0x1234,
    msecs: new Date("2020-01-01").getTime(),
   nsecs: 1000,
  }) as string;
  console.log(uuid);

To check whether a value is a valid version 1 UUID, you can use the below function.

  function validate(id: string): boolean

Example:

  const isValid = uuidV1.validate(uuid);
  console.log(isValid);

Generate and Validate UUID v4

A version 4 UUID is randomly generated. 4 bits are used to indicate the version. 2 or 3 bits are used to indicate the variant, with the values for variants 1 and 2 are 102 and 1102 respectively. Variant 1 has 122 randomly generated bits, while variant 2 has 121 bits to be randomly generated. That means the number of possible values of variant 2 is half as many as variant 1's.

  function generate(): string

Example:

  const uuid = uuidV4.generate();
  console.log(uuid);

You can check whether a value is a valid version 4 UUID using the below function.

  function validate(id: string): boolean

Example:

  const isValid = uuidV4.validate(uuid);
  console.log(isValid);
  function validate(id: string): boolean

Generate and Validate UUID v5

A version 5 UUID is generated by hashing a namespace identifier and name using SHA-1 algorithm. You need to define the UUID of the namespace which is transformed to a string of bytes. The result is then concatenated with the input name and hashed with SHA-1. The 160-bit digests generated by the SHA-1 is truncated to 128-bit. Like other UUID types, 4 bits are used to indicate the version. It has other 2 or 3 bits used to indicate the variant (10 for RFC 4122 or 1102 for legacy Microsoft GUID). That means it has 121 or 122 bits that affect the uniqueness of the UUID.

  function generate(
    options: V5Options,
    buf?: number[],
    offset?: number,
  ): string | number[]

V5Options has the following fields.

  • namespace: string | number[]: xxx

You are required to pass the value for namespace:

  const NAMESPACE = 'd9331660-6872-5f59-bfbb-c072f44d6b37';

  const uuid: string = uuidV5.generate({ value: '', namespace: NAMESPACE }) as string;
  console.log(uuid);

It also provides a function to check whether a given value is a valid version 5 UUID.

  function validate(id: string): boolean

Example:

  const isValid = uuidV5.validate(uuid);
  console.log(isValid);

That's it for this tutorial. You can choose which UUID version to use and also validate if a value is a valid UUID of a particular version.