278

だから私は掘り下げていて、PHPで有効なv4 UUIDを生成する関数をまとめようとしています。これは私が来ることができた中で最も近いです。16 進数、10 進数、2 進数、PHP のビット演算子などに関する私の知識はほとんどありません。この関数は、1 つの領域まで有効な v4 UUID を生成します。v4 UUID は次の形式である必要があります。

xxxxxxxx-xxxx- 4 xxx- y xxx-xxxxxxxxxxxx

ここで、 y は 8、9、A、または B です。これは、関数がそれに準拠していないために失敗する場所です。

この分野で私よりも多くの知識を持っている人が私に手を貸してくれ、この機能を修正してその規則に準拠できるようにしてくれることを望んでいました.

機能は次のとおりです。

<?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;
}

?>
4

17 に答える 17

419

個々のフィールドに分割する代わりに、ランダムなデータ ブロックを生成し、個々のバイト位置を変更する方が簡単です。また、mt_rand() よりも優れた乱数ジェネレーターを使用する必要があります。

RFC 4122 - セクション 4.4に従って、次のフィールドを変更する必要があります。

  1. time_hi_and_version(第 7 オクテットのビット 4 ~ 7)、
  2. clock_seq_hi_and_reserved(第 9 オクテットのビット 6 & 7)

他の 122 ビットはすべて十分にランダムでなければなりません。

次のアプローチでは、 を使用して 128 ビットのランダム データを生成openssl_random_pseudo_bytes()し、オクテットの順列を作成してから、 と を使用bin2hex()vsprintf()て最終的なフォーマットを行います。

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));

PHP 7 では、以下を使用して、ランダムなバイト シーケンスの生成がさらに簡単になりrandom_bytes()ます。

function guidv4($data = null)
{
    $data = $data ?? random_bytes(16);
    // ...
}
于 2013-04-08T09:21:10.553 に答える
322

PHP マニュアルのこのコメントから引用すると、次のように使用できます。

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 )
    );
}
于 2010-01-11T06:27:39.670 に答える
139

作曲家の依存関係を使用している人は、このライブラリを検討することをお勧めします: https://github.com/ramsey/uuid

これほど簡単なことはありません:

Uuid::uuid4();
于 2014-07-16T16:23:25.770 に答える
32

UNIX システムでは、システム カーネルを使用して uuid を生成します。

file_get_contents('/proc/sys/kernel/random/uuid')

https://serverfault.com/a/529319/210994のクレジット Samveen

注!: このメソッドを使用して uuid を取得すると、実際にはエントロピー プールがすぐに使い果たされます。頻繁に呼び出される場所でこれを使用することは避けます。

于 2014-04-17T19:04:55.020 に答える
5

私の答えは、コメントuniqid ユーザー コメントに基づいていますが、 openssl_random_pseudo_bytes関数を使用して、読み取りの代わりにランダムな文字列を生成します。/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
于 2013-04-08T08:27:52.167 に答える
5

使用する場合は、CakeTextCakePHPクラスのメソッドを使用して RFC4122 uuid を生成できます。CakeText::uuid();

于 2017-09-07T18:33:20.410 に答える
4

ここでbroofaの答えに触発されました。

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');

または、匿名関数を使用できない場合。

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');
于 2013-03-22T12:34:52.790 に答える
1

トムから、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])
于 2012-08-22T18:12:50.793 に答える