Funkcja PHP do generowania UUID v4

Więc trochę poszperałem i próbowałem poskładać funkcję, która generuje poprawny UUID v4 w PHP. To jest najbliżej, kiedy byłem w stanie przyjść. Moja wiedza z hex, decimal, binary, operatory bitowe PHP i tym podobne jest prawie nieistniejąca. Ta funkcja generuje poprawny UUID v4 do jednego obszaru. Uuid v4 powinien mieć postać:

Xxxxxxxx-xxxx-4xxx-y xxx-xxxxxxxxxx

Gdzie y jest 8, 9, A, lub B. tutaj funkcje zawodzą, ponieważ do tego nie przylegają.

Miałem nadzieję, że ktoś z większą wiedzą ode mnie w tej dziedzinie może pomóc mi naprawić tę funkcję, aby przestrzegała tej zasady.

Funkcja jest następująca:

<?php

function gen_uuid() {
 $uuid = array(
  'time_low'  => 0,
  'time_mid'  => 0,
  'time_hi'  => 0,
  'clock_seq_hi' => 0,
  'clock_seq_low' => 0,
  'node'   => array()
 );

 $uuid['time_low'] = mt_rand(0, 0xffff) + (mt_rand(0, 0xffff) << 16);
 $uuid['time_mid'] = mt_rand(0, 0xffff);
 $uuid['time_hi'] = (4 << 12) | (mt_rand(0, 0x1000));
 $uuid['clock_seq_hi'] = (1 << 7) | (mt_rand(0, 128));
 $uuid['clock_seq_low'] = mt_rand(0, 255);

 for ($i = 0; $i < 6; $i++) {
  $uuid['node'][$i] = mt_rand(0, 255);
 }

 $uuid = sprintf('%08x-%04x-%04x-%02x%02x-%02x%02x%02x%02x%02x%02x',
  $uuid['time_low'],
  $uuid['time_mid'],
  $uuid['time_hi'],
  $uuid['clock_seq_hi'],
  $uuid['clock_seq_low'],
  $uuid['node'][0],
  $uuid['node'][1],
  $uuid['node'][2],
  $uuid['node'][3],
  $uuid['node'][4],
  $uuid['node'][5]
 );

 return $uuid;
}

?>
Dzięki wszystkim, którzy mogą mi pomóc.
Author: WGH, 2010-01-11

10 answers

Zaczerpnięte z tego komentarza do podręcznika PHP, możesz użyć tego:

function gen_uuid() {
    return sprintf( '%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
        // 32 bits for "time_low"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ),

        // 16 bits for "time_mid"
        mt_rand( 0, 0xffff ),

        // 16 bits for "time_hi_and_version",
        // four most significant bits holds version number 4
        mt_rand( 0, 0x0fff ) | 0x4000,

        // 16 bits, 8 bits for "clk_seq_hi_res",
        // 8 bits for "clk_seq_low",
        // two most significant bits holds zero and one for variant DCE1.1
        mt_rand( 0, 0x3fff ) | 0x8000,

        // 48 bits for "node"
        mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff ), mt_rand( 0, 0xffff )
    );
}
 230
Author: William,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-01-11 06:27:39

Zamiast dzielić je na poszczególne pola, łatwiej jest wygenerować losowy blok danych i zmienić poszczególne pozycje bajtów. Powinieneś również użyć lepszego generatora liczb losowych niż mt_rand ().

Zgodnie z RFC 4122-sekcja 4.4, musisz zmienić te pola:

  1. time_hi_and_version (bity 4-7 z 7 oktetu),
  2. clock_seq_hi_and_reserved (bit 6 i 7 z 9 oktetu)

Wszystkie pozostałe 122 bity powinny być wystarczająco losowe.

The następujące podejście generuje 128 bitów losowych danych za pomocą openssl_random_pseudo_bytes(), tworzy permutacje na oktetach, a następnie używa bin2hex() oraz vsprintf() aby wykonać ostateczne formatowanie.

function guidv4($data)
{
    assert(strlen($data) == 16);

    $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
    $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10

    return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}

echo guidv4(openssl_random_pseudo_bytes(16));

W PHP 7 generowanie losowych sekwencji bajtów jest jeszcze prostsze przy użyciu random_bytes():

echo guidv4(random_bytes(16));
 292
Author: Ja͢ck,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-07-11 04:40:45

Każdy, kto używa composer zależności, może warto rozważyć tę bibliotekę: https://github.com/ramsey/uuid

It doesn ' t get any easier than this:

Uuid::uuid4();
 91
Author: djule5,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2014-07-16 16:23:25

W systemach uniksowych użyj jądra systemu do wygenerowania identyfikatora uuid.

