UUID_v5.php
42 lines
| 1 | <?php |
| 2 | |
| 3 | namespace OTGS\Installer\UUID; |
| 4 | |
| 5 | class UUID_v5 { |
| 6 | |
| 7 | |
| 8 | /** |
| 9 | * RFC 4122 compliant UUIDs. |
| 10 | * |
| 11 | * The RFC 4122 specification defines a Uniform Resource Name namespace for |
| 12 | * UUIDs (Universally Unique Identifier), also known as GUIDs (Globally |
| 13 | * Unique Identifier). A UUID is 128 bits long, and requires no central |
| 14 | * registration process. |
| 15 | * |
| 16 | * @package UUID |
| 17 | * @license https://www.gnu.org/licenses/gpl-2.0.txt GPLv2 |
| 18 | * @author bjornjohansen |
| 19 | * @see https://bjornjohansen.no/uuid-as-wordpress-guid |
| 20 | * |
| 21 | * RFC 4122 compliant UUID version 5. |
| 22 | * |
| 23 | * @param string $name The name to generate the UUID from. |
| 24 | * @param string $ns_uuid Namespace UUID. Default is for the NS when name string is a URL. |
| 25 | * |
| 26 | * @return string The UUID string. |
| 27 | */ |
| 28 | public static function generate( $name, $namespace ) { |
| 29 | $hash = sha1( $namespace . $name ); |
| 30 | |
| 31 | $octets = str_split( substr( $hash, 0, 16 ), 1 ); |
| 32 | |
| 33 | $octets[6] = chr( ord( $octets[6] ) & 0x0f | 0x50 ); |
| 34 | |
| 35 | $octets[8] = chr( ord( $octets[8] ) & 0x3f | 0x80 ); |
| 36 | |
| 37 | $octets = array_map( 'bin2hex', $octets ); |
| 38 | |
| 39 | return vsprintf( '%s%s-%s-%s-%s-%s%s%s', str_split( implode( '', $octets ), 4 ) ); |
| 40 | } |
| 41 | } |
| 42 |