Attention
RFC 4122 states, "If backward compatibility is not an issue, SHA-1 is preferred." As a result, the use of version 5 UUIDs
<rfc4122.version5>
is preferred over version 3 UUIDs, unless you have a specific use-case for version 3 UUIDs.
Note
To learn about name-based UUIDs, read the section rfc4122.version5
. Version 3 UUIDs behave exactly the same as version 5 UUIDs
<rfc4122.version5>
. The only difference is the hashing algorithm used to generate the UUID.
Version 3 UUIDs use MD5 as the hashing algorithm for combining the namespace and the name.
Due to the use of a different hashing algorithm, version 3 UUIDs generated with any given namespace and name will differ from version 5 UUIDs generated using the same namespace and name.
As an example, let's take a look at generating a version 3 UUID using the same namespace and name used in "rfc4122.version5.url-example
."
use Ramsey\Uuid\Uuid;
$uuid = Uuid::uuid3(Uuid::NAMESPACE_URL, 'https://www.php.net');
Even though the namespace and name are the same, the version 3 UUID generated will always be 3f703955-aaba-3e70-a3cb-baff6aa3b28f
.
Likewise, we can use the custom namespace we created in "rfc4122.version5.create-namespace
" to generate a version 3 UUID, but the result will be different from the version 5 UUID with the same custom namespace and name.
use Ramsey\Uuid\Uuid;
const WIDGET_NAMESPACE = '4bdbe8ec-5cb5-11ea-bc55-0242ac130003';
$uuid = Uuid::uuid3(WIDGET_NAMESPACE, 'widget/1234567890');
With this custom namespace, the version 3 UUID for the name "widget/1234567890" will always be 53564aa3-4154-3ca5-ac90-dba59dc7d3cb
.
Tip
Version 3 UUIDs generated in ramsey/uuid are instances of UuidV3. Check out the :phpRamsey\\Uuid\\Rfc4122\\UuidV3
API documentation to learn more about what you can do with a UuidV3 instance.