file_get_contents('/proc/sys/kernel/random/uuid')
/ Align = "center" bgcolor = "# E0ffe0 " / Cesarz Chin / / align = center / https://serverfault.com/a/529319/210994

Uwaga!: Użycie tej metody do uzyskania uuid w rzeczywistości wyczerpuje pulę entropii, bardzo szybko! Unikałbym używania tego, gdzie często by się to nazywało.

 15
Author: ThorSummoner,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-07-06 20:49:36

W moim poszukiwaniu tworzenia uuid v4, przyszedłem pierwszy na tę stronę, a następnie znalazłem to na http://php.net/manual/en/function.com-create-guid.php

function guidv4()
{
    if (function_exists('com_create_guid') === true)
        return trim(com_create_guid(), '{}');

    $data = openssl_random_pseudo_bytes(16);
    $data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
    $data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
    return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}

Kredyt: pavel.volyntsev

Edit: aby wyjaśnić, ta funkcja zawsze da ci uuid v4 (PHP > = 5.3.0).

Gdy funkcja com_create_guid jest dostępna (zwykle tylko w systemie Windows), użyje jej i usunie nawiasy klamrowe.

Jeśli nie będzie obecny (Linux), to powróci na ten silny losowy funkcja openssl_random_pseudo_bytes, następnie użyje vsprintf do sformatowania jej do v4 uuid.

 6
Author: Arie,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2018-05-11 14:20:36

Moja odpowiedź opiera się na komentarzu uniqid user comment ale używa funkcji openssl_random_pseudo_bytes do generowania losowego ciągu znaków zamiast odczytu z /dev/urandom

function guid()
{
    $randomString = openssl_random_pseudo_bytes(16);
    $time_low = bin2hex(substr($randomString, 0, 4));
    $time_mid = bin2hex(substr($randomString, 4, 2));
    $time_hi_and_version = bin2hex(substr($randomString, 6, 2));
    $clock_seq_hi_and_reserved = bin2hex(substr($randomString, 8, 2));
    $node = bin2hex(substr($randomString, 10, 6));

    /**
     * Set the four most significant bits (bits 12 through 15) of the
     * time_hi_and_version field to the 4-bit version number from
     * Section 4.1.3.
     * @see http://tools.ietf.org/html/rfc4122#section-4.1.3
    */
    $time_hi_and_version = hexdec($time_hi_and_version);
    $time_hi_and_version = $time_hi_and_version >> 4;
    $time_hi_and_version = $time_hi_and_version | 0x4000;

    /**
     * Set the two most significant bits (bits 6 and 7) of the
     * clock_seq_hi_and_reserved to zero and one, respectively.
     */
    $clock_seq_hi_and_reserved = hexdec($clock_seq_hi_and_reserved);
    $clock_seq_hi_and_reserved = $clock_seq_hi_and_reserved >> 2;
    $clock_seq_hi_and_reserved = $clock_seq_hi_and_reserved | 0x8000;

    return sprintf('%08s-%04s-%04x-%04x-%012s', $time_low, $time_mid, $time_hi_and_version, $clock_seq_hi_and_reserved, $node);
} // guid
 5
Author: Victor Smirnov,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-04-08 08:40:46

Zainspirowany przez broofa 's odpowiedź TUTAJ .

preg_replace_callback('/[xy]/', function ($matches)
{
  return dechex('x' == $matches[0] ? mt_rand(0, 15) : (mt_rand(0, 15) & 0x3 | 0x8));
}
, 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx');

Lub jeśli nie można użyć funkcji anonimowych.

preg_replace_callback('/[xy]/', create_function(
  '$matches',
  'return dechex("x" == $matches[0] ? mt_rand(0, 15) : (mt_rand(0, 15) & 0x3 | 0x8));'
)
, 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx');
 4
Author: MichaelRushton,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-23 12:34:45

Jeśli używasz CakePHP możesz użyć ich metody CakeText::uuid(); z klasy CakeText do wygenerowania uuid RFC4122.

 2
Author: bish,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-09-07 18:33:20

From tom, on http://www.php.net/manual/en/function.uniqid.php

$r = unpack('v*', fread(fopen('/dev/random', 'r'),16));
$uuid = sprintf('%04x%04x-%04x-%04x-%04x-%04x%04x%04x',
    $r[1], $r[2], $r[3], $r[4] & 0x0fff | 0x4000,
    $r[5] & 0x3fff | 0x8000, $r[6], $r[7], $r[8])
 1
Author: amgine,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2012-08-22 18:12:50

Jak o użyciu mysql do generowania uuid dla Ciebie?

$conn = new mysqli($servername, $username, $password, $dbname, $port);

$query = 'SELECT UUID()';
echo $conn->query($query)->fetch_row()[0];
 0
Author: Hoan,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-05-26 01:47:46