| 1 |
NAME |
| 2 |
UUID - Universally Unique Identifier library for Perl |
| 3 |
|
| 4 |
SYNOPSIS |
| 5 |
use UUID qw(uuid); # see EXPORTS |
| 6 |
my $str = uuid(); # generate version 4 UUID string |
| 7 |
|
| 8 |
$str = uuid1(); # new version 1 UUID string |
| 9 |
$str = uuid4(); # new version 4 UUID string |
| 10 |
$str = uuid6(); # new version 6 UUID string |
| 11 |
$str = uuid7(); # new version 7 UUID string |
| 12 |
|
| 13 |
UUID::generate_v1($bin); # new version 1 binary UUID |
| 14 |
UUID::generate_v4($bin); # new version 4 binary UUID |
| 15 |
UUID::generate_v6($bin); # new version 6 binary UUID |
| 16 |
UUID::generate_v7($bin); # new version 7 binary UUID |
| 17 |
|
| 18 |
UUID::parse($str, $bin); # map string to binary UUID |
| 19 |
|
| 20 |
UUID::unparse($bin, $str); # stringify $bin; prefer lowercase |
| 21 |
|
| 22 |
UUID::compare($bin1, $bin2); # compare binary UUIDs |
| 23 |
UUID::copy($dst, $src); # copy binary UUID from $src to $dst |
| 24 |
|
| 25 |
UUID::clear($bin); # set binary UUID to NULL |
| 26 |
UUID::is_null($bin); # compare binary UUID to NULL |
| 27 |
|
| 28 |
UUID::time($bin); # return UUID time |
| 29 |
UUID::type($bin); # return UUID type |
| 30 |
UUID::variant($bin); # return UUID variant |
| 31 |
UUID::version($bin); # return UUID version |
| 32 |
|
| 33 |
UUID::generate($bin); # alias for generate_v1() |
| 34 |
UUID::generate_time($bin); # alias for generate_v1() |
| 35 |
UUID::generate_random($bin); # alias for generate_v4() |
| 36 |
|
| 37 |
UUID::unparse_lower($bin, $str); # force lowercase stringify |
| 38 |
UUID::unparse_upper($bin, $str); # force uppercase stringify |
| 39 |
|
| 40 |
# NAMESPACE is 'dns', 'url', 'oid', or 'x500'; case-insensitive. |
| 41 |
$str = uuid3(dns => 'www.example.com'); |
| 42 |
$str = uuid5(url => 'https://www.example.com/foo.html'); |
| 43 |
|
| 44 |
UUID::generate_v3($bin, dns => 'www.example.com'); |
| 45 |
UUID::generate_v5($bin, url => 'https://www.example.com/foo.txt'); |
| 46 |
|
| 47 |
DESCRIPTION |
| 48 |
The UUID library is used to generate unique identifiers for objects that |
| 49 |
may be accessible beyond the local system. For instance, they could be |
| 50 |
used to generate unique HTTP cookies across multiple web servers without |
| 51 |
communication between the servers, and without fear of a name clash. |
| 52 |
|
| 53 |
The generated UUIDs can be reasonably expected to be unique within a |
| 54 |
system, and unique across all systems, and are compatible with those |
| 55 |
created by the Open Software Foundation (OSF) Distributed Computing |
| 56 |
Environment (DCE). |
| 57 |
|
| 58 |
All generated UUIDs are either version 1, 3, 4, 5, 6, or version 7. And |
| 59 |
all are variant 1, meaning compliant with the OSF DCE standard as |
| 60 |
described in RFC4122. |
| 61 |
|
| 62 |
Versions 6 and 7 are not yet standardized. They are presented here as |
| 63 |
proposed in RFC4122bis, version 14, and may change in the future. |
| 64 |
RFC4122bis is noted to replace RFC4122, if approved. |
| 65 |
|
| 66 |
FUNCTIONS |
| 67 |
Most of the UUID functions expose the historically underlying libuuid |
| 68 |
C interface rather directly. That is, many return their values in their |
| 69 |
parameters and nothing else. |
| 70 |
|
| 71 |
Not very Perlish, but it's been like that for a long time so not likely |
| 72 |
to change any time soon. |
| 73 |
|
| 74 |
All take or return UUIDs in either binary or string format. The string |
| 75 |
format resembles the following: |
| 76 |
|
| 77 |
21b081a3-de83-4480-a14f-e89a1dcf8f0f |
| 78 |
|
| 79 |
Or, in terms of printf(3) format: |
| 80 |
|
| 81 |
"%08x-%04x-%04x-%04x-%012x" |
| 82 |
|
| 83 |
The binary form is simply a packed 16 byte binary value. |
| 84 |
|
| 85 |
clear( $uuid ) |
| 86 |
Sets binary $uuid equal to the value of the NULL UUID. |
| 87 |
|
| 88 |
compare( $uuid1, $uuid2 ) |
| 89 |
Compares two binary UUIDs. |
| 90 |
|
| 91 |
Returns an integer less than, equal to, or greater than zero if $uuid1 |
| 92 |
is less than, equal to, or greater than $uuid2. |
| 93 |
|
| 94 |
If one is defined and the other not, the defined value is deemed the |
| 95 |
larger. |
| 96 |
|
| 97 |
If either operand is not a binary UUID, falls back to a simple string |
| 98 |
comparison returning similar values. |
| 99 |
|
| 100 |
copy( $dst, $src ) |
| 101 |
Copies the binary $src UUID to $dst. |
| 102 |
|
| 103 |
If $src isn't a UUID, $dst is set to the NULL UUID. |
| 104 |
|
| 105 |
generate( $uuid ) |
| 106 |
Alias for generate_v4(). |
| 107 |
|
| 108 |
Prior to version 0.33, this function provided either a binary version 4 |
| 109 |
UUID or fell back to version 1 in some cases. This is no longer the |
| 110 |
case. The fallback feature was removed with the addition of an onboard |
| 111 |
crypto-strength number generator. |
| 112 |
|
| 113 |
generate_random( $uuid ) |
| 114 |
Alias for generate_v4(). |
| 115 |
|
| 116 |
generate_time( $uuid ) |
| 117 |
Alias for generate_v1(). |
| 118 |
|
| 119 |
generate_v1( $uuid ) |
| 120 |
Generates a new version 1 binary UUID using the current time and the |
| 121 |
local ethernet MAC address, if available. |
| 122 |
|
| 123 |
If the MAC address is not available at startup, or a randomized address |
| 124 |
is requested (see :mac in EXPORTS), a random address is used. The |
| 125 |
multicast bit of this address is set to avoid conflict with addresses |
| 126 |
returned from network cards. |
| 127 |
|
| 128 |
generate_v3( $uuid, NAMESPACE => NAME ) |
| 129 |
Generate a new version 3 binary UUID using the given namespace and name |
| 130 |
hashed through the MD5 algorithm. |
| 131 |
|
| 132 |
Namespace is one of "dns", "url", "oid", or "x500", and |
| 133 |
case-insensitive. It is used to select the namespace UUID to hash with |
| 134 |
the name. |
| 135 |
|
| 136 |
Name should be an entity from the given namespace, but can really be any |
| 137 |
text. |
| 138 |
|
| 139 |
generate_v4( $uuid ) |
| 140 |
Generates a new version 4 binary UUID using mostly random data. There |
| 141 |
are 6 bits used for the UUID format, leaving 122 bits for randomness. |
| 142 |
|
| 143 |
generate_v5( $uuid, NAMESPACE => NAME ) |
| 144 |
Generate a new version 5 binary UUID using the given namespace and name |
| 145 |
hashed through the SHA1 algorithm. |
| 146 |
|
| 147 |
Namespace is one of "dns", "url", "oid", or "x500", and |
| 148 |
case-insensitive. It is used to select the namespace UUID to hash with |
| 149 |
the name. |
| 150 |
|
| 151 |
Name should be an entity from the given namespace, but can really be any |
| 152 |
text. |
| 153 |
|
| 154 |
generate_v6( $uuid ) |
| 155 |
Generates a new version 6 binary UUID using the current time and the |
| 156 |
local ethernet MAC address, if available. |
| 157 |
|
| 158 |
If the MAC address is not available at startup, or a randomized address |
| 159 |
is requested (see :mac in EXPORTS), a random address is used. The |
| 160 |
multicast bit of this address is set to avoid conflict with addresses |
| 161 |
returned from network cards. |
| 162 |
|
| 163 |
Version 6 is the same as version 1, with reversed time fields to make it |
| 164 |
more database friendly. |
| 165 |
|
| 166 |
generate_v7( $uuid ) |
| 167 |
Generates a new version 7 binary UUID using the current time and random |
| 168 |
data. There are 6 bits used for the UUID format and 48 bits for |
| 169 |
timestamp, leaving 74 bits for randomness. |
| 170 |
|
| 171 |
Version 7 is the same as version 6, in that it uses reversed timestamp |
| 172 |
fields, but also uses a Unix epoch time base instead of Gregorian. |
| 173 |
|
| 174 |
is_null( $uuid ) |
| 175 |
Compares the value of $uuid to the NULL UUID. |
| 176 |
|
| 177 |
Returns 1 if NULL, and 0 otherwise. |
| 178 |
|
| 179 |
parse( $string, $uuid ) |
| 180 |
Converts the string format UUID in $string to binary and returns in |
| 181 |
$uuid. The previous content of $uuid, if any, is lost. |
| 182 |
|
| 183 |
Returns 0 on success and -1 on failure. Additionally on failure, the |
| 184 |
content of $uuid is unchanged. |
| 185 |
|
| 186 |
time( $uuid ) |
| 187 |
Returns the time element of a binary UUID in seconds since the epoch, |
| 188 |
the same as Perl's time function. |
| 189 |
|
| 190 |
Keep in mind this only works for version 1, 6, and version 7 UUIDs. |
| 191 |
Values returned from other versions are always 0. |
| 192 |
|
| 193 |
type( $uuid ) |
| 194 |
Alias for version(). |
| 195 |
|
| 196 |
unparse( $uuid, $string ) |
| 197 |
Alias for unparse_lower(). |
| 198 |
|
| 199 |
Prior to version 0.32, casing of the return value was system-dependent. |
| 200 |
Later versions are lowercase, per RFC4122. |
| 201 |
|
| 202 |
unparse_lower( $uuid, $string ) |
| 203 |
Converts the binary UUID in $uuid to string format and returns in |
| 204 |
$string. The previous content of $string, if any, is lost. |
| 205 |
|
| 206 |
unparse_upper( $uuid, $string ) |
| 207 |
Same as unparse_lower() but $string is forced to upper case. |
| 208 |
|
| 209 |
uuid() |
| 210 |
Alias for uuid4(). |
| 211 |
|
| 212 |
uuid0() |
| 213 |
Returns a new string format NULL UUID. |
| 214 |
|
| 215 |
uuid1() |
| 216 |
Returns a new string format version 1 UUID. Functionally the equivalent |
| 217 |
of calling generate_v1() then unparse(), but throwing away the |
| 218 |
intermediate binary UUID. |
| 219 |
|
| 220 |
uuid3(NAMESPACE => NAME) |
| 221 |
Same as uuid1() but version 3. See generate_v3(). |
| 222 |
|
| 223 |
uuid4() |
| 224 |
Same as uuid1() but version 4. |
| 225 |
|
| 226 |
uuid5(NAMESPACE => NAME) |
| 227 |
Same as uuid1() but version 5. See generate_v5(). |
| 228 |
|
| 229 |
uuid6() |
| 230 |
Same as uuid1() but version 6. |
| 231 |
|
| 232 |
uuid7() |
| 233 |
Same as uuid1() but version 7. |
| 234 |
|
| 235 |
variant( $uuid ) |
| 236 |
Returns the variant of binary $uuid. |
| 237 |
|
| 238 |
This module only generates variant 1 UUIDs. Others may be found in the |
| 239 |
wild. |
| 240 |
|
| 241 |
Known variants: |
| 242 |
|
| 243 |
0 NCS |
| 244 |
1 DCE |
| 245 |
2 Microsoft |
| 246 |
3 Other |
| 247 |
|
| 248 |
version( $uuid ) |
| 249 |
Returns the version of binary $uuid. |
| 250 |
|
| 251 |
This module only generates version 1, 3, 4, 5, 6, and version 7 UUIDs. |
| 252 |
Others may be found in the wild. |
| 253 |
|
| 254 |
Known versions: |
| 255 |
|
| 256 |
v1 date/time and node address |
| 257 |
v2 date/time and node address, security version |
| 258 |
v3 namespace based, MD5 hash |
| 259 |
v4 random |
| 260 |
v5 namespace based, SHA-1 hash |
| 261 |
v6 reverse date/time and node address |
| 262 |
v7 reverse unix date/time and random |
| 263 |
v8 custom |
| 264 |
|
| 265 |
RANDOM NUMBERS |
| 266 |
Version 4 and 7 UUIDs are partially filled with random numbers, as well |
| 267 |
as versions 1 and 6 when used with the :mac option. |
| 268 |
|
| 269 |
Prior to version 0.33, UUID obtained randomness from the system's |
| 270 |
/dev/random device, or similar interface. On some platforms it called |
| 271 |
getrandom() and on others it read directly from /dev/urandom. And of |
| 272 |
course, Win32 did something completely different. |
| 273 |
|
| 274 |
Starting in version 0.33, UUID generates random numbers itself using the |
| 275 |
ChaCha20 algorithm which is considered crypto-strength in most circles. |
| 276 |
This is the same algo used as the basis for many modern kernel RNGs, |
| 277 |
albeit without the same entropy gathering ability. |
| 278 |
|
| 279 |
To compensate, UUID mixes the output from ChaCha with output from |
| 280 |
another RNG, Xoshiro. The idea is that by mixing the two, the true |
| 281 |
output from either is effectively hidden, making discovery of either's |
| 282 |
key much more unlikely than it already is. And without the keys, you |
| 283 |
can't predict the future. |
| 284 |
|
| 285 |
Well, that's the theory anyway. |
| 286 |
|
| 287 |
Starting in version 0.38, the whole mix is initially seeded from the |
| 288 |
best platform-specific source available which could be one or more of: |
| 289 |
|
| 290 |
arc4random() |
| 291 |
BCryptGenRandom() |
| 292 |
CryptGenRandom() |
| 293 |
getentropy() |
| 294 |
getrandom() |
| 295 |
/dev/urandom |
| 296 |
/dev/random |
| 297 |
|
| 298 |
TIME |
| 299 |
Time-based UUIDs are formed with time-of-day information gathered from |
| 300 |
Time::HiRes. This provides a portable timestamp with 1us resolution |
| 301 |
facilitating generation rates up to 1M UUIDs/sec without further action. |
| 302 |
|
| 303 |
If UUIDs are required faster than 1us (i.e. > 1M UUIDs/sec), an internal |
| 304 |
counter simulates 100ns intervals, which is the UUID resolution limit. |
| 305 |
Each UUID within the same 1us tick will thus appear to be at successive |
| 306 |
100ns intervals, which probably isn't true. |
| 307 |
|
| 308 |
Should the internal counter exceed 9 within the same 1us period, it is |
| 309 |
reset to 0 and the UUID sequence field is incremented, thus guaranteeing |
| 310 |
uniqueness even though the time sequence will repeat. |
| 311 |
|
| 312 |
This scheme raises the limit of time-based uniqueness to over 16G |
| 313 |
UUIDs/sec, which is well beyond the best benchmarks seen to date. |
| 314 |
(~14M/sec for v6 binary UUIDs on commodity hardware.) |
| 315 |
|
| 316 |
Starting in version 0.38, the internal counter is moved to shared |
| 317 |
memory, ensuring uniqueness among time-based UUIDs in forked or threaded |
| 318 |
environments. Note that UUID must be loaded before forking or creating |
| 319 |
threads for this guarantee to hold. |
| 320 |
|
| 321 |
NAMESPACES |
| 322 |
Versions 3 and 5 generate UUIDs within namespaces. What this really |
| 323 |
means is that the NAME value is concatenated with a dedicated |
| 324 |
NAMESPACE UUID before hashing. |
| 325 |
|
| 326 |
Available namespaces and UUIDs: |
| 327 |
|
| 328 |
dns 6ba7b810-9dad-11d1-80b4-00c04fd430c8 |
| 329 |
url 6ba7b811-9dad-11d1-80b4-00c04fd430c8 |
| 330 |
oid 6ba7b812-9dad-11d1-80b4-00c04fd430c8 |
| 331 |
x500 6ba7b814-9dad-11d1-80b4-00c04fd430c8 |
| 332 |
|
| 333 |
For example, if you need to create some UUIDs within your own |
| 334 |
"questions" and "answers" namespaces using SHA1: |
| 335 |
|
| 336 |
$ns_base = uuid5( dns => 'www.example.com' ); |
| 337 |
|
| 338 |
$ns_questions = uuid5( $ns_base, 'questions' ); |
| 339 |
$ns_answers = uuid5( $ns_base, 'answers' ); |
| 340 |
|
| 341 |
for $topic ( next_qa_aref() ) { |
| 342 |
($q, $a) = @$topic; |
| 343 |
$uuid_question = uuid5( $ns_questions, $q ); |
| 344 |
$uuid_answer = uuid5( $ns_answers, $a ); |
| 345 |
... |
| 346 |
} |
| 347 |
|
| 348 |
This way, you can deterministically convert existing (and likely |
| 349 |
colliding) namespaces over to one UUID namespace, which is often useful |
| 350 |
when merging datasets. |
| 351 |
|
| 352 |
You also don't need to publish your base and namespace UUIDs. Anyone |
| 353 |
using the same logic can generate the same question and answer UUIDs. |
| 354 |
|
| 355 |
MAINTAINING STATE |
| 356 |
Internal state is optionally maintained for timestamped UUIDs (versions |
| 357 |
1, 6, and 7) via a file designated by the :persist export tag. See |
| 358 |
EXPORTS for details. |
| 359 |
|
| 360 |
The file records various internal states at the time the last UUID is |
| 361 |
generated, preventing future instances from overlapping the prior UUID |
| 362 |
sequence. This allows the sequence to absolutely survive reboots and, |
| 363 |
more importantly, backwards resetting of system time. |
| 364 |
|
| 365 |
If :persist is not used, time resets will still be detected while the |
| 366 |
module is loaded and handled by incrementing the UUID clock_seq field. |
| 367 |
The clock_seq field is randomly initialized in this case anyway, so the |
| 368 |
chance of overlap is low but still exists since clock_seq is only 14 |
| 369 |
bits wide. Using a random MAC will help (see :mac in EXPORTS), adding an |
| 370 |
additional 48 bits of randomness. |
| 371 |
|
| 372 |
NOTE: Using :persist incurs a serious performance penalty, in excess of |
| 373 |
95% on tested platforms. You can run "make compare" in the distribution |
| 374 |
directory to see how this might affect your application, but unless you |
| 375 |
need many thousands of UUIDs/sec it's probably a non-issue. |
| 376 |
|
| 377 |
EXPORTS |
| 378 |
None by default. All functions may be imported in the usual manner, |
| 379 |
either individually or all at once using the :all tag. |
| 380 |
|
| 381 |
Beware that importing :all clobbers Perl's time(), not to mention a |
| 382 |
few other commonly used subs, like copy() from File::Copy. |
| 383 |
|
| 384 |
:mac=mode |
| 385 |
The MAC address used for MAC-inclusive UUIDS (versions 1 and 6) is |
| 386 |
forced to always be random in one of two modes: |
| 387 |
|
| 388 |
random The MAC address is generated once at startup and used |
| 389 |
through the lifetime of the process. This is the default if a real |
| 390 |
MAC cannot be found. |
| 391 |
|
| 392 |
unique A new MAC address is generated for each new UUID. It is not |
| 393 |
guaranteed to be unique beyond the probability of randomness. |
| 394 |
|
| 395 |
:persist=path/to/state.txt |
| 396 |
Path to timestamp state maintenance file. (See MAINTAINING STATE.) The |
| 397 |
path may be either relative or absolute. |
| 398 |
|
| 399 |
If the file does not exist, it will be created provided the path exists |
| 400 |
and the user has permission. |
| 401 |
|
| 402 |
If the file cannot be opened, cannot be created, or is a symlink, UUID |
| 403 |
will ignore it. No state will be maintained. |
| 404 |
|
| 405 |
WARNING: Do not :persist in a public directory. See CVE-2013-4184. UUID |
| 406 |
attempts to avoid this, but nothing is foolproof. Only YOU can prevent |
| 407 |
symlink attacks! |
| 408 |
|
| 409 |
:defer[=N] |
| 410 |
Persistence of state is deferred N seconds when generating time-based |
| 411 |
UUIDs. More precisely, state is only saved every N seconds. If UUIDs |
| 412 |
are generated more often, those within the N second window will not |
| 413 |
save state. |
| 414 |
|
| 415 |
Defer values greater than some platform-specific interval greatly reduce |
| 416 |
the performance penalty introduced through persistence. While the |
| 417 |
default, :defer=0.001, is probably fine, you can run make persist in the |
| 418 |
distribution directory to see the effect of various values. |
| 419 |
|
| 420 |
THREAD SAFETY |
| 421 |
This module is believed to be thread safe. |
| 422 |
|
| 423 |
UUID LIBRARY |
| 424 |
Releases prior to UUID-0.32 required libuuid or similar be installed |
| 425 |
first. This is no longer the case. Version 0.33 bundled the e2fsprogs |
| 426 |
UUID code, and version 0.34 removed it altogether. |
| 427 |
|
| 428 |
BENCHMARKS |
| 429 |
There are a few benchmarks in the distribution ubin directory which can |
| 430 |
be run either standalone or through the Makefile. |
| 431 |
|
| 432 |
make compare |
| 433 |
Runs all three of the following tests. |
| 434 |
|
| 435 |
make speeds |
| 436 |
Runs ubin/cmp_speeds.pl to compare the speeds of various UUID versions. |
| 437 |
|
| 438 |
make styles |
| 439 |
Runs ubin/cmp_styles.pl to compare different UUID calling styles. |
| 440 |
|
| 441 |
make persist |
| 442 |
Runs ubin/cmp_persist.pl to compare different deferral values for |
| 443 |
persistent state. |
| 444 |
|
| 445 |
COPYRIGHT AND LICENSE |
| 446 |
This software is Copyright (c) 2014-2026 by Rick Myers. |
| 447 |
|
| 448 |
This is free software, licensed under: |
| 449 |
|
| 450 |
The Artistic License 2.0 (GPL Compatible) |
| 451 |
|
| 452 |
Details of this license can be found within the 'LICENSE' text file. |
| 453 |
|
| 454 |
AUTHOR |
| 455 |
Current maintainer: |
| 456 |
|
| 457 |
Rick Myers <[email protected]>. |
| 458 |
|
| 459 |
Authors and/or previous maintainers: |
| 460 |
|
| 461 |
Lukas Zapletal |
| 462 |
|
| 463 |
Joseph N. Hall |
| 464 |
|
| 465 |
Colin Faber |
| 466 |
|
| 467 |
Peter J. Braam |
| 468 |
|
| 469 |
CONTRIBUTORS |
| 470 |
David E. Wheeler |
| 471 |
|
| 472 |
William Faulk |
| 473 |
|
| 474 |
gregor herrmann |
| 475 |
|
| 476 |
Slaven Rezic |
| 477 |
|
| 478 |
twata |
| 479 |
|
| 480 |
Christopher Rasch-Olsen Raa |
| 481 |
|
| 482 |
Petr Pisar |
| 483 |
|
| 484 |
Kurt Starsinic |
| 485 |
|
| 486 |
Matt Martini |
| 487 |
|
| 488 |
SEE ALSO |
| 489 |
RFC4122 - <https://www.rfc-editor.org/rfc/rfc4122> |
| 490 |
|
| 491 |
RFC4122bis - |
| 492 |
<https://www.ietf.org/archive/id/draft-ietf-uuidrev-rfc4122bis-14.html> |
| 493 |
|
| 494 |
perl(1). |
| 495 |
|