PHP Classes

PHP GUID Generation: Generate a unique identifier with custom data

Recommend this page to a friend!
  Info   View files Example   View files View files (4)   DownloadInstall with Composer Download .zip   Reputation   Support forum   Blog    
Ratings Unique User Downloads Download Rankings
StarStarStarStar 72%Total: 193 All time: 8,534 This week: 177Up
Version License PHP version Categories
oz_guid 1.0GNU General Publi...5PHP 5, Text processing
Description 

Author

This class can generate a unique identifier with custom data.

It can take as parameters a definition of a schema for the characters of a GUID (Global Unique Identifier) string from given parameters, and a secret key.

The class can generate a unique identifier from some data values according to the schema fields and encodes them using the secret key.

It can also take a previously generated GUID and decode it to extract the original encoded data using the same key.

Innovation Award
PHP Programming Innovation award nominee
November 2016
Number 7


Prize: One ebook of choice by Packt
Global Unique Identifiers (GUID) are useful to assign unique keys to products or license numbers for instance.

Usually GUID values have no special meaning other than being unique.

This class can generate GUID values that not only can contain fields of meaningful information about the object they are assigned to, but it also uses encryption to store the field information using a secret key.

This way an application can use the same key to decrypt an extract the key information.

Manuel Lemos
Picture of Oleg Zorin
  Performance   Level  
Name: Oleg Zorin <contact>
Classes: 3 packages by
Country: Russian Federation Russian Federation
Age: 39
All time rank: 186255 in Russian Federation Russian Federation
Week rank: 91 Up8 in Russian Federation Russian Federation Up
Innovation award
Innovation award
Nominee: 1x

Example

<?php
   
require_once('guid.class.php');
   
   
/* make it short */
   
use OZ\GUID as GUID;
   
   
/* SCHEMA - data schema of your guid, see README or guid.class.php for more instructions */
   
$GUID_schema = array(
       
'type' => 2,
       
'field1' => 4,
       
'field2' => 4,
       
'field3' => 4,
       
'flags1' => 1,
       
'flags2' => 1
   
);
   
   
/* KEY - your secret md5() hash */
   
$GUID_key = '7116bfe60a4d7393b2151400ef3a67ea';
   
    if(
GUID::init($GUID_schema, $GUID_key)) {
       
/* some date */
       
$data = array(
           
'type' => 1,
           
'field1' => 123,
           
'field2' => 12,
           
'field3' => 1,
           
'flags1' => 0, /* 0000 */
           
'flags2' => 6 /* 0111 */
       
);
       
       
/* data > guid */
       
echo 'Data to GUID: <br/>';
       
$guid1 = GUID::code($data);
       
print_r($data);
        echo
' > ' . $guid1;
        echo
'<br/>';
       
$data['flags2'] = 5; /* 0110, change data for 1 BIN digit */
       
$guid2 = GUID::code($data);
       
print_r($data);
        echo
' > ' . $guid2;
        echo
'<br/>';
       
        echo
'<br/>';
        echo
'GUID to data: <br/>';
        echo
$guid1 . ' > ';
       
print_r(GUID::decode($guid1));
        echo
'<br/>';
        echo
$guid2 . ' > ';
       
print_r(GUID::decode($guid2));
        echo
'<br/>';

        echo
'<br/>';
        echo
'Fake GUID to data: <br/>';
       
$guid3 = '7a368ea2-2eeb-851a-1258-f5dd806f7a08'; /* change last HEX digit */
       
$data3 = GUID::decode($guid3);
        echo
$guid3 . ' > ';
       
print_r(empty($data3) ? 'false' : $data3);
        echo
'<br/>';
       
$guid3 = '7a368ea2-2eeb-851a-2258-f5dd806f7a09'; /* change one middle HEX digit */
       
$data3 = GUID::decode($guid3);
        echo
$guid3 . ' > ';
       
print_r(empty($data3) ? 'false' : $data3);
        echo
'<br/>';
       
$guid3 = '7a368ea2-2eec-851a-1258-f5dd806f7a09'; /* change one middle HEX digit */
       
$data3 = GUID::decode($guid3);
        echo
$guid3 . ' > ';
       
print_r(empty($data3) ? 'false' : $data3);
        echo
'<br/>';
    }
    else {
        echo
'GUID class is not init';
    }
   


Details

GUID

The class can generate a unique identifier from some data values according to the schema fields and encodes them using the secret key.

It can also take a previously generated GUID and decode it to extract the original encoded data using the same key.

GUID could store upto 8 bytes of positive numeric data.

PHP Tested: 5.6.19, 7.0.11

CONTENTS

