Thanks to visit codestin.com
Credit goes to github.com

Skip to content

[Uid] work around buggy libuuid #36180

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 23, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion src/Symfony/Component/Uid/Tests/UuidTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@ public function testV3()
$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()
Expand All @@ -70,9 +71,10 @@ public function testV4()

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()
Expand Down
18 changes: 16 additions & 2 deletions src/Symfony/Component/Uid/Uuid.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,14 @@ final public static function v1(): UuidV1

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
Expand All @@ -84,7 +91,14 @@ final public static function v4(): UuidV4

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
Expand Down