devuri/uuid-generator

A PHP package to generate UUIDs more securely and efficiently

v0.0.1 2023-03-04 13:44 UTC

This package is auto-updated.

Last update: 2024-12-04 18:06:32 UTC


README

The UUIDGenerator class is a secure and efficient way to generate UUIDs (Universally Unique Identifiers) in PHP. It generates UUIDs according to the RFC 4122 standard, which ensures that the UUIDs are unique across time and space.

Installation

You can install the UUIDGenerator class using Composer. Run the following command in your terminal:

composer require devuri/uuid-generator

Alternatively, you can manually include the UUIDGenerator class in your PHP project.

Usage

To use the UUIDGenerator class, first import the class using the use keyword:

use Devuri\UUIDGenerator\UUIDGenerator;

Then, create a new instance of the UUIDGenerator class:

$uuidGenerator = new UUIDGenerator();

You can optionally specify the version and variant of the UUID you want to generate. The version determines the layout of the UUID, while the variant determines the encoding of the UUID. The default version is 4 (random), and the default variant is the RFC 4122 variant.

$uuidGenerator = new UUIDGenerator(1, 0x80);

To generate a new UUID, call the generateUUID() method:

$uuid = $uuidGenerator->generateUUID();

The generateUUID() method returns a string in the following format:

xxxxxxxx-xxxx-Mxxx-Nxxx-xxxxxxxxxxxx

Where:

  • **x** is a hexadecimal digit (0-9, a-f)
  • **M** is the version number (1-5)
  • N is the variant number (8, 9, a, b)

Example

Here's an example of how to use the UUIDGenerator class to generate a new UUID:

    use Devuri\UUIDGenerator\UUIDGenerator;
    
    $uuidGenerator = new UUIDGenerator();
    $uuid = $uuidGenerator->generateUUID();
    
    echo $uuid;

This code creates a new instance of the UUIDGenerator class, generates a new UUID, and then prints the UUID to the screen.

Security

The UUIDGenerator class uses a secure and efficient method to generate UUIDs. It generates random bytes using the random_bytes() function, which is a cryptographically secure random number generator. It then sets the version and variant bits according to the RFC 4122 standard.

License

The UUIDGenerator class is open-source software licensed under the MIT License. See the LICENSE file for more information.