1. CASES OF USAGE
2. PRINCIPLE OF GENERATION
	2.1. GUID code
	2.2. GUID decode
3. PUBLIC METHODS
	3.1. GUID::init()
	3.2. GUID::code()
	3.3. GUID::decode()

1. CASES OF USAGE

You can use it fo generate unique identifiers, which store some data.

It is useful for shards of huge database. It can be used as key field for sharding logic also.

Some times it's good idea to store some relations in GUID.

For example, schema of GUID (for more detail see section 3.1.):

<?php
  $schema = array(
    'type' => 2,
    'id_master' => 6,
    'id_second' => 6
  );
?>

GUID generation table:
Entity          type  id_master     id_second     Comment
----------------------------------------------------------------------------------------------------------------------------------
Comapny         1     <company_id>  0
User            2     <user_id>     <company_id>  If you decode user GUID, you'll get both userID and companyID.
Messages group  10    <user_id1>    <user_id2>    GUID of chat room. It's good idea to make user_id1 lower than user_id2.
Message         11    <message_id>  <user_id>     If you decode message GUID, you'll get both messageID and userID (author).

2. PRINCIPLE OF GENERATION

2.1. GUID code


  • Get an array of data.
  • Convert all values from DEC to HEX and normalize to its lengths (add zeros to make proper length)
  • Create data string (implode all normalized values to one string)
  • Normalize data string to length of 16 digits (add zeros to make proper length)
  • Get cehck sum of data string - CRC32 based on custom polynomials (gets from your secret key)
  • Add check sum to data string
  • Get hash - md5() of check sum
  • XOR data string and hash
  • Replace last 8 digits to check sum
  • Format string to guid like string

2.2. GUID decode

  • Get guid and format it to hexadecimal string
  • Get last 8 digits - check_sum_1
  • Get hash - md5() of check sum
  • XOR data string and hash
  • Get check sum (check_sum_2) and data string from XOR result
  • Check check_sum_1 and check_sum_2 and check sum of data string
  • Fill data array with values from data string
  • Conver all values from HEX to DEC

3. PUBLIC METHODS

GUID class is abstract, it provides 3 public and static methods.

3.1. GUID::init($schema, $key)

Initialization of class. Should be called before other methods.

$schema - associated array of data schema. Contains pairs 'filed => length':

filed - any valid associated array key.

lenght - int value, length of field in HEX digits:

1 - field could contain one HEX digit (0-15 DEC values);
2 - field could contain two HEX digit (0-255 DEC values);
...
Total length of all fields should be 16 digits (8 bytes).

$key - your secret key.

Examples of schemas:

<?php
  $schema = array(
    'type' => 2,       /type of GUID, contain values 0 - 255/
    'id_master' => 6,  /id field, contain values 0 - 16777215/
    'id_second' => 6   /id field, contain values 0 - 16777215/
  );
	
  $schema = array(
    'type' => 1,       /type of GUID, contain values 0 - 15/
    'id_1' => 4,       /id field, contain values 0 - 65535/
    'id_2' => 4,       /id field, contain values 0 - 65535/
    'id_3' => 4,       /id field, contain values 0 - 65535/
    'param' => 2,      /some param field, contain values 0 - 255/
    'flags' => 1       /flags field, contain 4 flags in dec 0 - 15 (0000, 0001, 0010, ..., 1111)/
  );
?>


3.2. GUID::code($data)

Generates GUID using $data.

$data - associated array. Keys of array equal to data schema. Values - exact value of the field.

If you skip some keys, they will get 0 values. If you use values lager than described in schema, they will be trancated. For examle:

<?php
  $schema = array(
    'type' => 1 /values from 0 - 15/
  );
?>
Type value 300 (HEX: 0x12C) will be trancated to 12 (HEX: 0xC).

Returns GUID or false (if class wasn't initialized or something very strange happend :D).


3.3. GUID::decode($guid)


Gets data from GUID.

$guid - GUID string or just hexadecimal string (length 32).

Returns data array or false (if class wasn't initialized or GUID fails with check sum).


  Files folder image Files  
File Role Description
Accessible without login Plain text file example.php Example Example
Plain text file guid.class.php Class OZ\GUID class
Accessible without login Plain text file LICENSE Lic. License GPL, 3.0
Accessible without login Plain text file README.md Doc. Documentation

 Version Control Unique User Downloads Download Rankings  
 100%
Total:193
This week:0
All time:8,534
This week:177Up
 User Ratings  
 
 All time
Utility:100%StarStarStarStarStarStar
Consistency:75%StarStarStarStar
Documentation:68%StarStarStarStar
Examples:81%StarStarStarStarStar
Tests:-
Videos:-
Overall:72%StarStarStarStar
Rank:173