Create a UUID version 4 via Utilities.getUuid in Google Sheets
Asked Answered
S

1

3

Post is related to this SO thread

According to the Utilities.getUuid Google Scripts docs:

Get a UUID as a string (equivalent to using the java.util.UUID.randomUUID()

Looking at the java.util.UUID.randomUUID() docs:

The variant field contains a value which identifies the layout of the UUID. The bit layout described above is valid only for a UUID with a variant value of 2, which indicates the Leach-Salz variant.

Does variant value of 2 means UUIDv2?
Assuming Utilities.getUuid generates s UUIDv2 and it doesn't pass UUIDv4 validation:

My project needs to generate UUIDv4 in JS code. Also, there's a restriction on _ids: they have to be UUIDv4 (it is non-negotiable, existing db etc).

Is it possible to generate a UUID V4 via the GS Utilities interface?

Or - is my best option to copy & call JS code which generates a UUIDv4 instead?

Shrubby answered 2/11, 2022 at 16:8 Comment(1)
It is type4. There's a version field.Bueschel
B
5

Variant is different from version. According to RFC4144, Variant denotes,

some bits of the eight octet variant field specified below determine finer structure.

The variant field determines the layout of the UUID. That is, the interpretation of all other bits in the UUID depends on the setting of the bits in the variant field

Version means

The version number is in the most significant 4 bits of the timestamp (bits 4 through 7 of the time_hi_and_version field).

The Uuid generated is version 4. RFC4144 describes the following format for Uuid:

  UUID                   = time-low "-" time-mid "-"
                           time-high-and-version "-"
                           clock-seq-and-reserved
                           clock-seq-low "-" node
  time-low               = 4hexOctet
  time-mid               = 2hexOctet
  time-high-and-version  = 2hexOctet
  clock-seq-and-reserved = hexOctet
  clock-seq-low          = hexOctet
  node                   = 6hexOctet
  hexOctet               = hexDigit hexDigit
  hexDigit =
        "0" / "1" / "2" / "3" / "4" / "5" / "6" / "7" / "8" / "9" /
        "a" / "b" / "c" / "d" / "e" / "f" /
        "A" / "B" / "C" / "D" / "E" / "F"

The following is an example of the string representation of a UUID as
a URN:

urn:uuid:f81d4fae-7dec-11d0-a765-00a0c91e6bf6

If the id is f81d4fae-7dec-11d0-a765-00a0c91e6bf6,

The third sequence 11d0 represents time-high-and-version, the first number here is the version in variant 2, i.e., 1 in 11d0. All the ids provided by Utilties.getUuid() are of type 4. They'll look like

f81d4fae-7dec-41d0-a765-00a0c91e6bf6
  • Variant - 8th octet(starting from 0): a7 ina765 above[Since this field is multiplexed with clock-seq-and-reserved, it'll be between 8...a in variant 2]
  • Version - 6th octet's most significant bit: 4 in 41d0 above

Also see variants in Wikipedia

Bueschel answered 2/11, 2022 at 17:17 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.