| 148 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
namespace Faker\Provider;
|
|
|
4 |
|
|
|
5 |
class Uuid extends Base
|
|
|
6 |
{
|
|
|
7 |
/**
|
|
|
8 |
* Generate name based md5 UUID (version 3).
|
|
|
9 |
*
|
|
|
10 |
* @example '7e57d004-2b97-0e7a-b45f-5387367791cd'
|
|
|
11 |
*
|
|
|
12 |
* @return string
|
|
|
13 |
*/
|
|
|
14 |
public static function uuid()
|
|
|
15 |
{
|
|
|
16 |
// fix for compatibility with 32bit architecture; each mt_rand call is restricted to 32bit
|
|
|
17 |
// two such calls will cause 64bits of randomness regardless of architecture
|
|
|
18 |
$seed = self::numberBetween(0, 2147483647) . '#' . self::numberBetween(0, 2147483647);
|
|
|
19 |
|
|
|
20 |
// Hash the seed and convert to a byte array
|
|
|
21 |
$val = md5($seed, true);
|
|
|
22 |
$byte = array_values(unpack('C16', $val));
|
|
|
23 |
|
|
|
24 |
// extract fields from byte array
|
|
|
25 |
$tLo = ($byte[0] << 24) | ($byte[1] << 16) | ($byte[2] << 8) | $byte[3];
|
|
|
26 |
$tMi = ($byte[4] << 8) | $byte[5];
|
|
|
27 |
$tHi = ($byte[6] << 8) | $byte[7];
|
|
|
28 |
$csLo = $byte[9];
|
|
|
29 |
$csHi = $byte[8] & 0x3f | (1 << 7);
|
|
|
30 |
|
|
|
31 |
// correct byte order for big edian architecture
|
|
|
32 |
if (pack('L', 0x6162797A) == pack('N', 0x6162797A)) {
|
|
|
33 |
$tLo = (($tLo & 0x000000ff) << 24) | (($tLo & 0x0000ff00) << 8)
|
|
|
34 |
| (($tLo & 0x00ff0000) >> 8) | (($tLo & 0xff000000) >> 24);
|
|
|
35 |
$tMi = (($tMi & 0x00ff) << 8) | (($tMi & 0xff00) >> 8);
|
|
|
36 |
$tHi = (($tHi & 0x00ff) << 8) | (($tHi & 0xff00) >> 8);
|
|
|
37 |
}
|
|
|
38 |
|
|
|
39 |
// apply version number
|
|
|
40 |
$tHi &= 0x0fff;
|
|
|
41 |
$tHi |= (3 << 12);
|
|
|
42 |
|
|
|
43 |
// cast to string
|
|
|
44 |
return sprintf(
|
|
|
45 |
'%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x',
|
|
|
46 |
$tLo,
|
|
|
47 |
$tMi,
|
|
|
48 |
$tHi,
|
|
|
49 |
$csHi,
|
|
|
50 |
$csLo,
|
|
|
51 |
$byte[10],
|
|
|
52 |
$byte[11],
|
|
|
53 |
$byte[12],
|
|
|
54 |
$byte[13],
|
|
|
55 |
$byte[14],
|
|
|
56 |
$byte[15],
|
|
|
57 |
);
|
|
|
58 |
}
|
|
|
59 |
}
|