<?php
declare(strict_types=1);

namespace SafeTun\Backend\Service;

use SafeTun\Backend\Crypto\Base64Url;

final class Ids
{
    public static function requestId(): string
    {
        $bytes = random_bytes(16);
        $bytes[6] = chr((ord($bytes[6]) & 0x0f) | 0x40);
        $bytes[8] = chr((ord($bytes[8]) & 0x3f) | 0x80);
        $hex = bin2hex($bytes);
        return substr($hex, 0, 8) . '-' . substr($hex, 8, 4) . '-' . substr($hex, 12, 4)
            . '-' . substr($hex, 16, 4) . '-' . substr($hex, 20);
    }

    public static function opaque(int $bytes = 16): string
    {
        return Base64Url::encode(random_bytes($bytes));
    }
}
