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