| 1 | package Acme::CPANModules::UUID; | ||
| 2 | |||
| 3 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |
| 4 | 1 | our $DATE = '2021-05-06'; # DATE | |
| 5 | 1 | our $DIST = 'Acme-CPANModules-UUID'; # DIST | |
| 6 | 1 | our $VERSION = '0.010'; # VERSION | |
| 7 | |||
| 8 | 1 | ▶ | use strict; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 9 | 1 | ▶ | use warnings; |
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||
| 10 | |||
| 11 | 1 | our $LIST = { | |
| 12 | summary => 'Modules that can generate immutable universally unique identifier (UUIDs)', | ||
| 13 | description => <<'_', | ||
| 14 | |||
| 15 | UUIDs (Universally Unique Identifiers), sometimes also called GUIDs (Globally | ||
| 16 | Unique Identifiers), are 128-bit numbers that can be used as permanent IDs or | ||
| 17 | keys in databases. There are several standards that specify UUID, one of which | ||
| 18 | is RFC 4122 (2005), which we will follow in this document. | ||
| 19 | |||
| 20 | UUIDs are canonically represented as 32 hexadecimal digits in the form of: | ||
| 21 | |||
| 22 | xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx | ||
| 23 | |||
| 24 | There are several variants of UUID. The variant information is encoded using 1-3 | ||
| 25 | bits in the `N` position. RFC 4122 defines 4 variants (0 to 3), two of which (0 | ||
| 26 | and 3) are for legacy UUIDs, so that leaves variants 1 and 2 as the current | ||
| 27 | specification. | ||
| 28 | |||
| 29 | There are 5 "versions" of UUID for both variants 1 & 2, each might be more | ||
| 30 | suitable than others in specific cases. The version information is encoded in | ||
| 31 | the M position. Version 1 (v1) UUIDs are generated from a time and a node ID | ||
| 32 | (usually the MAC address); version 2 (v2) UUIDs from an identifier (group/user | ||
| 33 | ID), a time, and a node ID; version 4 (v4) UUIDs from a rando/mpseudo-random | ||
| 34 | number; version 3 (v3) UUIDs from hashing a namespace using MD5; version 5 (v5) | ||
| 35 | from hashing a namespace using SHA-1. | ||
| 36 | |||
| 37 | <pm:Data::UUID> should be your first choice, and when you cannot install XS | ||
| 38 | modules you can use <pm:UUID::Tiny> instead. | ||
| 39 | |||
| 40 | Aside from the modules listed as entries below, there are also: | ||
| 41 | <pm:App::UUIDUtils> (containing CLIs to create/check UUID), <pm:Data::GUID> | ||
| 42 | (currently just a wrapper for Data::UUID). | ||
| 43 | |||
| 44 | _ | ||
| 45 | entry_features => { | ||
| 46 | v4_rfc4122 => {summary => 'Whether the generated v4 UUID follows RFC 4122 specification (i.e. encodes variant and version information in M & N positions)'}, | ||
| 47 | v4_secure_random => {summary => 'Whether the module uses cryptographically secure pseudo-random number generator for v4 UUIDs'}, | ||
| 48 | }, | ||
| 49 | entries => [ | ||
| 50 | { | ||
| 51 | module => 'Data::UUID', | ||
| 52 | description => <<'_', | ||
| 53 | |||
| 54 | This module creates v1 and v2 UUIDs. Depending on the OS, for MAC address, it | ||
| 55 | usually uses a hash of hostname instead. This module is XS, so performance is | ||
| 56 | good. If you cannot use an XS module, try <pm:UUID::Tiny> instead. | ||
| 57 | |||
| 58 | The benchmark code creates 1000+1 v1 string UUIDs. | ||
| 59 | |||
| 60 | _ | ||
| 61 | bench_code_template => 'my $u = Data::UUID->new; $u->create for 1..1000; $u->to_string($u->create)', | ||
| 62 | features => { | ||
| 63 | is_xs => 1, | ||
| 64 | is_pp => 0, | ||
| 65 | create_v1 => 1, | ||
| 66 | create_v2 => 1, | ||
| 67 | create_v3 => 0, | ||
| 68 | create_v4 => 0, | ||
| 69 | create_v5 => 0, | ||
| 70 | }, | ||
| 71 | }, | ||
| 72 | |||
| 73 | { | ||
| 74 | module => 'UUID::FFI', | ||
| 75 | description => <<'_', | ||
| 76 | |||
| 77 | This module provides access to libuuid via the FFI interface. It can create v1 | ||
| 78 | as well as v4 (random) UUIDs. Note that Data::UUID (XS-based) is faster this | ||
| 79 | module (FFI-based). | ||
| 80 | |||
| 81 | The benchmark code creates 1000+1 v1 string UUIDs. | ||
| 82 | |||
| 83 | _ | ||
| 84 | bench_code_template => 'UUID::FFI->new_time for 1..1000; UUID::FFI->new_time->as_hex', | ||
| 85 | features => { | ||
| 86 | is_xs => 1, | ||
| 87 | is_pp => 0, | ||
| 88 | create_v1 => 1, | ||
| 89 | create_v2 => 0, | ||
| 90 | create_v3 => 0, | ||
| 91 | create_v4 => 1, | ||
| 92 | v4_secure_random => 0, | ||
| 93 | v4_rfc4122 => 1, | ||
| 94 | create_v5 => 0, | ||
| 95 | }, | ||
| 96 | }, | ||
| 97 | |||
| 98 | { | ||
| 99 | module => 'UUID::Tiny', | ||
| 100 | description => <<'_', | ||
| 101 | |||
| 102 | This module should be your go-to choice if you cannot use an XS module. It can | ||
| 103 | create v1, v3, v4 UUIDs. However, the random v4 UUIDs are not cryptographically | ||
| 104 | secure; if you need cryptographically secure random UUIDs, use <pm:Crypt::Misc>. | ||
| 105 | |||
| 106 | The benchmark code creates 1000+1 v1 string UUIDs. | ||
| 107 | |||
| 108 | See also: <pm:Types::UUID> which is a type library that uses Data::UUID as the | ||
| 109 | backend. | ||
| 110 | |||
| 111 | _ | ||
| 112 | bench_code_template => 'UUID::Tiny::create_uuid() for 1..1000; UUID::Tiny::uuid_to_string(UUID::Tiny::create_uuid())', | ||
| 113 | features => { | ||
| 114 | is_xs => 0, | ||
| 115 | is_pp => 1, | ||
| 116 | create_v1 => 1, | ||
| 117 | create_v2 => 0, | ||
| 118 | create_v3 => 1, | ||
| 119 | create_v4 => 1, | ||
| 120 | v4_secure_random => 0, | ||
| 121 | v4_rfc4122 => 1, | ||
| 122 | create_v5 => 1, | ||
| 123 | }, | ||
| 124 | }, | ||
| 125 | |||
| 126 | { | ||
| 127 | module => 'UUID::Random', | ||
| 128 | description => <<'_', | ||
| 129 | |||
| 130 | This module simply uses 32 calls to Perl's C<rand()> to construct each random | ||
| 131 | hexadecimal digits of the UUID (v4). Not really recommended, since perl's | ||
| 132 | default pseudo-random generator is neither cryptographically secure nor has 128 | ||
| 133 | bit of entropy. It also does not produce v4 UUIDs that conform to RFC 4122 (no | ||
| 134 | encoding of variant & version information). | ||
| 135 | |||
| 136 | To create a cryptographically secure random UUIDs, use <pm:Crypt::Misc>. | ||
| 137 | |||
| 138 | The benchmark code creates 1000+1 v4 string UUIDs. | ||
| 139 | |||
| 140 | _ | ||
| 141 | bench_code_template => 'UUID::Random::generate() for 1..1000; ; UUID::Random::generate()', | ||
| 142 | features => { | ||
| 143 | is_xs => 0, | ||
| 144 | is_pp => 1, | ||
| 145 | create_v1 => 0, | ||
| 146 | create_v2 => 0, | ||
| 147 | create_v3 => 0, | ||
| 148 | create_v4 => 1, | ||
| 149 | v4_secure_random => 0, | ||
| 150 | v4_rfc4122 => 0, | ||
| 151 | create_v5 => 0, | ||
| 152 | }, | ||
| 153 | }, | ||
| 154 | |||
| 155 | { | ||
| 156 | module => 'UUID::Random::PERLANCAR', | ||
| 157 | description => <<'_', | ||
| 158 | |||
| 159 | Just another implementation of <pm:UUID::Random>. | ||
| 160 | |||
| 161 | The benchmark code creates 1000+1 v4 string UUIDs. | ||
| 162 | |||
| 163 | _ | ||
| 164 | features => { | ||
| 165 | is_xs => 0, | ||
| 166 | is_pp => 1, | ||
| 167 | create_v1 => 0, | ||
| 168 | create_v2 => 0, | ||
| 169 | create_v3 => 0, | ||
| 170 | create_v4 => 1, | ||
| 171 | v4_secure_random => 0, | ||
| 172 | v4_rfc4122 => 1, | ||
| 173 | create_v5 => 0, | ||
| 174 | }, | ||
| 175 | functions => { | ||
| 176 | generate => { | ||
| 177 | bench_code_template => 'UUID::Random::PERLANCAR::generate() for 1..1000; UUID::Random::PERLANCAR::generate()', | ||
| 178 | }, | ||
| 179 | generate_rfc => { | ||
| 180 | bench_code_template => 'UUID::Random::PERLANCAR::generate_rfc() for 1..1000; UUID::Random::PERLANCAR::generate_rfc()', | ||
| 181 | }, | ||
| 182 | }, | ||
| 183 | }, | ||
| 184 | |||
| 185 | { | ||
| 186 | module => 'UUID::Random::Secure', | ||
| 187 | description => <<'_', | ||
| 188 | |||
| 189 | Just like <pm:UUID::Random>, except it uses <pm:Math::Random::Secure>'s | ||
| 190 | `irand()` to produce random numbers. | ||
| 191 | |||
| 192 | The benchmark code creates 1000+1 v4 string UUIDs. | ||
| 193 | |||
| 194 | _ | ||
| 195 | features => { | ||
| 196 | is_xs => 0, | ||
| 197 | is_pp => 1, | ||
| 198 | create_v1 => 0, | ||
| 199 | create_v2 => 0, | ||
| 200 | create_v3 => 0, | ||
| 201 | create_v4 => 1, | ||
| 202 | v4_secure_random => 1, | ||
| 203 | v4_rfc4122 => 1, | ||
| 204 | create_v5 => 0, | ||
| 205 | }, | ||
| 206 | functions => { | ||
| 207 | generate => { | ||
| 208 | bench_code_template => 'UUID::Random::Secure::generate() for 1..1000; UUID::Random::Secure::generate()', | ||
| 209 | }, | ||
| 210 | generate_rfc => { | ||
| 211 | bench_code_template => 'UUID::Random::Secure::generate_rfc() for 1..1000; UUID::Random::Secure::generate_rfc()', | ||
| 212 | }, | ||
| 213 | }, | ||
| 214 | }, | ||
| 215 | |||
| 216 | { | ||
| 217 | module => 'Crypt::Misc', | ||
| 218 | description => <<'_', | ||
| 219 | |||
| 220 | This module from the <pm:CryptX> distribution has a function to create and check | ||
| 221 | v4 UUIDs. | ||
| 222 | |||
| 223 | The benchmark code creates 1000+1 v4 string UUIDs. | ||
| 224 | |||
| 225 | _ | ||
| 226 | bench_code_template => 'Crypt::Misc::random_v4uuid() for 1..1000; Crypt::Misc::random_v4uuid()', | ||
| 227 | features => { | ||
| 228 | is_xs => 0, | ||
| 229 | is_pp => 1, | ||
| 230 | create_v1 => 0, | ||
| 231 | create_v2 => 0, | ||
| 232 | create_v3 => 0, | ||
| 233 | create_v4 => 1, | ||
| 234 | v4_secure_random => 1, | ||
| 235 | v4_rfc4122 => 1, | ||
| 236 | create_v5 => 0, | ||
| 237 | }, | ||
| 238 | }, | ||
| 239 | |||
| 240 | ], | ||
| 241 | }; | ||
| 242 | |||
| 243 | 1 | 1; | |
| 244 | # ABSTRACT: Modules that can generate immutable universally unique identifier (UUIDs) | ||
| 245 | |||
| 246 | __END__ | ||