因此,我一直在做一些研究,我一直试图拼凑一个函数,在PHP中生成一个有效的v4 UUID。这是我能做的最接近的事了。我对十六进制、十进制、二进制、PHP的位运算符之类的知识几乎一无所知。这个函数生成一个有效的v4 UUID,直到一个区域。v4 UUID的形式应该是:

xxxxx-xxxx-4xxx-yxxx-xx

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

?>

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

来自汤姆,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])

灵感来自布鲁法的回答。

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

我的答案是基于评论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

与其将其分解为单个字段,不如生成一个随机数据块并更改单个字节位置。您还应该使用比mt_rand()更好的随机数生成器。

根据RFC 4122 -章节4.4,你需要改变这些字段:

Time_hi_and_version(第7字节的4-7位), Clock_seq_hi_and_reserved(第9字节的第6位和第7位)

所有其他122位都应该足够随机。

下面的方法使用openssl_random_pseudo_bytes()生成128位随机数据,对八字节进行排列,然后使用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);
    // ...
}

在Unix系统上,使用系统内核为您生成uuid。

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

请登录https://serverfault.com/a/529319/210994访问Samveen

注意!:使用这个方法来获取一个uuid,实际上会非常快地耗尽熵池!我将避免在频繁调用它的地方使用它。

任何使用编写器依赖项的人,都可以考虑这个库:https://github.com/ramsey/uuid

没有比这更简单的了:

Uuid::uuid4();

如何使用mysql为您生成uuid ?

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

$query = 'SELECT UUID()';
echo $conn->query($query)->fetch_row()[0];

在我搜索创建v4 uuid时,我首先来到了这个页面,然后在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));
}

信贷:pavel.volyntsev

编辑:澄清一下,这个函数总是给你一个v4 uuid (PHP >= 5.3.0)。

当com_create_guid函数可用时(通常仅在Windows上),它将使用该函数并去掉花括号。

如果不存在(Linux),它将使用强随机的openssl_random_pseudo_bytes函数,然后使用vsprintf将其格式化为v4 uuid。

如果你使用CakePHP,你可以使用他们的方法cakeetext::uuid();生成RFC4122 uuid。

我曾经搜索过完全相同的东西,我自己也几乎实现了一个版本,我认为值得一提的是,如果你在WordPress框架内做这个,WP有自己的超级方便的函数:

$myUUID = wp_generate_uuid4();

你可以在这里阅读描述和源代码。

我相信有一种更优雅的方法可以将4xxx和yxxx部分从二进制转换为十进制。但如果你想使用openssl_random_pseudo_bytes作为加密安全的数字生成器,这是我使用的:

return sprintf('%s-%s-%04x-%04x-%s',
    bin2hex(openssl_random_pseudo_bytes(4)),
    bin2hex(openssl_random_pseudo_bytes(2)),
    hexdec(bin2hex(openssl_random_pseudo_bytes(2))) & 0x0fff | 0x4000,
    hexdec(bin2hex(openssl_random_pseudo_bytes(2))) & 0x3fff | 0x8000,
    bin2hex(openssl_random_pseudo_bytes(6))
    );

Jack的回答略有变化,以增加对PHP < 7的支持:

// Get an RFC-4122 compliant globaly unique identifier
function get_guid() {
    $data = PHP_MAJOR_VERSION < 7 ? openssl_random_pseudo_bytes(16) : random_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));
}
// php version >= 7
$uuid = vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex(random_bytes(16)), 4));

使用Symfony Polyfill / Uuid 然后你可以生成uuid作为原生php函数:

$uuid = uuid_create(UUID_TYPE_RANDOM);

更多关于它,阅读官方Symfony blop后- https://symfony.com/blog/introducing-the-new-symfony-uuid-polyfill

这可以更简单吗?

$uuid = bin2hex(openssl_random_pseudo_bytes(16));
for($cnt = 8; $cnt <=23; $cnt+=5)
   $uuid = substr($uuid, 0, $cnt) . "-" . substr($uuid, $cnt);

echo $uuid . "\n";

这只是一个想法,但我最终获得V4 GUID的方法是使用数据库服务器。我使用的是SQL Server,在需要GUID的场景中,我已经运行了一个查询,所以我只是添加了newid()作为查询结果字段之一。这给了我所需的V4 GUID。

这显然取决于您正在使用的数据库服务器,以及需要GUID的代码中发生了什么(以及需要多少GUID),但如果您的DB服务器生成v4 GUID,特别是如果您正在运行查询,那么这是一种快速而简单的、与php版本无关的获取GUID的方法。

为PHP >= 7加密安全UUID v4。

<?php

function uuid4() {
    /* 32 random HEX + space for 4 hyphens */
    $out = bin2hex(random_bytes(18));

    $out[8]  = "-";
    $out[13] = "-";
    $out[18] = "-";
    $out[23] = "-";

    /* UUID v4 */
    $out[14] = "4";
    
    /* variant 1 - 10xx */
    $out[19] = ["8", "9", "a", "b"][random_int(0, 3)];

    return $out;
}

echo uuid4();

输出:c68469d2 - 065 b - 4 - b17 b36f - 5 - c40efb5f6cd