UUID Functions
UUIDgenerate.RdUUIDgenerate generates new Universally Unique Identifiers. It
can be either time-based or random.
UUIDfromName generates deterministic UUIDs based on namespace
UUID and a name (UUID version 3 and 5).
UUIDparse parses one of more UUIDs in string form and converts
them to other internal formats.
UUIDvalidate checks the valitiy of UUIDs in string form.
Arguments
- use.time
logical, if
TRUEthen time-based UUID is generated, ifFALSEthen a random UUID is generated, ifNAthen random one is generated if a sufficiently reliable source of random numbers can be found, otherwise a time-based UUID is generated.- n
integer, number of UUIDs to generate.
- output
type of the output. Valid types are:
"string"for a character vector with UUIDs in textual representation (always lowercase),"raw"for a vector or matrix of raw bytes,"uuid"for an object of the classUUIDand"logical"which only reports failure/success of the parsing, but not the actual values.- namespace
UUID defining the namespace
- name
character vector of names to use for generating UUIDs. The result will yield as many UUIDs as there are elements in this vector.
- type
string, type of the hash function to use when generating the UUIDs. "sha1" is recommended (version 5 UUID), "md5" is available for compatibility (version 3 UUID).
- what
character vector which will be parsed into UUIDs.
Value
UUIDgenerate, UUIDfromName and UUIDparse values
depend on the output argument as follows:
"string"character vector with each element UUID in lowercase form, for
UUIDparsestrings that cannot be parsed will result inNAvalues"raw"raw vector with the UUIDs stores each as 16 bytes seqeuntially. If the output is more than one UUID then the result is a raw matrix with 16 rows and as many columns as there are input elements.
"uuid"object of the class
UUIDwhich is a vector of UUIDs in 128-bit internal representation."logical"only supported in
UUIDparseand return codeTRUEfor valid UUID,FALSEfor invalid input andNAforNAinput.
UUIDvalidate is just a shorthand for
UUIDparse(what, output="logical").
Note
The first argument is not n for historical reasons, because the
first version did only generate a single UUID.
Note that time-based UUIDs are not unique by design: two
UUIDs generated at exactly the same time will be identical by
definition and the underlying libuuid library can generate
duplicate time-based UUIDs if the CPU is fast enough. The
UUIDgenerate function always keeps track of the last
generated time-based UUID and will wait and repeat generation in
case the next requested UUID is identical, so the results from
UUIDgenerate(TRUE, ...) call will not include duplicates.
However, any other means of generating UUID (or calls to the C
API from parallel threads) do not offer that guarantee.
Examples
UUIDgenerate()
#> [1] "75081a89-2ce9-426e-9927-45ac500193fe"
UUIDgenerate(TRUE)
#> [1] "fb8b188a-0182-11f1-971f-027b5ca26e59"
UUIDgenerate(FALSE)
#> [1] "e6c11259-0fd5-4f84-a0f5-4074f92de9e3"
## see if the randomness is any good
length(unique(UUIDgenerate(n=1000)))
#> [1] 1000
## generate a native UUID vector
(u <- UUIDgenerate(n=3, output="uuid"))
#> UUID vector:
#> [1] "8ffff354-b232-4068-980a-fe2987d3d7a9"
#> [2] "0ff0d9e8-43a5-4a76-8b56-c2c7fe30da92"
#> [3] "be664546-bcf8-4bd2-b94b-8ec52547a05a"
as.character(u)
#> [1] "8ffff354-b232-4068-980a-fe2987d3d7a9"
#> [2] "0ff0d9e8-43a5-4a76-8b56-c2c7fe30da92"
#> [3] "be664546-bcf8-4bd2-b94b-8ec52547a05a"
as.raw(u[1])
#> [1] 8f ff f3 54 b2 32 40 68 98 0a fe 29 87 d3 d7 a9
UUIDgenerate(output="raw")
#> [1] 65 bc f3 1a 57 4a 43 32 b1 d2 79 aa 49 38 2c 91
## UUID for DNS namespace
DNS.namespace <- "6ba7b810-9dad-11d1-80b4-00c04fd430c8"
## SHA1 (v5) - default
UUIDfromName(DNS.namespace, "r-project.org")
#> [1] "0cce4dd5-363b-5d7e-8baf-e5e06031f032"
## MD5 (v3)
UUIDfromName(DNS.namespace, "r-project.org", type="md5")
#> [1] "b9a02725-3dba-3d7c-81be-d05e0f134f8b"
## see ?UUID for more examples on UUID objects