ramsey/uuid - validate a name-based hashed has the name that we assign it to the uuid

1.5k views Asked by At

Any reliable uuid package from Packagist that I can download and use it for Slim framework?

Ideally,I would an uuid that I can hash a type, e.g. php, into the string. then i can check if that uuid has 'php'.

EDIT:

How can I validate a name-based hashed has the name that we assign it to the uuid?

// Generate a version 3 (name-based and hashed with MD5) UUID object
$uuid3 = Uuid::uuid3(Uuid::NAMESPACE_DNS, 'php.net');
$uuid = $uuid3->toString();

For instance:

if ($uuid3->getName($uuid) === 'php.net') {
// do something
}

Is this possible?

1

There are 1 answers

3
Rob Allen On BEST ANSWER

The best UUID package is https://github.com/ramsey/uuid

Just use composer require ramsey/uuid to install it.

Ideally,I would an uuid that I can hash a type, e.g. php, into the string. then i can check if that uuid has 'php'.

I don't understand this. UUIDs create a unique string. From Wikipedia:

A universally unique identifier (UUID) is a 128-bit number used to identify information in computer systems. The term globally unique identifier (GUID) is also used.

When generated according to the standard methods, UUIDs are for practical purposes unique, without depending for their uniqueness on a central registration authority or coordination between the parties generating them

Edit.

To answer your new question…

You can't reverse a hash, so you need to hash again and compare.

$uuid = Uuid::uuid3(Uuid::NAMESPACE_DNS, 'php.net')->toString();

$domainFromUser = 'example.com';
$userUuid = Uuid::uuid3(Uuid::NAMESPACE_DNS, $domainFromUser)->toString();

if ($uuid === $userUuid) {
    // user provided the right domain
}