Skip to content

Commit f18294b

Browse files
bug #36180 [Uid] work around buggy libuuid (nicolas-grekas)
This PR was merged into the 5.1-dev branch. Discussion ---------- [Uid] work around buggy libuuid | Q | A | ------------- | --- | Branch? | master | Bug fix? | yes | New feature? | no | Deprecations? | no | Tickets | - | License | MIT | Doc PR | - As found while working on the polyfill, fixed upstream in util-linux/util-linux@d6ddf07 Commits ------- ebf601b [Uid] work around buggy libuuid
2 parents e1eb80c + ebf601b commit f18294b

File tree

2 files changed

+19
-3
lines changed

2 files changed

+19
-3
lines changed

src/Symfony/Component/Uid/Tests/UuidTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ public function testV3()
5959
$uuid = Uuid::v3(new UuidV4(self::A_UUID_V4), 'the name');
6060

6161
$this->assertInstanceOf(UuidV3::class, $uuid);
62+
$this->assertSame('8dac64d3-937a-3e7c-aa1d-d5d6c06a61f5', (string) $uuid);
6263
}
6364

6465
public function testV4()
@@ -70,9 +71,10 @@ public function testV4()
7071

7172
public function testV5()
7273
{
73-
$uuid = Uuid::v5(new UuidV4(self::A_UUID_V4), 'the name');
74+
$uuid = Uuid::v5(new UuidV4('ec07aa88-f84e-47b9-a581-1c6b30a2f484'), 'the name');
7475

7576
$this->assertInstanceOf(UuidV5::class, $uuid);
77+
$this->assertSame('851def0c-b9c7-55aa-a991-130e769ec0a9', (string) $uuid);
7678
}
7779

7880
public function testV6()

src/Symfony/Component/Uid/Uuid.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,14 @@ final public static function v1(): UuidV1
7474

7575
final public static function v3(self $namespace, string $name): UuidV3
7676
{
77-
return new UuidV3(uuid_generate_md5($namespace->uid, $name));
77+
// don't use uuid_generate_md5(), some versions are buggy
78+
$uuid = md5(hex2bin(str_replace('-', '', $namespace->uid)).$name, true);
79+
$uuid[8] = $uuid[8] & "\x3F" | "\x80";
80+
$uuid = substr_replace(bin2hex($uuid), '-', 8, 0);
81+
$uuid = substr_replace($uuid, '-3', 13, 1);
82+
$uuid = substr_replace($uuid, '-', 18, 0);
83+
84+
return new UuidV3(substr_replace($uuid, '-', 23, 0));
7885
}
7986

8087
final public static function v4(): UuidV4
@@ -84,7 +91,14 @@ final public static function v4(): UuidV4
8491

8592
final public static function v5(self $namespace, string $name): UuidV5
8693
{
87-
return new UuidV5(uuid_generate_sha1($namespace->uid, $name));
94+
// don't use uuid_generate_sha1(), some versions are buggy
95+
$uuid = substr(sha1(hex2bin(str_replace('-', '', $namespace->uid)).$name, true), 0, 16);
96+
$uuid[8] = $uuid[8] & "\x3F" | "\x80";
97+
$uuid = substr_replace(bin2hex($uuid), '-', 8, 0);
98+
$uuid = substr_replace($uuid, '-5', 13, 1);
99+
$uuid = substr_replace($uuid, '-', 18, 0);
100+
101+
return new UuidV5(substr_replace($uuid, '-', 23, 0));
88102
}
89103

90104
final public static function v6(): UuidV6

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy