File Coverage

blib/lib/UUID.pm
Criterion Covered Total %
statement 44 45 97.7
branch 13 14 92.8
condition 15 18 83.3
subroutine 6 6 100.0
pod n/a
total 78 83 93.9


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