Data-UUID-MT-1.001/000755 000765 000024 00000000000 12160612467 013705 5ustar00davidstaff000000 000000 Data-UUID-MT-1.001/Changes000644 000765 000024 00000002061 12160612467 015177 0ustar00davidstaff000000 000000 Revision history for Data-UUID-MT 1.001 2013-06-20 10:44:03 America/New_York [FIXED] - Fixed post-fork reseed 1.000 2012-11-30 17:03:16 America/New_York - Hex and string output is now lowercase, per RFC 4122 standard [rt.cpan.org #81582] 0.006 2012-04-18 05:53:08 America/New_York - Added documentation about converting UUID formats with pack/unpack 0.005 2012-03-15 05:45:44 EST5EDT - Now backwards compatible to Perl v5.6 [fix suggested by Steffen Mueller] 0.004 2011-12-20 11:56:21 EST5EDT - Bumped Test::More test prereq to 0.96 (needed for subtest support) [reported by Javier Camuñas] 0.003 2011-08-16 21:48:37 America/New_York - Fixed typo: s/Data::GUID::MT/Data::UUID/MT (Why do I find these things *after* I release. ) 0.002 2011-08-16 21:39:59 America/New_York - Added note recommending against use of pseudo-random UUIDs as secure authentication tokens (suggested by Yuval Kogman) 0.001 2011-08-15 23:45:36 America/New_York - Initial release Data-UUID-MT-1.001/CONTRIBUTING000644 000765 000024 00000002616 12160612467 015544 0ustar00davidstaff000000 000000 README.PATCHING Thank you for considering contributing to this distribution. This file contains instructions that will help you work with the source code. The distribution is managed with Dist::Zilla. This means than many of the usual files you might expect are not in the repository, but are generated at release time (e.g. Makefile.PL). However, you can run tests directly using the 'prove' tool: $ prove -l $ prove -lv t/some_test_file.t For most distributions, 'prove' is entirely sufficent for you to test any patches you have. Likewise, much of the documentation Pod is generated at release time. Depending on the distribution, some documentation may be written in a Pod dialect called WikiDoc. (See Pod::WikiDoc on CPAN.) If you would like to submit a documentation edit, please limit yourself to the documentation you see. If you see typos or documentation issues in the generated docs, please email or open a bug ticket instead of patching. Dist::Zilla is a very powerful authoring tool, but requires a number of author-specific plugins. If you would like to use it for contributing, install it from CPAN, then run one of the following commands, depending on your CPAN client: $ cpan `dzil authordeps` $ dzil authordeps | cpanm Once installed, here are some dzil commands you might try: $ dzil build $ dzil test $ dzil xtest You can learn more about Dist::Zilla at http://dzil.org/ Data-UUID-MT-1.001/dist.ini000644 000765 000024 00000000753 12160612467 015356 0ustar00davidstaff000000 000000 name = Data-UUID-MT author = David Golden license = Apache_2_0 copyright_holder = David Golden copyright_year = 2011 [@DAGOLDEN] :version = 0.045 AutoMetaResources.bugtracker.rt = 0 AutoMetaResources.bugtracker.github = user:dagolden stopwords = AMD stopwords = IDentifier stopwords = Mersenne stopwords = OSX stopwords = UUID stopwords = UUIDs stopwords = benchmarking stopwords = cryptographically stopwords = libuuid stopwords = multicast stopwords = timestamp Data-UUID-MT-1.001/examples/000755 000765 000024 00000000000 12160612467 015523 5ustar00davidstaff000000 000000 Data-UUID-MT-1.001/lib/000755 000765 000024 00000000000 12160612467 014453 5ustar00davidstaff000000 000000 Data-UUID-MT-1.001/LICENSE000644 000765 000024 00000026354 12160612467 014724 0ustar00davidstaff000000 000000 This software is Copyright (c) 2011 by David Golden. This is free software, licensed under: The Apache License, Version 2.0, January 2004 Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. Data-UUID-MT-1.001/Makefile.PL000644 000765 000024 00000003270 12160612467 015661 0ustar00davidstaff000000 000000 use strict; use warnings; use 5.006; use ExtUtils::MakeMaker 6.30; my %WriteMakefileArgs = ( "ABSTRACT" => "Fast random UUID generator using the Mersenne Twister algorithm", "AUTHOR" => "David Golden ", "BUILD_REQUIRES" => {}, "CONFIGURE_REQUIRES" => { "ExtUtils::MakeMaker" => "6.30" }, "DISTNAME" => "Data-UUID-MT", "EXE_FILES" => [], "LICENSE" => "apache", "NAME" => "Data::UUID::MT", "PREREQ_PM" => { "Config" => 0, "Math::Random::MT::Auto" => 0, "Scalar::Util" => "1.10", "Time::HiRes" => 0, "strict" => 0, "warnings" => 0 }, "TEST_REQUIRES" => { "ExtUtils::MakeMaker" => 0, "File::Find" => 0, "File::Spec::Functions" => 0, "File::Temp" => 0, "List::AllUtils" => 0, "List::Util" => 0, "Test::More" => "0.96" }, "VERSION" => "1.001", "test" => { "TESTS" => "t/*.t" } ); unless ( eval { ExtUtils::MakeMaker->VERSION(6.63_03) } ) { my $tr = delete $WriteMakefileArgs{TEST_REQUIRES}; my $br = $WriteMakefileArgs{BUILD_REQUIRES}; for my $mod ( keys %$tr ) { if ( exists $br->{$mod} ) { $br->{$mod} = $tr->{$mod} if $tr->{$mod} > $br->{$mod}; } else { $br->{$mod} = $tr->{$mod}; } } } unless ( eval { ExtUtils::MakeMaker->VERSION(6.56) } ) { my $br = delete $WriteMakefileArgs{BUILD_REQUIRES}; my $pp = $WriteMakefileArgs{PREREQ_PM}; for my $mod ( keys %$br ) { if ( exists $pp->{$mod} ) { $pp->{$mod} = $br->{$mod} if $br->{$mod} > $pp->{$mod}; } else { $pp->{$mod} = $br->{$mod}; } } } delete $WriteMakefileArgs{CONFIGURE_REQUIRES} unless eval { ExtUtils::MakeMaker->VERSION(6.52) }; WriteMakefile(%WriteMakefileArgs); Data-UUID-MT-1.001/MANIFEST000644 000765 000024 00000000615 12160612467 015040 0ustar00davidstaff000000 000000 CONTRIBUTING Changes LICENSE MANIFEST META.json META.yml Makefile.PL README dist.ini examples/bench.pl examples/versions.pl lib/Data/UUID/MT.pm perlcritic.rc t/00-compile.t t/00-report-prereqs.t t/uuid.t xt/author/critic.t xt/author/pod-spell.t xt/release/distmeta.t xt/release/minimum-version.t xt/release/pod-coverage.t xt/release/pod-syntax.t xt/release/portability.t xt/release/test-version.t Data-UUID-MT-1.001/META.json000644 000765 000024 00000004231 12160612467 015326 0ustar00davidstaff000000 000000 { "abstract" : "Fast random UUID generator using the Mersenne Twister algorithm", "author" : [ "David Golden " ], "dynamic_config" : 0, "generated_by" : "Dist::Zilla version 4.300034, CPAN::Meta::Converter version 2.131560", "license" : [ "apache_2_0" ], "meta-spec" : { "url" : "http://search.cpan.org/perldoc?CPAN::Meta::Spec", "version" : "2" }, "name" : "Data-UUID-MT", "no_index" : { "directory" : [ "t", "xt", "examples", "corpus" ], "package" : [ "DB" ] }, "prereqs" : { "configure" : { "requires" : { "ExtUtils::MakeMaker" : "6.30" } }, "develop" : { "requires" : { "Pod::Coverage::TrustPod" : "0", "Test::CPAN::Meta" : "0", "Test::Pod" : "1.41", "Test::Pod::Coverage" : "1.08" } }, "runtime" : { "requires" : { "Config" : "0", "Math::Random::MT::Auto" : "0", "Scalar::Util" : "1.10", "Time::HiRes" : "0", "perl" : "5.006", "strict" : "0", "warnings" : "0" } }, "test" : { "requires" : { "ExtUtils::MakeMaker" : "0", "File::Find" : "0", "File::Spec::Functions" : "0", "File::Temp" : "0", "List::AllUtils" : "0", "List::Util" : "0", "Test::More" : "0.96" } } }, "provides" : { "Data::UUID::MT" : { "file" : "lib/Data/UUID/MT.pm", "version" : "1.001" } }, "release_status" : "stable", "resources" : { "bugtracker" : { "web" : "https://github.com/dagolden/data-uuid-mt/issues" }, "homepage" : "https://metacpan.org/release/Data-UUID-MT", "repository" : { "type" : "git", "url" : "git://github.com/dagolden/data-uuid-mt.git", "web" : "https://github.com/dagolden/data-uuid-mt" } }, "version" : "1.001", "x_contributors" : [ "Matt Koscica " ] } Data-UUID-MT-1.001/META.yml000644 000765 000024 00000002125 12160612467 015156 0ustar00davidstaff000000 000000 --- abstract: 'Fast random UUID generator using the Mersenne Twister algorithm' author: - 'David Golden ' build_requires: ExtUtils::MakeMaker: 0 File::Find: 0 File::Spec::Functions: 0 File::Temp: 0 List::AllUtils: 0 List::Util: 0 Test::More: 0.96 configure_requires: ExtUtils::MakeMaker: 6.30 dynamic_config: 0 generated_by: 'Dist::Zilla version 4.300034, CPAN::Meta::Converter version 2.131560' license: apache meta-spec: url: http://module-build.sourceforge.net/META-spec-v1.4.html version: 1.4 name: Data-UUID-MT no_index: directory: - t - xt - examples - corpus package: - DB provides: Data::UUID::MT: file: lib/Data/UUID/MT.pm version: 1.001 requires: Config: 0 Math::Random::MT::Auto: 0 Scalar::Util: 1.10 Time::HiRes: 0 perl: 5.006 strict: 0 warnings: 0 resources: bugtracker: https://github.com/dagolden/data-uuid-mt/issues homepage: https://metacpan.org/release/Data-UUID-MT repository: git://github.com/dagolden/data-uuid-mt.git version: 1.001 x_contributors: - 'Matt Koscica ' Data-UUID-MT-1.001/perlcritic.rc000644 000765 000024 00000000744 12160612467 016400 0ustar00davidstaff000000 000000 severity = 5 verbose = 8 [Variables::ProhibitPunctuationVars] allow = $@ $! # Turn these off [-BuiltinFunctions::ProhibitStringyEval] [-ControlStructures::ProhibitPostfixControls] [-ControlStructures::ProhibitUnlessBlocks] [-Documentation::RequirePodSections] [-InputOutput::ProhibitInteractiveTest] [-References::ProhibitDoubleSigils] [-RegularExpressions::RequireExtendedFormatting] [-InputOutput::ProhibitTwoArgOpen] # Turn this on [Lax::ProhibitStringyEval::ExceptForRequire] Data-UUID-MT-1.001/README000644 000765 000024 00000024506 12160612467 014574 0ustar00davidstaff000000 000000 NAME Data::UUID::MT - Fast random UUID generator using the Mersenne Twister algorithm VERSION version 1.001 SYNOPSIS use Data::UUID::MT; my $ug1 = Data::UUID::MT->new( version => 4 ); # "1", "4" or "4s" my $ug2 = Data::UUID::MT->new(); # default is "4" # method interface my $uuid1 = $ug->create(); # 16 byte binary string my $uuid2 = $ug->create_hex(); my $uuid3 = $ug->create_string(); # iterator -- avoids some method call overhead my $next = $ug->iterator; my $uuid4 = $next->(); DESCRIPTION This UUID generator uses the excellent Math::Random::MT::Auto module as a source of fast, high-quality (pseudo) random numbers. Three different types of UUIDs are supported. Two are consistent with RFC 4122 and one is a custom variant that provides a 'sequential UUID' that can be advantageous when used as a primary database key. Note: The Mersenne Twister pseudo-random number generator has excellent statistical properties, but it is not considered cryptographically secure. Pseudo-random UUIDs are not recommended for use as security authentication tokens in cookies or other user-visible session identifiers. Version 1 UUIDs The UUID generally follows the "version 1" spec from the RFC, however the clock sequence and MAC address are randomly generated each time. (This is permissible within the spec of the RFC.) The generated MAC address has the the multicast bit set as mandated by the RFC to ensure it does not conflict with real MAC addresses. This UUID has 60 bits of timestamp data, 61 bits of pseudo-random data and 7 mandated bits (multicast bit, "variant" field and "version" field). Version 4 UUIDs The UUID follows the "version 4" spec, with 122 pseudo-random bits and 6 mandated bits ("variant" field and "version" field). Version 4s UUIDs This is a custom UUID form that resembles "version 4" form, but that overlays the first 60 bits with a timestamp akin to "version 1", Unlike "version 1", this custom version preserves the ordering of bits from high to low, whereas "version 1" puts the low 32 bits of the timestamp first, then the middle 16 bits, then multiplexes the high bits with version field. This "4s" variant provides a "sequential UUID" with the timestamp providing order and the remaining random bits making collision with other UUIDs created at the exact same microsecond highly unlikely. This UUID has 60 timestamp bits, 62 pseudo-random bits and 6 mandated bits ("variant" field and "version" field). Unsupported: Versions 2, 3 and 5 This module focuses on generation of UUIDs with random elements and does not support UUID versions 2, 3 and 5. METHODS new my $ug = Data::UUID::MT->new( version => 4 ); Creates a UUID generator object. The only allowed versions are "1", "4" and "4s". If no version is specified, it defaults to "4". create my $uuid = $ug->create; Returns a UUID packed into a 16 byte string. create_hex my $uuid = $ug->create_hex(); Returns a UUID as a lowercase hex string, prefixed with "0x", e.g. 0xb0470602a64b11da863293ebf1c0e05a create_string my $uuid = $ug->create_string(); # Returns UUID as a lowercase string in "standard" format, e.g. "b0470602-a64b-11da-8632-93ebf1c0e05a" iterator my $next = $ug->iterator; my $uuid = $next->(); Returns a reference to the internal UUID generator function. Because this avoids method call overhead, it is slightly faster than calling "create". reseed $ug->reseed; Reseeds the internal pseudo-random number generator. This happens automatically after a fork or thread creation (assuming Scalar::Util::weaken), but may be called manually if desired for some reason. Any arguments provided are passed to Math::Random::MT::Auto::srand() for custom seeding. $ug->reseed('hotbits' => 250, '/dev/random'); UUID STRING REPRESENTATIONS A UUID contains 16 bytes. A hex string representation looks like 0xb0470602a64b11da863293ebf1c0e05a. A "standard" representation looks like "b0470602-a64b-11da-8632-93ebf1c0e05a". Sometimes these are seen in upper case and on Windows the standard format is often seen wrapped in parentheses. Converting back and forth is easy with "pack" and "unpack". # string to 16 bytes $string =~ s/^0x//i; # remove leading "0x" $string =~ tr/()-//d; # strip '-' and parentheses $binary = pack("H*", $string); # 16 bytes to uppercase string formats $hex = "0x" . uc unpack("H*", $binary); $std = uc join "-", unpack("H8H4H4H4H12", $binary); If you need a module that provides these conversions for you, consider UUID::Tiny. COMPARISON TO OTHER UUID MODULES At the time of writing, there are five other general purpose UUID generators on CPAN that I consider potential alternatives. Data::UUID::MT is included in the discussion below for comparison. * Data::GUID - version 1 UUIDs (wrapper around Data::UUID) * Data::UUID - version 1 or 3 UUIDs (derived from RFC 4122 code) * Data::UUID::LibUUID - version 1 or 4 UUIDs (libuuid) * UUID - version 1 or 4 UUIDs (libuuid) * UUID::Tiny - versions 1, 3, 4, or 5 (pure perl) * Data::UUID::MT - version 1 or 4 (or custom sequential "4s") "libuuid" based UUIDs may generally be either version 4 (preferred) or version 1 (fallback), depending on the availability of a good random bit source (e.g. /dev/random). "libuuid" version 1 UUIDs could also be provided by the "uuidd" daemon if available. UUID.pm leaves the choice of version up to "libuuid". Data::UUID::LibUUID does so by default, but also allows specifying a specific version. Note that Data::UUID::LibUUID incorrectly refers to version 1 UUIDs as version 2 UUIDs. For example, to get a version 1 binary UUID explicitly, you would call Data::UUID::LibUUID::new_uuid_binary(2). In addition to differences mentioned below, there are additional slight difference in how the modules (or "libuuid") treat the "clock sequence" field and otherwise attempt to keep state between calls, but this is generally immaterial. Use of Ethernet MAC addresses Version 1 UUID generators differ in whether they include the Ethernet MAC address as a "node identifier" as specified in RFC 4122. Including the MAC has security implications as Version 1 UUIDs can then be traced to a particular machine at a particular time. For "libuuid" based modules, Version 1 UUIDs will include the actual MAC address, if available, or will substitute a random MAC (with multicast bit set). Data::UUID version 1 UUIDs do not contain the MAC address, but replace it with an MD5 hash of data including the hostname and host id (possibly just the IP address), modified with the multicast bit. Both UUID::Tiny and Data::UUID::MT version 1 UUIDs do not contain the actual MAC address, but replace it with a random multicast MAC address. Source of random bits All the modules differ in the source of random bits. "libuuid" based modules get random bits from "/dev/random" or "/dev/urandom" or fall back to a pseudo-random number generator. Data::UUID only uses random data to see the clock sequence and gets bits from the C "rand()" function. UUID::Tiny uses Perl's "rand()" function. Data::UUID::MT gets random bits from Math::Random::MT::Auto, which uses the Mersenne Twister algorithm. Math::Random::MT::Auto seeds from system sources (including Win32 specific ones on that platform) if available and falls back to other less ideal sources if not. Fork and thread safety Pseudo-random number generators used in generating UUIDs should be reseeded if the process forks or if threads are created. Data::UUID::MT checks if the process ID has changed before generating a UUID and reseeds if necessary. If Scalar::Util is installed and provides "weaken()", Data::UUID::MT will also reseed its objects on thread creation. Data::UUID::LibUUID will reseed on fork on Mac OSX. I have not explored further whether other UUID generators are fork/thread safe. Benchmarks The examples/bench.pl program included with this module does some simple benchmarking of UUID generation speeds. Here is the output from my desktop system (AMD Phenom II X6 1045T CPU). Note that "v?" is used where the choice is left to "libuuid" -- which will result in version 4 UUIDs on my system. Benchmark on Perl v5.14.0 for x86_64-linux with 8 byte integers. Key: U => UUID 0.02 UT => UUID::Tiny 1.03 DG => Data::GUID 0.046 DU => Data::UUID 1.217 DULU => Data::UUID::LibUUID 0.05 DUMT => Data::UUID::MT 0.001 Benchmarks are marked as to which UUID version is generated. Some modules offer method ('meth') and function ('func') interfaces. UT|v1 85229/s UT|v4 110652/s DULU|v1 177495/s DULU|v? 178629/s DUMT|v4s|meth 274905/s DUMT|v1|meth 281942/s U|v? 288136/s DULU|v4 295107/s DUMT|v4s|func 307575/s DUMT|v1|func 313538/s DG|v1|func 335333/s DG|v1|meth 373515/s DUMT|v4|meth 450845/s DUMT|v4|func 588573/s DU|v1 1312946/s SEE ALSO * RFC 4122 A Universally Unique IDentifier (UUID) URN Namespace SUPPORT Bugs / Feature Requests Please report any bugs or feature requests through the issue tracker at . You will be notified automatically of any progress on your issue. Source Code This is open source software. The code repository is available for public review and contribution under the terms of the license. git clone git://github.com/dagolden/data-uuid-mt.git AUTHOR David Golden CONTRIBUTOR Matt Koscica COPYRIGHT AND LICENSE This software is Copyright (c) 2011 by David Golden. This is free software, licensed under: The Apache License, Version 2.0, January 2004 Data-UUID-MT-1.001/t/000755 000765 000024 00000000000 12160612467 014150 5ustar00davidstaff000000 000000 Data-UUID-MT-1.001/xt/000755 000765 000024 00000000000 12160612467 014340 5ustar00davidstaff000000 000000 Data-UUID-MT-1.001/xt/author/000755 000765 000024 00000000000 12160612467 015642 5ustar00davidstaff000000 000000 Data-UUID-MT-1.001/xt/release/000755 000765 000024 00000000000 12160612467 015760 5ustar00davidstaff000000 000000 Data-UUID-MT-1.001/xt/release/distmeta.t000644 000765 000024 00000000217 12160612467 017757 0ustar00davidstaff000000 000000 #!perl use Test::More; eval "use Test::CPAN::Meta"; plan skip_all => "Test::CPAN::Meta required for testing META.yml" if $@; meta_yaml_ok(); Data-UUID-MT-1.001/xt/release/minimum-version.t000644 000765 000024 00000000266 12160612467 021307 0ustar00davidstaff000000 000000 #!perl use Test::More; eval "use Test::MinimumVersion"; plan skip_all => "Test::MinimumVersion required for testing minimum versions" if $@; all_minimum_version_ok( qq{5.010} ); Data-UUID-MT-1.001/xt/release/pod-coverage.t000644 000765 000024 00000000527 12160612467 020524 0ustar00davidstaff000000 000000 #!perl use Test::More; eval "use Test::Pod::Coverage 1.08"; plan skip_all => "Test::Pod::Coverage 1.08 required for testing POD coverage" if $@; eval "use Pod::Coverage::TrustPod"; plan skip_all => "Pod::Coverage::TrustPod required for testing POD coverage" if $@; all_pod_coverage_ok({ coverage_class => 'Pod::Coverage::TrustPod' }); Data-UUID-MT-1.001/xt/release/pod-syntax.t000644 000765 000024 00000000212 12160612467 020246 0ustar00davidstaff000000 000000 #!perl use Test::More; eval "use Test::Pod 1.41"; plan skip_all => "Test::Pod 1.41 required for testing POD" if $@; all_pod_files_ok(); Data-UUID-MT-1.001/xt/release/portability.t000644 000765 000024 00000000332 12160612467 020505 0ustar00davidstaff000000 000000 #!perl use strict; use warnings; use Test::More; eval 'use Test::Portability::Files'; plan skip_all => 'Test::Portability::Files required for testing portability' if $@; options(test_one_dot => 0); run_tests(); Data-UUID-MT-1.001/xt/release/test-version.t000644 000765 000024 00000000643 12160612467 020612 0ustar00davidstaff000000 000000 use strict; use warnings; use Test::More; # generated by Dist::Zilla::Plugin::Test::Version 0.002004 BEGIN { eval "use Test::Version; 1;" or die $@; } my @imports = ( 'version_all_ok' ); my $params = { is_strict => 0, has_version => 1, }; push @imports, $params if version->parse( $Test::Version::VERSION ) >= version->parse('1.002'); Test::Version->import(@imports); version_all_ok; done_testing; Data-UUID-MT-1.001/xt/author/critic.t000644 000765 000024 00000000435 12160612467 017306 0ustar00davidstaff000000 000000 #!perl use strict; use warnings; use Test::More; use English qw(-no_match_vars); eval "use Test::Perl::Critic"; plan skip_all => 'Test::Perl::Critic required to criticise code' if $@; Test::Perl::Critic->import( -profile => "perlcritic.rc" ) if -e "perlcritic.rc"; all_critic_ok(); Data-UUID-MT-1.001/xt/author/pod-spell.t000644 000765 000024 00000000631 12160612467 017726 0ustar00davidstaff000000 000000 use strict; use warnings; use Test::More; # generated by Dist::Zilla::Plugin::Test::PodSpelling 2.006000 eval "use Test::Spelling 0.12; use Pod::Wordlist::hanekomu; 1" or die $@; add_stopwords(); all_pod_files_spelling_ok( qw( bin lib ) ); __DATA__ AMD IDentifier Mersenne OSX UUID UUIDs benchmarking cryptographically libuuid multicast timestamp David Golden dagolden Matt Koscica matt lib Data MT Data-UUID-MT-1.001/t/00-compile.t000644 000765 000024 00000003100 12160612467 016174 0ustar00davidstaff000000 000000 #!perl use strict; use warnings; use Test::More; use File::Find; use File::Temp qw{ tempdir }; my @modules; find( sub { return if $File::Find::name !~ /\.pm\z/; my $found = $File::Find::name; $found =~ s{^lib/}{}; $found =~ s{[/\\]}{::}g; $found =~ s/\.pm$//; # nothing to skip push @modules, $found; }, 'lib', ); sub _find_scripts { my $dir = shift @_; my @found_scripts = (); find( sub { return unless -f; my $found = $File::Find::name; # nothing to skip open my $FH, '<', $_ or do { note( "Unable to open $found in ( $! ), skipping" ); return; }; my $shebang = <$FH>; return unless $shebang =~ /^#!.*?\bperl\b\s*$/; push @found_scripts, $found; }, $dir, ); return @found_scripts; } my @scripts; do { push @scripts, _find_scripts($_) if -d $_ } for qw{ bin script scripts }; my $plan = scalar(@modules) + scalar(@scripts); $plan ? (plan tests => $plan) : (plan skip_all => "no tests to run"); { # fake home for cpan-testers local $ENV{HOME} = tempdir( CLEANUP => 1 ); like( qx{ $^X -Ilib -e "require $_; print '$_ ok'" }, qr/^\s*$_ ok/s, "$_ loaded ok" ) for sort @modules; SKIP: { eval "use Test::Script 1.05; 1;"; skip "Test::Script needed to test script compilation", scalar(@scripts) if $@; foreach my $file ( @scripts ) { my $script = $file; $script =~ s!.*/!!; script_compiles( $file, "$script script compiles" ); } } } Data-UUID-MT-1.001/t/00-report-prereqs.t000644 000765 000024 00000003137 12160612467 017550 0ustar00davidstaff000000 000000 #!perl use strict; use warnings; use Test::More tests => 1; use ExtUtils::MakeMaker; use File::Spec::Functions; use List::Util qw/max/; my @modules = qw( Config ExtUtils::MakeMaker File::Find File::Spec::Functions File::Temp List::AllUtils List::Util Math::Random::MT::Auto Scalar::Util Test::More Time::HiRes perl strict warnings ); # replace modules with dynamic results from MYMETA.json if we can # (hide CPAN::Meta from prereq scanner) my $cpan_meta = "CPAN::Meta"; if ( -f "MYMETA.json" && eval "require $cpan_meta" ) { ## no critic if ( my $meta = eval { CPAN::Meta->load_file("MYMETA.json") } ) { my $prereqs = $meta->prereqs; delete $prereqs->{develop}; my %uniq = map {$_ => 1} map { keys %$_ } map { values %$_ } values %$prereqs; $uniq{$_} = 1 for @modules; # don't lose any static ones @modules = sort keys %uniq; } } my @reports = [qw/Version Module/]; for my $mod ( @modules ) { next if $mod eq 'perl'; my $file = $mod; $file =~ s{::}{/}g; $file .= ".pm"; my ($prefix) = grep { -e catfile($_, $file) } @INC; if ( $prefix ) { my $ver = MM->parse_version( catfile($prefix, $file) ); $ver = "undef" unless defined $ver; # Newer MM should do this anyway push @reports, [$ver, $mod]; } else { push @reports, ["missing", $mod]; } } if ( @reports ) { my $vl = max map { length $_->[0] } @reports; my $ml = max map { length $_->[1] } @reports; splice @reports, 1, 0, ["-" x $vl, "-" x $ml]; diag "Prerequisite Report:\n", map {sprintf(" %*s %*s\n",$vl,$_->[0],-$ml,$_->[1])} @reports; } pass; # vim: ts=2 sts=2 sw=2 et: Data-UUID-MT-1.001/t/uuid.t000644 000765 000024 00000005715 12160612467 015313 0ustar00davidstaff000000 000000 use 5.006; use strict; use warnings; use Test::More 0.96; use Data::UUID::MT; use List::AllUtils qw/uniq/; # for diagnostics #sub _as_string { # return uc join "-", unpack("H8H4H4H4H12", shift); #} my @cases = ( {}, { version => '1' }, { version => '4' }, { version => '4s' }, ); for my $c ( @cases ) { my $label = $c->{version} || '4 (default)'; subtest "version => $label" => sub { my $ug = Data::UUID::MT->new( %$c ); my $version = $c->{version} || "4"; my $uuid1= $ug->create; # structural test my $binary = unpack("B*", $uuid1); ok( defined $uuid1, "Created a UUID" ); is( length $uuid1, 16, "UUID is 16 byte string" ); is( substr($binary,64,2), "10", "variant field correct" ); is( substr($binary,48,4), substr(unpack("B8", chr(substr($version,0,1))),4,4), "version field correct" ); # uniqueness test my @uuids; push @uuids, $ug->create for 1 .. 10000; my @uniq = uniq @uuids; is( scalar @uniq, scalar @uuids, "Generated 10,000 unique UUIDs" ); # sequence test my @seq; if ( $version eq "1" ) { # version 1 is time-low, time-mid, time-high-and-version @seq = map { substr($_,6,2) . substr($_,4,2) . substr($_,0,3) } @uuids; } else { # version 4 should be random except for version bits # version 4s should be sequential in the first 64 bits (albeit with # the version bits 'frozen') @seq = map { substr($_,0,8) } @uuids; } my @sorted = sort @seq; if ( $version eq "4" ) { ok( join("",@seq) ne join("",@sorted), "UUIDs are not ordered for version $version" ); } else { ok( join("",@seq) eq join("",@sorted), "UUIDs are correctly ordered for version $version" ); } } } # output tests my $ug = Data::UUID::MT->new; my $hex = $ug->create_hex; my $str = $ug->create_string; my $h = "[0-9a-f]"; # lc is( length $hex, 34, "create_hex length correct"); like( $hex, qr/\A0x${h}{32}\z/, "create_hex format correct" ); is( length $str, 36, "create_hex length correct"); like( $str, qr/\A${h}{8}-${h}{4}-${h}{4}-${h}{4}-${h}{12}\z/, "create_hex format correct" ); # iterator test my $next = $ug->iterator; my $uuid = $next->(); my $binary = unpack("B*", $uuid); is ( length $uuid, 16, "iterator produces 16 byte value" ); is( substr($binary,64,2), "10", "variant field correct" ); is( substr($binary,48,4), substr(unpack("B8", chr(4)), 4, 4), "version field correct" ); # reseed test $ug->reseed(12345); my $first = $ug->create_string; $ug->reseed(12345); my $second = $ug->create_string; is( $first, $second, "got same UUIDs after reseeding with same values" ); $ug->reseed; my $third = $ug->create_string; isnt( $second, $third, "got different UUID after default reseeding" ); done_testing; # # This file is part of Data-UUID-MT # # This software is Copyright (c) 2011 by David Golden. # # This is free software, licensed under: # # The Apache License, Version 2.0, January 2004 # Data-UUID-MT-1.001/lib/Data/000755 000765 000024 00000000000 12160612467 015324 5ustar00davidstaff000000 000000 Data-UUID-MT-1.001/lib/Data/UUID/000755 000765 000024 00000000000 12160612467 016072 5ustar00davidstaff000000 000000 Data-UUID-MT-1.001/lib/Data/UUID/MT.pm000644 000765 000024 00000045773 12160612467 016770 0ustar00davidstaff000000 000000 use 5.006; use strict; use warnings; package Data::UUID::MT; our $VERSION = '1.001'; # VERSION use Config; use Math::Random::MT::Auto; use Scalar::Util 1.10 (); use Time::HiRes (); # track objects across threads for reseeding my ($can_weaken, @objects); $can_weaken = Scalar::Util->can('weaken'); sub CLONE { defined($_) && $_->reseed for @objects } # HoH: $builders{$Config{uvsize}}{$version} my %builders = ( '8' => { '1' => ($] ge 5.010 ? '_build_64bit_v1' : '_build_64bit_v1_old' ), '4' => ($] ge 5.010 ? '_build_64bit_v4' : '_build_64bit_v4_old' ), '4s' => ($] ge 5.010 ? '_build_64bit_v4s' : '_build_64bit_v4s_old'), }, '4' => { '1' => '_build_32bit_v1', '4' => '_build_32bit_v4', '4s' => '_build_32bit_v4s', } ); sub new { my ($class, %args) = @_; $args{version} = 4 unless defined $args{version}; Carp::croak "Unsupported UUID version '$args{version}'" unless $args{version} =~ /^(?:1|4|4s)$/; my $int_size = $Config{uvsize}; Carp::croak "Unsupported integer size '$int_size'" unless $int_size == 4 || $int_size == 8; my $prng = Math::Random::MT::Auto->new; my $self = { _prng => $prng, _version => $args{version}, }; bless $self, $class; $self->{_iterator} = $self->_build_iterator; if ($can_weaken) { push @objects, $self; Scalar::Util::weaken($objects[-1]); } return $self; } sub _build_iterator { my $self = shift; # get the iterator based on int size and UUID version my $int_size = $Config{uvsize}; my $builder = $builders{$int_size}{$self->{_version}}; return $self->$builder; } sub create { return shift->{_iterator}->(); } sub create_hex { return "0x" . unpack("H*", shift->{_iterator}->() ); } sub create_string { return join "-", unpack("H8H4H4H4H12", shift->{_iterator}->()); } sub iterator { return shift->{_iterator}; } sub reseed { my $self = shift; $self->{_prng}->srand(@_ ? @_ : ()); } #--------------------------------------------------------------------------# # UUID algorithm closure generators #--------------------------------------------------------------------------# sub _build_64bit_v1 { my $self = shift; my $gregorian_offset = 12219292800 * 10_000_000; my $prng = $self->{_prng}; my $pid = $$; return sub { if ($$ != $pid) { $prng->srand(); $pid = $$; } my ($sec,$usec) = Time::HiRes::gettimeofday(); my $raw_time = pack("Q>", $sec*10_000_000 + $usec*10 + $gregorian_offset); # UUID v1 shuffles the time bits around my $uuid = substr($raw_time,4,4) . substr($raw_time,2,2) . substr($raw_time,0,2) . pack("Q>", $prng->irand); vec($uuid, 87, 1) = 0x1; # force MAC multicast bit on per RFC vec($uuid, 13, 4) = 0x1; # set UUID version vec($uuid, 35, 2) = 0x2; # set UUID variant return $uuid; } } # For Perl < v5.10, can't use "Q>" in pack sub _build_64bit_v1_old { my $self = shift; my $gregorian_offset = 12219292800 * 10_000_000; my $prng = $self->{_prng}; my $pid = $$; return sub { if ($$ != $pid) { $prng->srand(); $pid = $$; } my ($sec,$usec) = Time::HiRes::gettimeofday(); my $time_sum = $sec*10_000_000 + $usec*10 + $gregorian_offset; my $raw_time = pack("N2", $time_sum >> 32, $time_sum ); # UUID v1 shuffles the time bits around my $irand = $prng->irand; my $uuid = substr($raw_time,4,4) . substr($raw_time,2,2) . substr($raw_time,0,2) . pack("N2", $irand >> 32, $irand); vec($uuid, 87, 1) = 0x1; # force MAC multicast bit on per RFC vec($uuid, 13, 4) = 0x1; # set UUID version vec($uuid, 35, 2) = 0x2; # set UUID variant return $uuid; } } sub _build_32bit_v1 { my $self = shift; my $prng = $self->{_prng}; my $pid = $$; return sub { if ($$ != $pid) { $prng->srand(); $pid = $$; } # Adapted from UUID::Tiny my $timestamp = Time::HiRes::time(); # hi = time mod (1000000 / 0x100000000) my $hi = int( $timestamp / 65536.0 / 512 * 78125 ); $timestamp -= $hi * 512.0 * 65536 / 78125; my $low = int( $timestamp * 10000000.0 + 0.5 ); # MAGIC offset: 01B2-1DD2-13814000 if ( $low < 0xec7ec000 ) { $low += 0x13814000; } else { $low -= 0xec7ec000; $hi++; } if ( $hi < 0x0e4de22e ) { $hi += 0x01b21dd2; } else { $hi -= 0x0e4de22e; # wrap around } # UUID v1 shuffles the time bits around my $uuid = pack( 'NnnNN', $low, $hi & 0xffff, ( $hi >> 16 ) & 0x0fff, $prng->irand, $prng->irand ); vec($uuid, 87, 1) = 0x1; # force MAC multicast bit on per RFC vec($uuid, 13, 4) = 0x1; # set UUID version vec($uuid, 35, 2) = 0x2; # set UUID variant return $uuid; } } sub _build_64bit_v4 { my $self = shift; my $prng = $self->{_prng}; my $pid = $$; return sub { if ($$ != $pid) { $prng->srand(); $pid = $$; } my $uuid = pack("Q>2", $prng->irand, $prng->irand); vec($uuid, 13, 4) = 0x4; # set UUID version vec($uuid, 35, 2) = 0x2; # set UUID variant return $uuid; } } # For Perl < v5.10, can't use "Q>" in pack sub _build_64bit_v4_old { my $self = shift; my $prng = $self->{_prng}; my $pid = $$; return sub { if ($$ != $pid) { $prng->srand(); $pid = $$; } my @irand = ($prng->irand, $prng->irand); my $uuid = pack("N4", $irand[0] >> 32, $irand[0], $irand[1] >> 32, $irand[1] ); vec($uuid, 13, 4) = 0x4; # set UUID version vec($uuid, 35, 2) = 0x2; # set UUID variant return $uuid; } } sub _build_32bit_v4 { my $self = shift; my $prng = $self->{_prng}; my $pid = $$; return sub { if ($$ != $pid) { $prng->srand(); $pid = $$; } my $uuid = pack("N4", $prng->irand, $prng->irand, $prng->irand, $prng->irand ); vec($uuid, 13, 4) = 0x4; # set UUID version vec($uuid, 35, 2) = 0x2; # set UUID variant return $uuid; } } # "4s" is custom "random" with sequential override based on # 100 nanosecond intervals since epoch sub _build_64bit_v4s { my $self = shift; my $prng = $self->{_prng}; my $pid = $$; return sub { if ($$ != $pid) { $prng->srand(); $pid = $$; } my ($sec,$usec) = Time::HiRes::gettimeofday(); my $uuid = pack("Q>2", $sec*10_000_000 + $usec*10, $prng->irand ); # rotate last timestamp bits to make room for version field vec($uuid, 14, 4) = vec($uuid, 15, 4); vec($uuid, 15, 4) = vec($uuid, 12, 4); vec($uuid, 12, 4) = vec($uuid, 13, 4); vec($uuid, 13, 4) = 0x4; # set UUID version vec($uuid, 35, 2) = 0x2; # set UUID variant return $uuid; } } # "4s" is custom "random" with sequential override based on # 100 nanosecond intervals since epoch # For Perl < v5.10, can't use "Q>" in pack sub _build_64bit_v4s_old { my $self = shift; my $prng = $self->{_prng}; my $pid = $$; return sub { if ($$ != $pid) { $prng->srand(); $pid = $$; } my ($sec,$usec) = Time::HiRes::gettimeofday(); my @parts = ($sec*10_000_000 + $usec*10, $prng->irand); my $uuid = pack("N4", $parts[0] >> 32, $parts[0], $parts[1] >> 32, $parts[1] ); # rotate last timestamp bits to make room for version field vec($uuid, 14, 4) = vec($uuid, 15, 4); vec($uuid, 15, 4) = vec($uuid, 12, 4); vec($uuid, 12, 4) = vec($uuid, 13, 4); vec($uuid, 13, 4) = 0x4; # set UUID version vec($uuid, 35, 2) = 0x2; # set UUID variant return $uuid; } } # "4s" is custom "random" with sequential override based on # 100 nanosecond intervals since epoch sub _build_32bit_v4s { my $self = shift; my $prng = $self->{_prng}; my $pid = $$; return sub { if ($$ != $pid) { $prng->srand(); $pid = $$; } # Adapted from UUID::Tiny my $timestamp = Time::HiRes::time(); # hi = time mod (1000000 / 0x100000000) my $hi = int( $timestamp / 65536.0 / 512 * 78125 ); $timestamp -= $hi * 512.0 * 65536 / 78125; my $low = int( $timestamp * 10000000.0 + 0.5 ); # MAGIC offset: 01B2-1DD2-13814000 if ( $low < 0xec7ec000 ) { $low += 0x13814000; } else { $low -= 0xec7ec000; $hi++; } if ( $hi < 0x0e4de22e ) { $hi += 0x01b21dd2; } else { $hi -= 0x0e4de22e; # wrap around } my $uuid = pack("N4", $hi, $low, $prng->irand, $prng->irand); # rotate last timestamp bits to make room for version field vec($uuid, 14, 4) = vec($uuid, 15, 4); vec($uuid, 15, 4) = vec($uuid, 12, 4); vec($uuid, 12, 4) = vec($uuid, 13, 4); vec($uuid, 13, 4) = 0x4; # set UUID version vec($uuid, 35, 2) = 0x2; # set UUID variant return $uuid; } } 1; # ABSTRACT: Fast random UUID generator using the Mersenne Twister algorithm # vim: ts=2 sts=2 sw=2 et: __END__ =pod =encoding utf-8 =head1 NAME Data::UUID::MT - Fast random UUID generator using the Mersenne Twister algorithm =head1 VERSION version 1.001 =head1 SYNOPSIS use Data::UUID::MT; my $ug1 = Data::UUID::MT->new( version => 4 ); # "1", "4" or "4s" my $ug2 = Data::UUID::MT->new(); # default is "4" # method interface my $uuid1 = $ug->create(); # 16 byte binary string my $uuid2 = $ug->create_hex(); my $uuid3 = $ug->create_string(); # iterator -- avoids some method call overhead my $next = $ug->iterator; my $uuid4 = $next->(); =head1 DESCRIPTION This UUID generator uses the excellent L module as a source of fast, high-quality (pseudo) random numbers. Three different types of UUIDs are supported. Two are consistent with RFC 4122 and one is a custom variant that provides a 'sequential UUID' that can be advantageous when used as a primary database key. B: The Mersenne Twister pseudo-random number generator has excellent statistical properties, but it is not considered cryptographically secure. Pseudo-random UUIDs are not recommended for use as security authentication tokens in cookies or other user-visible session identifiers. =head2 Version 1 UUIDs The UUID generally follows the "version 1" spec from the RFC, however the clock sequence and MAC address are randomly generated each time. (This is permissible within the spec of the RFC.) The generated MAC address has the the multicast bit set as mandated by the RFC to ensure it does not conflict with real MAC addresses. This UUID has 60 bits of timestamp data, 61 bits of pseudo-random data and 7 mandated bits (multicast bit, "variant" field and "version" field). =head2 Version 4 UUIDs The UUID follows the "version 4" spec, with 122 pseudo-random bits and 6 mandated bits ("variant" field and "version" field). =head2 Version 4s UUIDs This is a custom UUID form that resembles "version 4" form, but that overlays the first 60 bits with a timestamp akin to "version 1", Unlike "version 1", this custom version preserves the ordering of bits from high to low, whereas "version 1" puts the low 32 bits of the timestamp first, then the middle 16 bits, then multiplexes the high bits with version field. This "4s" variant provides a "sequential UUID" with the timestamp providing order and the remaining random bits making collision with other UUIDs created at the exact same microsecond highly unlikely. This UUID has 60 timestamp bits, 62 pseudo-random bits and 6 mandated bits ("variant" field and "version" field). =head2 Unsupported: Versions 2, 3 and 5 This module focuses on generation of UUIDs with random elements and does not support UUID versions 2, 3 and 5. =head1 METHODS =head2 new my $ug = Data::UUID::MT->new( version => 4 ); Creates a UUID generator object. The only allowed versions are "1", "4" and "4s". If no version is specified, it defaults to "4". =head2 create my $uuid = $ug->create; Returns a UUID packed into a 16 byte string. =head2 create_hex my $uuid = $ug->create_hex(); Returns a UUID as a lowercase hex string, prefixed with "0x", e.g. C<0xb0470602a64b11da863293ebf1c0e05a> =head2 create_string my $uuid = $ug->create_string(); # Returns UUID as a lowercase string in "standard" format, e.g. C =head2 iterator my $next = $ug->iterator; my $uuid = $next->(); Returns a reference to the internal UUID generator function. Because this avoids method call overhead, it is slightly faster than calling C. =head2 reseed $ug->reseed; Reseeds the internal pseudo-random number generator. This happens automatically after a fork or thread creation (assuming Scalar::Util::weaken), but may be called manually if desired for some reason. Any arguments provided are passed to Math::Random::MT::Auto::srand() for custom seeding. $ug->reseed('hotbits' => 250, '/dev/random'); =for Pod::Coverage method_names_here =head1 UUID STRING REPRESENTATIONS A UUID contains 16 bytes. A hex string representation looks like C<0xb0470602a64b11da863293ebf1c0e05a>. A "standard" representation looks like C. Sometimes these are seen in upper case and on Windows the standard format is often seen wrapped in parentheses. Converting back and forth is easy with C and C. # string to 16 bytes $string =~ s/^0x//i; # remove leading "0x" $string =~ tr/()-//d; # strip '-' and parentheses $binary = pack("H*", $string); # 16 bytes to uppercase string formats $hex = "0x" . uc unpack("H*", $binary); $std = uc join "-", unpack("H8H4H4H4H12", $binary); If you need a module that provides these conversions for you, consider L. =head1 COMPARISON TO OTHER UUID MODULES At the time of writing, there are five other general purpose UUID generators on CPAN that I consider potential alternatives. Data::UUID::MT is included in the discussion below for comparison. =over 4 =item * L - version 1 UUIDs (wrapper around Data::UUID) =item * L - version 1 or 3 UUIDs (derived from RFC 4122 code) =item * L - version 1 or 4 UUIDs (libuuid) =item * L - version 1 or 4 UUIDs (libuuid) =item * L - versions 1, 3, 4, or 5 (pure perl) =item * L - version 1 or 4 (or custom sequential "4s") =back C based UUIDs may generally be either version 4 (preferred) or version 1 (fallback), depending on the availability of a good random bit source (e.g. /dev/random). C version 1 UUIDs could also be provided by the C daemon if available. UUID.pm leaves the choice of version up to C. Data::UUID::LibUUID does so by default, but also allows specifying a specific version. Note that Data::UUID::LibUUID incorrectly refers to version 1 UUIDs as version 2 UUIDs. For example, to get a version 1 binary UUID explicitly, you would call C. In addition to differences mentioned below, there are additional slight difference in how the modules (or C) treat the "clock sequence" field and otherwise attempt to keep state between calls, but this is generally immaterial. =head2 Use of Ethernet MAC addresses Version 1 UUID generators differ in whether they include the Ethernet MAC address as a "node identifier" as specified in RFC 4122. Including the MAC has security implications as Version 1 UUIDs can then be traced to a particular machine at a particular time. For C based modules, Version 1 UUIDs will include the actual MAC address, if available, or will substitute a random MAC (with multicast bit set). Data::UUID version 1 UUIDs do not contain the MAC address, but replace it with an MD5 hash of data including the hostname and host id (possibly just the IP address), modified with the multicast bit. Both UUID::Tiny and Data::UUID::MT version 1 UUIDs do not contain the actual MAC address, but replace it with a random multicast MAC address. =head2 Source of random bits All the modules differ in the source of random bits. C based modules get random bits from C or C or fall back to a pseudo-random number generator. Data::UUID only uses random data to see the clock sequence and gets bits from the C C function. UUID::Tiny uses Perl's C function. Data::UUID::MT gets random bits from L, which uses the Mersenne Twister algorithm. Math::Random::MT::Auto seeds from system sources (including Win32 specific ones on that platform) if available and falls back to other less ideal sources if not. =head2 Fork and thread safety Pseudo-random number generators used in generating UUIDs should be reseeded if the process forks or if threads are created. Data::UUID::MT checks if the process ID has changed before generating a UUID and reseeds if necessary. If L is installed and provides C, Data::UUID::MT will also reseed its objects on thread creation. Data::UUID::LibUUID will reseed on fork on Mac OSX. I have not explored further whether other UUID generators are fork/thread safe. =head2 Benchmarks The F program included with this module does some simple benchmarking of UUID generation speeds. Here is the output from my desktop system (AMD Phenom II X6 1045T CPU). Note that "v?" is used where the choice is left to C -- which will result in version 4 UUIDs on my system. Benchmark on Perl v5.14.0 for x86_64-linux with 8 byte integers. Key: U => UUID 0.02 UT => UUID::Tiny 1.03 DG => Data::GUID 0.046 DU => Data::UUID 1.217 DULU => Data::UUID::LibUUID 0.05 DUMT => Data::UUID::MT 0.001 Benchmarks are marked as to which UUID version is generated. Some modules offer method ('meth') and function ('func') interfaces. UT|v1 85229/s UT|v4 110652/s DULU|v1 177495/s DULU|v? 178629/s DUMT|v4s|meth 274905/s DUMT|v1|meth 281942/s U|v? 288136/s DULU|v4 295107/s DUMT|v4s|func 307575/s DUMT|v1|func 313538/s DG|v1|func 335333/s DG|v1|meth 373515/s DUMT|v4|meth 450845/s DUMT|v4|func 588573/s DU|v1 1312946/s =head1 SEE ALSO =over 4 =item * L =back =for :stopwords cpan testmatrix url annocpan anno bugtracker rt cpants kwalitee diff irc mailto metadata placeholders metacpan =head1 SUPPORT =head2 Bugs / Feature Requests Please report any bugs or feature requests through the issue tracker at L. You will be notified automatically of any progress on your issue. =head2 Source Code This is open source software. The code repository is available for public review and contribution under the terms of the license. L git clone git://github.com/dagolden/data-uuid-mt.git =head1 AUTHOR David Golden =head1 CONTRIBUTOR Matt Koscica =head1 COPYRIGHT AND LICENSE This software is Copyright (c) 2011 by David Golden. This is free software, licensed under: The Apache License, Version 2.0, January 2004 =cut Data-UUID-MT-1.001/examples/bench.pl000644 000765 000024 00000005401 12160612467 017137 0ustar00davidstaff000000 000000 use 5.010; use warnings; use version; use Benchmark qw( cmpthese timethese :hireswallclock ); use Config; use UUID; use Data::GUID 'guid' => { -as => 'dg_guid' }; use Data::UUID::LibUUID; use Data::UUID::MT; use Data::UUID; use UUID::Tiny; use List::AllUtils qw/max/; my $ug1 = Data::UUID::MT->new( version => 1 ); my $next1 = $ug1->iterator; my $ug4 = Data::UUID::MT->new( version => 4 ); my $next4 = $ug4->iterator; my $ug4s = Data::UUID::MT->new( version => '4s' ); my $next4s = $ug4s->iterator; my $duuid = Data::UUID->new; my $version = version->new($])->normal; say "Benchmark on Perl $version for $Config{archname} with $Config{uvsize} byte integers.\n"; print << "HERE"; Key: U => UUID $UUID::VERSION UT => UUID::Tiny $UUID::Tiny::VERSION DG => Data::GUID $Data::GUID::VERSION DU => Data::UUID $Data::UUID::VERSION DULU => Data::UUID::LibUUID $Data::UUID::LibUUID::VERSION DUMT => Data::UUID::MT $Data::UUID::MT::VERSION Benchmarks are marked as to which UUID version is generated. Some modules offer method ('meth') and function ('func') interfaces. HERE my $count = -3; my $results = timethese( $count, { 'U|v?' => sub { UUID::generate(my $u) }, 'UT|v1' => sub { my $u = create_UUID() }, 'UT|v4' => sub { my $u = create_UUID(UUID_V4) }, 'DG|v1|meth' => sub { my $u = Data::GUID->guid; }, 'DG|v1|func' => sub { my $u = dg_guid(); }, 'DU|v1' => sub { my $u = $duuid->create_bin() }, 'DULU|v?' => sub { my $u = new_uuid_binary() }, 'DULU|v1' => sub { my $u = new_uuid_binary(2) }, # must be 2 for v1 'DULU|v4' => sub { my $u = new_uuid_binary(4) }, 'DUMT|v1|meth' => sub { my $u = $ug1->create }, 'DUMT|v1|func' => sub { my $u = $next1->() }, 'DUMT|v4|meth' => sub { my $u = $ug4->create }, 'DUMT|v4|func' => sub { my $u = $next4->() }, 'DUMT|v4s|meth' => sub { my $u = $ug4s->create }, 'DUMT|v4s|func' => sub { my $u = $next4s->() }, }, "none" ); ## Copied from Benchmark.pm # Flatten in to an array of arrays with the name as the first field my @vals = map{ [ $_, @{$results->{$_}} ] } keys %$results; for (@vals) { # The epsilon fudge here is to prevent div by 0. Since clock # resolutions are much larger, it's below the noise floor. my $elapsed = do { # if ($style eq 'nop') {$_->[4]+$_->[5]} # elsif ($style eq 'noc') {$_->[2]+$_->[3]} # else {$_->[2]+$_->[3]+$_->[4]+$_->[5]} $_->[2]+$_->[3]+$_->[4]+$_->[5] }; my $rate = $_->[6]/(($elapsed)+0.000000000000001); $_->[7] = $rate; } # Sort by rate @vals = sort { $a->[7] <=> $b->[7] } @vals; ## end copy my $width = max map { length( $_->[0]) } @vals; my $format = "\%${width}s \%8d/s\n"; printf($format, $_->[0], $_->[7]) for @vals; Data-UUID-MT-1.001/examples/versions.pl000644 000765 000024 00000000320 12160612467 017723 0ustar00davidstaff000000 000000 use 5.010; use warnings; use Data::UUID::MT; for my $v (qw/1 4 4s/) { my $ug = Data::UUID::MT->new( version => $v ); say "Version $v UUIDs:"; for ( 1 .. 5 ) { say " " . $ug->create_string; } }