You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
virt/web/app/Services/Cloud/CloudLogIdentity.php

42 lines
1.1 KiB
PHP

<?php
namespace App\Services\Cloud;
/** Compact, lossless identities used only at the log-database boundary. */
final class CloudLogIdentity
{
public static function uuid(string $uuid): string
{
$uuid = strtolower(trim($uuid));
if (preg_match(
'/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/D',
$uuid
) !== 1) {
throw new \InvalidArgumentException('Invalid VM UUID');
}
$binary = hex2bin(str_replace('-', '', $uuid));
if (!is_string($binary) || strlen($binary) !== 16) {
throw new \RuntimeException('Unable to encode VM UUID');
}
return $binary;
}
public static function uuidKey(string $uuid): string
{
return bin2hex(self::uuid($uuid));
}
public static function storedUuidKey(string $binary): string
{
if (strlen($binary) !== 16) {
throw new \RuntimeException('Stored VM UUID is invalid');
}
return bin2hex($binary);
}
public static function sha256(string $value): string
{
return hash('sha256', $value, true);
}
}