OXID eShop CE  4.9.5
 All Classes Files Functions Variables Pages
oxuniversallyuniqueidgenerator.php
Go to the documentation of this file.
1 <?php
2 
7 {
8 
13 
19  public function __construct(oxOpenSSLFunctionalityChecker $openSSLChecker = null)
20  {
21  if (is_null($openSSLChecker)) {
22  $openSSLChecker = oxNew('oxOpenSSLFunctionalityChecker');
23  }
24  $this->_openSSLChecker = $openSSLChecker;
25  }
26 
32  public function generate()
33  {
34  $sSeed = $this->generateV4();
35 
36  return $this->generateV5($sSeed, php_uname('n'));
37  }
38 
44  public function generateV4()
45  {
46  if ($this->_getOpenSSLChecker()->isOpenSslRandomBytesGeneratorAvailable()) {
47  $sUUID = $this->_generateBasedOnOpenSSL();
48  } else {
49  $sUUID = $this->_generateBasedOnMtRand();
50  }
51 
52  return $sUUID;
53  }
54 
63  public function generateV5($sSeed, $sSalt)
64  {
65  $sSeed = str_replace(array('-', '{', '}'), '', $sSeed);
66  $sBinarySeed = '';
67  for ($i = 0; $i < strlen($sSeed); $i += 2) {
68  $sBinarySeed .= chr(hexdec($sSeed[$i] . $sSeed[$i + 1]));
69  }
70  $sHash = sha1($sBinarySeed . $sSalt);
71  $sUUID = sprintf(
72  '%08s-%04s-%04x-%04x-%12s',
73  substr($sHash, 0, 8), substr($sHash, 8, 4),
74  (hexdec(substr($sHash, 12, 4)) & 0x0fff) | 0x3000,
75  (hexdec(substr($sHash, 16, 4)) & 0x3fff) | 0x8000,
76  substr($sHash, 20, 12)
77  );
78 
79  return $sUUID;
80  }
81 
87  protected function _getOpenSSLChecker()
88  {
90  }
91 
97  protected function _generateBasedOnOpenSSL()
98  {
99  $sRandomData = openssl_random_pseudo_bytes(16);
100  $sRandomData[6] = chr(ord($sRandomData[6]) & 0x0f | 0x40); // set version to 0100
101  $sRandomData[8] = chr(ord($sRandomData[8]) & 0x3f | 0x80); // set bits 6-7 to 10
102 
103  return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($sRandomData), 4));
104  }
105 
111  protected function _generateBasedOnMtRand()
112  {
113  return sprintf(
114  '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
115  mt_rand(0, 0xffff), mt_rand(0, 0xffff),
116  mt_rand(0, 0xffff),
117  mt_rand(0, 0x0fff) | 0x4000,
118  mt_rand(0, 0x3fff) | 0x8000,
119  mt_rand(0, 0xffff), mt_rand(0, 0xffff), mt_rand(0, 0xffff)
120  );
121  }
122 }