문제

그래서 나는 약간의 파기를 해왔고 PHP에서 유효한 V4 UUID를 생성하는 함수를 함께 만들려고 노력했습니다. 이것은 내가 올 수 있었던 가장 가까운 곳입니다. 16 진수, 10 진수, 바이너리, PHP의 비트 연산자 등에 대한 나의 지식은 거의 존재하지 않습니다. 이 기능은 한 영역까지 유효한 V4 UUID를 생성합니다. v4 uuid는 다음의 형태이어야합니다.

xxxxxxxx-xxxx-4트리플 엑스-와이xxx-xxxxxxxxxxx

어디 와이 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 ()보다 더 나은 임의의 숫자 생성기를 사용해야합니다.

에 따르면 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);
    // ...
}

누구나 사용하는 사람 작곡가 종속성,이 라이브러리를 고려할 수 있습니다. 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];

나는 이진에서 소수로 변환하는 더 우아한 방법이 있다고 확신합니다. 4xxx 그리고 yxxx 부분. 그러나 당신이 사용하고 싶다면 openssl_random_pseudo_bytes Crytographically 보안 번호 생성기로서 이것이 제가 사용하는 것입니다.

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