<?php

declare(strict_types=1);



class LicenseToken
{
     
    private const PUBLIC_KEY = 'wRwRZPH67XN8ksSQqMITSgXdzQLLQh8Rf5Mlh0v6X50=';

    

    public static function verify(string $token): ?array
    {
        if ($token === '' || !function_exists('sodium_crypto_sign_verify_detached')) {
            return null;
        }
        $dot = strrpos($token, '.');
        if ($dot === false || $dot === 0) {
            return null;
        }
        $payloadJson = base64_decode(strtr(substr($token, 0, $dot), '-_', '+/'), true);
        $sig = @hex2bin(substr($token, $dot + 1));
        $pub = base64_decode(self::PUBLIC_KEY, true);
        if ($payloadJson === false || $sig === false || $pub === false
            || strlen($sig) !== SODIUM_CRYPTO_SIGN_BYTES
            || strlen($pub) !== SODIUM_CRYPTO_SIGN_PUBLICKEYBYTES) {
            return null;
        }
        if (!sodium_crypto_sign_verify_detached($sig, $payloadJson, $pub)) {
            return null;
        }
        $payload = json_decode($payloadJson, true);
        return is_array($payload) ? $payload : null;
    }
}
