diff --git a/src/Symfony/Component/Uid/Tests/UuidTest.php b/src/Symfony/Component/Uid/Tests/UuidTest.php index addb9dfa4b..875f99e582 100644 --- a/src/Symfony/Component/Uid/Tests/UuidTest.php +++ b/src/Symfony/Component/Uid/Tests/UuidTest.php @@ -59,6 +59,7 @@ class UuidTest extends TestCase $uuid = Uuid::v3(new UuidV4(self::A_UUID_V4), 'the name'); $this->assertInstanceOf(UuidV3::class, $uuid); + $this->assertSame('8dac64d3-937a-3e7c-aa1d-d5d6c06a61f5', (string) $uuid); } public function testV4() @@ -70,9 +71,10 @@ class UuidTest extends TestCase public function testV5() { - $uuid = Uuid::v5(new UuidV4(self::A_UUID_V4), 'the name'); + $uuid = Uuid::v5(new UuidV4('ec07aa88-f84e-47b9-a581-1c6b30a2f484'), 'the name'); $this->assertInstanceOf(UuidV5::class, $uuid); + $this->assertSame('851def0c-b9c7-55aa-a991-130e769ec0a9', (string) $uuid); } public function testV6() diff --git a/src/Symfony/Component/Uid/Uuid.php b/src/Symfony/Component/Uid/Uuid.php index 36fb430bd5..1e8a7d9c10 100644 --- a/src/Symfony/Component/Uid/Uuid.php +++ b/src/Symfony/Component/Uid/Uuid.php @@ -74,7 +74,14 @@ class Uuid extends AbstractUid final public static function v3(self $namespace, string $name): UuidV3 { - return new UuidV3(uuid_generate_md5($namespace->uid, $name)); + // don't use uuid_generate_md5(), some versions are buggy + $uuid = md5(hex2bin(str_replace('-', '', $namespace->uid)).$name, true); + $uuid[8] = $uuid[8] & "\x3F" | "\x80"; + $uuid = substr_replace(bin2hex($uuid), '-', 8, 0); + $uuid = substr_replace($uuid, '-3', 13, 1); + $uuid = substr_replace($uuid, '-', 18, 0); + + return new UuidV3(substr_replace($uuid, '-', 23, 0)); } final public static function v4(): UuidV4 @@ -84,7 +91,14 @@ class Uuid extends AbstractUid final public static function v5(self $namespace, string $name): UuidV5 { - return new UuidV5(uuid_generate_sha1($namespace->uid, $name)); + // don't use uuid_generate_sha1(), some versions are buggy + $uuid = substr(sha1(hex2bin(str_replace('-', '', $namespace->uid)).$name, true), 0, 16); + $uuid[8] = $uuid[8] & "\x3F" | "\x80"; + $uuid = substr_replace(bin2hex($uuid), '-', 8, 0); + $uuid = substr_replace($uuid, '-5', 13, 1); + $uuid = substr_replace($uuid, '-', 18, 0); + + return new UuidV5(substr_replace($uuid, '-', 23, 0)); } final public static function v6(): UuidV6