所以我一直在做一些挖掘和我一直在试图片一起的功能,产生一个有效的v4usb驱PHP.这是最接近我一直以来的。我的知识,在十六、十进制binary,PHP's bitwise operators等是几乎不存在.这一功能产生一个有效的v4usb驱直到一个区域。V4usb驱应的形式:

xxxxxxxx-xxxx-4xxxyxxx-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;
}

?>

由于任何人,可以帮助我。

有帮助吗?

解决方案

摘自 这个 评论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 )
    );
}

其他提示

而不是破坏它进入各个领域,它更容易随机产生一块的数据,并改变这个字节的位置。你也应该用一个较好的随机数发生器比mt_rand().

根据 RFC4122-第4.4节, 你需要改变这些领域:

  1. time_hi_and_version (比特4至7的第7八),
  2. clock_seq_hi_and_reserved (bit6和7的第9八)

所有的其他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));

PHP7,产生随机的字节的序列,甚至更简单的使用 random_bytes():

function guidv4($data = null)
{
    $data = $data ?? random_bytes(16);
    // ...
}

任何人使用的人 作曲家 依赖关系,您可能需要考虑此库: https://github.com/ramsey/uuid

它没有比这容易得多的:

Uuid::uuid4();

在UNIX系统上,使用系统内核为您生成UUID。

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

信用Samveen https://serverfault.com/a/529319/210994

注意!:使用此方法获取UUID实际上很快就会用尽熵池!我会避免在经常被调用的地方使用它。

在我寻找创建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。

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

如果您使用 CakePHP 您可以使用他们的方法 CakeText::uuid(); 来自 caketext 类别生成RFC4122 UUID。

受到启发 布罗法的答案 这里.

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

搜索了完全相同的东西 几乎 我本人实施此版本,我认为值得一提的是,如果您在 WordPress 框架,WP对此具有自己的超支功能:

$myUUID = wp_generate_uuid4();

您可以阅读描述和源 这里.

略有变化 杰克的答案 增加对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));
}

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

如何使用mySQL为您生成UUID?

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

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

我敢肯定,有一种更优雅的方法可以进行从二进制转换为十进制的转换 4xxxyxxx 部分。但是如果您想使用 openssl_random_pseudo_bytes 作为您的crytographsecraptic编号生成器,这就是我使用的:

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))
    );
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top