[Uid] work around buggy libuuid

This commit is contained in:
Nicolas Grekas 2020-03-23 15:03:28 +01:00
parent e1eb80c9f2
commit ebf601b1a6
2 changed files with 19 additions and 3 deletions

View File

@ -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()

View File

@ -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