version3.rst 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. .. _rfc4122.version3:
  2. ===========================
  3. Version 3: Name-based (MD5)
  4. ===========================
  5. .. attention::
  6. `RFC 4122`_ states, "If backward compatibility is not an issue, SHA-1 is
  7. preferred." As a result, the use of :ref:`version 5 UUIDs
  8. <rfc4122.version5>` is preferred over version 3 UUIDs, unless you have a
  9. specific use-case for version 3 UUIDs.
  10. .. note::
  11. To learn about name-based UUIDs, read the section :ref:`rfc4122.version5`.
  12. Version 3 UUIDs behave exactly the same as :ref:`version 5 UUIDs
  13. <rfc4122.version5>`. The only difference is the hashing algorithm used to
  14. generate the UUID.
  15. Version 3 UUIDs use `MD5`_ as the hashing algorithm for combining
  16. the namespace and the name.
  17. Due to the use of a different hashing algorithm, version 3 UUIDs generated with
  18. any given namespace and name will differ from version 5 UUIDs generated using
  19. the same namespace and name.
  20. As an example, let's take a look at generating a version 3 UUID using the same
  21. namespace and name used in ":ref:`rfc4122.version5.url-example`."
  22. .. code-block:: php
  23. :caption: Generate a version 3, name-based UUID for a URL
  24. :name: rfc4122.version3.url-example
  25. use Ramsey\Uuid\Uuid;
  26. $uuid = Uuid::uuid3(Uuid::NAMESPACE_URL, 'https://www.php.net');
  27. Even though the namespace and name are the same, the version 3 UUID generated
  28. will always be ``3f703955-aaba-3e70-a3cb-baff6aa3b28f``.
  29. Likewise, we can use the custom namespace we created in
  30. ":ref:`rfc4122.version5.create-namespace`" to generate a version 3 UUID, but the
  31. result will be different from the version 5 UUID with the same custom namespace
  32. and name.
  33. .. code-block:: php
  34. :caption: Use a custom namespace to create version 3, name-based UUIDs
  35. :name: rfc4122.version3.custom-example
  36. use Ramsey\Uuid\Uuid;
  37. const WIDGET_NAMESPACE = '4bdbe8ec-5cb5-11ea-bc55-0242ac130003';
  38. $uuid = Uuid::uuid3(WIDGET_NAMESPACE, 'widget/1234567890');
  39. With this custom namespace, the version 3 UUID for the name "widget/1234567890"
  40. will always be ``53564aa3-4154-3ca5-ac90-dba59dc7d3cb``.
  41. .. tip::
  42. Version 3 UUIDs generated in ramsey/uuid are instances of UuidV3. Check out
  43. the :php:class:`Ramsey\\Uuid\\Rfc4122\\UuidV3` API documentation to learn
  44. more about what you can do with a UuidV3 instance.
  45. .. _RFC 4122: https://tools.ietf.org/html/rfc4122
  46. .. _MD5: https://en.wikipedia.org/wiki/MD5