Here's my final version of a GUIDv4 function (based on others work here) that should work on all platforms and gracefully fallback to less cryptographically secure version if others are not supported. The exact number of possibilities is about ²¹². There’s still a chance that a UUID could be duplicated but it’s tiny because of the high number of possible combinations. Every bit of the string is totally randomly generated. $uuid1 ->equals( $uuid4) // false // this method returns: // * int(0) if $uuid1 and $uuid4 are equal // * int > 0 if $uuid1 is greater than $uuid4 // * int compare( $uuid4) // e.g.Getting Started Introduction A simple tutorial Language Reference Basic syntax Types Variables Constants Expressions Operators Control Structures Functions Classes and Objects Namespaces Enumerations Errors Exceptions Fibers Generators Attributes References Explained Predefined Variables Predefined Exceptions Predefined Interfaces and Classes Context options and parameters Supported Protocols and Wrappers Security Introduction General considerations Installed as CGI binary Installed as an Apache module Session Security Filesystem Security Database Security Error Reporting User Submitted Data Hiding PHP Keeping Current Features HTTP authentication with PHP Cookies Sessions Dealing with XForms Handling file uploads Using remote files Connection handling Persistent Database Connections Command line usage Garbage Collection DTrace Dynamic Tracing Function Reference Affecting PHP's Behaviour Audio Formats Manipulation Authentication Services Command Line Specific Extensions Compression and Archive Extensions Cryptography Extensions Database Extensions Date and Time Related Extensions File System Related Extensions Human Language and Character Encoding Support Image Processing and Generation Mail Related Extensions Mathematical Extensions Non-Text MIME Output Process Control Extensions Other Basic Extensions Other Services Search Engine Extensions Server Specific Extensions Session Extensions Text Processing Variable and Type Related Extensions Web Services Windows Only Extensions XML Manipulation GUI Extensions Keyboard Shortcuts ? This help j Next menu item k Previous menu item g p Previous man page g n Next man page G Scroll to bottom g g Scroll to top g h Goto homepage g s Goto search This version uses a pseudo-random number generator to generate the UUID. $uuid ->getDateTime() // returns a \DateTimeImmutable instance // checking if a given value is valid as UUID $isValid = Uuid ::isValid( $uuid) // true or false // comparing UUIDs and checking for equality $uuid1 = Uuid ::v1() The -r option create a random UUID based mostly on random bits (this is the default). Using the -t option will create a UUID based on system time and ethernet address if available. $uuid instanceof UuidV4 // true // getting the UUID datetime (it's only available in certain UUID types) $uuid = Uuid ::v1() Generating UUID with uuidgen Simply executing uuidgen will generate a random UUID. $uuid instanceof NilUuid // false // checking the type of UUID use Symfony\ Component\ Uid\ UuidV4 checking if the UUID is null (note that the class is called // NilUuid instead of NullUuid to follow the UUID standard notation) $uuid = Uuid ::v4() If your UUID value is already generated in another format, use any of theįollowing methods to create a Uuid object from it: It's defined in $uuid = Uuid ::v6() // $uuid is an instance of Symfony\Component\Uid\UuidV6 It's lexicographically sortable // (like ULIDs) and contains a 60-bit timestamp and 63 extra unique bits. $uuid = Uuid ::v3( $namespace, $name) // $uuid is an instance of Symfony\Component\Uid\UuidV3 $uuid = Uuid ::v5( $namespace, $name) // $uuid is an instance of Symfony\Component\Uid\UuidV5 // the namespaces defined by RFC 4122 (see ) // are available as PHP constants and as string values $uuid = Uuid ::v3(Uuid ::NAMESPACE_DNS, $name) // same as: Uuid::v3('dns', $name) $uuid = Uuid ::v3(Uuid ::NAMESPACE_URL, $name) // same as: Uuid::v3('url', $name) $uuid = Uuid ::v3(Uuid ::NAMESPACE_OID, $name) // same as: Uuid::v3('oid', $name) $uuid = Uuid ::v3(Uuid ::NAMESPACE_X500, $name) // same as: Uuid::v3('x500', $name) // UUID type 6 is not part of the UUID standard. a Type 4 UUID) // and the name is an arbitrary string (e.g. Type 3 uses // MD5 hashes and Type 5 uses SHA-1. $uuid = Uuid ::v4() // $uuid is an instance of Symfony\Component\Uid\UuidV4 // UUID type 3 and 5 generate a UUID hashing the given namespace and name. $uuid = Uuid ::v1() // $uuid is an instance of Symfony\Component\Uid\UuidV1 // UUID type 4 generates a random UUID, so you don't have to pass any constructor argument. Both are obtained automatically, so you don't have to pass any constructor argument. UUID type 1 generates the UUID using the MAC address of your device and a timestamp.
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |