| 1 |
NAME |
| 2 |
Data::UUID::MT - Fast random UUID generator using the Mersenne Twister |
| 3 |
algorithm |
| 4 |
|
| 5 |
VERSION |
| 6 |
version 1.001 |
| 7 |
|
| 8 |
SYNOPSIS |
| 9 |
use Data::UUID::MT; |
| 10 |
my $ug1 = Data::UUID::MT->new( version => 4 ); # "1", "4" or "4s" |
| 11 |
my $ug2 = Data::UUID::MT->new(); # default is "4" |
| 12 |
|
| 13 |
# method interface |
| 14 |
my $uuid1 = $ug->create(); # 16 byte binary string |
| 15 |
my $uuid2 = $ug->create_hex(); |
| 16 |
my $uuid3 = $ug->create_string(); |
| 17 |
|
| 18 |
# iterator -- avoids some method call overhead |
| 19 |
my $next = $ug->iterator; |
| 20 |
my $uuid4 = $next->(); |
| 21 |
|
| 22 |
DESCRIPTION |
| 23 |
This UUID generator uses the excellent Math::Random::MT::Auto module as |
| 24 |
a source of fast, high-quality (pseudo) random numbers. |
| 25 |
|
| 26 |
Three different types of UUIDs are supported. Two are consistent with |
| 27 |
RFC 4122 and one is a custom variant that provides a 'sequential UUID' |
| 28 |
that can be advantageous when used as a primary database key. |
| 29 |
|
| 30 |
Note: The Mersenne Twister pseudo-random number generator has excellent |
| 31 |
statistical properties, but it is not considered cryptographically |
| 32 |
secure. Pseudo-random UUIDs are not recommended for use as security |
| 33 |
authentication tokens in cookies or other user-visible session |
| 34 |
identifiers. |
| 35 |
|
| 36 |
Version 1 UUIDs |
| 37 |
The UUID generally follows the "version 1" spec from the RFC, however |
| 38 |
the clock sequence and MAC address are randomly generated each time. |
| 39 |
(This is permissible within the spec of the RFC.) The generated MAC |
| 40 |
address has the the multicast bit set as mandated by the RFC to ensure |
| 41 |
it does not conflict with real MAC addresses. This UUID has 60 bits of |
| 42 |
timestamp data, 61 bits of pseudo-random data and 7 mandated bits |
| 43 |
(multicast bit, "variant" field and "version" field). |
| 44 |
|
| 45 |
Version 4 UUIDs |
| 46 |
The UUID follows the "version 4" spec, with 122 pseudo-random bits and 6 |
| 47 |
mandated bits ("variant" field and "version" field). |
| 48 |
|
| 49 |
Version 4s UUIDs |
| 50 |
This is a custom UUID form that resembles "version 4" form, but that |
| 51 |
overlays the first 60 bits with a timestamp akin to "version 1", Unlike |
| 52 |
"version 1", this custom version preserves the ordering of bits from |
| 53 |
high to low, whereas "version 1" puts the low 32 bits of the timestamp |
| 54 |
first, then the middle 16 bits, then multiplexes the high bits with |
| 55 |
version field. This "4s" variant provides a "sequential UUID" with the |
| 56 |
timestamp providing order and the remaining random bits making collision |
| 57 |
with other UUIDs created at the exact same microsecond highly unlikely. |
| 58 |
This UUID has 60 timestamp bits, 62 pseudo-random bits and 6 mandated |
| 59 |
bits ("variant" field and "version" field). |
| 60 |
|
| 61 |
Unsupported: Versions 2, 3 and 5 |
| 62 |
This module focuses on generation of UUIDs with random elements and does |
| 63 |
not support UUID versions 2, 3 and 5. |
| 64 |
|
| 65 |
METHODS |
| 66 |
new |
| 67 |
my $ug = Data::UUID::MT->new( version => 4 ); |
| 68 |
|
| 69 |
Creates a UUID generator object. The only allowed versions are "1", "4" |
| 70 |
and "4s". If no version is specified, it defaults to "4". |
| 71 |
|
| 72 |
create |
| 73 |
my $uuid = $ug->create; |
| 74 |
|
| 75 |
Returns a UUID packed into a 16 byte string. |
| 76 |
|
| 77 |
create_hex |
| 78 |
my $uuid = $ug->create_hex(); |
| 79 |
|
| 80 |
Returns a UUID as a lowercase hex string, prefixed with "0x", e.g. |
| 81 |
0xb0470602a64b11da863293ebf1c0e05a |
| 82 |
|
| 83 |
create_string |
| 84 |
my $uuid = $ug->create_string(); # |
| 85 |
|
| 86 |
Returns UUID as a lowercase string in "standard" format, e.g. |
| 87 |
"b0470602-a64b-11da-8632-93ebf1c0e05a" |
| 88 |
|
| 89 |
iterator |
| 90 |
my $next = $ug->iterator; |
| 91 |
my $uuid = $next->(); |
| 92 |
|
| 93 |
Returns a reference to the internal UUID generator function. Because |
| 94 |
this avoids method call overhead, it is slightly faster than calling |
| 95 |
"create". |
| 96 |
|
| 97 |
reseed |
| 98 |
$ug->reseed; |
| 99 |
|
| 100 |
Reseeds the internal pseudo-random number generator. This happens |
| 101 |
automatically after a fork or thread creation (assuming |
| 102 |
Scalar::Util::weaken), but may be called manually if desired for some |
| 103 |
reason. |
| 104 |
|
| 105 |
Any arguments provided are passed to Math::Random::MT::Auto::srand() for |
| 106 |
custom seeding. |
| 107 |
|
| 108 |
$ug->reseed('hotbits' => 250, '/dev/random'); |
| 109 |
|
| 110 |
UUID STRING REPRESENTATIONS |
| 111 |
A UUID contains 16 bytes. A hex string representation looks like |
| 112 |
0xb0470602a64b11da863293ebf1c0e05a. A "standard" representation looks |
| 113 |
like "b0470602-a64b-11da-8632-93ebf1c0e05a". Sometimes these are seen in |
| 114 |
upper case and on Windows the standard format is often seen wrapped in |
| 115 |
parentheses. |
| 116 |
|
| 117 |
Converting back and forth is easy with "pack" and "unpack". |
| 118 |
|
| 119 |
# string to 16 bytes |
| 120 |
$string =~ s/^0x//i; # remove leading "0x" |
| 121 |
$string =~ tr/()-//d; # strip '-' and parentheses |
| 122 |
$binary = pack("H*", $string); |
| 123 |
|
| 124 |
# 16 bytes to uppercase string formats |
| 125 |
$hex = "0x" . uc unpack("H*", $binary); |
| 126 |
$std = uc join "-", unpack("H8H4H4H4H12", $binary); |
| 127 |
|
| 128 |
If you need a module that provides these conversions for you, consider |
| 129 |
UUID::Tiny. |
| 130 |
|
| 131 |
COMPARISON TO OTHER UUID MODULES |
| 132 |
At the time of writing, there are five other general purpose UUID |
| 133 |
generators on CPAN that I consider potential alternatives. |
| 134 |
Data::UUID::MT is included in the discussion below for comparison. |
| 135 |
|
| 136 |
* Data::GUID - version 1 UUIDs (wrapper around Data::UUID) |
| 137 |
|
| 138 |
* Data::UUID - version 1 or 3 UUIDs (derived from RFC 4122 code) |
| 139 |
|
| 140 |
* Data::UUID::LibUUID - version 1 or 4 UUIDs (libuuid) |
| 141 |
|
| 142 |
* UUID - version 1 or 4 UUIDs (libuuid) |
| 143 |
|
| 144 |
* UUID::Tiny - versions 1, 3, 4, or 5 (pure perl) |
| 145 |
|
| 146 |
* Data::UUID::MT - version 1 or 4 (or custom sequential "4s") |
| 147 |
|
| 148 |
"libuuid" based UUIDs may generally be either version 4 (preferred) or |
| 149 |
version 1 (fallback), depending on the availability of a good random bit |
| 150 |
source (e.g. /dev/random). "libuuid" version 1 UUIDs could also be |
| 151 |
provided by the "uuidd" daemon if available. |
| 152 |
|
| 153 |
UUID.pm leaves the choice of version up to "libuuid". |
| 154 |
Data::UUID::LibUUID does so by default, but also allows specifying a |
| 155 |
specific version. Note that Data::UUID::LibUUID incorrectly refers to |
| 156 |
version 1 UUIDs as version 2 UUIDs. For example, to get a version 1 |
| 157 |
binary UUID explicitly, you would call |
| 158 |
Data::UUID::LibUUID::new_uuid_binary(2). |
| 159 |
|
| 160 |
In addition to differences mentioned below, there are additional slight |
| 161 |
difference in how the modules (or "libuuid") treat the "clock sequence" |
| 162 |
field and otherwise attempt to keep state between calls, but this is |
| 163 |
generally immaterial. |
| 164 |
|
| 165 |
Use of Ethernet MAC addresses |
| 166 |
Version 1 UUID generators differ in whether they include the Ethernet |
| 167 |
MAC address as a "node identifier" as specified in RFC 4122. Including |
| 168 |
the MAC has security implications as Version 1 UUIDs can then be traced |
| 169 |
to a particular machine at a particular time. |
| 170 |
|
| 171 |
For "libuuid" based modules, Version 1 UUIDs will include the actual MAC |
| 172 |
address, if available, or will substitute a random MAC (with multicast |
| 173 |
bit set). |
| 174 |
|
| 175 |
Data::UUID version 1 UUIDs do not contain the MAC address, but replace |
| 176 |
it with an MD5 hash of data including the hostname and host id (possibly |
| 177 |
just the IP address), modified with the multicast bit. |
| 178 |
|
| 179 |
Both UUID::Tiny and Data::UUID::MT version 1 UUIDs do not contain the |
| 180 |
actual MAC address, but replace it with a random multicast MAC address. |
| 181 |
|
| 182 |
Source of random bits |
| 183 |
All the modules differ in the source of random bits. |
| 184 |
|
| 185 |
"libuuid" based modules get random bits from "/dev/random" or |
| 186 |
"/dev/urandom" or fall back to a pseudo-random number generator. |
| 187 |
|
| 188 |
Data::UUID only uses random data to see the clock sequence and gets bits |
| 189 |
from the C "rand()" function. |
| 190 |
|
| 191 |
UUID::Tiny uses Perl's "rand()" function. |
| 192 |
|
| 193 |
Data::UUID::MT gets random bits from Math::Random::MT::Auto, which uses |
| 194 |
the Mersenne Twister algorithm. Math::Random::MT::Auto seeds from system |
| 195 |
sources (including Win32 specific ones on that platform) if available |
| 196 |
and falls back to other less ideal sources if not. |
| 197 |
|
| 198 |
Fork and thread safety |
| 199 |
Pseudo-random number generators used in generating UUIDs should be |
| 200 |
reseeded if the process forks or if threads are created. |
| 201 |
|
| 202 |
Data::UUID::MT checks if the process ID has changed before generating a |
| 203 |
UUID and reseeds if necessary. If Scalar::Util is installed and provides |
| 204 |
"weaken()", Data::UUID::MT will also reseed its objects on thread |
| 205 |
creation. |
| 206 |
|
| 207 |
Data::UUID::LibUUID will reseed on fork on Mac OSX. |
| 208 |
|
| 209 |
I have not explored further whether other UUID generators are |
| 210 |
fork/thread safe. |
| 211 |
|
| 212 |
Benchmarks |
| 213 |
The examples/bench.pl program included with this module does some simple |
| 214 |
benchmarking of UUID generation speeds. Here is the output from my |
| 215 |
desktop system (AMD Phenom II X6 1045T CPU). Note that "v?" is used |
| 216 |
where the choice is left to "libuuid" -- which will result in version 4 |
| 217 |
UUIDs on my system. |
| 218 |
|
| 219 |
Benchmark on Perl v5.14.0 for x86_64-linux with 8 byte integers. |
| 220 |
|
| 221 |
Key: |
| 222 |
U => UUID 0.02 |
| 223 |
UT => UUID::Tiny 1.03 |
| 224 |
DG => Data::GUID 0.046 |
| 225 |
DU => Data::UUID 1.217 |
| 226 |
DULU => Data::UUID::LibUUID 0.05 |
| 227 |
DUMT => Data::UUID::MT 0.001 |
| 228 |
|
| 229 |
Benchmarks are marked as to which UUID version is generated. |
| 230 |
Some modules offer method ('meth') and function ('func') interfaces. |
| 231 |
|
| 232 |
UT|v1 85229/s |
| 233 |
UT|v4 110652/s |
| 234 |
DULU|v1 177495/s |
| 235 |
DULU|v? 178629/s |
| 236 |
DUMT|v4s|meth 274905/s |
| 237 |
DUMT|v1|meth 281942/s |
| 238 |
U|v? 288136/s |
| 239 |
DULU|v4 295107/s |
| 240 |
DUMT|v4s|func 307575/s |
| 241 |
DUMT|v1|func 313538/s |
| 242 |
DG|v1|func 335333/s |
| 243 |
DG|v1|meth 373515/s |
| 244 |
DUMT|v4|meth 450845/s |
| 245 |
DUMT|v4|func 588573/s |
| 246 |
DU|v1 1312946/s |
| 247 |
|
| 248 |
SEE ALSO |
| 249 |
* RFC 4122 A Universally Unique IDentifier (UUID) URN Namespace |
| 250 |
<http://www.apps.ietf.org/rfc/rfc4122.html> |
| 251 |
|
| 252 |
SUPPORT |
| 253 |
Bugs / Feature Requests |
| 254 |
Please report any bugs or feature requests through the issue tracker at |
| 255 |
<https://github.com/dagolden/data-uuid-mt/issues>. You will be notified |
| 256 |
automatically of any progress on your issue. |
| 257 |
|
| 258 |
Source Code |
| 259 |
This is open source software. The code repository is available for |
| 260 |
public review and contribution under the terms of the license. |
| 261 |
|
| 262 |
<https://github.com/dagolden/data-uuid-mt> |
| 263 |
|
| 264 |
git clone git://github.com/dagolden/data-uuid-mt.git |
| 265 |
|
| 266 |
AUTHOR |
| 267 |
David Golden <[email protected]> |
| 268 |
|
| 269 |
CONTRIBUTOR |
| 270 |
Matt Koscica <[email protected]> |
| 271 |
|
| 272 |
COPYRIGHT AND LICENSE |
| 273 |
This software is Copyright (c) 2011 by David Golden. |
| 274 |
|
| 275 |
This is free software, licensed under: |
| 276 |
|
| 277 |
The Apache License, Version 2.0, January 2004 |
| 278 |
|